void test_suite_1(void) { JSON_Value *val; TEST((val = json_parse_file("tests/test_1_1.txt")) != NULL); TEST(json_value_equals(json_parse_string(json_serialize_to_string(val)), val)); TEST(json_value_equals(json_parse_string(json_serialize_to_string_pretty(val)), val)); if (val) { json_value_free(val); } TEST((val = json_parse_file("tests/test_1_2.txt")) != NULL); TEST(json_value_equals(json_parse_string(json_serialize_to_string(val)), val)); TEST(json_value_equals(json_parse_string(json_serialize_to_string_pretty(val)), val)); if (val) { json_value_free(val); } TEST((val = json_parse_file("tests/test_1_3.txt")) != NULL); TEST(json_value_equals(json_parse_string(json_serialize_to_string(val)), val)); TEST(json_value_equals(json_parse_string(json_serialize_to_string_pretty(val)), val)); if (val) { json_value_free(val); } TEST((val = json_parse_file_with_comments("tests/test_1_1.txt")) != NULL); TEST(json_value_equals(json_parse_string(json_serialize_to_string(val)), val)); TEST(json_value_equals(json_parse_string(json_serialize_to_string_pretty(val)), val)); if (val) { json_value_free(val); } TEST((val = json_parse_file_with_comments("tests/test_1_2.txt")) != NULL); TEST(json_value_equals(json_parse_string(json_serialize_to_string(val)), val)); TEST(json_value_equals(json_parse_string(json_serialize_to_string_pretty(val)), val)); if (val) { json_value_free(val); } TEST((val = json_parse_file_with_comments("tests/test_1_3.txt")) != NULL); TEST(json_value_equals(json_parse_string(json_serialize_to_string(val)), val)); TEST(json_value_equals(json_parse_string(json_serialize_to_string_pretty(val)), val)); if (val) { json_value_free(val); } }
/* 3 test files from json.org */ void test_suite_1(void) { JSON_Value *val; TEST((val = json_parse_file("tests/test_1_1.txt")) != NULL); if (val) { json_value_free(val); } TEST((val = json_parse_file("tests/test_1_2.txt")) != NULL); if (val) { json_value_free(val); } TEST((val = json_parse_file("tests/test_1_3.txt")) != NULL); if (val) { json_value_free(val); } }
void test_suite_8(void) { const char *filename = "tests/test_2.txt"; const char *temp_filename = "tests/test_2_serialized.txt"; JSON_Value *a = NULL; JSON_Value *b = NULL; a = json_parse_file(filename); TEST(json_serialize_to_file(a, temp_filename) == JSONSuccess); b = json_parse_file(temp_filename); TEST(json_value_equals(a, b)); remove(temp_filename); }
void test_suite_6(void) { const char *filename = "tests/test_2.txt"; JSON_Value *a = NULL; JSON_Value *b = NULL; a = json_parse_file(filename); b = json_parse_file(filename); TEST(json_value_equals(a, b)); json_object_set_string(json_object(a), "string", "eki"); TEST(!json_value_equals(a, b)); a = json_value_deep_copy(b); TEST(json_value_equals(a, b)); json_array_append_number(json_object_get_array(json_object(b), "string array"), 1337); TEST(!json_value_equals(a, b)); }
void test_suite_8(void) { const char *filename = "tests/test_2.txt"; const char *temp_filename = "tests/test_2_serialized.txt"; JSON_Value *a = NULL; JSON_Value *b = NULL; char *buf = NULL; size_t serialization_size = 0; a = json_parse_file(filename); TEST(json_serialize_to_file(a, temp_filename) == JSONSuccess); b = json_parse_file(temp_filename); TEST(json_value_equals(a, b)); remove(temp_filename); serialization_size = json_serialization_size(a); buf = json_serialize_to_string(a); TEST((strlen(buf) + 1) == serialization_size); }
int MD5OpenGLMeshManagerCreate(const char* filename) { JSON_Value* root = NULL; JSON_Array* array = NULL; JSON_Object* rootObj = NULL; JSON_Object* object = NULL; size_t arraySize = 0; int i = 0; const char* md5filename = NULL; int id = 0; MD5OpenGLMesh* mesh = NULL; FxsMD5Animation* animation = NULL; if (wasInitialized) { ERR_MSG("Warning: MD5OpenGLMeshManagerCreate was already initialized") return 0; } root = json_parse_file(filename); if (!root) { sprintf(errMsg, "Failed to parse file: %s", filename); ERR_MSG(errMsg) return 0; }
void AppList::add_app_from_file(std::string file_path) { JSON_Value *root = json_parse_file(file_path.c_str()); if (!root) { std::cout << "Couldn't parse returned JSON"; return; } if (json_value_get_type(root) != JSONObject) { std::cout << "Returned JSON isn't a JSON object"; json_value_free(root); return; } JSON_Object *node = json_value_get_object(root); if (!node) { std::cout << "Couldn't get node"; json_value_free(root); return; } this->add_app(node); json_value_free(root); }
ctache_data_t *data_from_json(const char *file_name) { ctache_data_t *data; FILE *fp; struct json_parser *parser; struct json_token *tok; data = NULL; fp = fopen(file_name, "r"); if (fp == NULL) { return NULL; } parser = json_parse_file(fp); tok = json_next_token(parser); if (tok->type == JSON_BRACE_LEFT) { data = read_object(parser); } else { fprintf(stderr, "Bad JSON data\n"); return NULL; } fclose(fp); return data; }
/* Testing correctness of parsed values */ void test_suite_2(void) { JSON_Value *root_value; JSON_Object *object; JSON_Array *array; int i; const char *filename = "tests/test_2.txt"; printf("Testing %s:\n", filename); root_value = json_parse_file(filename); TEST(root_value); TEST(json_value_get_type(root_value) == JSONObject); object = json_value_get_object(root_value); TEST(STREQ(json_object_get_string(object, "string"), "lorem ipsum")); TEST(STREQ(json_object_get_string(object, "utf string"), "lorem ipsum")); TEST(json_object_get_number(object, "positive one") == 1.0); TEST(json_object_get_number(object, "negative one") == -1.0); TEST(json_object_get_number(object, "hard to parse number") == -0.000314); TEST(json_object_get_boolean(object, "boolean true") == 1); TEST(json_object_get_boolean(object, "boolean false") == 0); TEST(json_value_get_type(json_object_get_value(object, "null")) == JSONNull); array = json_object_get_array(object, "string array"); if (array != NULL && json_array_get_count(array) > 1) { TEST(STREQ(json_array_get_string(array, 0), "lorem")); TEST(STREQ(json_array_get_string(array, 1), "ipsum")); } else { tests_failed++; } array = json_object_get_array(object, "x^2 array"); if (array != NULL) { for (i = 0; i < json_array_get_count(array); i++) { TEST(json_array_get_number(array, i) == (i * i)); } } else { tests_failed++; } TEST(json_object_get_array(object, "non existent array") == NULL); TEST(STREQ(json_object_dotget_string(object, "object.nested string"), "str")); TEST(json_object_dotget_boolean(object, "object.nested true") == 1); TEST(json_object_dotget_boolean(object, "object.nested false") == 0); TEST(json_object_dotget_value(object, "object.nested null") != NULL); TEST(json_object_dotget_number(object, "object.nested number") == 123); TEST(json_object_dotget_value(object, "should.be.null") == NULL); TEST(json_object_dotget_value(object, "should.be.null.") == NULL); TEST(json_object_dotget_value(object, ".") == NULL); TEST(json_object_dotget_value(object, "") == NULL); array = json_object_dotget_array(object, "object.nested array"); if (array != NULL && json_array_get_count(array) > 1) { TEST(STREQ(json_array_get_string(array, 0), "lorem")); TEST(STREQ(json_array_get_string(array, 1), "ipsum")); } else { tests_failed++; } TEST(json_object_dotget_boolean(object, "nested true")); json_value_free(root_value); }
void test_precincts_file(void) { const char *filename = "tests/precincts.json"; JSON_Value *root_value = NULL; printf("Testing %s:\n", filename); root_value = json_parse_file(filename); test_suite_precincts(root_value); json_value_free(root_value); }
void test_suite_2_no_comments(void) { const char *filename = "tests/test_2.txt"; JSON_Value *root_value = NULL; root_value = json_parse_file(filename); test_suite_2(root_value); TEST(json_value_equals(root_value, json_parse_string(json_serialize_to_string(root_value)))); json_value_free(root_value); }
void test_suite_2_no_comments(void) { const char *filename = "tests/test_2.txt"; JSON_Value *root_value = NULL; printf("Testing %s:\n", filename); root_value = json_parse_file(filename); test_suite_2(root_value); json_value_free(root_value); }
static uintptr_t do_load_ppp_module(const char *fname) { tried_files = g_list_prepend(tried_files, g_strdup(fname)); module_dl_handler = dlopen(fname, RTLD_LAZY); if (!module_dl_handler) { trace_info_f("%s, can't open %s\n", __func__, fname); return 1; } int32_t (*ppp_initialize_module)(PP_Module module_id, PPB_GetInterface get_browser_interface); ppp_initialize_module = dlsym(module_dl_handler, "PPP_InitializeModule"); ppp_get_interface = dlsym(module_dl_handler, "PPP_GetInterface"); if (!ppp_initialize_module || !ppp_get_interface) { trace_error("%s, one of required PPP_* is missing\n", __func__); if (module_dl_handler) dlclose(module_dl_handler); module_dl_handler = NULL; return 1; } module_file_name = g_strdup(fname); if (!fpp_config_plugin_has_manifest()) { use_fallback_version_strings(); return 0; } // try to read manifest.json file (only for those who can have it) char *manifest_dir = strdup(fname); gchar *manifest_path = g_strdup_printf("%s/manifest.json", dirname(manifest_dir)); free(manifest_dir); JSON_Value *root_val = json_parse_file(manifest_path); g_free(manifest_path); if (!root_val) { use_fallback_version_strings(); return 0; } JSON_Object *root_obj = json_value_get_object(root_val); const char *version = json_object_get_string(root_obj, "version"); if (version) { int v1 = 0, v2 = 0, v3 = 0, v4 = 0; module_version = g_strdup(version); (void)sscanf(module_version, "%9d.%9d.%9d.%9d", &v1, &v2, &v3, &v4); module_descr = g_strdup_printf("%s %d.%d r%d", fpp_config_get_plugin_name(), v1, v2, v3); } else { use_fallback_version_strings(); } json_value_free(root_val); return 0; }
void test_suite_4() { const char *filename = "tests/test_2.txt"; JSON_Value *a = NULL, *a_copy = NULL; printf("Testing %s:\n", filename); a = json_parse_file(filename); TEST(json_value_equals(a, a)); /* test equality test */ a_copy = json_value_deep_copy(a); TEST(a_copy != NULL); TEST(json_value_equals(a, a_copy)); }
void test_suite_9(void) { const char *filename = "tests/test_2_pretty.txt"; const char *temp_filename = "tests/test_2_serialized_pretty.txt"; char *file_contents = NULL; char *serialized = NULL; JSON_Value *a = NULL; JSON_Value *b = NULL; size_t serialization_size = 0; a = json_parse_file(filename); TEST(json_serialize_to_file_pretty(a, temp_filename) == JSONSuccess); b = json_parse_file(temp_filename); TEST(json_value_equals(a, b)); remove(temp_filename); serialization_size = json_serialization_size_pretty(a); serialized = json_serialize_to_string_pretty(a); TEST((strlen(serialized) + 1) == serialization_size); file_contents = read_file(filename); TEST(STREQ(file_contents, serialized)); }
void test_suite_7(void) { JSON_Value *val_from_file = json_parse_file("tests/test_5.txt"); JSON_Value *schema = json_value_init_object(); JSON_Object *schema_obj = json_value_get_object(schema); json_object_set_string(schema_obj, "first", ""); json_object_set_string(schema_obj, "last", ""); json_object_set_number(schema_obj, "age", 0); json_object_set_null(schema_obj, "favorites"); TEST(json_validate(schema, val_from_file) == JSONSuccess); json_object_set_string(schema_obj, "age", ""); TEST(json_validate(schema, val_from_file) == JSONFailure); }
/* * Test KAT handler API. * The ctx is NULL, expecting failure. */ Test(CMAC_API, null_ctx) { val = json_parse_file("json/cmac/cmac_aes.json"); obj = ut_get_obj_from_rsp(val); if (!obj) { ACVP_LOG_ERR("JSON obj parse error"); return; } /* Test with NULL JSON object */ rv = acvp_cmac_kat_handler(NULL, obj); cr_assert(rv == ACVP_NO_CTX); json_value_free(val); }
void Config::parse_config_file(const std::string& config_file_path) { JSON_Value *root = json_parse_file(config_file_path.c_str()); if (!root) { std::cout << "Couldn't parse returned JSON\n"; return; } if (json_value_get_type(root) != JSONObject) { std::cout << "Returned JSON isn't a JSON object\n"; json_value_free(root); return; } JSON_Object *node = json_value_get_object(root); if (!node) { std::cout << "Couldn't get node\n"; json_value_free(root); return; } std::string val; bool rel_path; const std::string rel_path_pattern = "./"; for (auto it = this->config.begin(); it != this->config.end(); ++it) { val = get_json_val<std::string>(node, it->first); if (val.empty()) continue; rel_path = val.find("./") == 0; if (rel_path) { val = config_file_path.substr( 0, config_file_path.length() - this->conf_filename.length() ) + val.substr(rel_path_pattern.length()); } it->second = val; } json_value_free(root); }
int main(int argc, char **argv) { PValue v = NULL; StrTempl *st = NULL; char *res = NULL; char *jfile, *tfile; if (argc != 3) { jfile = "test.js"; tfile = "test.ctp"; } else { jfile = argv[1]; tfile = argv[2]; } printf("file '%s' '%s'\n",jfile,tfile); v = json_parse_file(jfile); if (value_is_error(v)) { printf("json error: %s\n",value_as_string(v)); goto err; } res = file_read_all(tfile,true); if (! res) { printf("cannot read '%s'\n",tfile); goto err; } st = str_templ_new(res,NULL); if (value_is_error(st)) { printf("template compilation error: %s\n",value_as_string(st)); goto err; } unref(res); str_templ_add_builtin("test",test_impl); res = str_templ_subst_values(st,v); if (value_is_error(res)) { printf("template evaluation error: %s\n",value_as_string(res)); goto err; } printf("%s\n",res); err: dispose(v,st,res); printf("kount = %d\n",obj_kount()); return 0; }
void persistence_example(void) { JSON_Value *schema = json_parse_string("{\"name\":\"\"}"); JSON_Value *user_data = json_parse_file("user_data.json"); char buf[256]; const char *name = NULL; if (user_data == NULL || json_validate(schema, user_data) != JSONSuccess) { puts("Enter your name:"); scanf("%s", buf); user_data = json_value_init_object(); json_object_set_string(json_object(user_data), "name", buf); json_serialize_to_file(user_data, "user_data.json"); } name = json_object_get_string(json_object(user_data), "name"); printf("Hello, %s.", name); json_value_free(schema); json_value_free(user_data); return; }
int main() { clock_t begin, end; double time_spent; begin = clock(); FILE *fp = fopen("/home/jan/.config/colors/config.json", "r"); json_object *root = json_parse_file(fp); end = clock(); time_spent = (double)(end-begin)/CLOCKS_PER_SEC; printf("Time spent: %f", time_spent); printf("Root: %s\n", root->key); json_object *menu = (json_object*)&root->value; json_object *popup = (json_object*)&menu->next->next->value; json_array *menuitem = (json_array*)&popup->value; json_object *first = (json_object*)&menuitem->value; printf("value first: %s\n", first->next->value); return 0; }
/* * Test the KAT handler API. * The ctx is empty (no capabilities), expecting failure. */ Test(CMAC_API, empty_ctx) { setup_empty_ctx(&ctx); val = json_parse_file("json/cmac/cmac_aes.json"); obj = ut_get_obj_from_rsp(val); if (!obj) { ACVP_LOG_ERR("JSON obj parse error"); goto end; } rv = acvp_cmac_kat_handler(ctx, obj); cr_assert(rv == ACVP_UNSUPPORTED_OP); json_value_free(val); end: if (ctx) teardown_ctx(&ctx); }
static RabbitMQ *config(char *confFile, MainTasks *main) { struct json_object *config = NULL; RabbitMQ *mq = NULL; int fdsock = open(confFile, O_RDONLY); if (fdsock < 0) { logconsole("Failed to open %s", confFile); } else { config = json_parse_file(fdsock); close(fdsock); if (config) { mq = rabbitmq_new(config, addTasks, NULL, main); json_object_put(config); } } return mq; }
void test_suite_5(void) { JSON_Value *val_from_file = json_parse_file("tests/test_5.txt"); JSON_Value *val = json_value_init_object(); JSON_Object *obj = json_value_get_object(val); TEST(json_object_set_string(obj, "first", "John") == JSONSuccess); TEST(json_object_set_string(obj, "last", "Doe") == JSONSuccess); TEST(json_object_set_number(obj, "age", 25) == JSONSuccess); TEST(json_object_set_boolean(obj, "registered", 1) == JSONSuccess); TEST(json_object_set_value(obj, "interests", json_value_init_array()) == JSONSuccess); TEST(json_array_append_string(json_object_get_array(obj, "interests"), "Writing") == JSONSuccess); TEST(json_array_append_string(json_object_get_array(obj, "interests"), "Mountain Biking") == JSONSuccess); TEST(json_array_replace_string(json_object_get_array(obj, "interests"), 0, "Reading") == JSONSuccess); TEST(json_object_dotset_string(obj, "favorites.color", "blue") == JSONSuccess); TEST(json_object_dotset_string(obj, "favorites.sport", "running") == JSONSuccess); TEST(json_object_dotset_string(obj, "favorites.fruit", "apple") == JSONSuccess); TEST(json_object_dotremove(obj, "favorites.fruit") == JSONSuccess); TEST(json_object_set_string(obj, "utf string", "\\u006corem\\u0020ipsum") == JSONSuccess); TEST(json_object_set_string(obj, "utf-8 string", "あいうえお") == JSONSuccess); TEST(json_object_set_string(obj, "surrogate string", "lorem\\uD834\\uDD1Eipsum\\uD834\\uDF67lorem") == JSONSuccess); TEST(json_value_equals(val_from_file, val)); }
void print_commits_info(const char *username, const char *repo) { JSON_Value *root_value; JSON_Array *commits; JSON_Object *commit; size_t i; char curl_command[512]; char cleanup_command[256]; char output_filename[] = "commits.json"; /* it ain't pretty, but it's not a libcurl tutorial */ sprintf(curl_command, "curl -s \"https://api.github.com/repos/%s/%s/commits\" > %s", username, repo, output_filename); sprintf(cleanup_command, "rm -f %s", output_filename); system(curl_command); /* parsing json and validating output */ root_value = json_parse_file(output_filename); if (json_value_get_type(root_value) != JSONArray) { system(cleanup_command); return; } /* getting array from root value and printing commit info */ commits = json_value_get_array(root_value); printf("%-10.10s %-10.10s %s\n", "Date", "SHA", "Author"); for (i = 0; i < json_array_get_count(commits); i++) { commit = json_array_get_object(commits, i); printf("%.10s %.10s %s\n", json_object_dotget_string(commit, "commit.author.date"), json_object_get_string(commit, "sha"), json_object_dotget_string(commit, "commit.author.name")); } /* cleanup code */ json_value_free(root_value); system(cleanup_command); }
int main(int argc, char** argv) { if (argc != 3) { logconsole("Usage: corpus-import input output\n"); return EXIT_FAILURE; } logconsole(PKGBANNER); logconsole(PKGBUILD); logconsole("Reading %s", argv[1]); int fsock = open(argv[1], O_RDONLY); if (fsock == -1) { logconsole("No source %s", argv[1]); return EXIT_FAILURE; } struct json_object *obj = json_parse_file(fsock); if (!obj) { logconsole("Failed to parse json"); return EXIT_FAILURE; } close(fsock); struct List data; list_init(&data); if (import_corpus(obj, &data)) { logconsole("Failed to import"); return EXIT_FAILURE; } logconsole("Writing %s", argv[2]); FILE *out = fopen(argv[2], "w"); int status = write_corpus_list(&data, out); fclose(out); return status ? EXIT_FAILURE : EXIT_SUCCESS; }
void *parse(void *data) { json_ctx *ctx; char *ptr; char *p = (char*)data; int j; ctx = json_ctx_new(); cstr path = cstr_create(128); ptr = rindex(p, '/'); if(ptr != NULL) { cstr_ncat(path, p, ptr - p); cstr_cat_char(path, '/'); } cstr_cat(path, "test1.json"); FILE *f = fopen(path,"r"); j = json_parse_file(ctx, f); printf("parse file result: %d\n", j); if(j) printf("error:%s, line:%d col:%d token:%s\n", ctx->err, ctx->lineno, ctx->colno ,ctx->token); fclose(f); cstr_free(path); json_ctx_free(ctx); return NULL; }
bud_config_t* bud_config_load(const char* path, int inlined, bud_error_t* err) { int i; JSON_Value* json; JSON_Value* val; JSON_Object* obj; JSON_Object* tmp; JSON_Object* log; JSON_Object* avail; JSON_Array* contexts; JSON_Array* backend; bud_config_t* config; bud_context_t* ctx; if (inlined) json = json_parse_string(path); else json = json_parse_file(path); if (json == NULL) { *err = bud_error_str(kBudErrJSONParse, path); goto end; } obj = json_value_get_object(json); if (obj == NULL) { *err = bud_error(kBudErrJSONNonObjectRoot); goto failed_get_object; } config = calloc(1, sizeof(*config)); if (config == NULL) { *err = bud_error_str(kBudErrNoMem, "bud_config_t"); goto failed_get_object; } /* Copy path or inlined config value */ config->path = strdup(path); if (config->path == NULL) { *err = bud_error_str(kBudErrNoMem, "bud_config_t strcpy(path)"); goto failed_alloc_path; } config->inlined = inlined; /* Allocate contexts and backends */ contexts = json_object_get_array(obj, "contexts"); backend = json_object_get_array(obj, "backend"); config->context_count = contexts == NULL ? 0 : json_array_get_count(contexts); config->backend_count = backend == NULL ? 0 : json_array_get_count(backend); config->contexts = calloc(config->context_count + 1, sizeof(*config->contexts)); config->backend = calloc(config->backend_count, sizeof(*config->backend)); if (config->contexts == NULL || config->backend == NULL) { *err = bud_error_str(kBudErrNoMem, "bud_context_t"); goto failed_get_index; } config->json = json; /* Workers configuration */ config->worker_count = -1; config->restart_timeout = -1; val = json_object_get_value(obj, "workers"); if (val != NULL) config->worker_count = json_value_get_number(val); val = json_object_get_value(obj, "restart_timeout"); if (val != NULL) config->restart_timeout = json_value_get_number(val); /* Logger configuration */ log = json_object_get_object(obj, "log"); config->log.stdio = -1; config->log.syslog = -1; if (log != NULL) { config->log.level = json_object_get_string(log, "level"); config->log.facility = json_object_get_string(log, "facility"); val = json_object_get_value(log, "stdio"); if (val != NULL) config->log.stdio = json_value_get_boolean(val); val = json_object_get_value(log, "syslog"); if (val != NULL) config->log.syslog = json_value_get_boolean(val); } /* Availability configuration */ avail = json_object_get_object(obj, "availability"); config->availability.death_timeout = -1; config->availability.revive_interval = -1; config->availability.retry_interval = -1; config->availability.max_retries = -1; if (avail != NULL) { val = json_object_get_value(avail, "death_timeout"); if (val != NULL) config->availability.death_timeout = json_value_get_number(val); val = json_object_get_value(avail, "revive_interval"); if (val != NULL) config->availability.revive_interval = json_value_get_number(val); val = json_object_get_value(avail, "retry_interval"); if (val != NULL) config->availability.retry_interval = json_value_get_number(val); val = json_object_get_value(avail, "max_retries"); if (val != NULL) config->availability.max_retries = json_value_get_number(val); } /* Frontend configuration */ *err = bud_config_load_frontend(json_object_get_object(obj, "frontend"), &config->frontend); if (!bud_is_ok(*err)) goto failed_get_index; /* Backend configuration */ config->balance = json_object_get_string(obj, "balance"); for (i = 0; i < config->backend_count; i++) { bud_config_load_backend(config, json_array_get_object(backend, i), &config->backend[i]); } /* SNI configuration */ bud_config_read_pool_conf(obj, "sni", &config->sni); /* OCSP Stapling configuration */ bud_config_read_pool_conf(obj, "stapling", &config->stapling); /* SSL Contexts */ /* TODO(indutny): sort them and do binary search */ for (i = 0; i < config->context_count; i++) { /* NOTE: contexts[0] - is a default context */ ctx = &config->contexts[i + 1]; obj = json_array_get_object(contexts, i); if (obj == NULL) { *err = bud_error(kBudErrJSONNonObjectCtx); goto failed_get_index; } ctx->servername = json_object_get_string(obj, "servername"); ctx->servername_len = ctx->servername == NULL ? 0 : strlen(ctx->servername); ctx->cert_file = json_object_get_string(obj, "cert"); ctx->key_file = json_object_get_string(obj, "key"); ctx->npn = json_object_get_array(obj, "npn"); ctx->ciphers = json_object_get_string(obj, "ciphers"); ctx->ecdh = json_object_get_string(obj, "ecdh"); ctx->ticket_key = json_object_get_string(obj, "ticket_key"); tmp = json_object_get_object(obj, "backend"); if (tmp != NULL) { ctx->backend = &ctx->backend_st; bud_config_load_backend(config, tmp, ctx->backend); } *err = bud_config_verify_npn(ctx->npn); if (!bud_is_ok(*err)) goto failed_get_index; } bud_config_set_defaults(config); *err = bud_ok(); return config; failed_get_index: free(config->contexts); config->contexts = NULL; free(config->backend); config->backend = NULL; free(config->path); config->path = NULL; failed_alloc_path: free(config); failed_get_object: json_value_free(json); end: return NULL; }
bud_config_t* bud_config_load(uv_loop_t* loop, const char* path, bud_error_t* err) { int i; int context_count; JSON_Value* json; JSON_Value* val; JSON_Object* obj; JSON_Object* log; JSON_Object* frontend; JSON_Object* backend; JSON_Array* contexts; bud_config_t* config; bud_context_t* ctx; json = json_parse_file(path); if (json == NULL) { *err = bud_error_str(kBudErrJSONParse, path); goto end; } obj = json_value_get_object(json); if (obj == NULL) { *err = bud_error(kBudErrJSONNonObjectRoot); goto failed_get_object; } contexts = json_object_get_array(obj, "contexts"); context_count = contexts == NULL ? 0 : json_array_get_count(contexts); config = calloc(1, sizeof(*config) + context_count * sizeof(*config->contexts)); if (config == NULL) { *err = bud_error_str(kBudErrNoMem, "bud_config_t"); goto failed_get_object; } config->loop = loop; config->json = json; /* Workers configuration */ config->worker_count = -1; config->restart_timeout = -1; val = json_object_get_value(obj, "workers"); if (val != NULL) config->worker_count = json_value_get_number(val); val = json_object_get_value(obj, "restart_timeout"); if (val != NULL) config->restart_timeout = json_value_get_number(val); /* Logger configuration */ log = json_object_get_object(obj, "log"); config->log.stdio = -1; config->log.syslog = -1; if (log != NULL) { config->log.level = json_object_get_string(log, "level"); config->log.facility = json_object_get_string(log, "facility"); val = json_object_get_value(log, "stdio"); if (val != NULL) config->log.stdio = json_value_get_boolean(val); val = json_object_get_value(log, "syslog"); if (val != NULL) config->log.syslog = json_value_get_boolean(val); } /* Frontend configuration */ frontend = json_object_get_object(obj, "frontend"); config->frontend.proxyline = -1; config->frontend.keepalive = -1; config->frontend.server_preference = -1; config->frontend.ssl3 = -1; if (frontend != NULL) { config->frontend.port = (uint16_t) json_object_get_number(frontend, "port"); config->frontend.host = json_object_get_string(frontend, "host"); config->frontend.security = json_object_get_string(frontend, "security"); config->frontend.npn = json_object_get_array(frontend, "npn"); config->frontend.ciphers = json_object_get_string(frontend, "ciphers"); config->frontend.cert_file = json_object_get_string(frontend, "cert"); config->frontend.key_file = json_object_get_string(frontend, "key"); config->frontend.reneg_window = json_object_get_number(frontend, "reneg_window"); config->frontend.reneg_limit = json_object_get_number(frontend, "reneg_limit"); *err = bud_config_verify_npn(config->frontend.npn); if (!bud_is_ok(*err)) goto failed_get_index; val = json_object_get_value(frontend, "proxyline"); if (val != NULL) config->frontend.proxyline = json_value_get_boolean(val); val = json_object_get_value(frontend, "keepalive"); if (val != NULL) config->frontend.keepalive = json_value_get_number(val); val = json_object_get_value(frontend, "server_preference"); if (val != NULL) config->frontend.server_preference = json_value_get_boolean(val); val = json_object_get_value(frontend, "ssl3"); if (val != NULL) config->frontend.ssl3 = json_value_get_boolean(val); } /* Backend configuration */ backend = json_object_get_object(obj, "backend"); config->backend.keepalive = -1; if (backend != NULL) { config->backend.port = (uint16_t) json_object_get_number(backend, "port"); config->backend.host = json_object_get_string(backend, "host"); val = json_object_get_value(backend, "keepalive"); if (val != NULL) config->backend.keepalive = json_value_get_number(val); } /* SNI configuration */ bud_config_read_pool_conf(obj, "sni", &config->sni); /* OCSP Stapling configuration */ bud_config_read_pool_conf(obj, "stapling", &config->stapling); /* SSL Contexts */ /* TODO(indutny): sort them and do binary search */ for (i = 0; i < context_count; i++) { /* NOTE: contexts[0] - is a default context */ ctx = &config->contexts[i + 1]; obj = json_array_get_object(contexts, i); if (obj == NULL) { *err = bud_error(kBudErrJSONNonObjectCtx); goto failed_get_index; } ctx->servername = json_object_get_string(obj, "servername"); ctx->servername_len = ctx->servername == NULL ? 0 : strlen(ctx->servername); ctx->cert_file = json_object_get_string(obj, "cert"); ctx->key_file = json_object_get_string(obj, "key"); ctx->npn = json_object_get_array(obj, "npn"); ctx->ciphers = json_object_get_string(obj, "ciphers"); *err = bud_config_verify_npn(ctx->npn); if (!bud_is_ok(*err)) goto failed_get_index; } config->context_count = context_count; bud_config_set_defaults(config); *err = bud_ok(); return config; failed_get_index: free(config); failed_get_object: json_value_free(json); end: return NULL; }
void test_suite_5(void) { JSON_Value *val_from_file = json_parse_file("tests/test_5.txt"); JSON_Value *val = NULL; JSON_Object *obj = NULL; JSON_Array *interests_arr = NULL; val = json_value_init_object(); TEST(val != NULL); obj = json_value_get_object(val); TEST(obj != NULL); TEST(json_object_set_string(obj, "first", "John") == JSONSuccess); TEST(json_object_set_string(obj, "last", "Doe") == JSONSuccess); TEST(json_object_set_number(obj, "age", 25) == JSONSuccess); TEST(json_object_set_boolean(obj, "registered", 1) == JSONSuccess); TEST(json_object_set_value(obj, "interests", json_value_init_array()) == JSONSuccess); interests_arr = json_object_get_array(obj, "interests"); TEST(interests_arr != NULL); TEST(json_array_append_string(interests_arr, "Writing") == JSONSuccess); TEST(json_array_append_string(interests_arr, "Mountain Biking") == JSONSuccess); TEST(json_array_replace_string(interests_arr, 0, "Reading") == JSONSuccess); TEST(json_object_dotset_string(obj, "favorites.color", "blue") == JSONSuccess); TEST(json_object_dotset_string(obj, "favorites.sport", "running") == JSONSuccess); TEST(json_object_dotset_string(obj, "favorites.fruit", "apple") == JSONSuccess); TEST(json_object_dotremove(obj, "favorites.fruit") == JSONSuccess); TEST(json_object_set_string(obj, "utf string", "lorem ipsum") == JSONSuccess); TEST(json_object_set_string(obj, "utf-8 string", "あいうえお") == JSONSuccess); TEST(json_object_set_string(obj, "surrogate string", "lorem𝄞ipsum𝍧lorem") == JSONSuccess); TEST(json_object_set_string(obj, "windows path", "C:\\Windows\\Path") == JSONSuccess); TEST(json_value_equals(val_from_file, val)); TEST(json_object_set_string(obj, NULL, "") == JSONFailure); TEST(json_object_set_string(obj, "last", NULL) == JSONFailure); TEST(json_object_set_string(obj, NULL, NULL) == JSONFailure); TEST(json_object_set_value(obj, NULL, NULL) == JSONFailure); TEST(json_object_dotset_string(obj, NULL, "") == JSONFailure); TEST(json_object_dotset_string(obj, "favorites.color", NULL) == JSONFailure); TEST(json_object_dotset_string(obj, NULL, NULL) == JSONFailure); TEST(json_object_dotset_value(obj, NULL, NULL) == JSONFailure); TEST(json_array_append_string(NULL, "lorem") == JSONFailure); TEST(json_array_append_value(interests_arr, NULL) == JSONFailure); TEST(json_array_append_value(NULL, NULL) == JSONFailure); TEST(json_array_remove(NULL, 0) == JSONFailure); TEST(json_array_replace_value(interests_arr, 0, NULL) == JSONFailure); TEST(json_array_replace_string(NULL, 0, "lorem") == JSONFailure); TEST(json_array_replace_string(interests_arr, 100, "not existing") == JSONFailure); TEST(json_array_append_string(json_object_get_array(obj, "interests"), NULL) == JSONFailure); /* UTF-8 tests */ TEST(json_object_set_string(obj, "correct string", "κόσμε") == JSONSuccess); TEST(json_object_set_string(obj, "boundary 1", "\xed\x9f\xbf") == JSONSuccess); TEST(json_object_set_string(obj, "boundary 2", "\xee\x80\x80") == JSONSuccess); TEST(json_object_set_string(obj, "boundary 3", "\xef\xbf\xbd") == JSONSuccess); TEST(json_object_set_string(obj, "boundary 4", "\xf4\x8f\xbf\xbf") == JSONSuccess); TEST(json_object_set_string(obj, "first continuation byte", "\x80") == JSONFailure); TEST(json_object_set_string(obj, "last continuation byte", "\xbf") == JSONFailure); TEST(json_object_set_string(obj, "impossible sequence 1", "\xfe") == JSONFailure); TEST(json_object_set_string(obj, "impossible sequence 2", "\xff") == JSONFailure); TEST(json_object_set_string(obj, "impossible sequence 3", "\xfe\xfe\xff\xff") == JSONFailure); TEST(json_object_set_string(obj, "overlong 1", "\xc0\xaf") == JSONFailure); TEST(json_object_set_string(obj, "overlong 2", "\xc1\xbf") == JSONFailure); TEST(json_object_set_string(obj, "overlong 3", "\xe0\x80\xaf") == JSONFailure); TEST(json_object_set_string(obj, "overlong 4", "\xe0\x9f\xbf") == JSONFailure); TEST(json_object_set_string(obj, "overlong 5", "\xf0\x80\x80\xaf") == JSONFailure); TEST(json_object_set_string(obj, "overlong 6", "\xf0\x8f\xbf\xbf") == JSONFailure); TEST(json_object_set_string(obj, "overlong 7", "\xf0\x8f\xbf\xbf") == JSONFailure); TEST(json_object_set_string(obj, "overlong null 1", "\xc0\x80") == JSONFailure); TEST(json_object_set_string(obj, "overlong null 2", "\xe0\x80\x80") == JSONFailure); TEST(json_object_set_string(obj, "overlong null 3", "\xf0\x80\x80\x80") == JSONFailure); TEST(json_object_set_string(obj, "overlong null 4", "\xf8\x80\x80\x80\x80") == JSONFailure); TEST(json_object_set_string(obj, "overlong null 5", "\xfc\x80\x80\x80\x80\x80") == JSONFailure); TEST(json_object_set_string(obj, "single surrogate 1", "\xed\xa0\x80") == JSONFailure); TEST(json_object_set_string(obj, "single surrogate 2", "\xed\xaf\xbf") == JSONFailure); TEST(json_object_set_string(obj, "single surrogate 3", "\xed\xbf\xbf") == JSONFailure); }