Skip to content
Myr

jq_test.c

This file

Path

src/jq_test.c

763 lines · 21.8 KiB · C

Last changed by

Fix crashes and resource leaks in `jq_testsuite` (#3509)itchynybefore adoptioncec6b0f

History of this file

Take it

1#include <assert.h>2#include <stdio.h>3#include <string.h>4#include <stdlib.h>5#ifdef HAVE_PTHREAD6#include <pthread.h>7#endif8#include "jv.h"9#include "jq.h"1011static void jv_test(void);12static void run_jq_tests(jv, int, FILE *, int, int);13static void run_jq_start_state_tests(void);14static void run_jq_compile_args_tests(void);15static void run_jq_recompile_tests(void);16static void run_jq_exhaust_and_reuse_tests(void);17#ifdef HAVE_PTHREAD18static void run_jq_pthread_tests(void);19#endif2021int jq_testsuite(jv libdirs, int verbose, int argc, char* argv[]) {22  int skip = -1;23  int take = -1;24  int nfiles = 0;25  jv_test();26  for (int i = 0; i < argc; i++) {27    if (!strcmp(argv[i], "--skip")) {28      if (++i >= argc) {29        fprintf(stderr, "--skip requires an argument\n");30        exit(1);31      }32      skip = atoi(argv[i]);33    } else if (!strcmp(argv[i], "--take")) {34      if (++i >= argc) {35        fprintf(stderr, "--take requires an argument\n");36        exit(1);37      }38      take = atoi(argv[i]);39    } else {40      FILE *testdata = fopen(argv[i], "r");41      if (!testdata) {42        perror("fopen");43        exit(1);44      }45      run_jq_tests(jv_copy(libdirs), verbose, testdata, skip, take);46      fclose(testdata);47      nfiles++;48    }49  }50  if (nfiles == 0)51    run_jq_tests(jv_copy(libdirs), verbose, stdin, skip, take);52  jv_free(libdirs);53  run_jq_start_state_tests();54  run_jq_compile_args_tests();55  run_jq_recompile_tests();56  run_jq_exhaust_and_reuse_tests();57#ifdef HAVE_PTHREAD58  run_jq_pthread_tests();59#endif60  return 0;61}6263static int skipline(const char* buf) {64  int p = 0;65  while (buf[p] == ' ' || buf[p] == '\t') p++;66  if (buf[p] == '#' || buf[p] == '\n' || buf[p] == 0) return 1;67  return 0;68}6970static int checkerrormsg(const char* buf) {71  return strcmp(buf, "%%FAIL\n") == 0;72}7374static int checkfail(const char* buf) {75  return strcmp(buf, "%%FAIL\n") == 0 || strcmp(buf, "%%FAIL IGNORE MSG\n") == 0;76}7778struct err_data {79  char buf[4096];80};8182static void test_err_cb(void *data, jv e) {83  struct err_data *err_data = data;84  if (jv_get_kind(e) != JV_KIND_STRING)85    e = jv_dump_string(e, JV_PRINT_INVALID);86  if (!strncmp(jv_string_value(e), "jq: error", sizeof("jq: error") - 1))87    snprintf(err_data->buf, sizeof(err_data->buf), "%s", jv_string_value(e));88  jv_free(e);89}9091static void run_jq_tests(jv lib_dirs, int verbose, FILE *testdata, int skip, int take) {92  char prog[4096] = {0};93  char buf[4096];94  struct err_data err_msg;95  int tests = 0, passed = 0, invalid = 0;96  unsigned int lineno = 0;97  int must_fail = 0;98  int check_msg = 0;99  jq_state *jq = NULL;100101  int tests_to_skip = skip > 0 ? skip : 0;102  int tests_to_take = take;103104  jq = jq_init();105  assert(jq);106  if (jv_get_kind(lib_dirs) == JV_KIND_NULL)107    lib_dirs = jv_array();108  jq_set_attr(jq, jv_string("JQ_LIBRARY_PATH"), lib_dirs);109110  while (1) {111    if (!fgets(prog, sizeof(prog), testdata)) break;112    lineno++;113    if (skipline(prog)) continue;114    if (checkfail(prog)) {115      must_fail = 1;116      check_msg = checkerrormsg(prog);117      jq_set_error_cb(jq, test_err_cb, &err_msg);118      continue;119    }120    if (prog[strlen(prog)-1] == '\n') prog[strlen(prog)-1] = 0;121122    if (skip > 0) {123      skip--;124      goto next;125    } else if (skip == 0) {126      printf("Skipped %d tests\n", tests_to_skip);127      skip = -1;128    }129130    if (take > 0) {131      take--;132    } else if (take == 0) {133      printf("Hit the number of tests limit (%d), breaking\n", tests_to_take);134      break;135    }136137    int pass = 1;138    tests++;139    printf("Test #%d: '%s' at line number %u\n", tests + tests_to_skip, prog, lineno);140    int compiled = jq_compile(jq, prog);141142    if (must_fail) {143      jq_set_error_cb(jq, NULL, NULL);144      if (compiled) {145        printf("*** Test program compiled successfully, but should fail at line number %u: %s\n", lineno, prog);146        goto fail;147      }148      char *err_buf = err_msg.buf;149      while (fgets(buf, sizeof(buf), testdata)) {150        lineno++;151        if (skipline(buf)) break;152        if (check_msg) {153          if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';154          if (strncmp(buf, err_buf, strlen(buf)) != 0) {155            if (strchr(err_buf, '\n')) *strchr(err_buf, '\n') = '\0';156            printf("*** Erroneous program failed with '%s', but expected '%s' at line number %u: %s\n", err_buf, buf, lineno, prog);157            goto fail;158          }159          err_buf += strlen(buf);160          if (*err_buf == '\n') err_buf++;161        }162      }163      if (check_msg && *err_buf != '\0') {164        if (strchr(err_buf, '\n')) *strchr(err_buf, '\n') = '\0';165        printf("*** Erroneous program failed with extra message '%s' at line %u: %s\n", err_buf, lineno, prog);166        invalid++;167        pass = 0;168      }169      must_fail = 0;170      check_msg = 0;171      passed += pass;172      continue;173    }174175    if (!compiled) {176      printf("*** Test program failed to compile at line %u: %s\n", lineno, prog);177      goto fail;178    }179    if (verbose) {180      printf("Disassembly:\n");181      jq_dump_disassembly(jq, 2);182      printf("\n");183    }184    if (!fgets(buf, sizeof(buf), testdata)) { invalid++; break; }185    lineno++;186    jv input = jv_parse(buf);187    if (!jv_is_valid(input)) {188      printf("*** Input is invalid on line %u: %s\n", lineno, buf);189      goto fail;190    }191    jq_start(jq, input, verbose ? JQ_DEBUG_TRACE : 0);192193    while (fgets(buf, sizeof(buf), testdata)) {194      lineno++;195      if (skipline(buf)) break;196      jv expected = jv_parse(buf);197      if (!jv_is_valid(expected)) {198        printf("*** Expected result is invalid on line %u: %s\n", lineno, buf);199        goto fail;200      }201      jv actual = jq_next(jq);202      if (!jv_is_valid(actual)) {203        jv_free(expected);204        jv_free(actual);205        printf("*** Insufficient results for test at line number %u: %s\n", lineno, prog);206        pass = 0;207        break;208      } else if (!jv_equal(jv_copy(expected), jv_copy(actual))) {209        printf("*** Expected ");210        jv_dump(jv_copy(expected), 0);211        printf(", but got ");212        jv_dump(jv_copy(actual), 0);213        printf(" for test at line number %u: %s\n", lineno, prog);214        pass = 0;215      }216#ifdef USE_DECNUM217      jv as_string = jv_dump_string(jv_copy(expected), 0);218      jv reparsed = jv_parse_sized(jv_string_value(as_string), jv_string_length_bytes(jv_copy(as_string)));219      if (!jv_equal(jv_copy(expected), jv_copy(reparsed))) {220        printf("*** Expected result should be equal after reparsing, but got ");221        jv_dump(jv_copy(reparsed), 0);222        printf(" for test at line %u: %s\n", lineno, buf);223        pass = 0;224      }225      jv_free(as_string);226      jv_free(reparsed);227#endif228      jv_free(expected);229      jv_free(actual);230    }231    if (pass) {232      jv extra = jq_next(jq);233      if (jv_is_valid(extra)) {234        printf("*** Superfluous result: ");235        jv_dump(extra, 0);236        printf(" for test at line number %u, %s\n", lineno, prog);237        invalid++;238        pass = 0;239      } else {240        jv_free(extra);241      }242    }243    passed += pass;244    continue;245246fail:247    invalid++;248249next:250    while (fgets(buf, sizeof(buf), testdata)) {251      lineno++;252      if (skipline(buf)) break;253    }254    must_fail = 0;255    check_msg = 0;256  }257  jq_teardown(&jq);258259  int total_skipped = tests_to_skip;260261  if (skip > 0) {262    total_skipped = tests_to_skip - skip;263  }264265  printf("%d of %d tests passed (%d malformed, %d skipped)\n",266    passed, tests, invalid, total_skipped);267268  if (skip > 0) {269    printf("WARN: skipped past the end of file, exiting with status 2\n");270    exit(2);271  }272273  if (passed != tests) exit(1);274}275276277static int test_start_state(jq_state *jq, char *prog) {278  int pass = 1;279  jv message = jq_get_error_message(jq);280  if (jv_is_valid(message)) {281    printf("*** Expected error_message to be invalid after jq_start: %s\n", prog);282    pass = 0;283  }284  jv_free(message);285286  jv exit_code = jq_get_exit_code(jq);287  if (jv_is_valid(exit_code)) {288    printf("*** Expected exit_code to be invalid after jq_start: %s\n", prog);289    pass = 0;290  }291  jv_free(exit_code);292293  if (jq_halted(jq)) {294    printf("*** Expected jq to not be halted after jq_start: %s\n", prog);295    pass = 0;296  }297298  return pass;299}300301// Test jq_state is reset after subsequent calls to jq_start.302static void test_jq_start_resets_state(char *prog, const char *input) {303  printf("Test jq_state: %s\n", prog);304  jq_state *jq = jq_init();305  assert(jq);306307  int compiled = jq_compile(jq, prog);308  assert(compiled);309310  // First call to jq_start. Run until completion.311  jv parsed_input = jv_parse(input);312  assert(jv_is_valid(parsed_input));313  jq_start(jq, parsed_input, 0);314  assert(test_start_state(jq, prog));315  while (1) {316    jv result = jq_next(jq);317    int valid = jv_is_valid(result);318    jv_free(result);319    if (!valid) { break; }320  }321322  // Second call to jq_start.323  jv parsed_input2 = jv_parse(input);324  assert(jv_is_valid(parsed_input2));325  jq_start(jq, parsed_input2, 0);326  assert(test_start_state(jq, prog));327328  jq_teardown(&jq);329}330331static void run_jq_start_state_tests(void) {332  test_jq_start_resets_state(".[]", "[1,2,3]");333  test_jq_start_resets_state(".[] | if .%2 == 0 then halt_error else . end", "[1,2,3]");334}335336static void compile_args_and_check(jq_state *jq, const char *prog,337                                   jv args, jv input, jv expected) {338  printf("  subtest: %s\n", prog);339  int compiled = jq_compile_args(jq, prog, args);340  assert(compiled);341342  jq_start(jq, input, 0);343  jv result = jq_next(jq);344  assert(jv_is_valid(result));345  assert(jv_equal(result, expected));346347  jv extra = jq_next(jq);348  assert(!jv_is_valid(extra));349  jv_free(extra);350}351352// Test that jq_compile_args() with array arguments handles jv353// refcounting correctly in args2obj(). The array path converts354// [{"name":"k","value":"v"}, ...] into {"k":"v", ...}.355static void run_jq_compile_args_tests(void) {356  printf("Test jq_compile_args with array args\n");357  jq_state *jq = jq_init();358  assert(jq);359360  // Empty array: loop body never runs, kk/vk allocated and freed361  compile_args_and_check(jq, "42",362    jv_array(), jv_null(), jv_number(42));363364  // Single element: one iteration, no reuse of kk/vk365  compile_args_and_check(jq, "$val",366    JV_ARRAY(JV_OBJECT(jv_string("name"), jv_string("val"),367                       jv_string("value"), jv_number(42))),368    jv_null(), jv_number(42));369370  // Two elements: minimum to trigger former UAF on iteration 2371  compile_args_and_check(jq, "$x + $y",372    JV_ARRAY(JV_OBJECT(jv_string("name"), jv_string("x"),373                       jv_string("value"), jv_number(1)),374             JV_OBJECT(jv_string("name"), jv_string("y"),375                       jv_string("value"), jv_number(2))),376    jv_null(), jv_number(3));377378  // Three elements: exercises further loop iterations379  compile_args_and_check(jq, "$a + $b + $c",380    JV_ARRAY(JV_OBJECT(jv_string("name"), jv_string("a"),381                       jv_string("value"), jv_string("hello")),382             JV_OBJECT(jv_string("name"), jv_string("b"),383                       jv_string("value"), jv_string(" ")),384             JV_OBJECT(jv_string("name"), jv_string("c"),385                       jv_string("value"), jv_string("world"))),386    jv_null(), jv_string("hello world"));387388  // Object args: bypasses args2obj loop via early return389  compile_args_and_check(jq, "$x * $y",390    JV_OBJECT(jv_string("x"), jv_number(10),391              jv_string("y"), jv_number(20)),392    jv_null(), jv_number(200));393394  jq_teardown(&jq);395}396397398// Test that recompiling on the same jq_state doesn't leak or399// double-free the previous bytecode, and that switching between400// jq_compile and jq_compile_args works correctly.401static void run_jq_recompile_tests(void) {402  printf("Test jq recompile on same state\n");403  jq_state *jq = jq_init();404  assert(jq);405406  // First program via jq_compile407  int compiled = jq_compile(jq, ". + 1");408  assert(compiled);409  jq_start(jq, jv_number(1), 0);410  jv r = jq_next(jq);411  assert(jv_is_valid(r));412  assert(jv_number_value(r) == 2);413  jv_free(r);414  r = jq_next(jq);415  assert(!jv_is_valid(r));416  jv_free(r);417418  // Recompile with a different program on the same state419  compiled = jq_compile(jq, ". * 2");420  assert(compiled);421  jq_start(jq, jv_number(5), 0);422  r = jq_next(jq);423  assert(jv_is_valid(r));424  assert(jv_number_value(r) == 10);425  jv_free(r);426  r = jq_next(jq);427  assert(!jv_is_valid(r));428  jv_free(r);429430  // Recompile via jq_compile_args with array args (args2obj path)431  compiled = jq_compile_args(jq, "$n + $m",432    JV_ARRAY(JV_OBJECT(jv_string("name"), jv_string("n"),433                       jv_string("value"), jv_number(100)),434             JV_OBJECT(jv_string("name"), jv_string("m"),435                       jv_string("value"), jv_number(7))));436  assert(compiled);437  jq_start(jq, jv_null(), 0);438  r = jq_next(jq);439  assert(jv_is_valid(r));440  assert(jv_number_value(r) == 107);441  jv_free(r);442  r = jq_next(jq);443  assert(!jv_is_valid(r));444  jv_free(r);445446  // Back to jq_compile after jq_compile_args447  compiled = jq_compile(jq, ". - 1");448  assert(compiled);449  jq_start(jq, jv_number(10), 0);450  r = jq_next(jq);451  assert(jv_is_valid(r));452  assert(jv_number_value(r) == 9);453  jv_free(r);454  r = jq_next(jq);455  assert(!jv_is_valid(r));456  jv_free(r);457458  jq_teardown(&jq);459}460461462// Test that exhausting jq_next doesn't corrupt state, and that463// the same jq_state can be reused with jq_start after exhaustion.464static void run_jq_exhaust_and_reuse_tests(void) {465  printf("Test jq exhaust and reuse\n");466  jq_state *jq = jq_init();467  assert(jq);468469  int compiled = jq_compile(jq, ".[]");470  assert(compiled);471472  // First run: drain all results473  jq_start(jq, jv_parse("[1,2,3]"), 0);474  for (int i = 1; i <= 3; i++) {475    jv r = jq_next(jq);476    assert(jv_is_valid(r));477    assert(jv_number_value(r) == i);478    jv_free(r);479  }480  jv r = jq_next(jq);481  assert(!jv_is_valid(r));482  jv_free(r);483484  // Reuse with new input on the same compiled program485  jq_start(jq, jv_parse("[10,20]"), 0);486  r = jq_next(jq);487  assert(jv_is_valid(r));488  assert(jv_number_value(r) == 10);489  jv_free(r);490  r = jq_next(jq);491  assert(jv_is_valid(r));492  assert(jv_number_value(r) == 20);493  jv_free(r);494  r = jq_next(jq);495  assert(!jv_is_valid(r));496  jv_free(r);497498  // Reuse again with empty array (zero results)499  jq_start(jq, jv_parse("[]"), 0);500  r = jq_next(jq);501  assert(!jv_is_valid(r));502  jv_free(r);503504  // Reuse once more after zero-result run505  jq_start(jq, jv_parse("[99]"), 0);506  r = jq_next(jq);507  assert(jv_is_valid(r));508  assert(jv_number_value(r) == 99);509  jv_free(r);510  r = jq_next(jq);511  assert(!jv_is_valid(r));512  jv_free(r);513514  jq_teardown(&jq);515}516517518/// pthread regression test519#ifdef HAVE_PTHREAD520#define NUMBER_OF_THREADS 3521struct test_pthread_data {522    int result;523};524525static int test_pthread_jq_parse(jq_state *jq, struct jv_parser *parser)526{527    int rv = 0;528    jv value;529530    value = jv_parser_next(parser);531    while (jv_is_valid(value)) {532        jq_start(jq, value, 0);533        jv result = jq_next(jq);534535        while (jv_is_valid(result)) {536            jv_free(result);537            result = jq_next(jq);538        }539        jv_free(result);540        value = jv_parser_next(parser);541    }542    jv_free(value);543    return rv;544}545546static void *test_pthread_run(void *ptr) {547    int rv;548    jq_state *jq;549    const char *prg = ".data";550    const char *buf = "{ \"data\": 1 }";551    struct test_pthread_data *data = ptr;552553    jq = jq_init();554    if (jq_compile(jq, prg) == 0) {555        jq_teardown(&jq);556        return NULL;557    }558559    struct jv_parser *parser = jv_parser_new(0);560    jv_parser_set_buf(parser, buf, strlen(buf), 0);561    rv = test_pthread_jq_parse(jq, parser);562563    data->result = rv;564565    jv_parser_free(parser);566    jq_teardown(&jq);567    return NULL;568}569570static void run_jq_pthread_tests(void) {571    pthread_t threads[NUMBER_OF_THREADS];572    struct test_pthread_data data[NUMBER_OF_THREADS];573    int createerror;574    int a;575576    memset(&threads, 0, sizeof(threads));577    memset(&data, 0, sizeof(data));578579    // Create all threads580    for (a = 0; a < NUMBER_OF_THREADS; ++a) {581        createerror = pthread_create(&threads[a], NULL, test_pthread_run, &data[a]);582        assert(createerror == 0);583    }584585    // wait for all threads586    for(a = 0; a < NUMBER_OF_THREADS; ++a) {587#ifdef __MVS__588        if (threads[a].__ != 0) {589#else590        if (threads[a] != 0) {591#endif592            pthread_join(threads[a], NULL);593        }594    }595596    // check results597    for(a = 0; a < NUMBER_OF_THREADS; ++a) {598        assert(data[a].result == 0);599    }600}601#endif // HAVE_PTHREAD602603604static void jv_test(void) {605  /// JSON parser regression tests606  {607    jv v = jv_parse("{\"a':\"12\"}");608    assert(jv_get_kind(v) == JV_KIND_INVALID);609    v = jv_invalid_get_msg(v);610    assert(strcmp(jv_string_value(v), "Expected separator between values at line 1, column 9 (while parsing '{\"a':\"12\"}')") == 0);611    jv_free(v);612  }613  /// Arrays and numbers614  {615    jv a = jv_array();616    assert(jv_get_kind(a) == JV_KIND_ARRAY);617    assert(jv_array_length(jv_copy(a)) == 0);618    assert(jv_array_length(jv_copy(a)) == 0);619620    a = jv_array_append(a, jv_number(42));621    assert(jv_array_length(jv_copy(a)) == 1);622    assert(jv_number_value(jv_array_get(jv_copy(a), 0)) == 42);623624    jv a2 = jv_array_append(jv_array(), jv_number(42));625    assert(jv_equal(jv_copy(a), jv_copy(a)));626    assert(jv_equal(jv_copy(a2), jv_copy(a2)));627    assert(jv_equal(jv_copy(a), jv_copy(a2)));628    assert(jv_equal(jv_copy(a2), jv_copy(a)));629    jv_free(a2);630631    a2 = jv_array_append(jv_array(), jv_number(19));632    assert(!jv_equal(jv_copy(a), jv_copy(a2)));633    assert(!jv_equal(jv_copy(a2), jv_copy(a)));634    jv_free(a2);635636637    assert(jv_get_refcnt(a) == 1);638    a = jv_array_append(a, jv_copy(a));639    assert(jv_get_refcnt(a) == 1);640641    assert(jv_array_length(jv_copy(a)) == 2);642    assert(jv_number_value(jv_array_get(jv_copy(a), 0)) == 42);643644    for (int i=0; i<10; i++) {645      jv subarray = jv_array_get(jv_copy(a), 1);646      assert(jv_get_kind(subarray) == JV_KIND_ARRAY);647      assert(jv_array_length(jv_copy(subarray)) == 1);648      assert(jv_number_value(jv_array_get(jv_copy(subarray), 0)) == 42);649      jv_free(subarray);650    }651652653    jv subarray = jv_array_get(jv_copy(a), 1);654    assert(jv_get_kind(subarray) == JV_KIND_ARRAY);655    assert(jv_array_length(jv_copy(subarray)) == 1);656    assert(jv_number_value(jv_array_get(jv_copy(subarray), 0)) == 42);657658    jv sub2 = jv_copy(subarray);659    sub2 = jv_array_append(sub2, jv_number(19));660661    assert(jv_get_kind(sub2) == JV_KIND_ARRAY);662    assert(jv_array_length(jv_copy(sub2)) == 2);663    assert(jv_number_value(jv_array_get(jv_copy(sub2), 0)) == 42);664    assert(jv_number_value(jv_array_get(jv_copy(sub2), 1)) == 19);665666    assert(jv_get_kind(subarray) == JV_KIND_ARRAY);667    assert(jv_array_length(jv_copy(subarray)) == 1);668    assert(jv_number_value(jv_array_get(jv_copy(subarray), 0)) == 42);669670    jv_free(subarray);671672    void* before = sub2.u.ptr;673    sub2 = jv_array_append(sub2, jv_number(200));674    void* after = sub2.u.ptr;675    assert(before == after);676    jv_free(sub2);677678    jv a3 = jv_array_append(jv_copy(a), jv_number(19));679    assert(jv_array_length(jv_copy(a3)) == 3);680    assert(jv_number_value(jv_array_get(jv_copy(a3), 0)) == 42);681    assert(jv_array_length(jv_array_get(jv_copy(a3), 1)) == 1);682    assert(jv_number_value(jv_array_get(jv_copy(a3), 2)) == 19);683    jv_free(a3);684685686    jv a4 = jv_array();687    a4 = jv_array_append(a4, jv_number(1));688    a4 = jv_array_append(a4, jv_number(2));689    jv a5 = jv_copy(a4);690    a4 = jv_array_append(a4, jv_number(3));691    a4 = jv_array_slice(a4, 0, 1);692    assert(jv_array_length(jv_copy(a4)) == 1);693    a4 = jv_array_append(a4, jv_number(4));694    assert(jv_array_length(jv_copy(a4)) == 2);695    assert(jv_array_length(jv_copy(a5)) == 2);696    jv_free(a4);697    jv_free(a5);698699700    assert(jv_array_length(jv_copy(a)) == 2);701    assert(jv_number_value(jv_array_get(jv_copy(a), 0)) == 42);702    assert(jv_array_length(jv_array_get(jv_copy(a), 1)) == 1);703704705    //jv_dump(jv_copy(a), 0); printf("\n");706    jv_free(a);707  }708709710  /// Strings711  {712    assert(jv_equal(jv_string("foo"), jv_string_sized("foo", 3)));713    char nasty[] = "foo\0";714    jv shortstr = jv_string(nasty), longstr = jv_string_sized(nasty, sizeof(nasty));715    assert(jv_string_length_bytes(jv_copy(shortstr)) == (int)strlen(nasty));716    assert(jv_string_length_bytes(jv_copy(longstr)) == (int)sizeof(nasty));717    jv_free(shortstr);718    jv_free(longstr);719720721    char a1s[] = "hello", a2s[] = "hello", bs[] = "goodbye";722    jv a1 = jv_string(a1s), a2 = jv_string(a2s), b = jv_string(bs);723    assert(jv_equal(jv_copy(a1), jv_copy(a2)));724    assert(jv_equal(jv_copy(a2), jv_copy(a1)));725    assert(!jv_equal(jv_copy(a1), jv_copy(b)));726727    assert(jv_string_hash(jv_copy(a1)) == jv_string_hash(jv_copy(a1)));728    assert(jv_string_hash(jv_copy(a1)) == jv_string_hash(jv_copy(a2)));729    assert(jv_string_hash(jv_copy(b)) != jv_string_hash(jv_copy(a1)));730    jv_free(a1);731    jv_free(a2);732    jv_free(b);733734    assert(jv_equal(jv_string("hello42!"), jv_string_fmt("hello%d%s", 42, "!")));735    char big[20000];736    for (int i=0; i<(int)sizeof(big); i++) big[i] = 'a';737    big[sizeof(big)-1] = 0;738    jv str = jv_string_fmt("%s", big);739    assert(jv_string_length_bytes(jv_copy(str)) == sizeof(big) - 1);740    assert(!strcmp(big, jv_string_value(str)));741    jv_free(str);742  }743744  /// Objects745  {746    jv o1 = jv_object();747    o1 = jv_object_set(o1, jv_string("foo"), jv_number(42));748    o1 = jv_object_set(o1, jv_string("bar"), jv_number(24));749    assert(jv_number_value(jv_object_get(jv_copy(o1), jv_string("foo"))) == 42);750    assert(jv_number_value(jv_object_get(jv_copy(o1), jv_string("bar"))) == 24);751752    jv o2 = jv_object_set(jv_copy(o1), jv_string("foo"), jv_number(420));753    o2 = jv_object_set(o2, jv_string("bar"), jv_number(240));754    assert(jv_number_value(jv_object_get(jv_copy(o1), jv_string("foo"))) == 42);755    assert(jv_number_value(jv_object_get(jv_copy(o1), jv_string("bar"))) == 24);756    assert(jv_number_value(jv_object_get(jv_copy(o2), jv_string("foo"))) == 420);757    jv_free(o1);758    assert(jv_number_value(jv_object_get(jv_copy(o2), jv_string("bar"))) == 240);759760    //jv_dump(jv_copy(o2), 0); printf("\n");761    jv_free(o2);762  }763}