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";
53 EXPECT_EQ(true, value) << key << " feature is not supported";
58 static void __test_callback(wifi_manager_error_e result, void* user_data)
64 static gboolean __timeout_callback(gpointer data)
66 EXPECT_TRUE(0) << "Wi-Fi TC callback timeout!";
71 static void __device_state_changed_cb(int error_code,
72 wifi_direct_device_state_e device_state, void *user_data)
75 dev_state = device_state;
79 static void __enabled_cb(softap_error_e error, bool is_requested, void *data)
82 g_is_requested = is_requested;
86 static void __disabled_cb(softap_error_e error, softap_disabled_cause_e code, void *data)
93 static void __set_p2p_ifname(void)
95 GKeyFile *key_file = NULL;
99 key_file = g_key_file_new();
100 if (!g_key_file_load_from_file(key_file, WIFI_P2P_CONFIG_PATH, G_KEY_FILE_NONE, &error)) {
101 g_clear_error(&error);
102 g_key_file_free(key_file);
107 ifn = g_key_file_get_string(key_file, WFD_CONF_GROUP_NAME, "p2p_interface", &error);
109 g_snprintf(p2p_ifname, MAX_PATH_LENGTH, "/sys/class/net/%s/address", ifn);
112 g_clear_error(&error);
113 g_key_file_free(key_file);
116 static bool __found_ap_callback(wifi_manager_ap_h ap, void *user_data)
118 char *ap_name = NULL;
119 char *ap_name_part = (char *)user_data;
120 size_t ap_name_len = strlen(ap_name_part);
121 int ret = WIFI_MANAGER_ERROR_NONE;
124 ret = wifi_manager_ap_get_essid(ap, &ap_name);
125 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Unable to get essid";
130 if (!strncmp(ap_name, ap_name_part, ap_name_len)) {
132 ret = wifi_manager_ap_clone(&g_hWifiAP, ap);
133 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Unable to clone the AP handle";
135 rst = WIFI_MANAGER_ERROR_NONE;
144 TEST(Hal_wifi, Init_p)
146 g_bFeatureWifi = __check_feature_supported((char*)FEATURE_WIFI);
147 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
149 int rv = wifi_manager_initialize(&wifi);
150 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Initialization failure";
153 TEST(Hal_wifi, Activate_p)
155 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
160 rv = wifi_manager_is_activated(wifi, &activated);
161 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to get Wi-Fi device state";
166 rv = wifi_manager_activate(wifi, __test_callback, NULL);
167 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to activate Wi-Fi device";
169 RUN_GMAIN_LOOP(__timeout_callback);
171 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Activetion failure";
172 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
175 rv = access(WIFI_ADDRESS_PATH, F_OK);
176 EXPECT_EQ(0, rv) << "Could not access " << WIFI_ADDRESS_PATH;
179 TEST(Hal_wifi, Scan_p)
181 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
183 int rv = wifi_manager_scan(wifi, __test_callback, NULL);
184 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to scan";
186 RUN_GMAIN_LOOP(__timeout_callback);
188 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Scan failure";
189 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
192 TEST(Hal_wifi, Connect_p)
194 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
196 wifi_manager_connection_state_e connection_state;
197 int ret = WIFI_MANAGER_ERROR_NONE;
199 ret = wifi_manager_get_connection_state(wifi, &connection_state);
200 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to get connection state";
202 if (connection_state == WIFI_MANAGER_CONNECTION_STATE_CONNECTED)
208 ret = wifi_manager_foreach_found_ap(wifi, __found_ap_callback, (char *)ap_name);
209 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Failed to find the AP";
210 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
212 ret = wifi_manager_connect(wifi, g_hWifiAP, __test_callback, NULL);
213 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to connect";
215 RUN_GMAIN_LOOP(__timeout_callback);
217 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Connection failure";
218 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
221 TEST(Hal_wifi, Disconnect_p)
223 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
225 int ret = WIFI_MANAGER_ERROR_NONE;
226 wifi_manager_ap_h hAP = NULL;
228 ret = wifi_manager_get_connected_ap(wifi, &hAP);
229 if (ret == WIFI_MANAGER_ERROR_NO_CONNECTION)
231 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to get connected AP";
233 ret = wifi_manager_disconnect(wifi, hAP, __test_callback, NULL);
234 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to disconnect";
236 RUN_GMAIN_LOOP(__timeout_callback);
238 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Disconnection failure";
239 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
241 ret = wifi_manager_forget_ap(wifi, hAP);
242 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to forget AP";
245 TEST(Hal_wifi, Deactivate_p)
247 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
249 int rv = wifi_manager_deactivate(wifi, __test_callback, NULL);
250 ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to deactivate Wi-Fi device";
252 RUN_GMAIN_LOOP(__timeout_callback);
254 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Deactivation failure";
255 rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
257 rv = access(WIFI_ADDRESS_PATH, F_OK);
258 EXPECT_EQ(-1, rv) << WIFI_ADDRESS_PATH << " is exist";
261 TEST(Hal_wifi, Deinit_p)
263 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
265 int rv = wifi_manager_deinitialize(wifi);
266 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Deinitialization failure";
269 TEST(Hal_wifi_direct, Init_p)
271 g_bFeatureP2P = __check_feature_supported((char*)WIFIDIRECT_FEATURE);
272 ASSERT_EQ(true, g_bFeatureP2P) << WIFIDIRECT_FEATURE << " feature is not supported";
274 int rv = wifi_direct_initialize();
275 EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Initialization failure";
278 TEST(Hal_wifi_direct, Activate_p)
280 ASSERT_EQ(true, g_bFeatureP2P) << WIFIDIRECT_FEATURE << " feature is not supported";
283 wifi_direct_state_e state = WIFI_DIRECT_STATE_ACTIVATED;
285 rv = wifi_direct_get_state(&state);
286 ASSERT_EQ(0, rv) << "Failed to get Wi-Fi Direct device state";
288 if (state != WIFI_DIRECT_STATE_DEACTIVATED)
291 rv = wifi_direct_set_device_state_changed_cb(__device_state_changed_cb, NULL);
292 ASSERT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to set activation callback";
294 rv = wifi_direct_activate();
295 ASSERT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to activate Wi-Fi Direct device";
297 RUN_GMAIN_LOOP(__timeout_callback);
299 EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rst_p2p) << "Activetion failure";
300 rst_p2p = WIFI_DIRECT_ERROR_OPERATION_FAILED;
302 rv = wifi_direct_get_state(&state);
303 EXPECT_EQ(0, rv) << "Failed to get Wi-Fi Direct device state";
304 EXPECT_EQ(WIFI_DIRECT_STATE_ACTIVATED, state) << "Activetion failure";
308 rv = access(p2p_ifname, F_OK);
310 rv = access(WIFI_P2P_PATH, F_OK);
311 EXPECT_EQ(0, rv) << "Could not access " << WIFI_ADDRESS_PATH;
314 TEST(Hal_wifi_direct, Deactivate_p)
316 ASSERT_EQ(true, g_bFeatureP2P) << WIFIDIRECT_FEATURE << " feature is not supported";
320 rv = wifi_direct_deactivate();
321 ASSERT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to deactivate Wi-Fi Direct";
323 RUN_GMAIN_LOOP(__timeout_callback);
325 EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rst_p2p) << "Deactivation failure";
326 rst_p2p = WIFI_DIRECT_ERROR_OPERATION_FAILED;
328 wifi_direct_state_e state = WIFI_DIRECT_STATE_ACTIVATED;
329 rv = wifi_direct_get_state(&state);
330 EXPECT_EQ(0, rv) << "Failed to get Wi-Fi Direct device state";
332 EXPECT_EQ(WIFI_DIRECT_STATE_DEACTIVATED, state) << "Deactivetion failure";
335 rv = access(p2p_ifname, F_OK);
337 rv = access(WIFI_P2P_PATH, F_OK);
338 EXPECT_EQ(-1, rv) << WIFI_P2P_PATH << " is exist";
340 rv = wifi_direct_unset_device_state_changed_cb();
341 EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to unset activation callback";
344 TEST(Hal_wifi_direct, Deinit_p)
346 ASSERT_EQ(true, g_bFeatureP2P) << WIFIDIRECT_FEATURE << " feature is not supported";
348 int rv = wifi_direct_deinitialize();
349 EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Deinitialization failure";
352 TEST(Hal_softap, Init_p)
354 g_bFeatureWifi = __check_feature_supported((char*)FEATURE_WIFI);
355 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
357 int ret = SOFTAP_ERROR_NONE;
359 ret = softap_create(&sa);
360 ASSERT_EQ(SOFTAP_ERROR_NONE, ret) << "Initialization failure";
362 ret = softap_set_enabled_cb(sa, __enabled_cb, NULL);
363 EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Fail to set enabled callback!!";
365 ret = softap_set_disabled_cb(sa, __disabled_cb, NULL);
366 EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Fail to set disabled callback!!";
369 TEST(Hal_softap, Activate_p)
371 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
373 int ret = SOFTAP_ERROR_NONE;
374 bool enabled = false;
376 ret = softap_is_enabled(sa, &enabled);
377 ASSERT_EQ(SOFTAP_ERROR_NONE, ret) << "Failed to get SoftAP state";
382 ret = softap_enable(sa);
383 ASSERT_EQ(SOFTAP_ERROR_NONE, ret) << "Failed to enable SoftAP";
385 RUN_GMAIN_LOOP(__timeout_callback);
387 EXPECT_EQ(SOFTAP_ERROR_NONE, g_error) << "Failed to enable SoftAP";
388 EXPECT_EQ(true, g_is_requested) << "Failed to enable SoftAP";
391 ret = access(WIFI_ADDRESS_PATH, F_OK);
392 EXPECT_EQ(0, ret) << "Could not access " << WIFI_ADDRESS_PATH;
395 TEST(Hal_softap, Deactivate_p)
397 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
399 int ret = SOFTAP_ERROR_NONE;
401 ret = softap_disable(sa);
402 ASSERT_EQ(SOFTAP_ERROR_NONE, ret) << "Failed to disable SoftAP";
404 RUN_GMAIN_LOOP(__timeout_callback);
406 EXPECT_EQ(SOFTAP_ERROR_NONE, g_error) << "Failed to disable SoftAP" << g_code;
408 ret = access(WIFI_ADDRESS_PATH, F_OK);
409 EXPECT_EQ(-1, ret) << WIFI_ADDRESS_PATH << " is exist";
412 TEST(Hal_softap, Deinit_p)
414 ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
416 int ret = SOFTAP_ERROR_NONE;
418 ret = softap_unset_enabled_cb(sa);
419 EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Fail to unset enabled callback!!";
421 ret = softap_unset_disabled_cb(sa);
422 EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Fail to unset disabled callback!!";
424 ret = softap_destroy(sa);
425 EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Deinitialization failure";
428 int main(int argc, char **argv)
430 if (argc > 1 && argv[argc - 1][0] != '-') {
431 g_strlcpy(ap_name, argv[argc - 1], MAX_AP_LENGTH);
436 InitGoogleTest(&argc, argv);
437 return RUN_ALL_TESTS();