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.
17 #include "muse_gtest_json.h"
18 #include "muse_core.h"
20 static gint _read_file(gchar *file_path, gchar **buf);
21 static void *_json_thread_function(gpointer buf);
23 static gint _read_file(gchar *file_path, gchar **buf)
31 LOGE("file path is null");
36 LOGE("buf should not be NULL");
40 fp = fopen(file_path, "r");
47 fseek(fp, 0, SEEK_END);
48 file_size = ftell(fp);
49 fseek(fp, 0, SEEK_SET);
51 tbuf = (gchar *)g_malloc0(file_size);
53 if (!fgets(tbuf, file_size, fp)) {
69 static void *_json_thread_function(gpointer buf)
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;
78 return GINT_TO_POINTER(FALSE);
80 jobj = muse_core_msg_object_new((gchar *)buf, NULL, &err);
83 LOGE("failed to get msg object. jobj err : %d", err);
87 if (!muse_core_msg_object_get_value("api", jobj, MUSE_TYPE_INT, &api)) {
88 LOGE("failed to get api");
91 LOGD("api (gint) : %d\n", api);
93 if (!muse_core_msg_object_get_value("key_request_length", jobj, MUSE_TYPE_INT, &arr_len)) {
94 LOGE("failed to get length");
97 LOGD("key_request_length (gint) : %d\n", arr_len);
99 if (!muse_core_msg_object_get_value("server_url_length", jobj, MUSE_TYPE_INT, &url_len)) {
100 LOGE("failed to get length");
103 LOGD("server_url_length (gint) : %d\n", url_len);
105 str = (gchar *)g_try_new0(gchar, MUSE_MSG_LEN_MAX);
111 if (!muse_core_msg_object_get_value("server_url_name", jobj, MUSE_TYPE_STRING, str)) {
112 LOGE("failed to get length");
115 LOGD("server_url_name (string) : %s\n", str);
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]);
125 LOGD("key_request_name (array) : END");
126 g_free(key_request_name);
129 muse_core_msg_object_free(jobj);
131 return GINT_TO_POINTER(TRUE);
136 muse_core_msg_object_free(jobj);
138 return GINT_TO_POINTER(FALSE);
141 gboolean muse_gtest_json(gchar *file_path)
145 gboolean result = FALSE;
147 if (-1 == _read_file(file_path, &buf))
150 thread = g_thread_new(NULL, _json_thread_function, (gpointer)buf);
151 result = GPOINTER_TO_INT(g_thread_join(thread));
157 gboolean muse_gtest_multi_threaded_json(gchar *file_path)
160 GPtrArray *threads = NULL;
161 GThread *thread = NULL;
162 gboolean result = TRUE;
166 if (-1 == _read_file(file_path, &buf))
169 threads = g_ptr_array_new();
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);
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);
181 g_ptr_array_free(threads, TRUE);