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-direct.h>
24 #include <system_info.h>
27 using ::testing::InitGoogleTest;
28 using ::testing::Test;
29 using ::testing::TestCase;
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];
35 static bool __check_feature_supported(char *key)
38 int ret = system_info_get_platform_bool(key, &value);
40 EXPECT_EQ(SYSTEM_INFO_ERROR_NONE, ret) << "system_info_get_platform_bool failed";
41 EXPECT_EQ(true, value) << key << " feature is not supported";
46 static gboolean __timeout_callback(gpointer data)
48 EXPECT_TRUE(0) << "Wi-Fi Direct callback timeout!";
53 static void __device_state_changed_cb(int error_code,
54 wifi_direct_device_state_e device_state, void *user_data)
57 dev_state = device_state;
61 static void __set_p2p_ifname(void)
63 GKeyFile *key_file = NULL;
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);
75 ifn = g_key_file_get_string(key_file, WFD_CONF_GROUP_NAME, "p2p_interface", &error);
77 g_snprintf(p2p_ifname, MAX_PATH_LENGTH, "/sys/class/net/%s/address", ifn);
80 g_clear_error(&error);
81 g_key_file_free(key_file);
85 TEST(Hal_wifi_direct, Init_p)
87 g_bFeatureP2P = __check_feature_supported((char*)WIFIDIRECT_FEATURE);
88 ASSERT_EQ(true, g_bFeatureP2P) << WIFIDIRECT_FEATURE << " feature is not supported";
90 int rv = wifi_direct_initialize();
91 EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Initialization failure";
94 TEST(Hal_wifi_direct, Activate_p)
96 ASSERT_EQ(true, g_bFeatureP2P) << WIFIDIRECT_FEATURE << " feature is not supported";
99 wifi_direct_state_e state = WIFI_DIRECT_STATE_ACTIVATED;
101 rv = wifi_direct_get_state(&state);
102 ASSERT_EQ(0, rv) << "Failed to get Wi-Fi Direct device state";
104 if (state != WIFI_DIRECT_STATE_DEACTIVATED)
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";
110 rv = wifi_direct_activate();
111 ASSERT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to activate Wi-Fi Direct device";
113 RUN_GMAIN_LOOP(__timeout_callback);
115 EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rst) << "Activetion failure";
116 rst = WIFI_DIRECT_ERROR_OPERATION_FAILED;
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";
124 rv = access(p2p_ifname, F_OK);
126 rv = access(WIFI_P2P_PATH, F_OK);
127 EXPECT_EQ(0, rv) << "Could not access " << WIFI_ADDRESS_PATH;
130 TEST(Hal_wifi_direct, Deactivate_p)
132 ASSERT_EQ(true, g_bFeatureP2P) << WIFIDIRECT_FEATURE << " feature is not supported";
136 rv = wifi_direct_deactivate();
137 ASSERT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to deactivate Wi-Fi Direct";
139 RUN_GMAIN_LOOP(__timeout_callback);
141 EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rst) << "Deactivation failure";
142 rst = WIFI_DIRECT_ERROR_OPERATION_FAILED;
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";
148 EXPECT_EQ(WIFI_DIRECT_STATE_DEACTIVATED, state) << "Deactivetion failure";
151 rv = access(p2p_ifname, F_OK);
153 rv = access(WIFI_P2P_PATH, F_OK);
154 EXPECT_EQ(-1, rv) << WIFI_P2P_PATH << " is exist";
156 rv = wifi_direct_unset_device_state_changed_cb();
157 EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Failed to unset activation callback";
160 TEST(Hal_wifi_direct, Deinit_p)
162 ASSERT_EQ(true, g_bFeatureP2P) << WIFIDIRECT_FEATURE << " feature is not supported";
164 int rv = wifi_direct_deinitialize();
165 EXPECT_EQ(WIFI_DIRECT_ERROR_NONE, rv) << "Deinitialization failure";
168 int main(int argc, char **argv)
172 InitGoogleTest(&argc, argv);
173 return RUN_ALL_TESTS();