static GString *gmime_message_to_json(GMimeMessage *message, guint content_option) { MessageData *mdata = convert_message(message, content_option); JSON_Value *root_value = json_value_init_object(); JSON_Object *root_object = json_value_get_object(root_value); json_object_set_value(root_object, "from", addresses_list_to_json(mdata->from)); json_object_set_value(root_object, "to", addresses_list_to_json(mdata->to)); json_object_set_value(root_object, "replyTo", addresses_list_to_json(mdata->reply_to)); json_object_set_value(root_object, "cc", addresses_list_to_json(mdata->cc)); json_object_set_value(root_object, "bcc", addresses_list_to_json(mdata->bcc)); json_object_set_string(root_object, "messageId", mdata->message_id); json_object_set_string(root_object, "subject", mdata->subject); json_object_set_string(root_object, "date", mdata->date); json_object_set_value(root_object, "inReplyTo", references_to_json(mdata->in_reply_to)); json_object_set_value(root_object, "references", references_to_json(mdata->references)); json_object_set_value(root_object, "text", message_body_to_json(mdata->text)); json_object_set_value(root_object, "html", message_body_to_json(mdata->html)); json_object_set_value(root_object, "attachments", message_attachments_list_to_json(mdata->attachments)); free_message_data(mdata); gchar *serialized_string = json_serialize_to_string(root_value); json_value_free(root_value); GString *json_string = g_string_new(serialized_string); g_free(serialized_string); return json_string; }
static JSON_Value *address_to_json(Address *addr) { if (!addr) return NULL; JSON_Value *address_value = json_value_init_object(); JSON_Object *address_object = json_value_get_object(address_value); json_object_set_string(address_object, "name", addr->name); json_object_set_string(address_object, "address", addr->address); return address_value; }
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); }
static JSON_Value *message_body_to_json(MessageBody *mbody) { if (!mbody) return NULL; JSON_Value *body_value = json_value_init_object(); JSON_Object *body_object = json_value_get_object(body_value); json_object_set_string(body_object, "type", mbody->content_type); json_object_set_string(body_object, "content", mbody->content->str); json_object_set_number(body_object, "size", mbody->size); return body_value; }
//get message - string void getMessage(jsonMessage_t jsonMessage,String* message){ *message=NULL; //init JSON_Value *root_value = json_value_init_object(); JSON_Object *root_object = json_value_get_object(root_value); json_object_set_string(root_object, "type", jsonMessage.type); json_object_set_string(root_object, "address", jsonMessage.address); json_object_set_string(root_object, "replyAddress", jsonMessage.replyAddress); json_object_set_value(root_object, "headers", jsonMessage.headers); json_object_set_value(root_object, "body", jsonMessage.body); //to string *message=json_serialize_to_string_pretty(root_value); //free json_value_free(root_value); }
static JSON_Value* querySpecification_toJson(const PROVISIONING_QUERY_SPECIFICATION* query_spec) { JSON_Value* root_value = NULL; JSON_Object* root_object = NULL; //Setup if ((root_value = json_value_init_object()) == NULL) { LogError("json_value_init_object failed"); } else if ((root_object = json_value_get_object(root_value)) == NULL) { LogError("json_value_get_object failed"); json_value_free(root_value); root_value = NULL; } //Set data else if (json_object_set_string(root_object, QUERY_SPECIFICATION_JSON_KEY_QUERY, query_spec->query_string) != JSONSuccess) { LogError("Failed to set %s in JSON string", QUERY_SPECIFICATION_JSON_KEY_QUERY); json_value_free(root_value); root_value = NULL; } return root_value; }
/* * After the test case has been processed by the DUT, the results * need to be JSON formated to be included in the vector set results * file that will be uploaded to the server. This routine handles * the JSON processing for a single test case. */ static ACVP_RESULT acvp_cmac_output_tc(ACVP_CTX *ctx, ACVP_CMAC_TC *stc, JSON_Object *tc_rsp) { ACVP_RESULT rv = ACVP_SUCCESS; char *tmp = NULL; tmp = calloc(ACVP_CMAC_MACLEN_MAX + 1, sizeof(char)); if (!tmp) { ACVP_LOG_ERR("Unable to malloc in acvp_cmac_output_tc"); return ACVP_MALLOC_FAIL; } if (stc->verify) { json_object_set_boolean(tc_rsp, "testPassed", stc->ver_disposition); } else { rv = acvp_bin_to_hexstr(stc->mac, stc->mac_len, tmp, ACVP_CMAC_MACLEN_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("hex conversion failure (mac)"); goto end; } json_object_set_string(tc_rsp, "mac", tmp); } end: if (tmp) free(tmp); return rv; }
int main(int argc, char **argv) { json_object *tmp=json_object_new_int(123); assert (json_object_get_int(tmp)==123); json_object_set_int(tmp,321); assert (json_object_get_int(tmp)==321); printf("INT PASSED\n"); json_object_set_int64(tmp,(int64_t)321321321); assert (json_object_get_int64(tmp)==321321321); json_object_put(tmp); printf("INT64 PASSED\n"); tmp=json_object_new_boolean(TRUE); assert (json_object_get_boolean(tmp)==TRUE); json_object_set_boolean(tmp,FALSE); assert (json_object_get_boolean(tmp)==FALSE); json_object_set_boolean(tmp,TRUE); assert (json_object_get_boolean(tmp)==TRUE); json_object_put(tmp); printf("BOOL PASSED\n"); tmp=json_object_new_double(12.34); assert (json_object_get_double(tmp)==12.34); json_object_set_double(tmp,34.56); assert (json_object_get_double(tmp)==34.56); json_object_set_double(tmp,6435.34); assert (json_object_get_double(tmp)==6435.34); json_object_put(tmp); printf("DOUBLE PASSED\n"); #define SHORT "SHORT" #define MID "A MID STRING" // 12345678901234567890123456789012.... #define HUGE "A string longer than 32 chars as to check non local buf codepath" tmp=json_object_new_string(SHORT); assert (strcmp(json_object_get_string(tmp),SHORT)==0); json_object_set_string(tmp,MID); assert (strcmp(json_object_get_string(tmp),MID)==0); json_object_set_string(tmp,HUGE); assert (strcmp(json_object_get_string(tmp),HUGE)==0); json_object_set_string(tmp,SHORT); assert (strcmp(json_object_get_string(tmp),SHORT)==0); json_object_put(tmp); printf("STRING PASSED\n"); printf("PASSED\n"); return 0; }
/* * Forward prototypes for local functions */ static ACVP_RESULT acvp_kdf135_ikev1_output_tc(ACVP_CTX *ctx, ACVP_KDF135_IKEV1_TC *stc, JSON_Object *tc_rsp) { ACVP_RESULT rv; char *tmp = NULL; tmp = calloc(ACVP_KDF135_IKEV1_SKEY_STR_MAX + 1, sizeof(char)); if (!tmp) { ACVP_LOG_ERR("Unable to malloc in acvp_kdf135 tpm_output_tc"); return ACVP_MALLOC_FAIL; } rv = acvp_bin_to_hexstr(stc->s_key_id, stc->s_key_id_len, tmp, ACVP_KDF135_IKEV1_SKEY_STR_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("hex conversion failure (s_key_id)"); goto err; } json_object_set_string(tc_rsp, "sKeyId", (const char *)tmp); memzero_s(tmp, ACVP_KDF135_IKEV1_SKEY_STR_MAX); rv = acvp_bin_to_hexstr(stc->s_key_id_d, stc->s_key_id_d_len, tmp, ACVP_KDF135_IKEV1_SKEY_STR_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("hex conversion failure (s_key_id_d)"); goto err; } json_object_set_string(tc_rsp, "sKeyIdD", (const char *)tmp); memzero_s(tmp, ACVP_KDF135_IKEV1_SKEY_STR_MAX); rv = acvp_bin_to_hexstr(stc->s_key_id_a, stc->s_key_id_a_len, tmp, ACVP_KDF135_IKEV1_SKEY_STR_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("hex conversion failure (s_key_id_a)"); goto err; } json_object_set_string(tc_rsp, "sKeyIdA", (const char *)tmp); memzero_s(tmp, ACVP_KDF135_IKEV1_SKEY_STR_MAX); rv = acvp_bin_to_hexstr(stc->s_key_id_e, stc->s_key_id_e_len, tmp, ACVP_KDF135_IKEV1_SKEY_STR_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("hex conversion failure (s_key_id_e)"); goto err; } json_object_set_string(tc_rsp, "sKeyIdE", (const char *)tmp); memzero_s(tmp, ACVP_KDF135_IKEV1_SKEY_STR_MAX); err: free(tmp); return rv; }
void serialize_item_dictionary(JSON_Object *joutput_obj) { for (int i = 0; i < NUMKEYS; i++) { char buffer[4]; snprintf(buffer, 4, "%d", item_dictionary[i].key); json_object_set_string(joutput_obj, buffer, (char *) item_dictionary[i].id.data); } }
static JSON_Value *message_attachments_list_to_json(MessageAttachmentsList *matts) { if (!matts) return NULL; JSON_Value *attachments_value = json_value_init_array(); JSON_Array *attachments_array = json_value_get_array(attachments_value); guint i; for (i = 0; i < matts->len; i++) { MessageAttachment *att = message_attachments_list_get(matts, i); JSON_Value *attachment_value = json_value_init_object(); JSON_Object *attachment_object = json_value_get_object(attachment_value); json_object_set_number(attachment_object, "partId", att->part_id); json_object_set_string(attachment_object, "type", att->content_type); json_object_set_string(attachment_object, "filename", att->filename); json_object_set_number(attachment_object, "size", att->size); json_array_append_value(attachments_array, attachment_value); } return attachments_value; }
/* * After the test case has been processed by the DUT, the results * need to be JSON formated to be included in the vector set results * file that will be uploaded to the server. This routine handles * the JSON processing for a single test case. */ static ACVP_RESULT acvp_kdf135_srtp_output_tc(ACVP_CTX *ctx, ACVP_KDF135_SRTP_TC *stc, JSON_Object *tc_rsp) { ACVP_RESULT rv = ACVP_SUCCESS; char *tmp = NULL; tmp = calloc(ACVP_KDF135_SRTP_OUTPUT_MAX + 1, sizeof(char)); if (!tmp) { return ACVP_MALLOC_FAIL; } rv = acvp_bin_to_hexstr(stc->srtp_ke, stc->aes_keylen / 8, tmp, ACVP_KDF135_SRTP_OUTPUT_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("hex conversion failure (srtp_ke)"); goto err; } json_object_set_string(tc_rsp, "srtpKe", (const char *)tmp); memzero_s(tmp, ACVP_KDF135_SRTP_OUTPUT_MAX); rv = acvp_bin_to_hexstr(stc->srtp_ka, 160 / 8, tmp, ACVP_KDF135_SRTP_OUTPUT_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("hex conversion failure (srtp_ka)"); goto err; } json_object_set_string(tc_rsp, "srtpKa", (const char *)tmp); memzero_s(tmp, ACVP_KDF135_SRTP_OUTPUT_MAX); rv = acvp_bin_to_hexstr(stc->srtp_ks, 112 / 8, tmp, ACVP_KDF135_SRTP_OUTPUT_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("hex conversion failure (srtp_ks)"); goto err; } json_object_set_string(tc_rsp, "srtpKs", (const char *)tmp); memzero_s(tmp, ACVP_KDF135_SRTP_OUTPUT_MAX); rv = acvp_bin_to_hexstr(stc->srtcp_ke, stc->aes_keylen / 8, tmp, ACVP_KDF135_SRTP_OUTPUT_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("hex conversion failure (srtcp_ke)"); goto err; } json_object_set_string(tc_rsp, "srtcpKe", (const char *)tmp); memzero_s(tmp, ACVP_KDF135_SRTP_OUTPUT_MAX); rv = acvp_bin_to_hexstr(stc->srtcp_ka, 160 / 8, tmp, ACVP_KDF135_SRTP_OUTPUT_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("hex conversion failure (srtcp_ka)"); goto err; } json_object_set_string(tc_rsp, "srtcpKa", (const char *)tmp); memzero_s(tmp, ACVP_KDF135_SRTP_OUTPUT_MAX); rv = acvp_bin_to_hexstr(stc->srtcp_ks, 112 / 8, tmp, ACVP_KDF135_SRTP_OUTPUT_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("hex conversion failure (srtcp_ks)"); goto err; } json_object_set_string(tc_rsp, "srtcpKs", (const char *)tmp); memzero_s(tmp, ACVP_KDF135_SRTP_OUTPUT_MAX); err: free(tmp); return rv; }
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)); }
char *djcs_export_private_key(djcs_private_key *vk) { char *buffer; char *retstr; /* Allocate space for largest buffer output value used */ size_t buffer_size = mpz_sizeinbase((mpz_cmp(vk->n[0], vk->d) >= 0 ? vk->n[0] : vk->d), HCS_INTERNAL_BASE) + 2; buffer = malloc(buffer_size); JSON_Value *root = json_value_init_object(); JSON_Object *obj = json_value_get_object(root); json_object_set_number(obj, "s", vk->s); mpz_get_str(buffer, HCS_INTERNAL_BASE, vk->n[0]); json_object_set_string(obj, "n", buffer); mpz_get_str(buffer, HCS_INTERNAL_BASE, vk->d); json_object_set_string(obj, "d", buffer); retstr = json_serialize_to_string(root); json_value_free(root); free(buffer); return retstr; }
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 serialization_example(void) { JSON_Value *root_value = json_value_init_object(); JSON_Object *root_object = json_value_get_object(root_value); char *serialized_string = NULL; json_object_set_string(root_object, "name", "John Smith"); json_object_set_number(root_object, "age", 25); json_object_dotset_string(root_object, "address.city", "Cupertino"); json_object_dotset_value(root_object, "contact.emails", json_parse_string("[\"[email protected]\", \"[email protected]\"]")); serialized_string = json_serialize_to_string(root_value); puts(serialized_string); json_free_serialized_string(serialized_string); json_value_free(root_value); }
char *djcs_export_public_key(djcs_public_key *pk) { char *buffer; char *retstr; JSON_Value *root = json_value_init_object(); JSON_Object *obj = json_value_get_object(root); buffer = mpz_get_str(NULL, HCS_INTERNAL_BASE, pk->n[0]); json_object_set_number(obj, "s", pk->s); json_object_set_string(obj, "n", buffer); retstr = json_serialize_to_string(root); json_value_free(root); free(buffer); return retstr; }
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; }
/* * After the test case has been processed by the DUT, the results * need to be JSON formated to be included in the vector set results * file that will be uploaded to the server. This routine handles * the JSON processing for a single test case. */ static ACVP_RESULT acvp_kdf135_ssh_output_tc(ACVP_CTX *ctx, ACVP_KDF135_SSH_TC *stc, JSON_Object *tc_rsp) { char *tmp = NULL; ACVP_RESULT rv = ACVP_SUCCESS; if ((stc->iv_len * 2) > ACVP_KDF135_SSH_STR_OUT_MAX || (stc->e_key_len * 2) > ACVP_KDF135_SSH_STR_OUT_MAX || (stc->i_key_len * 2) > ACVP_KDF135_SSH_STR_OUT_MAX) { ACVP_LOG_ERR("iv_len*2(%u) || e_key_len*2(%u) || i_key_len*2(%u) > ACVP_KDF135_SSH_STR_OUT_MAX(%u)", (stc->iv_len * 2), (stc->e_key_len * 2), (stc->i_key_len * 2), ACVP_KDF135_SSH_STR_OUT_MAX); ACVP_LOG_ERR("Hint, make sure user isn't modifying those field values"); return ACVP_DATA_TOO_LARGE; } tmp = calloc(ACVP_KDF135_SSH_STR_OUT_MAX + 1, sizeof(char)); if (!tmp) { ACVP_LOG_ERR("Unable to malloc"); return ACVP_MALLOC_FAIL; } rv = acvp_bin_to_hexstr(stc->cs_init_iv, stc->iv_len, tmp, ACVP_KDF135_SSH_STR_OUT_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("acvp_bin_to_hexstr() failure"); goto err; } json_object_set_string(tc_rsp, "initialIvClient", tmp); memzero_s(tmp, ACVP_KDF135_SSH_STR_OUT_MAX); rv = acvp_bin_to_hexstr(stc->cs_encrypt_key, stc->e_key_len, tmp, ACVP_KDF135_SSH_STR_OUT_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("acvp_bin_to_hexstr() failure"); goto err; } json_object_set_string(tc_rsp, "encryptionKeyClient", tmp); memzero_s(tmp, ACVP_KDF135_SSH_STR_OUT_MAX); rv = acvp_bin_to_hexstr(stc->cs_integrity_key, stc->i_key_len, tmp, ACVP_KDF135_SSH_STR_OUT_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("acvp_bin_to_hexstr() failure"); goto err; } json_object_set_string(tc_rsp, "integrityKeyClient", tmp); memzero_s(tmp, ACVP_KDF135_SSH_STR_OUT_MAX); rv = acvp_bin_to_hexstr(stc->sc_init_iv, stc->iv_len, tmp, ACVP_KDF135_SSH_STR_OUT_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("acvp_bin_to_hexstr() failure"); goto err; } json_object_set_string(tc_rsp, "initialIvServer", tmp); memzero_s(tmp, ACVP_KDF135_SSH_STR_OUT_MAX); rv = acvp_bin_to_hexstr(stc->sc_encrypt_key, stc->e_key_len, tmp, ACVP_KDF135_SSH_STR_OUT_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("acvp_bin_to_hexstr() failure"); goto err; } json_object_set_string(tc_rsp, "encryptionKeyServer", tmp); memzero_s(tmp, ACVP_KDF135_SSH_STR_OUT_MAX); rv = acvp_bin_to_hexstr(stc->sc_integrity_key, stc->i_key_len, tmp, ACVP_KDF135_SSH_STR_OUT_MAX); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("acvp_bin_to_hexstr() failure"); goto err; } json_object_set_string(tc_rsp, "integrityKeyServer", tmp); err: free(tmp); return rv; }
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); }
char* parse_response(void* conn, const char* request, int length) { json_t* root = NULL; json_t* obj = NULL; json_error_t err; char* response = NULL; std::string resaction = "errorResponse"; try { memset(&err, 0, sizeof(json_error_t)); root = json_loadb(request, length, JSON_DECODE_ANY, &err); if(!root) { throw std::runtime_error("Request is not a valid JSON object"); } const char* action; json_object_get_string(root, obj, "action", action); if(!action) { throw std::runtime_error("Request type is unknown"); } else if(strcmp(action, "loginType1") == 0) { int ret; const char* challenge; resaction = "loginType1Response"; ret = lmice::token_get_challenge(conn, &challenge); if(ret != 0) throw std::runtime_error("Client connection is invalid"); json_t* rep = json_object(); json_object_set_string(rep, "action", resaction.c_str()); json_object_set_bool(rep, "status", true); json_object_set_string(rep, "challenge", challenge); json_object_set_string(rep, "error", "OK"); response = json_dumps(rep, JSON_INDENT(4)); json_decref(rep); } else if(strcmp(action, "loginType2") == 0) { int id; int ret; const char* name; const char* pass; const char* challenge; const char* email; const char* password; const char* display_name; resaction = "loginType2Response"; json_object_get_string( root, obj, "name", name); json_object_get_string( root, obj, "password", pass); json_object_get_string( root, obj, "challenge", challenge); ret = lmice::token_compare_challenge(conn, challenge); if(ret != 0) throw std::runtime_error("Client status is incorrect"); ret = lmice::user_find_name(name, &id, &display_name, &email, &password); if(ret != 0) throw std::runtime_error("User name is invalid"); ret = lmice::user_compare_password(id, challenge, pass); if( ret != 0) throw std::runtime_error("Password is incorrect"); lmice::token_update_user(conn, id); json_t* rep = json_object(); json_object_set_string(rep, "action", resaction.c_str()); json_object_set_bool(rep, "status", true); json_object_set_string(rep, "token", "123"); json_object_set_string(rep, "displayName", display_name); json_object_set_string(rep, "email", email); json_object_set_string(rep, "error", "OK"); response = json_dumps(rep, JSON_INDENT(4)); json_decref(rep); } else if(strcmp(action, "logout") == 0) { // Logout const char* token; json_object_get_string(root, obj, "token", token); lmice::token_reset(conn); if(!token) { throw std::runtime_error("Client status is incorrect"); } json_t* rep = json_object(); json_object_set_string(rep, "action", "logoutResponse"); json_object_set_bool(rep, "status", true); json_object_set_string(rep, "error", "OK"); response = json_dumps(rep, JSON_INDENT(4)); json_decref(rep); } else if(strcmp(action, "register") == 0) { // Register const char* name; const char* display_name; const char* password; const char* email; int ret; int id; resaction = "registerResponse"; json_object_get_string( root, obj, "name", name); json_object_get_string( root, obj, "password", password); json_object_get_string( root, obj, "display_name", display_name); json_object_get_string( root, obj, "email", email); if(!name) throw std::runtime_error("Name is null"); if(!password) throw std::runtime_error("Password is null"); if(!display_name) display_name = name; if(!email) throw std::runtime_error("Email is null"); ret = lmice::user_add_new( name, display_name, email, password, &id); switch(ret) { case lmice::EUSER_ID_EXIST: throw std::runtime_error("Create new user failed(ID)"); case lmice::EUSER_NAME_EXIST: throw std::runtime_error("Create new user failed(Name)"); case lmice::EUSER_EMAIL_EXIST: throw std::runtime_error("Create new user failed(Email)"); } json_t* rep = json_object(); json_object_set_string(rep, "action", "registerResponse"); json_object_set_bool(rep, "status", true); json_object_set_string(rep, "error", "OK"); response = json_dumps(rep, JSON_INDENT(4)); json_decref(rep); } } catch(const std::exception& e) { json_t* rep = json_object(); json_object_set_string(rep, "action", resaction.c_str()); json_object_set_bool(rep, "status", false); json_object_set_string(rep, "error", e.what()); response = json_dumps(rep, JSON_INDENT(4)); json_decref(rep); } json_decref(root); return response; }