static void test_parse_tzz_evil_key(void) { const char *data = "{ \"third key! can? be$ anything&\": [ \"a\", \"b\", \"c\" ]}"; JsonElement *obj = NULL; assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &obj)); assert_string_equal("b", JsonArrayGetAsString(JsonObjectGetAsArray(obj, "third key! can? be$ anything&"), 1)); JsonDestroy(obj); }
static void test_object_get_array(void) { JsonElement *arr = JsonArrayCreate(10); JsonArrayAppendString(arr, "one"); JsonArrayAppendString(arr, "two"); JsonElement *obj = JsonObjectCreate(10); JsonObjectAppendArray(obj, "array", arr); JsonElement *arr2 = JsonObjectGetAsArray(obj, "array"); assert_string_equal(JsonArrayGetAsString(arr2, 1), "two"); JsonDestroy(obj); }
static JsonElement *BundleTypesToJson(void) { JsonElement *bundle_types = JsonObjectCreate(50); Seq *common_promise_types = SeqNew(50, free); for (int module_index = 0; module_index < CF3_MODULES; module_index++) { for (int promise_type_index = 0; CF_ALL_PROMISE_TYPES[module_index][promise_type_index].promise_type; promise_type_index++) { const PromiseTypeSyntax *promise_type_syntax = &CF_ALL_PROMISE_TYPES[module_index][promise_type_index]; // skip global constraints if (strcmp("*", promise_type_syntax->promise_type) == 0) { continue; } // collect common promise types to be appended at the end if (strcmp("*", promise_type_syntax->bundle_type) == 0) { SeqAppend(common_promise_types, xstrdup(promise_type_syntax->promise_type)); continue; } if (promise_type_syntax->status == SYNTAX_STATUS_REMOVED) { continue; } JsonElement *bundle_type = JsonObjectGet(bundle_types, promise_type_syntax->bundle_type); if (!bundle_type) { bundle_type = JsonBundleTypeNew(); JsonObjectAppendObject(bundle_types, promise_type_syntax->bundle_type, bundle_type); } assert(bundle_type); JsonElement *promise_types = JsonObjectGet(bundle_type, "promiseTypes"); assert(promise_types); JsonArrayAppendString(promise_types, promise_type_syntax->promise_type); } } // Append the common bundle, which has only common promise types, but is not declared in syntax { JsonElement *bundle_type = JsonBundleTypeNew(); JsonObjectAppendObject(bundle_types, "common", bundle_type); } JsonIterator it = JsonIteratorInit(bundle_types); const char *bundle_type = NULL; while ((bundle_type = JsonIteratorNextKey(&it))) { JsonElement *promise_types = JsonObjectGetAsArray(JsonObjectGetAsObject(bundle_types, bundle_type), "promiseTypes"); for (int i = 0; i < SeqLength(common_promise_types); i++) { const char *common_promise_type = SeqAt(common_promise_types, i); JsonArrayAppendString(promise_types, common_promise_type); } } SeqDestroy(common_promise_types); return bundle_types; }
static void test_policy_json_offsets(void) { JsonElement *json = NULL; { Policy *original = LoadPolicy("benchmark.cf"); json = PolicyToJson(original); PolicyDestroy(original); } assert_true(json); JsonElement *json_bundles = JsonObjectGetAsArray(json, "bundles"); { JsonElement *main_bundle = JsonArrayGetAsObject(json_bundles, 0); int line = JsonPrimitiveGetAsInteger(JsonObjectGet(main_bundle, "line")); assert_int_equal(9, line); JsonElement *json_promise_types = JsonObjectGetAsArray(main_bundle, "promiseTypes"); { JsonElement *json_reports_type = JsonArrayGetAsObject(json_promise_types, 0); line = JsonPrimitiveGetAsInteger(JsonObjectGet(json_reports_type, "line")); assert_int_equal(11, line); JsonElement *json_contexts = JsonObjectGetAsArray(json_reports_type, "contexts"); JsonElement *cf_context = JsonArrayGetAsObject(json_contexts, 0); JsonElement *cf_context_promises = JsonObjectGetAsArray(cf_context, "promises"); JsonElement *hello_cf_promise = JsonArrayGetAsObject(cf_context_promises, 0); line = JsonPrimitiveGetAsInteger(JsonObjectGet(hello_cf_promise, "line")); assert_int_equal(13, line); JsonElement *hello_cf_attribs = JsonObjectGetAsArray(hello_cf_promise, "attributes"); { JsonElement *friend_pattern_attrib = JsonArrayGetAsObject(hello_cf_attribs, 0); line = JsonPrimitiveGetAsInteger(JsonObjectGet(friend_pattern_attrib, "line")); assert_int_equal(14, line); } } } JsonElement *json_bodies = JsonObjectGetAsArray(json, "bodies"); { JsonElement *control_body = JsonArrayGetAsObject(json_bodies, 0); int line = JsonPrimitiveGetAsInteger(JsonObjectGet(control_body, "line")); assert_int_equal(4, line); JsonElement *myperms_body = JsonArrayGetAsObject(json_bodies, 1); line = JsonPrimitiveGetAsInteger(JsonObjectGet(myperms_body, "line")); assert_int_equal(28, line); JsonElement *myperms_contexts = JsonObjectGetAsArray(myperms_body, "contexts"); JsonElement *any_context = JsonArrayGetAsObject(myperms_contexts, 0); JsonElement *any_attribs = JsonObjectGetAsArray(any_context, "attributes"); { JsonElement *mode_attrib = JsonArrayGetAsObject(any_attribs, 0); line = JsonPrimitiveGetAsInteger(JsonObjectGet(mode_attrib, "line")); assert_int_equal(30, line); } } JsonDestroy(json); }