Merge "Avoid Null Check before g_free()." into tizen
[platform/core/connectivity/net-config.git] / unittest / wifi_direct_hal_tc.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-direct.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 int rst = WIFI_DIRECT_ERROR_OPERATION_FAILED;
32 static wifi_direct_device_state_e dev_state = WIFI_DIRECT_DEVICE_STATE_DEACTIVATED;
33 static char p2p_ifname[MAX_PATH_LENGTH];
34
35 static bool __check_feature_supported(char *key)
36 {
37         bool value = false;
38         int ret = system_info_get_platform_bool(key, &value);
39
40         EXPECT_EQ(SYSTEM_INFO_ERROR_NONE, ret) << "system_info_get_platform_bool failed";
41         EXPECT_EQ(true, value) << key << " feature is not supported";
42
43         return value;
44 }
45
46 static gboolean __timeout_callback(gpointer data)
47 {
48         EXPECT_TRUE(0) << "Wi-Fi Direct callback timeout!";
49         QUIT_GMAIN_LOOP;
50         return FALSE;
51 }
52
53 static void __device_state_changed_cb(int error_code,
54                 wifi_direct_device_state_e device_state, void *user_data)
55 {
56         rst = error_code;
57         dev_state = device_state;
58         QUIT_GMAIN_LOOP;
59 }
60
61 static void __set_p2p_ifname(void)
62 {
63         GKeyFile *key_file = NULL;
64         GError *error = NULL;
65         char *ifn = NULL;
66
67         key_file = g_key_file_new();
68         if (!g_key_file_load_from_file(key_file, WIFI_P2P_CONFIG_PATH, G_KEY_FILE_NONE, &error)) {
69                 g_clear_error(&error);
70                 g_key_file_free(key_file);
71                 key_file = NULL;
72                 return;
73         }
74
75         ifn = g_key_file_get_string(key_file, WFD_CONF_GROUP_NAME, "p2p_interface", &error);
76         if (ifn)
77                 g_snprintf(p2p_ifname, MAX_PATH_LENGTH, "/sys/class/net/%s/address", ifn);
78
79         if (error)
80                 g_clear_error(&error);
81         g_key_file_free(key_file);
82 }
83
84
85 TEST(Hal_wifi_direct, Init_p)
86 {
87         g_bFeatureP2P = __check_feature_supported((char*)WIFIDIRECT_FEATURE);
88         ASSERT_EQ(true, g_bFeatureP2P) << WIFIDIRECT_FEATURE << " feature is not supported";
89
90         int rv = wifi_direct_initialize();
91         EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Initialization failure";
92 }
93
94 TEST(Hal_wifi_direct, Activate_p)
95 {
96         ASSERT_EQ(true, g_bFeatureP2P) << WIFIDIRECT_FEATURE << " feature is not supported";
97
98         int rv;
99         wifi_direct_state_e state = WIFI_DIRECT_STATE_ACTIVATED;
100
101         rv = wifi_direct_get_state(&state);
102         ASSERT_EQ(0, rv) << "Failed to get Wi-Fi Direct device state";
103
104         if (state != WIFI_DIRECT_STATE_DEACTIVATED)
105                 goto done;
106
107         rv = wifi_direct_set_device_state_changed_cb(__device_state_changed_cb, NULL);
108         ASSERT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to set activation callback";
109
110         rv = wifi_direct_activate();
111         ASSERT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to activate Wi-Fi Direct device";
112
113         RUN_GMAIN_LOOP(__timeout_callback);
114
115         EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rst) << "Activetion failure";
116         rst = WIFI_DIRECT_ERROR_OPERATION_FAILED;
117
118         rv = wifi_direct_get_state(&state);
119         EXPECT_EQ(0, rv) << "Failed to get Wi-Fi Direct device state";
120         EXPECT_EQ(WIFI_DIRECT_STATE_ACTIVATED, state) << "Activetion failure";
121
122 done:
123         if (p2p_ifname[0])
124                 rv = access(p2p_ifname, F_OK);
125         else
126                 rv = access(WIFI_P2P_PATH, F_OK);
127         EXPECT_EQ(0, rv) << "Could not access " << WIFI_ADDRESS_PATH;
128 }
129
130 TEST(Hal_wifi_direct, Deactivate_p)
131 {
132         ASSERT_EQ(true, g_bFeatureP2P) << WIFIDIRECT_FEATURE << " feature is not supported";
133
134         int rv;
135
136         rv = wifi_direct_deactivate();
137         ASSERT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to deactivate Wi-Fi Direct";
138
139         RUN_GMAIN_LOOP(__timeout_callback);
140
141         EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rst) << "Deactivation failure";
142         rst = WIFI_DIRECT_ERROR_OPERATION_FAILED;
143
144         wifi_direct_state_e state = WIFI_DIRECT_STATE_ACTIVATED;
145         rv = wifi_direct_get_state(&state);
146         EXPECT_EQ(0, rv) << "Failed to get Wi-Fi Direct device state";
147
148         EXPECT_EQ(WIFI_DIRECT_STATE_DEACTIVATED, state) << "Deactivetion failure";
149
150         if (p2p_ifname[0])
151                 rv = access(p2p_ifname, F_OK);
152         else
153                 rv = access(WIFI_P2P_PATH, F_OK);
154         EXPECT_EQ(-1, rv) << WIFI_P2P_PATH << " is exist";
155
156         rv = wifi_direct_unset_device_state_changed_cb();
157         EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to unset activation callback";
158 }
159
160 TEST(Hal_wifi_direct, Deinit_p)
161 {
162         ASSERT_EQ(true, g_bFeatureP2P) << WIFIDIRECT_FEATURE << " feature is not supported";
163
164         int rv = wifi_direct_deinitialize();
165         EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Deinitialization failure";
166 }
167
168 int main(int argc, char **argv)
169 {
170         __set_p2p_ifname();
171
172         InitGoogleTest(&argc, argv);
173         return RUN_ALL_TESTS();
174 }