1/*
2 * Copyright (C) 2016, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gmock/gmock.h>
18#include <gtest/gtest.h>
19
20#include <string>
21#include <vector>
22
23#include "wifi_system/hostapd_manager.h"
24
25using std::string;
26using std::vector;
27
28namespace android {
29namespace wifi_system {
30namespace {
31
32const char kTestInterfaceName[] = "foobar0";
33const char kTestSsidStr[] = "helloisitme";
34const char kTestPassphraseStr[] = "yourelookingfor";
35const int kTestChannel = 2;
36
37#define CONFIG_COMMON_PREFIX \
38    "interface=foobar0\n" \
39    "driver=nl80211\n" \
40    "ctrl_interface=/data/misc/wifi/hostapd/ctrl\n" \
41    "ssid2=68656c6c6f" "6973" "6974" "6d65\n" \
42    "channel=2\n" \
43    "ieee80211n=1\n" \
44    "hw_mode=g\n"
45
46// If you generate your config file with both the test ssid
47// and the test passphrase, you'll get this line in the config.
48#define CONFIG_PSK_LINE \
49    "wpa_psk=dffa36815281e5a6eca1910f254717fa2528681335e3bbec5966d2aa9221a66e\n"
50
51#define CONFIG_WPA_SUFFIX \
52    "wpa=3\n" \
53    "wpa_pairwise=TKIP CCMP\n" \
54    CONFIG_PSK_LINE
55
56#define CONFIG_WPA2_SUFFIX \
57    "wpa=2\n" \
58    "rsn_pairwise=CCMP\n" \
59    CONFIG_PSK_LINE
60
61const char kExpectedOpenConfig[] =
62  CONFIG_COMMON_PREFIX
63  "ignore_broadcast_ssid=0\n"
64  "wowlan_triggers=any\n";
65
66const char kExpectedWpaConfig[] =
67    CONFIG_COMMON_PREFIX
68    "ignore_broadcast_ssid=0\n"
69    "wowlan_triggers=any\n"
70    CONFIG_WPA_SUFFIX;
71
72const char kExpectedWpa2Config[] =
73    CONFIG_COMMON_PREFIX
74    "ignore_broadcast_ssid=0\n"
75    "wowlan_triggers=any\n"
76    CONFIG_WPA2_SUFFIX;
77
78class HostapdManagerTest : public ::testing::Test {
79 protected:
80  string GetConfigForEncryptionType(
81      HostapdManager::EncryptionType encryption_type) {
82    return HostapdManager().CreateHostapdConfig(
83        kTestInterfaceName,
84        cstr2vector(kTestSsidStr),
85        false,  // not hidden
86        kTestChannel,
87        encryption_type,
88        cstr2vector(kTestPassphraseStr));
89  }
90
91  vector<uint8_t> cstr2vector(const char* data) {
92    return vector<uint8_t>(data, data + strlen(data));
93  }
94};  // class HostapdManagerTest
95
96}  // namespace
97
98TEST_F(HostapdManagerTest, GeneratesCorrectOpenConfig) {
99  string config = GetConfigForEncryptionType(
100      HostapdManager::EncryptionType::kOpen);
101  EXPECT_FALSE(config.empty());
102  EXPECT_EQ(kExpectedOpenConfig, config);
103}
104
105TEST_F(HostapdManagerTest, GeneratesCorrectWpaConfig) {
106  string config = GetConfigForEncryptionType(
107      HostapdManager::EncryptionType::kWpa);
108  EXPECT_FALSE(config.empty());
109  EXPECT_EQ(kExpectedWpaConfig, config);
110}
111
112TEST_F(HostapdManagerTest, GeneratesCorrectWpa2Config) {
113  string config = GetConfigForEncryptionType(
114      HostapdManager::EncryptionType::kWpa2);
115  EXPECT_FALSE(config.empty());
116  EXPECT_EQ(kExpectedWpa2Config, config);
117}
118
119TEST_F(HostapdManagerTest, RespectsHiddenSetting) {
120  string config = HostapdManager().CreateHostapdConfig(
121        kTestInterfaceName,
122        cstr2vector(kTestSsidStr),
123        true,
124        kTestChannel,
125        HostapdManager::EncryptionType::kOpen,
126        vector<uint8_t>());
127  EXPECT_FALSE(config.find("ignore_broadcast_ssid=1\n") == string::npos);
128  EXPECT_TRUE(config.find("ignore_broadcast_ssid=0\n") == string::npos);
129}
130
131TEST_F(HostapdManagerTest, CorrectlyInfersHwMode) {
132  string config = HostapdManager().CreateHostapdConfig(
133        kTestInterfaceName,
134        cstr2vector(kTestSsidStr),
135        true,
136        44,
137        HostapdManager::EncryptionType::kOpen,
138        vector<uint8_t>());
139  EXPECT_FALSE(config.find("hw_mode=a\n") == string::npos);
140  EXPECT_TRUE(config.find("hw_mode=g\n") == string::npos);
141}
142
143
144}  // namespace wifi_system
145}  // namespace android
146