Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 46c5fcf

Browse files
author
Wu Yu Wei
authored
Merge pull request #146 from HowJMay/test_driver_redisgn
fix(driver): Redesign test driver for target tx disapearing
2 parents 9bb4e75 + 16cea5a commit 46c5fcf

File tree

5 files changed

+121
-15
lines changed

5 files changed

+121
-15
lines changed

accelerator/errors.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ typedef enum {
7575
/**< flex_trits conversion error */
7676
SC_CCLIENT_HASH = 0x06 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR,
7777
/**< hash container operation error */
78+
SC_CCLIENT_JSON_KEY = 0x07 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR,
79+
/**< JSON key not found */
80+
SC_CCLIENT_JSON_PARSE = 0x08 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR,
81+
/**< json parsing error, might the wrong format */
7882

7983
// Serializer module
8084
SC_SERIALIZER_JSON_CREATE = 0x01 | SC_MODULE_SERIALIZER | SC_SEVERITY_FATAL,

serializer/serializer.c

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ void fill_tag(char* new_tag, char* old_tag, size_t tag_len) {
66
sprintf(new_tag, "%s%*.*s", old_tag, pad_len, pad_len, nines);
77
}
88

9-
status_t ta_hash243_stack_to_json_array(hash243_stack_t stack,
10-
cJSON* const json_root,
11-
char const* const obj_name) {
9+
static status_t ta_hash243_stack_to_json_array(hash243_stack_t stack,
10+
cJSON* const json_root,
11+
char const* const obj_name) {
1212
size_t array_count = 0;
1313
cJSON* array_obj = NULL;
1414
hash243_stack_entry_t* s_iter = NULL;
@@ -41,9 +41,9 @@ status_t ta_hash243_stack_to_json_array(hash243_stack_t stack,
4141
return SC_OK;
4242
}
4343

44-
status_t ta_hash243_queue_to_json_array(hash243_queue_t queue,
45-
cJSON* const json_root,
46-
char const* const obj_name) {
44+
static status_t ta_hash243_queue_to_json_array(hash243_queue_t queue,
45+
cJSON* const json_root,
46+
char const* const obj_name) {
4747
size_t array_count;
4848
cJSON* array_obj = NULL;
4949
hash243_queue_entry_t* q_iter = NULL;
@@ -75,6 +75,28 @@ status_t ta_hash243_queue_to_json_array(hash243_queue_t queue,
7575
return SC_OK;
7676
}
7777

78+
static status_t ta_json_get_string(cJSON const* const json_obj,
79+
char const* const obj_name,
80+
char* const text) {
81+
status_t ret = SC_OK;
82+
if (json_obj == NULL || obj_name == NULL || text == NULL) {
83+
return SC_SERIALIZER_NULL;
84+
}
85+
86+
cJSON* json_value = cJSON_GetObjectItemCaseSensitive(json_obj, obj_name);
87+
if (json_value == NULL) {
88+
return SC_CCLIENT_JSON_KEY;
89+
}
90+
91+
if (cJSON_IsString(json_value) && (json_value->valuestring != NULL)) {
92+
strcpy(text, json_value->valuestring);
93+
} else {
94+
return SC_CCLIENT_JSON_PARSE;
95+
}
96+
97+
return ret;
98+
}
99+
78100
status_t iota_transaction_to_json_object(iota_transaction_t const* const txn,
79101
cJSON** txn_json) {
80102
if (txn == NULL) {
@@ -431,6 +453,37 @@ status_t send_mam_res_serialize(char** obj, const send_mam_res_t* const res) {
431453
return ret;
432454
}
433455

456+
status_t send_mam_res_deserialize(const char* const obj,
457+
send_mam_res_t* const res) {
458+
if (obj == NULL) {
459+
return SC_SERIALIZER_NULL;
460+
}
461+
cJSON* json_obj = cJSON_Parse(obj);
462+
status_t ret = SC_OK;
463+
tryte_t addr[NUM_TRYTES_ADDRESS + 1];
464+
465+
if (json_obj == NULL) {
466+
ret = SC_SERIALIZER_JSON_PARSE;
467+
goto done;
468+
}
469+
470+
if (ta_json_get_string(json_obj, "channel", (char*)addr) != SC_OK) {
471+
ret = SC_SERIALIZER_NULL;
472+
goto done;
473+
}
474+
send_mam_res_set_channel_id(res, addr);
475+
476+
if (ta_json_get_string(json_obj, "bundle_hash", (char*)addr) != SC_OK) {
477+
ret = SC_SERIALIZER_NULL;
478+
goto done;
479+
}
480+
send_mam_res_set_bundle_hash(res, addr);
481+
482+
done:
483+
cJSON_Delete(json_obj);
484+
return ret;
485+
}
486+
434487
status_t send_mam_req_deserialize(const char* const obj, send_mam_req_t* req) {
435488
if (obj == NULL) {
436489
return SC_SERIALIZER_NULL;

serializer/serializer.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,19 @@ status_t receive_mam_message_serialize(char** obj, const char** res);
136136
*/
137137
status_t send_mam_res_serialize(char** obj, const send_mam_res_t* const res);
138138

139+
/**
140+
* @brief Deserialze JSON string to type of send_mam_res_t
141+
*
142+
* @param[in] obj Input values in JSON
143+
* @param[out] res Response data in type of send_mam_res_t
144+
*
145+
* @return
146+
* - SC_OK on success
147+
* - non-zero on error
148+
*/
149+
status_t send_mam_res_deserialize(const char* const obj,
150+
send_mam_res_t* const res);
151+
139152
/**
140153
* @brief Deserialze JSON string to type of send_mam_req_t
141154
*

tests/driver.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,23 @@
55
static ta_core_t ta_core;
66
struct timespec start_time, end_time;
77

8+
char driver_tag_msg[NUM_TRYTES_TAG];
9+
send_mam_res_t* res;
10+
811
#if defined(ENABLE_STAT)
912
#define TEST_COUNT 100
1013
#else
1114
#define TEST_COUNT 1
1215
#endif
1316

17+
static void gen_rand_tag(char* tag) {
18+
const char tryte_alpahbet[] = "NOPQRSTUVWXYZ9ABCDEFGHIJKLM";
19+
20+
for (int i = 0; i < NUM_TRYTES_TAG; i++) {
21+
tag[i] = tryte_alpahbet[rand() % 27];
22+
}
23+
}
24+
1425
double diff_time(struct timespec start, struct timespec end) {
1526
struct timespec diff;
1627
if (end.tv_nsec - start.tv_nsec < 0) {
@@ -81,14 +92,19 @@ void test_get_tips(void) {
8192
}
8293

8394
void test_send_transfer(void) {
84-
const char* json =
95+
const char* pre_json =
8596
"{\"value\":100,"
86-
"\"message\":\"" TAG_MSG "\",\"tag\":\"" TAG_MSG
87-
"\","
97+
"\"message\":\"" TAG_MSG
98+
"\",\"tag\":\"%s\","
8899
"\"address\":\"" TRYTES_81_1 "\"}";
89100
char* json_result;
90101
double sum = 0;
91102

103+
gen_rand_tag(driver_tag_msg);
104+
int json_len = strlen(pre_json);
105+
char json[json_len + NUM_TRYTES_TAG];
106+
sprintf(json, pre_json, driver_tag_msg);
107+
92108
for (size_t count = 0; count < TEST_COUNT; count++) {
93109
test_time_start(&start_time);
94110
TEST_ASSERT_EQUAL_INT32(
@@ -123,7 +139,7 @@ void test_find_transactions_by_tag(void) {
123139
test_time_start(&start_time);
124140

125141
TEST_ASSERT_EQUAL_INT32(
126-
SC_OK, api_find_transactions_by_tag(&ta_core.service, FIND_TAG_MSG,
142+
SC_OK, api_find_transactions_by_tag(&ta_core.service, driver_tag_msg,
127143
&json_result));
128144
test_time_end(&start_time, &end_time, &sum);
129145
free(json_result);
@@ -139,8 +155,8 @@ void test_find_transactions_obj_by_tag(void) {
139155
test_time_start(&start_time);
140156

141157
TEST_ASSERT_EQUAL_INT32(
142-
SC_OK, api_find_transactions_obj_by_tag(&ta_core.service, FIND_TAG_MSG,
143-
&json_result));
158+
SC_OK, api_find_transactions_obj_by_tag(&ta_core.service,
159+
driver_tag_msg, &json_result));
144160
test_time_end(&start_time, &end_time, &sum);
145161
free(json_result);
146162
}
@@ -151,12 +167,15 @@ void test_send_mam_message(void) {
151167
double sum = 0;
152168
const char* json = "{\"message\":\"" TEST_PAYLOAD "\"}";
153169
char* json_result;
170+
res = send_mam_res_new();
154171

155172
for (size_t count = 0; count < TEST_COUNT; count++) {
156173
test_time_start(&start_time);
157174
TEST_ASSERT_EQUAL_INT32(
158175
SC_OK, api_mam_send_message(&ta_core.tangle, &ta_core.service, json,
159176
&json_result));
177+
send_mam_res_deserialize(json_result, res);
178+
160179
test_time_end(&start_time, &end_time, &sum);
161180
free(json_result);
162181
}
@@ -171,15 +190,17 @@ void test_receive_mam_message(void) {
171190
test_time_start(&start_time);
172191

173192
TEST_ASSERT_EQUAL_INT32(
174-
SC_OK, api_receive_mam_message(&ta_core.service, TEST_BUNDLE_HASH,
175-
&json_result));
193+
SC_OK, api_receive_mam_message(&ta_core.service,
194+
(char*)res->bundle_hash, &json_result));
176195
test_time_end(&start_time, &end_time, &sum);
177196
free(json_result);
178197
}
179198
printf("Average time of receive_mam_message: %lf\n", sum / TEST_COUNT);
180199
}
181200

182201
int main(void) {
202+
srand(time(NULL));
203+
183204
UNITY_BEGIN();
184205

185206
ta_config_default_init(&ta_core.info, &ta_core.tangle, &ta_core.cache,
@@ -194,8 +215,8 @@ int main(void) {
194215
RUN_TEST(test_get_transaction_object);
195216
RUN_TEST(test_find_transactions_by_tag);
196217
RUN_TEST(test_find_transactions_obj_by_tag);
197-
RUN_TEST(test_receive_mam_message);
198218
RUN_TEST(test_send_mam_message);
219+
RUN_TEST(test_receive_mam_message);
199220
ta_config_destroy(&ta_core.service);
200221
return UNITY_END();
201222
}

tests/test_serializer.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,20 @@ void test_serialize_send_mam_message(void) {
269269
send_mam_res_free(&res);
270270
}
271271

272+
void test_deserialize_send_mam_message_response(void) {
273+
const char* json = "{\"channel\":\"" TRYTES_81_1
274+
"\","
275+
"\"bundle_hash\":\"" TRYTES_81_2 "\"}";
276+
send_mam_res_t* res = send_mam_res_new();
277+
278+
send_mam_res_deserialize(json, res);
279+
280+
TEST_ASSERT_EQUAL_STRING(TRYTES_81_1, res->channel_id);
281+
TEST_ASSERT_EQUAL_STRING(TRYTES_81_2, res->bundle_hash);
282+
283+
send_mam_res_free(&res);
284+
}
285+
272286
void test_deserialize_send_mam_message(void) {
273287
const char* json = "{\"message\":\"" TEST_PAYLOAD "\"}";
274288
send_mam_req_t* req = send_mam_req_new();
@@ -296,6 +310,7 @@ int main(void) {
296310
RUN_TEST(test_serialize_ta_find_transactions_by_tag);
297311
RUN_TEST(test_serialize_ta_find_transactions_obj_by_tag);
298312
RUN_TEST(test_serialize_send_mam_message);
313+
RUN_TEST(test_deserialize_send_mam_message_response);
299314
RUN_TEST(test_deserialize_send_mam_message);
300315
return UNITY_END();
301316
}

0 commit comments

Comments
 (0)