Newer
Older
AST_TEST_DEFINE(json_test_object_iter)
{
struct ast_json_iter *iter;
int count;
int uut_res;
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "object_iter";
info->category = CATEGORY;
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
info->summary = "Testing iterating through JSON objects.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* Object iterator testing */
uut = ast_json_pack("{s: i, s: i, s: i, s: i, s: i}", "one", 1, "two", 2, "three", 3, "four", 4, "five", 5);
/* Iterate through the object; be aware that order isn't specified */
iter = ast_json_object_iter(uut);
ast_test_validate(test, NULL != iter);
count = 0;
while (NULL != iter) {
if (0 == strcmp("one", ast_json_object_iter_key(iter))) {
ast_test_validate(test, 1 == ast_json_integer_get(ast_json_object_iter_value(iter)));
} else if (0 == strcmp("two", ast_json_object_iter_key(iter))) {
ast_test_validate(test, 2 == ast_json_integer_get(ast_json_object_iter_value(iter)));
} else if (0 == strcmp("three", ast_json_object_iter_key(iter))) {
ast_test_validate(test, 3 == ast_json_integer_get(ast_json_object_iter_value(iter)));
} else if (0 == strcmp("four", ast_json_object_iter_key(iter))) {
ast_test_validate(test, 4 == ast_json_integer_get(ast_json_object_iter_value(iter)));
} else if (0 == strcmp("five", ast_json_object_iter_key(iter))) {
ast_test_validate(test, 5 == ast_json_integer_get(ast_json_object_iter_value(iter)));
} else {
/* Unexpected key */
ast_test_validate(test, 0);
}
iter = ast_json_object_iter_next(uut, iter);
++count;
}
ast_test_validate(test, 5 == count);
/* iterator non-existing key */
iter = ast_json_object_iter_at(uut, "dne");
ast_test_validate(test, NULL == iter);
/* iterator specific key */
iter = ast_json_object_iter_at(uut, "three");
ast_test_validate(test, NULL != iter);
ast_test_validate(test, 3 == ast_json_integer_get(ast_json_object_iter_value(iter)));
/* set via iter */
iter = ast_json_object_iter_at(uut, "three");
uut_res = ast_json_object_iter_set(uut, iter, ast_json_integer_create(-3));
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, -3 == ast_json_integer_get(ast_json_object_get(uut, "three")));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_object_iter_null)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "object_iter_null";
info->category = CATEGORY;
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
info->summary = "Testing JSON object iterator NULL testings.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* iterator NULL tests */
uut = ast_json_object_create();
ast_test_validate(test, NULL == ast_json_object_iter(NULL));
ast_test_validate(test, NULL == ast_json_object_iter_at(NULL, "not null"));
ast_test_validate(test, NULL == ast_json_object_iter_next(NULL, NULL));
ast_test_validate(test, NULL == ast_json_object_iter_next(uut, NULL));
ast_test_validate(test, NULL == ast_json_object_iter_key(NULL));
ast_test_validate(test, NULL == ast_json_object_iter_value(NULL));
ast_test_validate(test, -1 == ast_json_object_iter_set(NULL, NULL, ast_json_null()));
ast_test_validate(test, -1 == ast_json_object_iter_set(uut, NULL, ast_json_null()));
return AST_TEST_PASS;
}
Kevin Harwell
committed
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
AST_TEST_DEFINE(json_test_object_create_vars)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_variable *, vars, NULL, ast_variables_destroy);
const char *value;
struct ast_variable *new_var;
switch (cmd) {
case TEST_INIT:
info->name = "object_create_vars";
info->category = CATEGORY;
info->summary = "Testing JSON object creation initialized using Asterisk variables.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* NULL case */
ast_test_validate(test, (uut = ast_json_object_create_vars(NULL, NULL)));
ast_test_validate(test, !(value = ast_json_object_string_get(uut, "foo")));
ast_test_validate(test, (new_var = ast_variable_new("foo", "bar", "")));
ast_variable_list_append(&vars, new_var);
ast_test_validate(test, (new_var = ast_variable_new("bar", "baz", "")));
ast_variable_list_append(&vars, new_var);
/* Variables case */
ast_json_unref(uut);
ast_test_validate(test, (uut = ast_json_object_create_vars(vars, NULL)));
ast_test_validate(test, (value = ast_json_object_string_get(uut, "foo")));
ast_test_validate(test, !strcmp("bar", value));
ast_test_validate(test, (value = ast_json_object_string_get(uut, "bar")));
ast_test_validate(test, !strcmp("baz", value));
/* Variables with excludes case */
ast_json_unref(uut);
ast_test_validate(test, (uut = ast_json_object_create_vars(vars, "foo")));
ast_test_validate(test, !(value = ast_json_object_string_get(uut, "foo")));
ast_test_validate(test, (value = ast_json_object_string_get(uut, "bar")));
ast_test_validate(test, !strcmp("baz", value));
ast_json_unref(uut);
ast_test_validate(test, (uut = ast_json_object_create_vars(vars, "foo2")));
ast_test_validate(test, (value = ast_json_object_string_get(uut, "foo")));
ast_test_validate(test, (value = ast_json_object_string_get(uut, "bar")));
ast_test_validate(test, !strcmp("baz", value));
ast_json_unref(uut);
ast_test_validate(test, (uut = ast_json_object_create_vars(vars, "foobar,baz")));
ast_test_validate(test, (value = ast_json_object_string_get(uut, "foo")));
ast_test_validate(test, (value = ast_json_object_string_get(uut, "bar")));
ast_test_validate(test, !strcmp("baz", value));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_dump_load_string)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
RAII_VAR(char *, str, NULL, json_debug_free);
switch (cmd) {
case TEST_INIT:
info->name = "dump_load_string";
info->category = CATEGORY;
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
info->summary = "Testing dumping strings from JSON.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
expected = ast_json_pack("{ s: i }", "one", 1);
str = ast_json_dump_string(expected);
ast_test_validate(test, NULL != str);
uut = ast_json_load_string(str, NULL);
ast_test_validate(test, NULL != uut);
ast_test_validate(test, ast_json_equal(expected, uut));
/* dump_string NULL */
ast_test_validate(test, NULL == ast_json_dump_string(NULL));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_dump_load_str)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
RAII_VAR(struct ast_str *, astr, NULL, ast_free);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "dump_load_str";
info->category = CATEGORY;
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
info->summary = "Testing dumping ast_str from JSON.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* dump/load ast_str */
expected = ast_json_pack("{ s: i }", "one", 1);
astr = ast_str_create(1); /* should expand to hold output */
uut_res = ast_json_dump_str(expected, &astr);
ast_test_validate(test, 0 == uut_res);
uut = ast_json_load_str(astr, NULL);
ast_test_validate(test, NULL != uut);
ast_test_validate(test, ast_json_equal(expected, uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_dump_str_fail)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
struct ast_str *astr;
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "dump_str_fail";
info->category = CATEGORY;
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
info->summary = "Testing dumping to ast_str when it can't grow.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* dump ast_str growth failure */
expected = ast_json_pack("{ s: i }", "one", 1);
astr = ast_str_alloca(1); /* cannot grow */
uut_res = ast_json_dump_str(expected, &astr);
ast_test_validate(test, 0 != uut_res);
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_load_buffer)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
const char *str;
switch (cmd) {
case TEST_INIT:
info->name = "load_buffer";
info->category = CATEGORY;
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
info->summary = "Testing loading JSON from buffer.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* load buffer */
str = "{ \"one\": 1 } trailing garbage";
uut = ast_json_load_string(str, NULL);
ast_test_validate(test, NULL == uut);
uut = ast_json_load_buf(str, strlen("{ \"one\": 1 }"), NULL);
ast_test_validate(test, NULL != uut);
return AST_TEST_PASS;
}
/*! \brief \a fclose isn't NULL safe. */
static int safe_fclose(FILE *f)
{
if (f) {
return fclose(f);
}
return 0;
}
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
static FILE *mkstemp_file(char *template, const char *mode)
{
int fd = mkstemp(template);
FILE *file;
if (fd < 0) {
ast_log(LOG_ERROR, "Failed to create temp file: %s\n",
strerror(errno));
return NULL;
}
file = fdopen(fd, mode);
if (!file) {
ast_log(LOG_ERROR, "Failed to create temp file: %s\n",
strerror(errno));
return NULL;
}
return file;
}
AST_TEST_DEFINE(json_test_dump_load_file)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
char filename[] = "/tmp/ast_json.XXXXXX";
RAII_VAR(char *, rm_on_exit, filename, unlink);
RAII_VAR(FILE *, file, NULL, safe_fclose);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "dump_load_file";
info->category = CATEGORY;
info->summary = "Testing dumping/loading JSON to/from file by FILE *.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* dump/load file */
expected = ast_json_pack("{ s: i }", "one", 1);
file = mkstemp_file(filename, "w");
ast_test_validate(test, NULL != file);
uut_res = ast_json_dump_file(expected, file);
ast_test_validate(test, 0 == uut_res);
fclose(file);
file = fopen(filename, "r");
ast_test_validate(test, NULL != file);
uut = ast_json_load_file(file, NULL);
ast_test_validate(test, ast_json_equal(expected, uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_dump_load_new_file)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
char filename[] = "/tmp/ast_json.XXXXXX";
RAII_VAR(char *, rm_on_exit, filename, unlink);
RAII_VAR(FILE *, file, NULL, safe_fclose);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "dump_load_new_file";
info->category = CATEGORY;
info->summary = "Testing dumping/load JSON to/from file by filename.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* dump/load filename */
expected = ast_json_pack("{ s: i }", "one", 1);
file = mkstemp_file(filename, "w");
ast_test_validate(test, NULL != file);
uut_res = ast_json_dump_new_file(expected, filename);
ast_test_validate(test, 0 == uut_res);
uut = ast_json_load_new_file(filename, NULL);
ast_test_validate(test, ast_json_equal(expected, uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_dump_load_null)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
char filename[] = "/tmp/ast_json.XXXXXX";
RAII_VAR(char *, rm_on_exit, filename, unlink);
RAII_VAR(FILE *, file, NULL, safe_fclose);
switch (cmd) {
case TEST_INIT:
info->name = "dump_load_null";
info->category = CATEGORY;
info->summary = "Testing NULL handling of dump/load functions.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* dump/load NULL tests */
uut = ast_json_load_string("{ \"one\": 1 }", NULL);
ast_test_validate(test, NULL != uut);
file = mkstemp_file(filename, "w");
ast_test_validate(test, NULL != file);
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
ast_test_validate(test, NULL == ast_json_dump_string(NULL));
ast_test_validate(test, -1 == ast_json_dump_file(NULL, file));
ast_test_validate(test, -1 == ast_json_dump_file(uut, NULL));
ast_test_validate(test, -1 == ast_json_dump_file(NULL, NULL));
ast_test_validate(test, -1 == ast_json_dump_new_file(uut, NULL));
ast_test_validate(test, -1 == ast_json_dump_new_file(NULL, filename));
ast_test_validate(test, -1 == ast_json_dump_new_file(NULL, NULL));
ast_test_validate(test, NULL == ast_json_load_string(NULL, NULL));
ast_test_validate(test, NULL == ast_json_load_buf(NULL, 0, NULL));
ast_test_validate(test, NULL == ast_json_load_file(NULL, NULL));
ast_test_validate(test, NULL == ast_json_load_new_file(NULL, NULL));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_parse_errors)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "parse_errors";
info->category = CATEGORY;
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
info->summary = "Testing various parse errors.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* parse errors */
ast_test_validate(test, NULL == ast_json_load_string("'singleton'", NULL));
ast_test_validate(test, NULL == ast_json_load_string("{ no value }", NULL));
ast_test_validate(test, NULL == ast_json_load_string("{ 'no': 'curly' ", NULL));
ast_test_validate(test, NULL == ast_json_load_string("[ 'no', 'square'", NULL));
ast_test_validate(test, NULL == ast_json_load_string("{ 1: 'int key' }", NULL));
ast_test_validate(test, NULL == ast_json_load_string("", NULL));
ast_test_validate(test, NULL == ast_json_load_string("{ 'missing' 'colon' }", NULL));
ast_test_validate(test, NULL == ast_json_load_string("[ 'missing' 'comma' ]", NULL));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_pack)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "pack";
info->category = CATEGORY;
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
info->summary = "Testing json_pack function.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* pack test */
expected = ast_json_array_create();
ast_json_array_append(expected, ast_json_array_create());
ast_json_array_append(expected, ast_json_object_create());
ast_json_array_append(ast_json_array_get(expected, 0), ast_json_integer_create(1));
ast_json_array_append(ast_json_array_get(expected, 0), ast_json_integer_create(2));
ast_json_object_set(ast_json_array_get(expected, 1), "cool", ast_json_true());
uut = ast_json_pack("[[i,i],{s:b}]", 1, 2, "cool", 1);
ast_test_validate(test, NULL != uut);
ast_test_validate(test, ast_json_equal(expected, uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_pack_ownership)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "pack_ownership";
info->category = CATEGORY;
info->summary = "Testing json_pack failure conditions.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
uut = ast_json_pack("[o]", ast_json_string_create("Am I freed?"));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_pack_errors)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "object_alloc";
info->category = CATEGORY;
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
info->summary = "Testing json_pack failure conditions.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* pack errors */
ast_test_validate(test, NULL == ast_json_pack(NULL));
ast_test_validate(test, NULL == ast_json_pack("{s:i", "no curly", 911));
ast_test_validate(test, NULL == ast_json_pack("[s, s", "no", "square"));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_copy)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "copy";
info->category = CATEGORY;
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
info->summary = "Testing copying JSON.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* copy test */
expected = ast_json_pack("{s: {s: i}}", "outer", "inner", 8675309);
uut = ast_json_copy(expected);
ast_test_validate(test, NULL != uut);
ast_test_validate(test, ast_json_equal(expected, uut));
ast_test_validate(test, ast_json_object_get(expected, "outer") == ast_json_object_get(uut, "outer"));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_deep_copy)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "deep_copy";
info->category = CATEGORY;
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
info->summary = "Testing deep copying of JSON.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* deep copy test */
expected = ast_json_pack("{s: {s: i}}", "outer", "inner", 8675309);
uut = ast_json_deep_copy(expected);
ast_test_validate(test, NULL != uut);
ast_test_validate(test, ast_json_equal(expected, uut));
ast_test_validate(test, ast_json_object_get(expected, "outer") != ast_json_object_get(uut, "outer"));
/* Changing the inner value of one should not change the other */
ast_json_integer_set(ast_json_object_get(ast_json_object_get(uut, "outer"), "inner"), 411);
ast_test_validate(test, !ast_json_equal(expected, uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_copy_null)
{
switch (cmd) {
case TEST_INIT:
info->name = "copy_null";
info->category = CATEGORY;
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
info->summary = "Testing NULL handling of copy functions.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* copy NULL */
ast_test_validate(test, NULL == ast_json_copy(NULL));
ast_test_validate(test, NULL == ast_json_deep_copy(NULL));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_circular_object)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "circular_object";
info->category = CATEGORY;
info->summary = "Object cannot be added to itself.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* circular reference testing */
/* Cannot add self */
uut = ast_json_object_create();
uut_res = ast_json_object_set(uut, "myself", ast_json_ref(uut));
ast_test_validate(test, -1 == uut_res);
ast_test_validate(test, 0 == ast_json_object_size(uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_circular_array)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "circular_array";
info->category = CATEGORY;
info->summary = "Array cannot be added to itself.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
uut = ast_json_array_create();
ast_test_validate(test, 0 == ast_json_array_size(uut));
uut_res = ast_json_array_append(uut, ast_json_ref(uut));
ast_test_validate(test, -1 == uut_res);
ast_test_validate(test, 0 == ast_json_array_size(uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_clever_circle)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, inner_child, NULL, ast_json_unref);
RAII_VAR(char *, str, NULL, json_debug_free);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "clever_circle";
info->category = CATEGORY;
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
info->summary = "JSON with circular references cannot be encoded.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* can add to self if you're clever enough, but it should not encode */
uut = ast_json_object_create();
inner_child = ast_json_object_create();
uut_res = ast_json_object_set(uut, "inner_child", ast_json_ref(inner_child)); /* incref to keep a reference */
ast_test_validate(test, 0 == uut_res);
uut_res = ast_json_object_set(inner_child, "parent", ast_json_ref(uut)); /* incref to keep a reference */
ast_test_validate(test, 0 == uut_res);
str = ast_json_dump_string(uut);
ast_test_validate(test, NULL == str);
/* Circular refs screw up reference counting, so break the cycle */
ast_json_object_clear(inner_child);
return AST_TEST_PASS;
}
static int test_name_number(const char *name, const char *number)
int res;
struct ast_json *uut;
struct ast_json *expected;
expected = ast_json_pack("{s: s, s: s}",
"name", name ?: "",
"number", number ?: "");
uut = ast_json_name_number(name, number);
res = ast_json_equal(expected, uut);
ast_json_unref(expected);
ast_json_unref(uut);
return res;
}
AST_TEST_DEFINE(json_test_name_number)
{
switch (cmd) {
case TEST_INIT:
info->name = "name_number";
info->category = CATEGORY;
info->summary = "JSON encoding of name/number pair.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
ast_test_validate(test, test_name_number("name", NULL));
ast_test_validate(test, test_name_number(NULL, "1234"));
ast_test_validate(test, test_name_number(NULL, NULL));
ast_test_validate(test, test_name_number("Jenny", "867-5309"));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_timeval)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
struct timeval tv = {};
switch (cmd) {
case TEST_INIT:
info->name = "type_timeval";
info->category = CATEGORY;
info->summary = "JSON encoding of timevals.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
expected = ast_json_string_create("2013-02-07T09:32:34.314-0600");
tv.tv_sec = 1360251154;
tv.tv_usec = 314159;
uut = ast_json_timeval(tv, "America/Chicago");
ast_test_validate(test, ast_json_equal(expected, uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_cep)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "cep";
info->category = CATEGORY;
info->summary = "JSON with circular references cannot be encoded.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
expected = ast_json_pack("{s: o, s: o, s: o, s: o, s: o}",
"context", ast_json_null(),
"exten", ast_json_null(),
"priority", ast_json_null(),
"app_name", ast_json_null(),
"app_data", ast_json_null()
);
uut = ast_json_dialplan_cep_app(NULL, NULL, -1, NULL, NULL);
ast_test_validate(test, ast_json_equal(expected, uut));
ast_json_unref(expected);
ast_json_unref(uut);
expected = ast_json_pack("{s: s, s: s, s: i, s: s, s: s}",
"context", "main",
"exten", "4321",
"priority", 7,
"app_name", "",
"app_data", ""
);
uut = ast_json_dialplan_cep_app("main", "4321", 7, "", "");
ast_test_validate(test, ast_json_equal(expected, uut));
return AST_TEST_PASS;
}
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
static int unload_module(void)
{
AST_TEST_UNREGISTER(json_test_false);
AST_TEST_UNREGISTER(json_test_true);
AST_TEST_UNREGISTER(json_test_bool0);
AST_TEST_UNREGISTER(json_test_bool1);
AST_TEST_UNREGISTER(json_test_null);
AST_TEST_UNREGISTER(json_test_null_val);
AST_TEST_UNREGISTER(json_test_string);
AST_TEST_UNREGISTER(json_test_string_null);
AST_TEST_UNREGISTER(json_test_stringf);
AST_TEST_UNREGISTER(json_test_int);
AST_TEST_UNREGISTER(json_test_non_int);
AST_TEST_UNREGISTER(json_test_array_create);
AST_TEST_UNREGISTER(json_test_array_append);
AST_TEST_UNREGISTER(json_test_array_inset);
AST_TEST_UNREGISTER(json_test_array_set);
AST_TEST_UNREGISTER(json_test_array_remove);
AST_TEST_UNREGISTER(json_test_array_clear);
AST_TEST_UNREGISTER(json_test_array_extend);
AST_TEST_UNREGISTER(json_test_array_null);
AST_TEST_UNREGISTER(json_test_object_alloc);
AST_TEST_UNREGISTER(json_test_object_set);
AST_TEST_UNREGISTER(json_test_object_set_overwrite);
AST_TEST_UNREGISTER(json_test_object_get);
AST_TEST_UNREGISTER(json_test_object_del);
AST_TEST_UNREGISTER(json_test_object_clear);
AST_TEST_UNREGISTER(json_test_object_merge_all);
AST_TEST_UNREGISTER(json_test_object_merge_existing);
AST_TEST_UNREGISTER(json_test_object_merge_missing);
AST_TEST_UNREGISTER(json_test_object_null);
AST_TEST_UNREGISTER(json_test_object_iter);
AST_TEST_UNREGISTER(json_test_object_iter_null);
Kevin Harwell
committed
AST_TEST_UNREGISTER(json_test_object_create_vars);
AST_TEST_UNREGISTER(json_test_dump_load_string);
AST_TEST_UNREGISTER(json_test_dump_load_str);
AST_TEST_UNREGISTER(json_test_dump_str_fail);
AST_TEST_UNREGISTER(json_test_load_buffer);
AST_TEST_UNREGISTER(json_test_dump_load_file);
AST_TEST_UNREGISTER(json_test_dump_load_new_file);
AST_TEST_UNREGISTER(json_test_dump_load_null);
AST_TEST_UNREGISTER(json_test_parse_errors);
AST_TEST_UNREGISTER(json_test_pack);
AST_TEST_UNREGISTER(json_test_pack_ownership);
AST_TEST_UNREGISTER(json_test_pack_errors);
AST_TEST_UNREGISTER(json_test_copy);
AST_TEST_UNREGISTER(json_test_deep_copy);
AST_TEST_UNREGISTER(json_test_copy_null);
AST_TEST_UNREGISTER(json_test_circular_object);
AST_TEST_UNREGISTER(json_test_circular_array);
AST_TEST_UNREGISTER(json_test_clever_circle);
AST_TEST_UNREGISTER(json_test_name_number);
AST_TEST_UNREGISTER(json_test_timeval);
AST_TEST_UNREGISTER(json_test_cep);
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
return 0;
}
static int load_module(void)
{
AST_TEST_REGISTER(json_test_false);
AST_TEST_REGISTER(json_test_true);
AST_TEST_REGISTER(json_test_bool0);
AST_TEST_REGISTER(json_test_bool1);
AST_TEST_REGISTER(json_test_null);
AST_TEST_REGISTER(json_test_null_val);
AST_TEST_REGISTER(json_test_string);
AST_TEST_REGISTER(json_test_string_null);
AST_TEST_REGISTER(json_test_stringf);
AST_TEST_REGISTER(json_test_int);
AST_TEST_REGISTER(json_test_non_int);
AST_TEST_REGISTER(json_test_array_create);
AST_TEST_REGISTER(json_test_array_append);
AST_TEST_REGISTER(json_test_array_inset);
AST_TEST_REGISTER(json_test_array_set);
AST_TEST_REGISTER(json_test_array_remove);
AST_TEST_REGISTER(json_test_array_clear);
AST_TEST_REGISTER(json_test_array_extend);
AST_TEST_REGISTER(json_test_array_null);
AST_TEST_REGISTER(json_test_object_alloc);
AST_TEST_REGISTER(json_test_object_set);
AST_TEST_REGISTER(json_test_object_set_overwrite);
AST_TEST_REGISTER(json_test_object_get);
AST_TEST_REGISTER(json_test_object_del);
AST_TEST_REGISTER(json_test_object_clear);
AST_TEST_REGISTER(json_test_object_merge_all);
AST_TEST_REGISTER(json_test_object_merge_existing);
AST_TEST_REGISTER(json_test_object_merge_missing);
AST_TEST_REGISTER(json_test_object_null);
AST_TEST_REGISTER(json_test_object_iter);
AST_TEST_REGISTER(json_test_object_iter_null);
Kevin Harwell
committed
AST_TEST_REGISTER(json_test_object_create_vars);
AST_TEST_REGISTER(json_test_dump_load_string);
AST_TEST_REGISTER(json_test_dump_load_str);
AST_TEST_REGISTER(json_test_dump_str_fail);
AST_TEST_REGISTER(json_test_load_buffer);
AST_TEST_REGISTER(json_test_dump_load_file);
AST_TEST_REGISTER(json_test_dump_load_new_file);
AST_TEST_REGISTER(json_test_dump_load_null);
AST_TEST_REGISTER(json_test_parse_errors);
AST_TEST_REGISTER(json_test_pack);
AST_TEST_REGISTER(json_test_pack_ownership);
AST_TEST_REGISTER(json_test_pack_errors);
AST_TEST_REGISTER(json_test_copy);
AST_TEST_REGISTER(json_test_deep_copy);
AST_TEST_REGISTER(json_test_copy_null);
AST_TEST_REGISTER(json_test_circular_object);
AST_TEST_REGISTER(json_test_circular_array);
AST_TEST_REGISTER(json_test_clever_circle);
AST_TEST_REGISTER(json_test_name_number);
AST_TEST_REGISTER(json_test_timeval);
AST_TEST_REGISTER(json_test_cep);
ast_test_register_init(CATEGORY, json_test_init);
ast_test_register_cleanup(CATEGORY, json_test_cleanup);
AST_MODULE_INFO(ASTERISK_GPL_KEY, 0, "JSON testing",
.support_level = AST_MODULE_SUPPORT_CORE,
Rodrigo Ramírez Norambuena
committed
.load = load_module,
.unload = unload_module
);