Revert "Revise driver load routine"
[platform/core/connectivity/net-config.git] / unittest / gtest_hal_wifi.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 <system_info.h>
25 #include "unittest.h"
26
27 using ::testing::InitGoogleTest;
28 using ::testing::Test;
29 using ::testing::TestCase;
30
31 static char ap_name[MAX_AP_LENGTH];
32 static wifi_manager_h wifi = NULL;
33 static wifi_manager_error_e rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
34 wifi_manager_ap_h g_hWifiAP;
35
36
37 static bool __check_feature_supported(char *key)
38 {
39         bool value = false;
40         int ret = system_info_get_platform_bool(key, &value);
41
42         EXPECT_EQ(SYSTEM_INFO_ERROR_NONE, ret) << "system_info_get_platform_bool failed";
43         EXPECT_EQ(true, value) << key << " feature is not supported";
44
45         return value;
46 }
47
48 static void __test_callback(wifi_manager_error_e result, void* user_data)
49 {
50         rst = result;
51         QUIT_GMAIN_LOOP;
52 }
53
54 static gboolean __timeout_callback(gpointer data)
55 {
56         EXPECT_TRUE(0) << "Wi-Fi Manager callback timeout!";
57         QUIT_GMAIN_LOOP;
58         return FALSE;
59 }
60
61 static bool __found_ap_callback(wifi_manager_ap_h ap, void *user_data)
62 {
63         char *ap_name = NULL;
64         char *ap_name_part = (char *)user_data;
65         size_t ap_name_len = strlen(ap_name_part);
66         int ret = WIFI_MANAGER_ERROR_NONE;
67
68         if (ap) {
69                 ret = wifi_manager_ap_get_essid(ap, &ap_name);
70                 EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Unable to get essid";
71
72                 if (!ap_name)
73                         return true;
74
75                 if (!strncmp(ap_name, ap_name_part, ap_name_len)) {
76                         g_free(ap_name);
77                         ret = wifi_manager_ap_clone(&g_hWifiAP, ap);
78                         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Unable to clone the AP handle";
79                         if (g_hWifiAP) {
80                                 rst = WIFI_MANAGER_ERROR_NONE;
81                                 return false;
82                         }
83                 }
84         }
85
86         return true;
87 }
88
89 TEST(Hal_wifi, Init_p)
90 {
91         g_bFeatureWifi = __check_feature_supported((char*)FEATURE_WIFI);
92         ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
93
94         int rv = wifi_manager_initialize(&wifi);
95         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Initialization failure";
96 }
97
98 TEST(Hal_wifi, Activate_p)
99 {
100         ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
101
102         int rv;
103         bool activated;
104
105         rv = wifi_manager_is_activated(wifi, &activated);
106         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to get Wi-Fi device state";
107
108         if (activated)
109                 goto done;
110
111         rv = wifi_manager_activate(wifi, __test_callback, NULL);
112         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to activate Wi-Fi device";
113
114         RUN_GMAIN_LOOP(__timeout_callback);
115
116         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Activetion failure";
117         rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
118
119 done:
120         rv = access(WIFI_ADDRESS_PATH, F_OK);
121         EXPECT_EQ(0, rv) << "Could not access " << WIFI_ADDRESS_PATH;
122 }
123
124 TEST(Hal_wifi, Scan_p)
125 {
126         ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
127
128         int rv = wifi_manager_scan(wifi, __test_callback, NULL);
129         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to scan";
130
131         RUN_GMAIN_LOOP(__timeout_callback);
132
133         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Scan failure";
134         rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
135 }
136
137 TEST(Hal_wifi, Connect_p)
138 {
139         ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
140
141         wifi_manager_connection_state_e connection_state;
142         int ret = WIFI_MANAGER_ERROR_NONE;
143
144         ret = wifi_manager_get_connection_state(wifi, &connection_state);
145         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to get connection state";
146
147         if (connection_state == WIFI_MANAGER_CONNECTION_STATE_CONNECTED)
148                 return;
149
150         if (!ap_name[0])
151                 return;
152
153         ret = wifi_manager_foreach_found_ap(wifi, __found_ap_callback, (char *)ap_name);
154         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Failed to find the AP";
155         rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
156
157         ret = wifi_manager_connect(wifi, g_hWifiAP, __test_callback, NULL);
158         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to connect";
159
160         RUN_GMAIN_LOOP(__timeout_callback);
161
162         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Connection failure";
163         rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
164 }
165
166 TEST(Hal_wifi, Disconnect_p)
167 {
168         ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
169
170         int ret = WIFI_MANAGER_ERROR_NONE;
171         wifi_manager_ap_h hAP = NULL;
172
173         ret = wifi_manager_get_connected_ap(wifi, &hAP);
174         if (ret == WIFI_MANAGER_ERROR_NO_CONNECTION)
175                 return;
176         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to get connected AP";
177
178         ret = wifi_manager_disconnect(wifi, hAP, __test_callback, NULL);
179         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to disconnect";
180
181         RUN_GMAIN_LOOP(__timeout_callback);
182
183         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Disconnection failure";
184         rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
185
186         ret = wifi_manager_forget_ap(wifi, hAP);
187         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, ret) << "Failed to forget AP";
188 }
189
190 TEST(Hal_wifi, Deactivate_p)
191 {
192         ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
193
194         int rv = wifi_manager_deactivate(wifi, __test_callback, NULL);
195         ASSERT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Failed to deactivate Wi-Fi device";
196
197         RUN_GMAIN_LOOP(__timeout_callback);
198
199         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rst) << "Deactivation failure";
200         rst = WIFI_MANAGER_ERROR_NOT_SUPPORTED;
201
202         rv = access(WIFI_ADDRESS_PATH, F_OK);
203         EXPECT_EQ(-1, rv) << WIFI_ADDRESS_PATH << " is exist";
204 }
205
206 TEST(Hal_wifi, Deinit_p)
207 {
208         ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
209
210         int rv = wifi_manager_deinitialize(wifi);
211         EXPECT_EQ(WIFI_MANAGER_ERROR_NONE, rv) << "Deinitialization failure";
212 }
213
214 int main(int argc, char **argv)
215 {
216         if (argc > 1 && argv[argc - 1][0] != '-') {
217                 g_strlcpy(ap_name, argv[argc - 1], MAX_AP_LENGTH);
218                 argc--;
219         }
220
221         InitGoogleTest(&argc, argv);
222         return RUN_ALL_TESTS();
223 }