static JsonElement *LookupVariable(Seq *hash_stack, const char *name, size_t name_len) { assert(SeqLength(hash_stack) > 0); size_t num_comps = StringCountTokens(name, name_len, "."); JsonElement *base_var = NULL; { StringRef base_comp = StringGetToken(name, name_len, 0, "."); char *base_comp_str = xstrndup(base_comp.data, base_comp.len); for (ssize_t i = SeqLength(hash_stack) - 1; i >= 0; i--) { JsonElement *hash = SeqAt(hash_stack, i); if (!hash) { continue; } if (JsonGetElementType(hash) == JSON_ELEMENT_TYPE_CONTAINER && JsonGetContainerType(hash) == JSON_CONTAINER_TYPE_OBJECT) { JsonElement *var = JsonObjectGet(hash, base_comp_str); if (var) { base_var = var; break; } } } free(base_comp_str); } if (!base_var) { return NULL; } for (size_t i = 1; i < num_comps; i++) { if (JsonGetElementType(base_var) != JSON_ELEMENT_TYPE_CONTAINER || JsonGetContainerType(base_var) != JSON_CONTAINER_TYPE_OBJECT) { return NULL; } StringRef comp = StringGetToken(name, name_len, i, "."); char *comp_str = xstrndup(comp.data, comp.len); base_var = JsonObjectGet(base_var, comp_str); free(comp_str); if (!base_var) { return NULL; } } assert(base_var); return base_var; }
static JsonElement *BodyTypesToJson(void) { JsonElement *body_types = JsonObjectCreate(50); 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]; for (int constraint_index = 0; promise_type_syntax->constraints[constraint_index].lval; constraint_index++) { const ConstraintSyntax *constraint_syntax = &promise_type_syntax->constraints[constraint_index]; if (constraint_syntax->dtype != CF_DATA_TYPE_BODY) { continue; } if (constraint_syntax->status == SYNTAX_STATUS_REMOVED) { continue; } const BodySyntax *body_syntax = constraint_syntax->range.body_type_syntax; JsonElement *body_type = JsonObjectGet(body_types, body_syntax->body_type); if (!body_type) { JsonElement *body_type = BodySyntaxToJson(body_syntax); JsonObjectAppendObject(body_types, body_syntax->body_type, body_type); } } } } for (int i = 0; CONTROL_BODIES[i].body_type; i++) { const BodySyntax *body_syntax = &CONTROL_BODIES[i]; if (body_syntax->status == SYNTAX_STATUS_REMOVED) { continue; } JsonElement *body_type = JsonObjectGet(body_types, body_syntax->body_type); if (!body_type) { JsonElement *body_type = BodySyntaxToJson(body_syntax); JsonObjectAppendObject(body_types, body_syntax->body_type, body_type); } } return body_types; }
static JsonElement *PromiseTypesToJson(void) { JsonElement *promise_types = JsonObjectCreate(50); const PromiseTypeSyntax *global_promise_type = PromiseTypeSyntaxGet("*", "*"); 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 and bundle-local common constraints if (strcmp("*", promise_type_syntax->promise_type) == 0) { continue; } if (promise_type_syntax->status == SYNTAX_STATUS_REMOVED) { continue; } JsonElement *promise_type = JsonObjectGet(promise_types, promise_type_syntax->promise_type); if (!promise_type) { promise_type = JsonPromiseTypeNew(promise_type_syntax->status); JsonObjectAppendObject(promise_types, promise_type_syntax->promise_type, promise_type); } assert(promise_type); JsonElement *attributes = JsonObjectGet(promise_type, "attributes"); assert(attributes); for (int i = 0; promise_type_syntax->constraints[i].lval; i++) { const ConstraintSyntax *constraint_syntax = &promise_type_syntax->constraints[i]; JsonElement *json_constraint = ConstraintSyntaxToJson(constraint_syntax); JsonObjectAppendString(json_constraint, "visibility", "promiseType"); JsonObjectAppendObject(attributes, constraint_syntax->lval, json_constraint); } // append bundle common constraints const PromiseTypeSyntax *bundle_promise_type = PromiseTypeSyntaxGet(promise_type_syntax->bundle_type, "*"); if (strcmp("*", bundle_promise_type->bundle_type) != 0) { for (int i = 0; bundle_promise_type->constraints[i].lval; i++) { const ConstraintSyntax *constraint_syntax = &bundle_promise_type->constraints[i]; JsonElement *json_constraint = ConstraintSyntaxToJson(constraint_syntax); JsonObjectAppendString(json_constraint, "visibility", "bundle"); JsonObjectAppendObject(attributes, constraint_syntax->lval, json_constraint); } } // append global common constraints for (int i = 0; global_promise_type->constraints[i].lval; i++) { const ConstraintSyntax *constraint_syntax = &global_promise_type->constraints[i]; JsonElement *json_constraint = ConstraintSyntaxToJson(constraint_syntax); JsonObjectAppendString(json_constraint, "visibility", "global"); JsonObjectAppendObject(attributes, constraint_syntax->lval, json_constraint); } } } return promise_types; }
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); }