2 * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
23 #include <wifi-manager.h>
24 #include <wifi-direct.h>
26 #include <system_info.h>
29 using ::testing::InitGoogleTest;
30 using ::testing::Test;
31 using ::testing::TestCase;
33 static char ap_name[MAX_AP_LENGTH];
34 static wifi_manager_h wifi = NULL;
35 static wifi_manager_error_e rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
36 wifi_manager_ap_h g_hWifiAP;
38 static int rst_p2p = WIFI_DIRECT_ERROR_OPERATION_FAILED;
39 static wifi_direct_device_state_e dev_state = WIFI_DIRECT_DEVICE_STATE_DEACTIVATED;
40 static char p2p_ifname[MAX_PATH_LENGTH];
42 static softap_h sa = NULL;
43 static bool g_is_requested;
44 static softap_error_e g_error;
45 static softap_disabled_cause_e g_code;
47 static bool __check_feature_supported(char *key)
50 int ret = system_info_get_platform_bool(key, &value);
52 EXPECT_EQ(SYSTEM_INFO_ERROR_NONE, ret) << "system_info_get_platform_bool failed";
57 static void __test_callback(wifi_manager_error_e result, void* user_data)
63 static gboolean __timeout_callback(gpointer data)
65 EXPECT_TRUE(0) << "Wi-Fi TC callback timeout!";
70 static void __device_state_changed_cb(int error_code,
71 wifi_direct_device_state_e device_state, void *user_data)
74 dev_state = device_state;
78 static void __enabled_cb(softap_error_e error, bool is_requested, void *data)
81 g_is_requested = is_requested;
85 static void __disabled_cb(softap_error_e error, softap_disabled_cause_e code, void *data)
92 static void __set_p2p_ifname(void)
94 GKeyFile *key_file = NULL;
98 key_file = g_key_file_new();
99 if (!g_key_file_load_from_file(key_file, WIFI_P2P_CONFIG_PATH, G_KEY_FILE_NONE, &error)) {
100 g_clear_error(&error);
101 g_key_file_free(key_file);
106 ifn = g_key_file_get_string(key_file, WFD_CONF_GROUP_NAME, "p2p_interface", &error);
108 g_snprintf(p2p_ifname, MAX_PATH_LENGTH, "/sys/class/net/%s/address", ifn);
111 g_clear_error(&error);
112 g_key_file_free(key_file);
115 static bool __found_ap_callback(wifi_manager_ap_h ap, void *user_data)
117 char *ap_name = NULL;
118 char *ap_name_part = (char *)user_data;
119 size_t ap_name_len = strlen(ap_name_part);
120 int ret = WIFI_MANAGER_ERROR_NONE;
123 ret = wifi_manager_ap_get_essid(ap, &ap_name);
124 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Unable to get essid";
129 if (!strncmp(ap_name, ap_name_part, ap_name_len)) {
131 ret = wifi_manager_ap_clone(&g_hWifiAP, ap);
132 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Unable to clone the AP handle";
134 rst = WIFI_MANAGER_ERROR_NONE;
143 TEST(Hal_wifi, Init_p)
145 g_bFeatureWifi = __check_feature_supported((char*)FEATURE_WIFI);
146 SKIP_NOT_SUPPORTED(g_bFeatureWifi);
148 int rv = wifi_manager_initialize(&wifi);
149 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Initialization failure";
152 TEST(Hal_wifi, Activate_p)
154 SKIP_NOT_SUPPORTED(g_bFeatureWifi);
159 rv = wifi_manager_is_activated(wifi, &activated);
160 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to get Wi-Fi device state";
165 rv = wifi_manager_activate(wifi, __test_callback, NULL);
166 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to activate Wi-Fi device";
168 RUN_GMAIN_LOOP(__timeout_callback);
170 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Activetion failure";
171 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
174 rv = access(WIFI_ADDRESS_PATH, F_OK);
175 EXPECT_EQ(0, rv) << "Could not access " << WIFI_ADDRESS_PATH;
178 TEST(Hal_wifi, Scan_p)
180 SKIP_NOT_SUPPORTED(g_bFeatureWifi);
182 int rv = wifi_manager_scan(wifi, __test_callback, NULL);
183 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to scan";
185 RUN_GMAIN_LOOP(__timeout_callback);
187 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Scan failure";
188 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
191 TEST(Hal_wifi, Connect_p)
193 SKIP_NOT_SUPPORTED(g_bFeatureWifi);
195 wifi_manager_connection_state_e connection_state;
196 int ret = WIFI_MANAGER_ERROR_NONE;
198 ret = wifi_manager_get_connection_state(wifi, &connection_state);
199 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to get connection state";
201 if (connection_state == WIFI_MANAGER_CONNECTION_STATE_CONNECTED)
207 ret = wifi_manager_foreach_found_ap(wifi, __found_ap_callback, (char *)ap_name);
208 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Failed to find the AP";
209 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
211 ret = wifi_manager_connect(wifi, g_hWifiAP, __test_callback, NULL);
212 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to connect";
214 RUN_GMAIN_LOOP(__timeout_callback);
216 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Connection failure";
217 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
220 TEST(Hal_wifi, Disconnect_p)
222 SKIP_NOT_SUPPORTED(g_bFeatureWifi);
224 int ret = WIFI_MANAGER_ERROR_NONE;
225 wifi_manager_ap_h hAP = NULL;
227 ret = wifi_manager_get_connected_ap(wifi, &hAP);
228 if (ret == WIFI_MANAGER_ERROR_NO_CONNECTION)
230 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to get connected AP";
232 ret = wifi_manager_disconnect(wifi, hAP, __test_callback, NULL);
233 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to disconnect";
235 RUN_GMAIN_LOOP(__timeout_callback);
237 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Disconnection failure";
238 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
240 ret = wifi_manager_forget_ap(wifi, hAP);
241 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to forget AP";
244 TEST(Hal_wifi, Deactivate_p)
246 SKIP_NOT_SUPPORTED(g_bFeatureWifi);
248 int rv = wifi_manager_deactivate(wifi, __test_callback, NULL);
249 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to deactivate Wi-Fi device";
251 RUN_GMAIN_LOOP(__timeout_callback);
253 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Deactivation failure";
254 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
257 TEST(Hal_wifi, Deinit_p)
259 SKIP_NOT_SUPPORTED(g_bFeatureWifi);
261 int rv = wifi_manager_deinitialize(wifi);
262 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Deinitialization failure";
265 TEST(Hal_wifi_direct, Init_p)
267 g_bFeatureP2P = __check_feature_supported((char*)FEATURE_WIFIDIRECT);
268 SKIP_NOT_SUPPORTED(g_bFeatureP2P);
270 int rv = wifi_direct_initialize();
271 EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Initialization failure";
274 TEST(Hal_wifi_direct, Activate_p)
276 SKIP_NOT_SUPPORTED(g_bFeatureP2P);
279 wifi_direct_state_e state = WIFI_DIRECT_STATE_ACTIVATED;
281 rv = wifi_direct_get_state(&state);
282 ASSERT_EQ(0, rv) << "Failed to get Wi-Fi Direct device state";
284 if (state != WIFI_DIRECT_STATE_DEACTIVATED)
287 rv = wifi_direct_set_device_state_changed_cb(__device_state_changed_cb, NULL);
288 ASSERT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to set activation callback";
290 rv = wifi_direct_activate();
291 ASSERT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to activate Wi-Fi Direct device";
293 RUN_GMAIN_LOOP(__timeout_callback);
295 EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rst_p2p) << "Activetion failure";
296 rst_p2p = WIFI_DIRECT_ERROR_OPERATION_FAILED;
298 rv = wifi_direct_get_state(&state);
299 EXPECT_EQ(0, rv) << "Failed to get Wi-Fi Direct device state";
300 EXPECT_EQ(WIFI_DIRECT_STATE_ACTIVATED, state) << "Activetion failure";
304 rv = access(p2p_ifname, F_OK);
306 rv = access(WIFI_P2P_PATH, F_OK);
307 EXPECT_EQ(0, rv) << "Could not access " << WIFI_ADDRESS_PATH;
310 TEST(Hal_wifi_direct, Deactivate_p)
312 SKIP_NOT_SUPPORTED(g_bFeatureP2P);
316 rv = wifi_direct_deactivate();
317 ASSERT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to deactivate Wi-Fi Direct";
319 RUN_GMAIN_LOOP(__timeout_callback);
321 EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rst_p2p) << "Deactivation failure";
322 rst_p2p = WIFI_DIRECT_ERROR_OPERATION_FAILED;
324 wifi_direct_state_e state = WIFI_DIRECT_STATE_ACTIVATED;
325 rv = wifi_direct_get_state(&state);
326 EXPECT_EQ(0, rv) << "Failed to get Wi-Fi Direct device state";
328 EXPECT_EQ(WIFI_DIRECT_STATE_DEACTIVATED, state) << "Deactivetion failure";
330 rv = wifi_direct_unset_device_state_changed_cb();
331 EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to unset activation callback";
334 TEST(Hal_wifi_direct, Deinit_p)
336 SKIP_NOT_SUPPORTED(g_bFeatureP2P);
338 int rv = wifi_direct_deinitialize();
339 EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Deinitialization failure";
342 TEST(Hal_softap, Init_p)
344 g_bFeatureSoftap = __check_feature_supported((char*)FEATURE_SOFTAP);
345 SKIP_NOT_SUPPORTED(g_bFeatureSoftap);
347 int ret = SOFTAP_ERROR_NONE;
349 ret = softap_create(&sa);
350 ASSERT_EQ(SOFTAP_ERROR_NONE, ret) << "Initialization failure";
352 ret = softap_set_enabled_cb(sa, __enabled_cb, NULL);
353 EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Fail to set enabled callback!!";
355 ret = softap_set_disabled_cb(sa, __disabled_cb, NULL);
356 EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Fail to set disabled callback!!";
359 TEST(Hal_softap, Activate_p)
361 SKIP_NOT_SUPPORTED(g_bFeatureSoftap);
363 int ret = SOFTAP_ERROR_NONE;
364 bool enabled = false;
366 ret = softap_is_enabled(sa, &enabled);
367 ASSERT_EQ(SOFTAP_ERROR_NONE, ret) << "Failed to get SoftAP state";
372 ret = softap_enable(sa);
373 ASSERT_EQ(SOFTAP_ERROR_NONE, ret) << "Failed to enable SoftAP";
375 RUN_GMAIN_LOOP(__timeout_callback);
377 EXPECT_EQ(SOFTAP_ERROR_NONE, g_error) << "Failed to enable SoftAP";
378 EXPECT_EQ(true, g_is_requested) << "Failed to enable SoftAP";
381 ret = access(WIFI_ADDRESS_PATH, F_OK);
382 EXPECT_EQ(0, ret) << "Could not access " << WIFI_ADDRESS_PATH;
385 TEST(Hal_softap, Deactivate_p)
387 SKIP_NOT_SUPPORTED(g_bFeatureSoftap);
389 int ret = SOFTAP_ERROR_NONE;
391 ret = softap_disable(sa);
392 ASSERT_EQ(SOFTAP_ERROR_NONE, ret) << "Failed to disable SoftAP";
394 RUN_GMAIN_LOOP(__timeout_callback);
396 EXPECT_EQ(SOFTAP_ERROR_NONE, g_error) << "Failed to disable SoftAP" << g_code;
399 TEST(Hal_softap, Deinit_p)
401 SKIP_NOT_SUPPORTED(g_bFeatureSoftap);
403 int ret = SOFTAP_ERROR_NONE;
405 ret = softap_unset_enabled_cb(sa);
406 EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Fail to unset enabled callback!!";
408 ret = softap_unset_disabled_cb(sa);
409 EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Fail to unset disabled callback!!";
411 ret = softap_destroy(sa);
412 EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Deinitialization failure";
415 int main(int argc, char **argv)
417 if (argc > 1 && argv[argc - 1][0] != '-') {
418 g_strlcpy(ap_name, argv[argc - 1], MAX_AP_LENGTH);
423 InitGoogleTest(&argc, argv);
424 return RUN_ALL_TESTS();