Fix Svace issue of SEC_DO_NOT_ASSIGN_SIZE_OF_INT
[platform/core/multimedia/mmsvc-core.git] / unittest / muse_gtest_json.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 "muse_gtest_json.h"
18 #include "muse_core.h"
19
20 static gint _read_file(gchar *file_path, gchar **buf);
21 static void *_json_thread_function(gpointer buf);
22
23 static gint _read_file(gchar *file_path, gchar **buf)
24 {
25         FILE *fp = NULL;
26         gchar* tbuf = NULL;
27
28         long file_size = 0;
29
30         if (!file_path) {
31                 LOGE("file path is null");
32                 return -1;
33         }
34
35         if (!buf) {
36                 LOGE("buf should not be NULL");
37                 return -1;
38         }
39
40         fp = fopen(file_path, "r");
41
42         if (!fp) {
43                 LOGE("fp error");
44                 return -1;
45         }
46
47         fseek(fp, 0, SEEK_END);
48         file_size = ftell(fp);
49         fseek(fp, 0, SEEK_SET);
50
51         tbuf = (gchar *)g_malloc0(file_size);
52
53         if (!fgets(tbuf, file_size, fp)) {
54                 LOGE("Read error");
55                 goto error;
56         }
57
58         *buf = tbuf;
59
60         return file_size;
61
62 error:
63         g_free(tbuf);
64         fclose(fp);
65
66         return -1;
67 }
68
69 static void *_json_thread_function(gpointer buf)
70 {
71         gint *key_request_name = NULL;
72         muse_core_msg_parse_err_e err = MUSE_MSG_PARSE_ERROR_NONE;
73         gint api, idx, arr_len, url_len;
74         void *jobj = NULL;
75         gchar *str = NULL;
76
77         if (!buf)
78                 return GINT_TO_POINTER(FALSE);
79
80         jobj = muse_core_msg_object_new((gchar *)buf, NULL, &err);
81
82         if (!jobj) {
83                 LOGE("failed to get msg object. jobj err : %d", err);
84                 goto out;
85         }
86
87         if (!muse_core_msg_object_get_value("api", jobj, MUSE_TYPE_INT, &api)) {
88                 LOGE("failed to get api");
89                 goto out;
90         }
91         LOGD("api (gint) : %d\n", api);
92
93         if (!muse_core_msg_object_get_value("key_request_length", jobj, MUSE_TYPE_INT, &arr_len)) {
94                 LOGE("failed to get length");
95                 goto out;
96         }
97         LOGD("key_request_length (gint) : %d\n", arr_len);
98
99         if (!muse_core_msg_object_get_value("server_url_length", jobj, MUSE_TYPE_INT, &url_len)) {
100                 LOGE("failed to get length");
101                 goto out;
102         }
103         LOGD("server_url_length (gint) : %d\n", url_len);
104
105         str = (gchar *)g_try_new0(gchar, MUSE_MSG_LEN_MAX);
106         if (!str) {
107                 LOGE("str is null");
108                 goto out;
109         }
110
111         if (!muse_core_msg_object_get_value("server_url_name", jobj, MUSE_TYPE_STRING, str)) {
112                 LOGE("failed to get length");
113                 goto out;
114         }
115         LOGD("server_url_name (string) : %s\n", str);
116         g_free(str);
117
118         key_request_name = (gint *)g_try_new0(gint, arr_len);
119         if (key_request_name) {
120                 LOGD("key_request_name (array) : START");
121                 if (muse_core_msg_object_get_value("key_request_name", jobj, MUSE_TYPE_ARRAY, key_request_name)) {
122                         for (idx = 0; idx < arr_len; idx++)
123                                 LOGD("%d ", key_request_name[idx]);
124                 }
125                 LOGD("key_request_name (array) : END");
126                 g_free(key_request_name);
127         }
128
129         muse_core_msg_object_free(jobj);
130
131         return GINT_TO_POINTER(TRUE);
132 out:
133         g_free(str);
134
135         if (jobj)
136                 muse_core_msg_object_free(jobj);
137
138         return GINT_TO_POINTER(FALSE);
139 }
140
141 gboolean muse_gtest_json(gchar *file_path)
142 {
143         GThread *thread;
144         gchar* buf = NULL;
145         gboolean result = FALSE;
146
147         if (-1 == _read_file(file_path, &buf))
148                 return FALSE;
149
150         thread = g_thread_new(NULL, _json_thread_function, (gpointer)buf);
151         result = GPOINTER_TO_INT(g_thread_join(thread));
152         g_free(buf);
153
154         return result;
155 }
156
157 gboolean muse_gtest_multi_threaded_json(gchar *file_path)
158 {
159         gint i;
160         GPtrArray *threads = NULL;
161         GThread *thread = NULL;
162         gboolean result = TRUE;
163         gpointer ret;
164         gchar* buf = NULL;
165
166         if (-1 == _read_file(file_path, &buf))
167                 return FALSE;
168
169         threads = g_ptr_array_new();
170
171         for (i = 0; i < NTHREADS; i++) {
172                 thread = g_thread_new(NULL, _json_thread_function, (gpointer)buf);
173                 g_ptr_array_add(threads, thread);
174         }
175
176         for (i = 0; i < NTHREADS; i++) {
177                 ret = g_thread_join((GThread *)g_ptr_array_index(threads, i));
178                 result &= GPOINTER_TO_INT(ret);
179         }
180
181         g_ptr_array_free(threads, TRUE);
182         g_free(buf);
183
184         return result;
185 }
186