blob: 16c44750034c21adc803756a04159a71004cef98 [file] [log] [blame]
Vitaly Buka7ce499f2015-06-09 08:04:11 -07001// Copyright 2014 The Chromium OS 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 "buffet/privet/wifi_ssid_generator.h"
6
7#include <base/strings/utf_string_conversions.h>
8#include <gtest/gtest.h>
9
10#include "buffet/privet/mock_delegates.h"
11#include "buffet/privet/openssl_utils.h"
12
13namespace privetd {
14
15class 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
25TEST_F(WifiSsidGeneratorTest, GenerateFlags) {
26 EXPECT_EQ(ssid_generator_.GenerateFlags().size(), 2);
27
28 wifi_.connection_state_ = ConnectionState{ConnectionState::kUnconfigured};
29 gcd_.connection_state_ = ConnectionState{ConnectionState::kUnconfigured};
30 EXPECT_EQ("DB", ssid_generator_.GenerateFlags());
31
32 wifi_.connection_state_ = ConnectionState{ConnectionState::kOnline};
33 EXPECT_EQ("CB", ssid_generator_.GenerateFlags());
34
35 gcd_.connection_state_ = ConnectionState{ConnectionState::kOffline};
36 EXPECT_EQ("AB", ssid_generator_.GenerateFlags());
37
38 wifi_.connection_state_ = ConnectionState{ConnectionState::kUnconfigured};
39 EXPECT_EQ("BB", ssid_generator_.GenerateFlags());
40}
41
42TEST_F(WifiSsidGeneratorTest, GenerateSsid31orLess) {
43 EXPECT_LE(ssid_generator_.GenerateSsid().size(), 31);
44}
45
46TEST_F(WifiSsidGeneratorTest, GenerateSsidValue) {
47 SetRandomForTests(47);
48 EXPECT_EQ("TestDevice 47.ABMIDABprv", ssid_generator_.GenerateSsid());
49
50 SetRandomForTests(9);
51 EXPECT_EQ("TestDevice 9.ABMIDABprv", ssid_generator_.GenerateSsid());
52}
53
54TEST_F(WifiSsidGeneratorTest, GenerateSsidLongName) {
55 SetRandomForTests(99);
56 EXPECT_CALL(gcd_, GetName(_, _))
57 .WillRepeatedly(
58 DoAll(SetArgPointee<0>("Very Long Device Name"), Return(true)));
59 EXPECT_EQ("Very Long Device 99.ABMIDABprv", ssid_generator_.GenerateSsid());
60}
61
62TEST_F(WifiSsidGeneratorTest, GenerateSsidNoName) {
63 SetRandomForTests(99);
64 EXPECT_CALL(gcd_, GetName(_, _)).WillRepeatedly(Return(false));
65 EXPECT_EQ("", ssid_generator_.GenerateSsid());
66}
67
68} // namespace privetd