Extract SoftApVconf class 96/303296/5
authorcheoleun moon <chleun.moon@samsung.com>
Fri, 22 Dec 2023 15:03:16 +0000 (00:03 +0900)
committercheoleun moon <chleun.moon@samsung.com>
Wed, 27 Dec 2023 05:44:59 +0000 (14:44 +0900)
The softap.cpp is a very long file with 1704 lines.
This made it difficult to understand the code, so it was necessary to reduce the length of the file.
First, the functions that use vconf to get data were moved to be processed in SoftApVconf.

Test Result
[   15s] [----------] 8 tests from SoftapVconfTest
[   15s] [ RUN      ] SoftapVconfTest.getCommonSsidP
[   15s] [       OK ] SoftapVconfTest.getCommonSsidP (0 ms)
[   15s] [ RUN      ] SoftapVconfTest.getCommonSsidN
[   15s] [       OK ] SoftapVconfTest.getCommonSsidN (8 ms)
[   15s] [ RUN      ] SoftapVconfTest.getSecurityTypeP
[   15s] [       OK ] SoftapVconfTest.getSecurityTypeP (0 ms)
[   15s] [ RUN      ] SoftapVconfTest.getSecurityTypeN
[   15s] [       OK ] SoftapVconfTest.getSecurityTypeN (0 ms)
[   15s] [ RUN      ] SoftapVconfTest.getVisibilityP
[   15s] [       OK ] SoftapVconfTest.getVisibilityP (0 ms)
[   15s] [ RUN      ] SoftapVconfTest.getVisibilityN
[   15s] [       OK ] SoftapVconfTest.getVisibilityN (0 ms)
[   15s] [ RUN      ] SoftapVconfTest.getVsieP
[   15s] [       OK ] SoftapVconfTest.getVsieP (0 ms)
[   15s] [ RUN      ] SoftapVconfTest.getVsieN
[   15s] [       OK ] SoftapVconfTest.getVsieN (0 ms)
[   15s] [----------] 8 tests from SoftapVconfTest (11 ms total)

Change-Id: I640077183444a18bc4d9f76164522de92bfa975d

src/SoftapVconf.cpp [new file with mode: 0644]
src/include/SoftapVconf.h [new file with mode: 0644]
src/softap.cpp
tests/mocks/softap_vconf.c
tests/softap-gtest-vconf.cpp [new file with mode: 0644]

diff --git a/src/SoftapVconf.cpp b/src/SoftapVconf.cpp
new file mode 100644 (file)
index 0000000..0d7b9d9
--- /dev/null
@@ -0,0 +1,116 @@
+#include "softap_dlog.h"
+#include "softap_private.h"
+#include "SoftapVconf.h"
+
+#include <glib.h>
+
+int SoftapVconf::getCommonSsid(char *ssid, unsigned int size)
+{
+       if (ssid == NULL) {
+               ERR("ssid is null!!");
+               return SOFTAP_ERROR_INVALID_PARAMETER;
+       }
+
+       char *device_name = NULL;
+       char *end = NULL;
+
+       device_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
+       if (device_name == NULL) {
+               ERR("vconf_get_str is failed and set default ssid!!");
+               g_strlcpy(ssid, SOFTAP_DEFAULT_SSID, size);
+       } else
+               g_strlcpy(ssid, device_name, size);
+
+       if (!g_utf8_validate(ssid, -1, (const char **)&end))
+               *end = '\0';
+
+       free(device_name);
+
+       return SOFTAP_ERROR_NONE;
+}
+
+softap_error_e SoftapVconf::getSecurityType(softap_security_type_e *security_type)
+{
+       if (security_type == NULL) {
+               ERR("Invalid param\n");
+               return SOFTAP_ERROR_INVALID_PARAMETER;
+       }
+
+       if (vconf_get_int(VCONFKEY_SOFTAP_SECURITY,
+                               (int *)security_type) < 0) {
+               ERR("vconf_get_int is failed\n");
+               return SOFTAP_ERROR_OPERATION_FAILED;
+       }
+
+       return SOFTAP_ERROR_NONE;
+}
+
+softap_error_e SoftapVconf::getVisibility(bool *visible)
+{
+       if (visible == NULL) {
+               ERR("Invalid param\n");
+               return SOFTAP_ERROR_INVALID_PARAMETER;
+       }
+
+       int hide = 0;
+
+       if (vconf_get_int(VCONFKEY_SOFTAP_HIDE, &hide) < 0) {
+               ERR("vconf_get_int is failed\n");
+               return SOFTAP_ERROR_OPERATION_FAILED;
+       }
+
+       if (hide)
+               *visible = false;
+       else
+               *visible = true;
+
+       return SOFTAP_ERROR_NONE;
+}
+
+softap_error_e SoftapVconf::getVsie(char *vsie, unsigned int size)
+{
+       if (vsie == NULL) {
+               ERR("vsie is NULL\n");
+               return SOFTAP_ERROR_INVALID_PARAMETER;
+       }
+       char *ptr = NULL;
+
+       ptr = vconf_get_str(VCONFKEY_SOFTAP_VSIE);
+       if (ptr == NULL)
+               return SOFTAP_ERROR_OPERATION_FAILED;
+
+       g_strlcpy(vsie, ptr, size);
+       free(ptr);
+
+       return SOFTAP_ERROR_NONE;
+}
+
+softap_error_e SoftapVconf::getChannel(int *channel)
+{
+       if (channel == NULL) {
+               ERR("Invalid param\n");
+               return SOFTAP_ERROR_INVALID_PARAMETER;
+       }
+
+       if (vconf_get_int(VCONFKEY_SOFTAP_CHANNEL, channel) < 0) {
+               ERR("vconf_get_int is failed\n");
+               return SOFTAP_ERROR_OPERATION_FAILED;
+       }
+
+       return SOFTAP_ERROR_NONE;
+}
+
+softap_error_e SoftapVconf::getMode(softap_wireless_mode_e *mode)
+{
+       if (mode == NULL) {
+               ERR("Invalid param\n");
+               return SOFTAP_ERROR_INVALID_PARAMETER;
+       }
+
+       if (vconf_get_int(VCONFKEY_SOFTAP_MODE, (int *)mode) < 0) {
+               ERR("vconf_get_int is failed\n");
+               return SOFTAP_ERROR_OPERATION_FAILED;
+       }
+
+       return SOFTAP_ERROR_NONE;
+}
\ No newline at end of file
diff --git a/src/include/SoftapVconf.h b/src/include/SoftapVconf.h
new file mode 100644 (file)
index 0000000..03b826d
--- /dev/null
@@ -0,0 +1,15 @@
+#pragma once
+
+#include "softap.h"
+
+#include <vconf.h>
+
+class SoftapVconf {
+public:
+    static int getCommonSsid(char *ssid, unsigned int size);
+    static softap_error_e getSecurityType(softap_security_type_e *security_type);
+    static softap_error_e getVisibility(bool *visible);
+    static softap_error_e getVsie(char *vsie, unsigned int size);
+    static softap_error_e getChannel(int *channel);
+    static softap_error_e getMode(softap_wireless_mode_e *mode);
+};
\ No newline at end of file
index a79e228c4729e0b213a3fdc8c3b232007c923256..d124b11c1ef2d876184d144448c5cda26a11c686 100755 (executable)
@@ -25,7 +25,6 @@
 #include <unistd.h>
 #include <dbus/dbus.h>
 #include <gio/gio.h>
-#include <vconf.h>
 
 #include "softap_private.h"
 #include "softap_gdbus.h"
 
 #include "SoftapEventHandler.h"
 #include "SoftapEventCallback.h"
+#include "SoftapVconf.h"
 
 static int retry = 0;
 
-static int __get_common_ssid(char *ssid, unsigned int size)
-{
-       if (ssid == NULL) {
-               ERR("ssid is null!!");
-               return SOFTAP_ERROR_INVALID_PARAMETER;
-       }
-
-       char *device_name = NULL;
-       char *end = NULL;
-
-       device_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
-       if (device_name == NULL) {
-               ERR("vconf_get_str is failed and set default ssid!!");
-               g_strlcpy(ssid, SOFTAP_DEFAULT_SSID, size);
-       } else
-               g_strlcpy(ssid, device_name, size);
-
-       if (!g_utf8_validate(ssid, -1, (const char **)&end))
-               *end = '\0';
-
-       free(device_name);
-
-       return SOFTAP_ERROR_NONE;
-}
-
-static softap_error_e __get_security_type(softap_security_type_e *security_type)
-{
-       if (security_type == NULL) {
-               ERR("Invalid param\n");
-               return SOFTAP_ERROR_INVALID_PARAMETER;
-       }
-
-       if (vconf_get_int(VCONFKEY_SOFTAP_SECURITY,
-                               (int *)security_type) < 0) {
-               ERR("vconf_get_int is failed\n");
-               return SOFTAP_ERROR_OPERATION_FAILED;
-       }
-
-       return SOFTAP_ERROR_NONE;
-}
-
-static softap_error_e __get_visibility(bool *visible)
-{
-       if (visible == NULL) {
-               ERR("Invalid param\n");
-               return SOFTAP_ERROR_INVALID_PARAMETER;
-       }
-
-       int hide = 0;
-
-       if (vconf_get_int(VCONFKEY_SOFTAP_HIDE, &hide) < 0) {
-               ERR("vconf_get_int is failed\n");
-               return SOFTAP_ERROR_OPERATION_FAILED;
-       }
-
-       if (hide)
-               *visible = false;
-       else
-               *visible = true;
-
-       return SOFTAP_ERROR_NONE;
-}
-
-static softap_error_e __get_vsie(char *vsie, unsigned int size)
-{
-       char *ptr = NULL;
-
-       ptr = vconf_get_str(VCONFKEY_SOFTAP_VSIE);
-       if (ptr == NULL)
-               return SOFTAP_ERROR_OPERATION_FAILED;
-
-       g_strlcpy(vsie, ptr, size);
-       free(ptr);
-
-       return SOFTAP_ERROR_NONE;
-}
-
-static softap_error_e __get_channel(int *channel)
-{
-       if (channel == NULL) {
-               ERR("Invalid param\n");
-               return SOFTAP_ERROR_INVALID_PARAMETER;
-       }
-
-       if (vconf_get_int(VCONFKEY_SOFTAP_CHANNEL, channel) < 0) {
-               ERR("vconf_get_int is failed\n");
-               return SOFTAP_ERROR_OPERATION_FAILED;
-       }
-
-       return SOFTAP_ERROR_NONE;
-}
-
-static softap_error_e __get_mode(softap_wireless_mode_e *mode)
-{
-       if (mode == NULL) {
-               ERR("Invalid param\n");
-               return SOFTAP_ERROR_INVALID_PARAMETER;
-       }
-
-       if (vconf_get_int(VCONFKEY_SOFTAP_MODE, (int *)mode) < 0) {
-               ERR("vconf_get_int is failed\n");
-               return SOFTAP_ERROR_OPERATION_FAILED;
-       }
-
-       return SOFTAP_ERROR_NONE;
-}
-
 static int __get_initial_passphrase(char *passphrase, unsigned int size)
 {
        if (passphrase == NULL ||
@@ -599,7 +492,7 @@ API int softap_create(softap_h *softap)
        sa->sec_type = SOFTAP_SECURITY_TYPE_WPA2_PSK;
 
        char ssid[SOFTAP_SSID_MAX_LEN + 1] = {0, };
-       if (__get_common_ssid(ssid, sizeof(ssid)) != SOFTAP_ERROR_NONE) {
+       if (SoftapVconf::getCommonSsid(ssid, sizeof(ssid)) != SOFTAP_ERROR_NONE) {
                ERR("Fail to get default ssid!!");
                free(sa);
                return SOFTAP_ERROR_OPERATION_FAILED;
@@ -1315,7 +1208,7 @@ API int softap_get_security_type(softap_h softap, softap_security_type_e *type)
        _retvm_if(type == NULL, SOFTAP_ERROR_INVALID_PARAMETER,
                                "parameter(type) is NULL\n");
 
-       return __get_security_type(type);
+       return SoftapVconf::getSecurityType(type);
 }
 
 API int softap_set_ssid(softap_h softap, const char *ssid)
@@ -1368,7 +1261,7 @@ API int softap_get_ssid(softap_h softap, char **ssid)
        char val[SOFTAP_SSID_MAX_LEN + 1] = {0, };
 
        if (__get_ssid_from_vconf(VCONFKEY_SOFTAP_SSID, val, sizeof(val)) == false) {
-               if (__get_common_ssid(val, sizeof(val)) != SOFTAP_ERROR_NONE) {
+               if (SoftapVconf::getCommonSsid(val, sizeof(val)) != SOFTAP_ERROR_NONE) {
                        ERR("Fail to get default ssid!!");
                        free(sa);
                        return SOFTAP_ERROR_OPERATION_FAILED;
@@ -1411,7 +1304,7 @@ API int softap_get_ssid_visibility(softap_h softap, bool *visible)
        _retvm_if(visible == NULL, SOFTAP_ERROR_INVALID_PARAMETER,
                        "parameter(visible) is NULL\n");
 
-       return __get_visibility(visible);
+       return SoftapVconf::getVisibility(visible);
 }
 
 API int softap_set_passphrase(softap_h softap, const char *passphrase)
@@ -1530,7 +1423,7 @@ API int softap_get_vendor_element(softap_h softap, char **vendor_element)
 
        char val[SOFTAP_VENDOR_MAX_LEN + 1] = {0, };
 
-       int ret = __get_vsie(val, sizeof(val));
+       int ret = SoftapVconf::getVsie(val, sizeof(val));
        if (ret != SOFTAP_ERROR_NONE) {
                ERR("Failed to get vendor elements");
                return ret;
@@ -1600,7 +1493,7 @@ API int softap_get_channel(softap_h softap, int *channel)
        _retvm_if(channel == NULL, SOFTAP_ERROR_INVALID_PARAMETER,
                                "parameter(channel) is NULL\n");
 
-       return __get_channel(channel);
+       return SoftapVconf::getChannel(channel);
 }
 
 API int softap_set_mode(softap_h softap, softap_wireless_mode_e mode)
@@ -1630,7 +1523,7 @@ API int softap_get_mode(softap_h softap, softap_wireless_mode_e *mode)
 
        __softap_h *sa = (__softap_h *)softap;
        *mode = sa->mode;
-       return __get_mode(mode);
+       return SoftapVconf::getMode(mode);
 }
 
 API int softap_enable_dhcp(softap_h softap)
index c6e3aa9e3e2540c7e5a4d48bbc29f5cc2f15cf15..a36c7b9876f3a2ddd004c0dc5102b6cc02cee9dc 100644 (file)
@@ -43,12 +43,11 @@ API int __wrap_vconf_get_int(const char *in_key, int *intval)
        if (!softap_mock_vconf_result)
                return -1;
 
+       *intval = 0;
        if (softap_mock_enabled_state
                        && strncmp(in_key, VCONFKEY_MOBILE_HOTSPOT_MODE,
                                strlen(VCONFKEY_MOBILE_HOTSPOT_MODE)) == 0)
                *intval |= VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI_AP;
-       else
-               *intval = 0;
 
        return 0;
 }
diff --git a/tests/softap-gtest-vconf.cpp b/tests/softap-gtest-vconf.cpp
new file mode 100644 (file)
index 0000000..f1d385c
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+* Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+#include <gtest/gtest.h>
+
+#include "SoftapVconf.h"
+#include "SoftapEventCallback.h"
+#include "softap_gdbus.h"
+
+extern "C" {
+#include "mocks/softap_memory.h"
+#include "mocks/softap_vconf.h"
+}
+
+class SoftapVconfTest: public ::testing::Test {
+       protected:
+               void SetUp() override
+               {
+                       softap_mock_set_memory_result(true);
+                       softap_mock_set_vconf_result(true);
+                       softap_mock_set_enabled_state(true);
+               }
+
+               void TearDown() override
+               {
+                       softap_mock_set_memory_result(true);
+                       softap_mock_set_vconf_result(false);
+                       softap_mock_set_enabled_state(false);
+               }
+};
+
+TEST_F(SoftapVconfTest, getCommonSsidP)
+{
+       char ssid[100];
+       EXPECT_EQ(SOFTAP_ERROR_NONE, SoftapVconf::getCommonSsid(ssid, 100));
+       EXPECT_STREQ("vconf result string", ssid);
+}
+
+TEST_F(SoftapVconfTest, getCommonSsidN)
+{
+       EXPECT_EQ(SOFTAP_ERROR_INVALID_PARAMETER,
+               SoftapVconf::getCommonSsid(NULL, 0));
+}
+
+TEST_F(SoftapVconfTest, getSecurityTypeP)
+{
+       softap_security_type_e type;
+       EXPECT_EQ(SOFTAP_ERROR_NONE, SoftapVconf::getSecurityType(&type));
+       EXPECT_EQ(0, type);
+}
+
+TEST_F(SoftapVconfTest, getSecurityTypeN)
+{
+       EXPECT_EQ(SOFTAP_ERROR_INVALID_PARAMETER,
+               SoftapVconf::getSecurityType(NULL));
+}
+
+TEST_F(SoftapVconfTest, getVisibilityP)
+{
+       bool visible;
+       EXPECT_EQ(SOFTAP_ERROR_NONE, SoftapVconf::getVisibility(&visible));
+       EXPECT_EQ(true, visible);
+}
+
+TEST_F(SoftapVconfTest, getVisibilityN)
+{
+       EXPECT_EQ(SOFTAP_ERROR_INVALID_PARAMETER,
+               SoftapVconf::getVisibility(NULL));
+}
+
+TEST_F(SoftapVconfTest, getVsieP)
+{
+       char vsie[100];
+       EXPECT_EQ(SOFTAP_ERROR_NONE, SoftapVconf::getVsie(vsie, 100));
+       EXPECT_STREQ("vconf result string", vsie);
+}
+
+TEST_F(SoftapVconfTest, getVsieN)
+{
+       EXPECT_EQ(SOFTAP_ERROR_INVALID_PARAMETER,
+               SoftapVconf::getVsie(NULL, 0));
+}