Separate HAL TC code into a separate package
[platform/core/connectivity/net-config.git] / unittest / hostap_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 <system_info.h>
24 #include <softap.h>
25 #include "unittest.h"
26
27 using ::testing::InitGoogleTest;
28 using ::testing::Test;
29 using ::testing::TestCase;
30
31 static softap_h sa = NULL;
32 static bool g_is_requested;
33 static softap_error_e g_error;
34 static softap_disabled_cause_e g_code;
35
36 static bool __check_feature_supported(char *key)
37 {
38         bool value = false;
39         int ret = system_info_get_platform_bool(key, &value);
40
41         EXPECT_EQ(SYSTEM_INFO_ERROR_NONE, ret) << "system_info_get_platform_bool failed";
42         EXPECT_EQ(true, value) << key << " feature is not supported";
43
44         return value;
45 }
46
47 static gboolean __timeout_callback(gpointer data)
48 {
49         EXPECT_TRUE(0) << "SoftAP callback timeout!";
50         QUIT_GMAIN_LOOP;
51         return FALSE;
52 }
53
54 static void __enabled_cb(softap_error_e error, bool is_requested, void *data)
55 {
56         g_error = error;
57         g_is_requested = is_requested;
58         QUIT_GMAIN_LOOP;
59 }
60
61 static void __disabled_cb(softap_error_e error, softap_disabled_cause_e code, void *data)
62 {
63         g_error = error;
64         g_code = code;
65         QUIT_GMAIN_LOOP;
66 }
67
68 TEST(Hal_softap, Init_p)
69 {
70         g_bFeatureWifi = __check_feature_supported((char*)FEATURE_WIFI);
71         ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
72
73         int ret = SOFTAP_ERROR_NONE;
74
75         ret = softap_create(&sa);
76         ASSERT_EQ(SOFTAP_ERROR_NONE, ret) << "Initialization failure";
77
78         ret = softap_set_enabled_cb(sa, __enabled_cb, NULL);
79         EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Fail to set enabled callback!!";
80
81         ret = softap_set_disabled_cb(sa, __disabled_cb, NULL);
82         EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Fail to set disabled callback!!";
83 }
84
85 TEST(Hal_softap, Activate_p)
86 {
87         ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
88
89         int ret = SOFTAP_ERROR_NONE;
90         bool enabled = false;
91
92         ret = softap_is_enabled(sa, &enabled);
93         ASSERT_EQ(SOFTAP_ERROR_NONE, ret) << "Failed to get SoftAP state";
94
95         if (enabled)
96                 goto done;
97
98         ret = softap_enable(sa);
99         ASSERT_EQ(SOFTAP_ERROR_NONE, ret) << "Failed to enable SoftAP";
100
101         RUN_GMAIN_LOOP(__timeout_callback);
102
103         EXPECT_EQ(SOFTAP_ERROR_NONE, g_error) << "Failed to enable SoftAP";
104         EXPECT_EQ(true, g_is_requested) << "Failed to enable SoftAP";
105
106 done:
107         ret = access(WIFI_ADDRESS_PATH, F_OK);
108         EXPECT_EQ(0, ret) << "Could not access " << WIFI_ADDRESS_PATH;
109 }
110
111 TEST(Hal_softap, Deactivate_p)
112 {
113         ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
114
115         int ret = SOFTAP_ERROR_NONE;
116
117         ret = softap_disable(sa);
118         ASSERT_EQ(SOFTAP_ERROR_NONE, ret) << "Failed to disable SoftAP";
119
120         RUN_GMAIN_LOOP(__timeout_callback);
121
122         EXPECT_EQ(SOFTAP_ERROR_NONE, g_error) << "Failed to disable SoftAP" << g_code;
123
124         ret = access(WIFI_ADDRESS_PATH, F_OK);
125         EXPECT_EQ(-1, ret) << WIFI_ADDRESS_PATH << " is exist";
126 }
127
128 TEST(Hal_softap, Deinit_p)
129 {
130         ASSERT_EQ(true, g_bFeatureWifi) << FEATURE_WIFI << " feature is not supported";
131
132         int ret = SOFTAP_ERROR_NONE;
133
134         ret = softap_unset_enabled_cb(sa);
135         EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Fail to unset enabled callback!!";
136
137         ret = softap_unset_disabled_cb(sa);
138         EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Fail to unset disabled callback!!";
139
140         ret = softap_destroy(sa);
141         EXPECT_EQ(SOFTAP_ERROR_NONE, ret) << "Deinitialization failure";
142 }
143
144 int main(int argc, char **argv)
145 {
146         InitGoogleTest(&argc, argv);
147         return RUN_ALL_TESTS();
148 }