AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <stdlib.h>5#include <stdio.h>6 7/* Wrapper provided in the module for the static function */8extern void test_jsonDebugPrintBlob(JsonParse *pParse, u32 iStart, u32 iEnd, int nIndent, sqlite3_str *pOut);9 10static sqlite3 *gDb = NULL;11 12/* Helpers */13static char* run_dump(const unsigned char *blob, int nBlob, int nAlloc, u32 iStart, u32 iEnd, int indent){14 JsonParse p;15 memset(&p, 0, sizeof(p));16 p.aBlob = (u8*)blob;17 p.nBlob = (u32)nBlob;18 p.nBlobAlloc = (u32)nAlloc;19 p.delta = 0;20 sqlite3_str *pOut = sqlite3_str_new(gDb);21 TEST_ASSERT_NOT_NULL(pOut);22 test_jsonDebugPrintBlob(&p, iStart, iEnd, indent, pOut);23 char *z = sqlite3_str_finish(pOut);24 TEST_ASSERT_NOT_NULL(z);25 return z;26}27 28void setUp(void) {29 int rc = sqlite3_open(":memory:", &gDb);30 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);31}32 33void tearDown(void) {34 if( gDb ){35 sqlite3_close(gDb);36 gDb = NULL;37 }38}39 40/* Test 1: Simple NULL element with zero-length inline header */41void test_jsonDebugPrintBlob_null_simple(void){42 unsigned char blob[] = {43 (0<<4) | JSONB_NULL /* 0x00 */44 };45 char *z = run_dump(blob, (int)sizeof(blob), (int)sizeof(blob), 0, (u32)sizeof(blob), 0);46 const char *exp = " 0: 00 <-- null\n";47 TEST_ASSERT_EQUAL_STRING(exp, z);48 sqlite3_free(z);49}50 51/* Test 2: TEXT with inline payload length "abc" */52void test_jsonDebugPrintBlob_text_inline(void){53 unsigned char blob[] = {54 (3<<4) | JSONB_TEXT, /* 0x37 = 3-byte payload, TEXT */55 'a','b','c'56 };57 char *z = run_dump(blob, (int)sizeof(blob), (int)sizeof(blob), 0, (u32)sizeof(blob), 0);58 const char *exp = " 0: 37 61 62 63 <-- text: \"abc\"\n";59 TEST_ASSERT_EQUAL_STRING(exp, z);60 sqlite3_free(z);61}62 63/* Test 3: ARRAY with nested elements: [ null, "x" ] */64void test_jsonDebugPrintBlob_array_nested(void){65 unsigned char blob[] = {66 (3<<4) | JSONB_ARRAY, /* array payload size = 3 bytes */67 (0<<4) | JSONB_NULL,68 (1<<4) | JSONB_TEXT, 'x'69 };70 char *z = run_dump(blob, (int)sizeof(blob), (int)sizeof(blob), 0, (u32)sizeof(blob), 0);71 const char *exp =72 " 0: 3b <-- array, 3 bytes\n"73 " 1: 00 <-- null\n"74 " 2: 17 78 <-- text: \"x\"\n";75 TEST_ASSERT_EQUAL_STRING(exp, z);76 sqlite3_free(z);77}78 79/* Test 4: OBJECT with one pair: {"a": true} */80void test_jsonDebugPrintBlob_object_nested(void){81 unsigned char blob[] = {82 (3<<4) | JSONB_OBJECT, /* object with 3-byte payload: key + value */83 (1<<4) | JSONB_TEXT, 'a',84 (0<<4) | JSONB_TRUE85 };86 char *z = run_dump(blob, (int)sizeof(blob), (int)sizeof(blob), 0, (u32)sizeof(blob), 0);87 const char *exp =88 " 0: 3c <-- object, 3 bytes\n"89 " 1: 17 61 <-- text: \"a\"\n"90 " 3: 01 <-- true\n";91 TEST_ASSERT_EQUAL_STRING(exp, z);92 sqlite3_free(z);93}94 95/* Test 5: Invalid node size (truncated size field for size-code 13) */96void test_jsonDebugPrintBlob_invalid_node_size(void){97 unsigned char blob[] = {98 (13<<4) | JSONB_TEXT /* 0xd7, but missing 2 size bytes */99 };100 char *z = run_dump(blob, (int)sizeof(blob), (int)sizeof(blob), 0, (u32)sizeof(blob), 0);101 const char *exp = " 0: d7 ERROR invalid node size\n";102 TEST_ASSERT_EQUAL_STRING(exp, z);103 sqlite3_free(z);104}105 106/* Test 6: Unknown node type (lower nibble = 15) with zero payload */107void test_jsonDebugPrintBlob_unknown_type(void){108 unsigned char blob[] = {109 (0<<4) | 15 /* reserved/unknown type */110 };111 char *z = run_dump(blob, (int)sizeof(blob), (int)sizeof(blob), 0, (u32)sizeof(blob), 0);112 const char *exp = " 0: 0f <-- ERROR: unknown node type\n";113 TEST_ASSERT_EQUAL_STRING(exp, z);114 sqlite3_free(z);115}116 117/* Test 7: Non-minimal size encoding (size-code 13) and nBlobAlloc > nBlob */118void test_jsonDebugPrintBlob_nonminimal_and_nblobaloc(void){119 unsigned char blob[6];120 blob[0] = (13<<4) | JSONB_TEXT; /* 0xd7 */121 blob[1] = 0x00; /* size high byte */122 blob[2] = 0x03; /* size low byte = 3 */123 blob[3] = 'a';124 blob[4] = 'b';125 blob[5] = 'c';126 /* Pretend only first 3 bytes are part of nBlob, but 6 are allocated */127 char *z = run_dump(blob, 3, 6, 0, 3, 0);128 const char *exp = " 0: d7 00 03 61 62 63 <-- text: \"abc\"\n";129 TEST_ASSERT_EQUAL_STRING(exp, z);130 sqlite3_free(z);131}132 133/* Test 8: Indentation parameter for top-level element */134void test_jsonDebugPrintBlob_indentation_param(void){135 unsigned char blob[] = {136 (0<<4) | JSONB_NULL137 };138 char *z = run_dump(blob, (int)sizeof(blob), (int)sizeof(blob), 0, (u32)sizeof(blob), 4);139 const char *exp = " 0: 00 <-- null\n";140 TEST_ASSERT_EQUAL_STRING(exp, z);141 sqlite3_free(z);142}143 144/* Test 9: TEXT containing non-printable and >=0x7f bytes -> shown as '.' */145void test_jsonDebugPrintBlob_text_nonascii_printable_mapping(void){146 unsigned char blob[] = {147 (4<<4) | JSONB_TEXT, /* 4-byte payload */148 'a', 0x01, 0x7f, 'b'149 };150 char *z = run_dump(blob, (int)sizeof(blob), (int)sizeof(blob), 0, (u32)sizeof(blob), 0);151 const char *exp = " 0: 47 61 01 7f 62 <-- text: \"a..b\"\n";152 TEST_ASSERT_EQUAL_STRING(exp, z);153 sqlite3_free(z);154}155 156int main(void){157 UNITY_BEGIN();158 RUN_TEST(test_jsonDebugPrintBlob_null_simple);159 RUN_TEST(test_jsonDebugPrintBlob_text_inline);160 RUN_TEST(test_jsonDebugPrintBlob_array_nested);161 RUN_TEST(test_jsonDebugPrintBlob_object_nested);162 RUN_TEST(test_jsonDebugPrintBlob_invalid_node_size);163 RUN_TEST(test_jsonDebugPrintBlob_unknown_type);164 RUN_TEST(test_jsonDebugPrintBlob_nonminimal_and_nblobaloc);165 RUN_TEST(test_jsonDebugPrintBlob_indentation_param);166 RUN_TEST(test_jsonDebugPrintBlob_text_nonascii_printable_mapping);167 return UNITY_END();168}