Merge "Add LCOV macro for coverage" into tizen
[platform/core/api/connection.git] / unittest / utc-connection-common.c
1 //
2 // Copyright (c) 2020 Samsung Electronics Co., Ltd.
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 "utc-connection-common.h"
18
19 GMainLoop *g_pMainLoop;
20 guint g_nTimeoutId;
21 int g_CallbackRet;
22
23 bool wifi_supported;
24 bool telephony_supported;
25 bool bt_tethering_supported;
26 bool ethernet_supported;
27 bool route_supported;
28 bool all_features_not_supported;
29
30 connection_profile_h profile_cellular;
31 connection_profile_h profile_wifi;
32 connection_profile_h profile_temp;
33 connection_h connection;
34
35 gboolean test_connection_timeout_callback(gpointer data)
36 {
37         g_CallbackRet = CONNECTION_ERROR_OPERATION_FAILED;
38         PRINT_RETURN("test_connection_timeout_callback", g_CallbackRet);
39
40         return false;
41 }
42
43 const char *connection_get_error(connection_error_e error)
44 {
45         switch (error) {
46         case CONNECTION_ERROR_NONE:
47                 return "CONNECTION_ERROR_NONE";
48         case CONNECTION_ERROR_INVALID_PARAMETER:
49                 return "CONNECTION_ERROR_INVALID_PARAMETER";
50         case CONNECTION_ERROR_OUT_OF_MEMORY:
51                 return "CONNECTION_ERROR_OUT_OF_MEMORY";
52         case CONNECTION_ERROR_INVALID_OPERATION:
53                 return "CONNECTION_ERROR_INVALID_OPERATION";
54         case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
55                 return "CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED";
56         case CONNECTION_ERROR_OPERATION_FAILED:
57                 return "CONNECTION_ERROR_OPERATION_FAILED";
58         case CONNECTION_ERROR_ITERATOR_END:
59                 return "CONNECTION_ERROR_ITERATOR_END";
60         case CONNECTION_ERROR_NO_CONNECTION:
61                 return "CONNECTION_ERROR_NO_CONNECTION";
62         case CONNECTION_ERROR_NOW_IN_PROGRESS:
63                 return "CONNECTION_ERROR_NOW_IN_PROGRESS";
64         case CONNECTION_ERROR_ALREADY_EXISTS:
65                 return "CONNECTION_ERROR_ALREADY_EXISTS";
66         case CONNECTION_ERROR_OPERATION_ABORTED:
67                 return "CONNECTION_ERROR_OPERATION_ABORTED";
68         case CONNECTION_ERROR_DHCP_FAILED:
69                 return "CONNECTION_ERROR_DHCP_FAILED";
70         case CONNECTION_ERROR_INVALID_KEY:
71                 return "CONNECTION_ERROR_INVALID_KEY";
72         case CONNECTION_ERROR_NO_REPLY:
73                 return "CONNECTION_ERROR_NO_REPLY";
74         case CONNECTION_ERROR_PERMISSION_DENIED:
75                 return "CONNECTION_ERROR_PERMISSION_DENIED";
76         case CONNECTION_ERROR_NOT_SUPPORTED:
77                 return "CONNECTION_ERROR_NOT_SUPPORTED";
78         case CONNECTION_ERROR_ALREADY_INITIALIZED:
79                 return "CONNECTION_ERROR_ALREADY_INITIALIZED";
80         case CONNECTION_ERROR_NOT_INITIALIZED:
81                 return "CONNECTION_ERROR_NOT_INITIALIZED";
82         default:
83                 return "CONNECTION_ERROR_UNKNOWN";
84         }
85 }
86
87 bool connection_check_feature_supported(char *key)
88 {
89         bool value = false;
90         int ret = system_info_get_platform_bool(key, &value);
91         if (ret != SYSTEM_INFO_ERROR_NONE)
92                 return false;
93         return value;
94 }
95
96 int test_get_any_profile(connection_profile_h *profile)
97 {
98         connection_profile_h profile_h;
99         connection_profile_iterator_h profile_iter;
100
101         int  rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_REGISTERED, &profile_iter);
102         PRINT_RETURN("connection_get_profile_iterator", rv);
103         if (CONNECTION_ERROR_NONE != rv)
104                 return -1;
105
106         rv = connection_profile_iterator_next(profile_iter, &profile_h);
107         PRINT_RETURN("connection_profile_iterator_next", rv);
108         if (CONNECTION_ERROR_NONE != rv)
109                 return -1;
110
111         rv = connection_profile_clone(profile, profile_h);
112         PRINT_RETURN("connection_profile_clone", rv);
113         if (CONNECTION_ERROR_NONE != rv)
114                 return -1;
115
116         return 1;
117 }
118
119 int test_get_profile_by_type(connection_profile_h *profile, connection_profile_type_e type)
120 {
121         connection_profile_h profile_h;
122         connection_profile_iterator_h profile_iter;
123         connection_profile_type_e profile_type;
124
125         int  rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_REGISTERED, &profile_iter);
126         CHECK_RETURN("connection_get_profile_iterator", rv, CONNECTION_ERROR_NONE);
127
128         while (connection_profile_iterator_has_next(profile_iter)) {
129                 rv = connection_profile_iterator_next(profile_iter, &profile_h);
130                 CHECK_RETURN("connection_profile_iterator_next", rv, CONNECTION_ERROR_NONE);
131
132                 rv = connection_profile_get_type(profile_h, &profile_type);
133                 CHECK_RETURN("connection_profile_get_type", rv, CONNECTION_ERROR_NONE);
134
135                 if (profile_type == type) {
136                         rv = connection_profile_clone(profile, profile_h);
137                         CHECK_RETURN("connection_profile_clone", rv, CONNECTION_ERROR_NONE);
138                         return CONNECTION_ERROR_NONE;
139                 }
140         }
141
142         return CONNECTION_ERROR_NONE;
143 }