Fix resource leak
[platform/core/connectivity/net-config.git] / haltests / wifi-haltests.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
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 <stdio.h>
18 #include <stdlib.h>
19 #include <iostream>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <unistd.h>
23 #include <wifi-manager.h>
24 #include <wifi-direct.h>
25 #include <softap.h>
26 #include <system_info.h>
27 #include "haltests.h"
28
29 using ::testing::InitGoogleTest;
30 using ::testing::Test;
31 using ::testing::TestCase;
32
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;
37
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];
41
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;
46
47 static bool __check_feature_supported(char *key)
48 {
49         bool value = false;
50         int ret = system_info_get_platform_bool(key, &value);
51
52         EXPECT_EQ(SYSTEM_INFO_ERROR_NONE, ret) << "system_info_get_platform_bool failed";
53
54         return value;
55 }
56
57 static void __test_callback(wifi_manager_error_e result, void* user_data)
58 {
59         rst = result;
60         QUIT_GMAIN_LOOP;
61 }
62
63 static gboolean __timeout_callback(gpointer data)
64 {
65         EXPECT_TRUE(0) << "Wi-Fi TC callback timeout!";
66         QUIT_GMAIN_LOOP;
67         return FALSE;
68 }
69
70 static void __device_state_changed_cb(int error_code,
71                 wifi_direct_device_state_e device_state, void *user_data)
72 {
73         rst_p2p = error_code;
74         dev_state = device_state;
75         QUIT_GMAIN_LOOP;
76 }
77
78 static void __enabled_cb(softap_error_e error, bool is_requested, void *data)
79 {
80         g_error = error;
81         g_is_requested = is_requested;
82         QUIT_GMAIN_LOOP;
83 }
84
85 static void __disabled_cb(softap_error_e error, softap_disabled_cause_e code, void *data)
86 {
87         g_error = error;
88         g_code = code;
89         QUIT_GMAIN_LOOP;
90 }
91
92 static void __set_p2p_ifname(void)
93 {
94         GKeyFile *key_file = NULL;
95         GError *error = NULL;
96         char *ifn = NULL;
97
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);
102                 key_file = NULL;
103                 return;
104         }
105
106         ifn = g_key_file_get_string(key_file, WFD_CONF_GROUP_NAME, "p2p_interface", &error);
107         if (ifn)
108                 g_snprintf(p2p_ifname, MAX_PATH_LENGTH, "/sys/class/net/%s/address", ifn);
109
110         if (error)
111                 g_clear_error(&error);
112         g_key_file_free(key_file);
113     g_free(ifn);
114 }
115
116 static bool __found_ap_callback(wifi_manager_ap_h ap, void *user_data)
117 {
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;
122
123         if (ap) {
124                 ret = wifi_manager_ap_get_essid(ap, &ap_name);
125                 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Unable to get essid";
126
127                 if (!ap_name)
128                         return true;
129
130                 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";
133                         if (g_hWifiAP) {
134                                 rst = WIFI_MANAGER_ERROR_NONE;
135                                 g_free(ap_name);
136                                 return false;
137                         }
138                 }
139
140                 g_free(ap_name);
141         }
142
143         return true;
144 }
145
146 TEST(WifiHaltest, WifiInit_p)
147 {
148         g_bFeatureWifi = __check_feature_supported((char*)FEATURE_WIFI);
149         SKIP_NOT_SUPPORTED(g_bFeatureWifi);
150
151         int rv = wifi_manager_initialize(&wifi);
152         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Initialization failure";
153 }
154
155 TEST(WifiHaltest, WifiActivate_p)
156 {
157         SKIP_NOT_SUPPORTED(g_bFeatureWifi);
158
159         int rv;
160         bool activated;
161         char *ifname = NULL;
162         char path_buff[MAX_PATH_LENGTH];
163
164         rv = wifi_manager_is_activated(wifi, &activated);
165         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to get Wi-Fi device state";
166
167         if (activated)
168                 goto done;
169
170         rv = wifi_manager_activate(wifi, __test_callback, NULL);
171         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to activate Wi-Fi device";
172
173         RUN_GMAIN_LOOP(__timeout_callback);
174
175         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Activetion failure";
176         rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
177
178 done:
179         rv = wifi_manager_get_network_interface_name(wifi, &ifname);
180         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to get wifi interface name";
181
182         g_snprintf(path_buff, MAX_PATH_LENGTH, "/sys/class/net/%s/address", ifname);
183         g_free(ifname);
184
185         rv = access(path_buff, F_OK);
186         EXPECT_EQ(0, rv) << "Could not access " << WIFI_ADDRESS_PATH;
187 }
188
189 TEST(WifiHaltest, WifiScan_p)
190 {
191         SKIP_NOT_SUPPORTED(g_bFeatureWifi);
192
193         int rv = wifi_manager_scan(wifi, __test_callback, NULL);
194         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to scan";
195
196         RUN_GMAIN_LOOP(__timeout_callback);
197
198         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Scan failure";
199         rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
200 }
201
202 TEST(WifiHaltest, WifiConnect_p)
203 {
204         SKIP_NOT_SUPPORTED(g_bFeatureWifi);
205
206         wifi_manager_connection_state_e connection_state;
207         int ret = WIFI_MANAGER_ERROR_NONE;
208
209         ret = wifi_manager_get_connection_state(wifi, &connection_state);
210         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to get connection state";
211
212         if (connection_state == WIFI_MANAGER_CONNECTION_STATE_CONNECTED)
213                 return;
214
215         if (!ap_name[0])
216                 return;
217
218         ret = wifi_manager_foreach_found_ap(wifi, __found_ap_callback, (char *)ap_name);
219         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Failed to find the AP";
220         rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
221
222         ret = wifi_manager_connect(wifi, g_hWifiAP, __test_callback, NULL);
223         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to connect";
224
225         RUN_GMAIN_LOOP(__timeout_callback);
226
227         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Connection failure";
228         rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
229 }
230
231 TEST(WifiHaltest, WifiDisconnect_p)
232 {
233         SKIP_NOT_SUPPORTED(g_bFeatureWifi);
234
235         int ret = WIFI_MANAGER_ERROR_NONE;
236         wifi_manager_ap_h hAP = NULL;
237
238         ret = wifi_manager_get_connected_ap(wifi, &hAP);
239         if (ret == WIFI_MANAGER_ERROR_NO_CONNECTION)
240                 return;
241         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to get connected AP";
242
243         ret = wifi_manager_disconnect(wifi, hAP, __test_callback, NULL);
244         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to disconnect";
245
246         RUN_GMAIN_LOOP(__timeout_callback);
247
248         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Disconnection failure";
249         rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
250
251         ret = wifi_manager_forget_ap(wifi, hAP);
252         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to forget AP";
253 }
254
255 TEST(WifiHaltest, WifiDeactivate_p)
256 {
257         SKIP_NOT_SUPPORTED(g_bFeatureWifi);
258
259         int rv = wifi_manager_deactivate(wifi, __test_callback, NULL);
260         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to deactivate Wi-Fi device";
261
262         RUN_GMAIN_LOOP(__timeout_callback);
263
264         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Deactivation failure";
265         rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
266 }
267
268 TEST(WifiHaltest, WifiDeinit_p)
269 {
270         SKIP_NOT_SUPPORTED(g_bFeatureWifi);
271
272         int rv = wifi_manager_deinitialize(wifi);
273         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Deinitialization failure";
274 }
275
276 TEST(WifiHaltest, WifiDirectInit_p)
277 {
278         g_bFeatureP2P = __check_feature_supported((char*)FEATURE_WIFIDIRECT);
279         SKIP_NOT_SUPPORTED(g_bFeatureP2P);
280
281         int rv = wifi_direct_initialize();
282         EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Initialization failure";
283 }
284
285 TEST(WifiHaltest, WifiDirectActivate_p)
286 {
287         SKIP_NOT_SUPPORTED(g_bFeatureP2P);
288
289         int rv;
290         wifi_direct_state_e state = WIFI_DIRECT_STATE_ACTIVATED;
291
292         rv = wifi_direct_get_state(&state);
293         ASSERT_EQ(0, rv) << "Failed to get Wi-Fi Direct device state";
294
295         if (state != WIFI_DIRECT_STATE_DEACTIVATED)
296                 goto done;
297
298         rv = wifi_direct_set_device_state_changed_cb(__device_state_changed_cb, NULL);
299         ASSERT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to set activation callback";
300
301         rv = wifi_direct_activate();
302         ASSERT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to activate Wi-Fi Direct device";
303
304         RUN_GMAIN_LOOP(__timeout_callback);
305
306         EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rst_p2p) << "Activetion failure";
307         rst_p2p = WIFI_DIRECT_ERROR_OPERATION_FAILED;
308
309         rv = wifi_direct_get_state(&state);
310         EXPECT_EQ(0, rv) << "Failed to get Wi-Fi Direct device state";
311         EXPECT_EQ(WIFI_DIRECT_STATE_ACTIVATED, state) << "Activetion failure";
312
313 done:
314         if (p2p_ifname[0])
315                 rv = access(p2p_ifname, F_OK);
316         else
317                 rv = access(WIFI_P2P_PATH, F_OK);
318         EXPECT_EQ(0, rv) << "Could not access " << WIFI_ADDRESS_PATH;
319 }
320
321 TEST(WifiHaltest, WifiDirectDeactivate_p)
322 {
323         SKIP_NOT_SUPPORTED(g_bFeatureP2P);
324
325         int rv;
326
327         rv = wifi_direct_deactivate();
328         ASSERT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to deactivate Wi-Fi Direct";
329
330         RUN_GMAIN_LOOP(__timeout_callback);
331
332         EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rst_p2p) << "Deactivation failure";
333         rst_p2p = WIFI_DIRECT_ERROR_OPERATION_FAILED;
334
335         wifi_direct_state_e state = WIFI_DIRECT_STATE_ACTIVATED;
336         rv = wifi_direct_get_state(&state);
337         EXPECT_EQ(0, rv) << "Failed to get Wi-Fi Direct device state";
338
339         EXPECT_EQ(WIFI_DIRECT_STATE_DEACTIVATED, state) << "Deactivetion failure";
340
341         rv = wifi_direct_unset_device_state_changed_cb();
342         EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to unset activation callback";
343 }
344
345 TEST(WifiHaltest, WifiDirectDeinit_p)
346 {
347         SKIP_NOT_SUPPORTED(g_bFeatureP2P);
348
349         int rv = wifi_direct_deinitialize();
350         EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Deinitialization failure";
351 }
352
353 TEST(WifiHaltest, SoftapInit_p)
354 {
355         g_bFeatureSoftap = __check_feature_supported((char*)FEATURE_SOFTAP);
356         SKIP_NOT_SUPPORTED(g_bFeatureSoftap);
357
358         int ret = SOFTAP_ERROR_NONE;
359
360         ret = softap_create(&sa);
361         ASSERT_EQ(SOFTAP_ERROR_NONE, ret) << "Initialization failure";
362
363         ret = softap_set_enabled_cb(sa, __enabled_cb, NULL);
364         EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Fail to set enabled callback!!";
365
366         ret = softap_set_disabled_cb(sa, __disabled_cb, NULL);
367         EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Fail to set disabled callback!!";
368 }
369
370 TEST(WifiHaltest, SoftapActivate_p)
371 {
372         SKIP_NOT_SUPPORTED(g_bFeatureSoftap);
373
374         int ret = SOFTAP_ERROR_NONE;
375         bool enabled = false;
376
377         ret = softap_is_enabled(sa, &enabled);
378         ASSERT_EQ(SOFTAP_ERROR_NONE, ret) << "Failed to get SoftAP state";
379
380         if (enabled)
381                 goto done;
382
383         ret = softap_enable(sa);
384         ASSERT_EQ(SOFTAP_ERROR_NONE, ret) << "Failed to enable SoftAP";
385
386         RUN_GMAIN_LOOP(__timeout_callback);
387
388         EXPECT_EQ(SOFTAP_ERROR_NONE, g_error) << "Failed to enable SoftAP";
389         EXPECT_EQ(true, g_is_requested) << "Failed to enable SoftAP";
390
391 done:
392         ret = access(WIFI_ADDRESS_PATH, F_OK);
393         EXPECT_EQ(0, ret) << "Could not access " << WIFI_ADDRESS_PATH;
394 }
395
396 TEST(WifiHaltest, SoftapDeactivate_p)
397 {
398         SKIP_NOT_SUPPORTED(g_bFeatureSoftap);
399
400         int ret = SOFTAP_ERROR_NONE;
401
402         ret = softap_disable(sa);
403         ASSERT_EQ(SOFTAP_ERROR_NONE, ret) << "Failed to disable SoftAP";
404
405         RUN_GMAIN_LOOP(__timeout_callback);
406
407         EXPECT_EQ(SOFTAP_ERROR_NONE, g_error) << "Failed to disable SoftAP" << g_code;
408 }
409
410 TEST(WifiHaltest, SoftapDeinit_p)
411 {
412         SKIP_NOT_SUPPORTED(g_bFeatureSoftap);
413
414         int ret = SOFTAP_ERROR_NONE;
415
416         ret = softap_unset_enabled_cb(sa);
417         EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Fail to unset enabled callback!!";
418
419         ret = softap_unset_disabled_cb(sa);
420         EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Fail to unset disabled callback!!";
421
422         ret = softap_destroy(sa);
423         EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Deinitialization failure";
424 }
425
426 int main(int argc, char **argv)
427 {
428         int ret = -1;
429
430         if (argc > 1 && argv[argc - 1][0] != '-') {
431                 g_strlcpy(ap_name, argv[argc - 1], MAX_AP_LENGTH);
432                 argc--;
433         }
434
435         __set_p2p_ifname();
436
437         try {
438                 InitGoogleTest(&argc, argv);
439         } catch(...) {
440                 std::cout << "Exception occurred." << std::endl;
441         }
442
443         try {
444                 ret = RUN_ALL_TESTS();
445         } catch (const ::testing::internal::GoogleTestFailureException& e) {
446                 ret = -1;
447                 std::cout << "GoogleTestFailureException was thrown:" << e.what() << std::endl;
448         }
449
450         return ret;
451 }