1 // Copyright 2015 The Weave Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/privet/wifi_ssid_generator.h"
6 
7 #include <gtest/gtest.h>
8 
9 #include "src/privet/mock_delegates.h"
10 #include "src/privet/openssl_utils.h"
11 
12 namespace weave {
13 namespace privet {
14 
15 class WifiSsidGeneratorTest : public testing::Test {
16  protected:
SetRandomForTests(int n)17   void SetRandomForTests(int n) { ssid_generator_.SetRandomForTests(n); }
18 
19   testing::StrictMock<MockCloudDelegate> gcd_;
20   testing::StrictMock<MockWifiDelegate> wifi_;
21 
22   WifiSsidGenerator ssid_generator_{&gcd_, &wifi_};
23 };
24 
TEST_F(WifiSsidGeneratorTest,GenerateFlagsWithWifi24)25 TEST_F(WifiSsidGeneratorTest, GenerateFlagsWithWifi24) {
26   EXPECT_CALL(wifi_, GetTypes())
27       .WillRepeatedly(Return(std::set<WifiType>{WifiType::kWifi24}));
28 
29   EXPECT_EQ(ssid_generator_.GenerateFlags().size(), 2u);
30 
31   wifi_.connection_state_ = ConnectionState{ConnectionState::kUnconfigured};
32   gcd_.connection_state_ = ConnectionState{ConnectionState::kUnconfigured};
33   EXPECT_EQ("DB", ssid_generator_.GenerateFlags());
34 
35   wifi_.connection_state_ = ConnectionState{ConnectionState::kOnline};
36   EXPECT_EQ("CB", ssid_generator_.GenerateFlags());
37 
38   gcd_.connection_state_ = ConnectionState{ConnectionState::kOffline};
39   EXPECT_EQ("AB", ssid_generator_.GenerateFlags());
40 
41   wifi_.connection_state_ = ConnectionState{ConnectionState::kUnconfigured};
42   EXPECT_EQ("BB", ssid_generator_.GenerateFlags());
43 }
44 
TEST_F(WifiSsidGeneratorTest,GenerateFlagsWithWifi50)45 TEST_F(WifiSsidGeneratorTest, GenerateFlagsWithWifi50) {
46   EXPECT_CALL(wifi_, GetTypes())
47       .WillRepeatedly(Return(std::set<WifiType>{WifiType::kWifi50}));
48 
49   EXPECT_EQ(ssid_generator_.GenerateFlags().size(), 2u);
50 
51   wifi_.connection_state_ = ConnectionState{ConnectionState::kUnconfigured};
52   gcd_.connection_state_ = ConnectionState{ConnectionState::kUnconfigured};
53   EXPECT_EQ("DC", ssid_generator_.GenerateFlags());
54 
55   wifi_.connection_state_ = ConnectionState{ConnectionState::kOnline};
56   EXPECT_EQ("CC", ssid_generator_.GenerateFlags());
57 
58   gcd_.connection_state_ = ConnectionState{ConnectionState::kOffline};
59   EXPECT_EQ("AC", ssid_generator_.GenerateFlags());
60 
61   wifi_.connection_state_ = ConnectionState{ConnectionState::kUnconfigured};
62   EXPECT_EQ("BC", ssid_generator_.GenerateFlags());
63 }
64 
TEST_F(WifiSsidGeneratorTest,GenerateSsid31orLess)65 TEST_F(WifiSsidGeneratorTest, GenerateSsid31orLess) {
66   EXPECT_LE(ssid_generator_.GenerateSsid().size(), 31u);
67 }
68 
TEST_F(WifiSsidGeneratorTest,GenerateSsidValue)69 TEST_F(WifiSsidGeneratorTest, GenerateSsidValue) {
70   SetRandomForTests(47);
71   EXPECT_EQ("TestDevice 47.ABMIDABprv", ssid_generator_.GenerateSsid());
72 
73   SetRandomForTests(9);
74   EXPECT_EQ("TestDevice 9.ABMIDABprv", ssid_generator_.GenerateSsid());
75 }
76 
TEST_F(WifiSsidGeneratorTest,GenerateSsidLongName)77 TEST_F(WifiSsidGeneratorTest, GenerateSsidLongName) {
78   SetRandomForTests(99);
79   EXPECT_CALL(gcd_, GetName()).WillRepeatedly(Return("Very Long Device Name"));
80   EXPECT_EQ("Very Long Device  99.ABMIDABprv", ssid_generator_.GenerateSsid());
81 }
82 
83 }  // namespace privet
84 }  // namespace weave
85