Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 5 | #include "src/privet/wifi_ssid_generator.h" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 6 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 7 | #include <gtest/gtest.h> |
| 8 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 9 | #include "src/privet/mock_delegates.h" |
| 10 | #include "src/privet/openssl_utils.h" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 11 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 12 | namespace weave { |
| 13 | namespace privet { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 14 | |
| 15 | class WifiSsidGeneratorTest : public testing::Test { |
| 16 | protected: |
| 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 | |
Vitaly Buka | 85fbeaf | 2015-11-16 20:48:30 -0800 | [diff] [blame] | 25 | TEST_F(WifiSsidGeneratorTest, GenerateFlagsNoHostedAp) { |
Vitaly Buka | 52d006a | 2015-11-21 17:14:51 -0800 | [diff] [blame] | 26 | EXPECT_EQ(ssid_generator_.GenerateFlags().size(), 2u); |
Vitaly Buka | 85fbeaf | 2015-11-16 20:48:30 -0800 | [diff] [blame] | 27 | |
| 28 | wifi_.connection_state_ = ConnectionState{ConnectionState::kUnconfigured}; |
| 29 | gcd_.connection_state_ = ConnectionState{ConnectionState::kUnconfigured}; |
| 30 | EXPECT_EQ("DA", ssid_generator_.GenerateFlags()); |
| 31 | |
| 32 | wifi_.connection_state_ = ConnectionState{ConnectionState::kOnline}; |
| 33 | EXPECT_EQ("CA", ssid_generator_.GenerateFlags()); |
| 34 | |
| 35 | gcd_.connection_state_ = ConnectionState{ConnectionState::kOffline}; |
| 36 | EXPECT_EQ("AA", ssid_generator_.GenerateFlags()); |
| 37 | |
| 38 | wifi_.connection_state_ = ConnectionState{ConnectionState::kUnconfigured}; |
| 39 | EXPECT_EQ("BA", ssid_generator_.GenerateFlags()); |
| 40 | } |
| 41 | |
| 42 | TEST_F(WifiSsidGeneratorTest, GenerateFlagsWithHostedAp) { |
| 43 | EXPECT_CALL(wifi_, GetHostedSsid()) |
| 44 | .WillRepeatedly(Return(ssid_generator_.GenerateSsid())); |
Vitaly Buka | 52d006a | 2015-11-21 17:14:51 -0800 | [diff] [blame] | 45 | EXPECT_EQ(ssid_generator_.GenerateFlags().size(), 2u); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 46 | |
| 47 | wifi_.connection_state_ = ConnectionState{ConnectionState::kUnconfigured}; |
| 48 | gcd_.connection_state_ = ConnectionState{ConnectionState::kUnconfigured}; |
| 49 | EXPECT_EQ("DB", ssid_generator_.GenerateFlags()); |
| 50 | |
| 51 | wifi_.connection_state_ = ConnectionState{ConnectionState::kOnline}; |
| 52 | EXPECT_EQ("CB", ssid_generator_.GenerateFlags()); |
| 53 | |
| 54 | gcd_.connection_state_ = ConnectionState{ConnectionState::kOffline}; |
| 55 | EXPECT_EQ("AB", ssid_generator_.GenerateFlags()); |
| 56 | |
| 57 | wifi_.connection_state_ = ConnectionState{ConnectionState::kUnconfigured}; |
| 58 | EXPECT_EQ("BB", ssid_generator_.GenerateFlags()); |
| 59 | } |
| 60 | |
| 61 | TEST_F(WifiSsidGeneratorTest, GenerateSsid31orLess) { |
Vitaly Buka | 52d006a | 2015-11-21 17:14:51 -0800 | [diff] [blame] | 62 | EXPECT_LE(ssid_generator_.GenerateSsid().size(), 31u); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | TEST_F(WifiSsidGeneratorTest, GenerateSsidValue) { |
| 66 | SetRandomForTests(47); |
| 67 | EXPECT_EQ("TestDevice 47.ABMIDABprv", ssid_generator_.GenerateSsid()); |
| 68 | |
| 69 | SetRandomForTests(9); |
| 70 | EXPECT_EQ("TestDevice 9.ABMIDABprv", ssid_generator_.GenerateSsid()); |
| 71 | } |
| 72 | |
| 73 | TEST_F(WifiSsidGeneratorTest, GenerateSsidLongName) { |
| 74 | SetRandomForTests(99); |
Vitaly Buka | 658aa36 | 2015-09-15 20:59:12 -0700 | [diff] [blame] | 75 | EXPECT_CALL(gcd_, GetName()).WillRepeatedly(Return("Very Long Device Name")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 76 | EXPECT_EQ("Very Long Device 99.ABMIDABprv", ssid_generator_.GenerateSsid()); |
| 77 | } |
| 78 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 79 | } // namespace privet |
| 80 | } // namespace weave |