AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <stdlib.h>5 6/* The wrapper for the static function under test is provided by the module:7 u32 test_jsonbArrayCount(JsonParse *pParse, u32 iRoot);8*/9u32 test_jsonbArrayCount(JsonParse *pParse, u32 iRoot);10 11/* Unity setup/teardown */12void setUp(void) {13 /* no-op */14}15void tearDown(void) {16 /* no-op */17}18 19/* Helper: run count on a given blob at a given root offset */20static u32 run_count(const u8 *blob, u32 nBlob, u32 iRoot){21 JsonParse p;22 memset(&p, 0, sizeof(p));23 p.aBlob = (u8*)blob;24 p.nBlob = nBlob;25 /* p.delta defaults to 0; other fields unused */26 return test_jsonbArrayCount(&p, iRoot);27}28 29/* Test 1: Empty array with minimal header (payload size 0) */30void test_jsonbArrayCount_empty_array_minimal(void){31 /* Array []: first byte = (0<<4)|11 = 0x0B */32 const u8 blob[] = { 0x0B };33 TEST_ASSERT_EQUAL_UINT32(0, run_count(blob, (u32)sizeof(blob), 0));34}35 36/* Test 2: Single element TRUE with minimal headers */37void test_jsonbArrayCount_single_true_minimal(void){38 /* Array payload size 1: (1<<4)|11 = 0x1B; TRUE element 0x01 */39 const u8 blob[] = { 0x1B, 0x01 };40 TEST_ASSERT_EQUAL_UINT32(1, run_count(blob, (u32)sizeof(blob), 0));41}42 43/* Test 3: Two simple elements (TRUE, FALSE), minimal headers */44void test_jsonbArrayCount_two_elements_minimal(void){45 /* Payload size 2: (2<<4)|11 = 0x2B; elements: 0x01 (TRUE), 0x02 (FALSE) */46 const u8 blob[] = { 0x2B, 0x01, 0x02 };47 TEST_ASSERT_EQUAL_UINT32(2, run_count(blob, (u32)sizeof(blob), 0));48}49 50/* Test 4: Mixed element sizes (TEXT, INT, empty ARRAY, empty OBJECT), minimal headers */51void test_jsonbArrayCount_mixed_elements_minimal(void){52 /* Elements:53 - TEXT "abc": 3-byte payload: (3<<4)|7 = 0x37, 'a','b','c'54 - INT "42": 2-byte payload: (2<<4)|3 = 0x23, '4','2'55 - ARRAY []: (0<<4)|11 = 0x0B56 - OBJECT {}: (0<<4)|12 = 0x0C57 Total payload = 4 + 3 + 1 + 1 = 958 Array header: (9<<4)|11 = 0x9B59 */60 const u8 blob[] = {61 0x9B,62 0x37, 'a','b','c',63 0x23, '4','2',64 0x0B,65 0x0C66 };67 TEST_ASSERT_EQUAL_UINT32(4, run_count(blob, (u32)sizeof(blob), 0));68}69 70/* Test 5: Array header uses form 12 (one-byte payload length), payload size 1 */71void test_jsonbArrayCount_array_header_form_12(void){72 /* Header: 0xCB (type ARRAY=11, form=12), length byte 0x01, element TRUE 0x01 */73 const u8 blob[] = { 0xCB, 0x01, 0x01 };74 TEST_ASSERT_EQUAL_UINT32(1, run_count(blob, (u32)sizeof(blob), 0));75}76 77/* Test 6: Array header uses form 13 (two-byte payload length), payload size 2 */78void test_jsonbArrayCount_array_header_form_13(void){79 /* Header: 0xDB, length 0x0002, elements: NULL(0x00), TRUE(0x01) */80 const u8 blob[] = { 0xDB, 0x00, 0x02, 0x00, 0x01 };81 TEST_ASSERT_EQUAL_UINT32(2, run_count(blob, (u32)sizeof(blob), 0));82}83 84/* Test 7: Array header uses form 14 (four-byte payload length), payload size 3 */85void test_jsonbArrayCount_array_header_form_14(void){86 /* Header: 0xEB, length 0x00000003, elements: NULL, TRUE, FALSE */87 const u8 blob[] = { 0xEB, 0x00, 0x00, 0x00, 0x03, 0x00, 0x01, 0x02 };88 TEST_ASSERT_EQUAL_UINT32(3, run_count(blob, (u32)sizeof(blob), 0));89}90 91/* Test 8: Array located at a non-zero offset within the blob */92void test_jsonbArrayCount_array_at_nonzero_offset(void){93 const u8 prefix[] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE };94 const u8 arr[] = { 0x1B, 0x01 };95 u8 blob[sizeof(prefix) + sizeof(arr)];96 memcpy(blob, prefix, sizeof(prefix));97 memcpy(blob + sizeof(prefix), arr, sizeof(arr));98 TEST_ASSERT_EQUAL_UINT32(1, run_count(blob, (u32)sizeof(blob), (u32)sizeof(prefix)));99}100 101/* Test 9: Invalid array header (claims payload 1 but blob ends) -> count 0 */102void test_jsonbArrayCount_invalid_array_header_overflow_returns_zero(void){103 /* Array header with payload size 1: 0x1B but no payload byte follows */104 const u8 blob[] = { 0x1B };105 TEST_ASSERT_EQUAL_UINT32(0, run_count(blob, (u32)sizeof(blob), 0));106}107 108/* Test 10: Invalid element inside array causes one count then stop (observed behavior) */109void test_jsonbArrayCount_invalid_element_counts_once_then_stops(void){110 /* Array header payload size 1: 0x1B. Element byte is 0xC0 which111 indicates form 12 (needs one more byte), but blob ends immediately.112 jsonbPayloadSize() for the element returns 0 (error). The loop113 counts one iteration before terminating -> returns 1. */114 const u8 blob[] = { 0x1B, 0xC0 };115 TEST_ASSERT_EQUAL_UINT32(1, run_count(blob, (u32)sizeof(blob), 0));116}117 118/* Test 11: Element with extended header form 12 (element TEXT length 20) */119void test_jsonbArrayCount_element_header_form_12(void){120 /* Element: TEXT (type=7) with payload 20121 Header: 0xC7, size byte 0x14, then 20 'a's.122 Array payload size = 2 + 20 = 22 -> array header 0xCB, 0x16123 */124 u8 blob[2 + 2 + 20];125 blob[0] = 0xCB; blob[1] = 0x16; /* array header form-12, len 22 */126 blob[2] = 0xC7; blob[3] = 0x14; /* element header form-12, len 20 */127 for(int i=0;i<20;i++) blob[4+i] = 'a';128 TEST_ASSERT_EQUAL_UINT32(1, run_count(blob, (u32)sizeof(blob), 0));129}130 131/* Test 12: Element with extended header form 13 (two-byte length) */132void test_jsonbArrayCount_element_header_form_13(void){133 /* Element: TEXT (type=7) with payload 10, but use form-13 encoding.134 Element header: 0xD7, 0x00, 0x0A, payload 10 bytes.135 Array payload size = 3 + 10 = 13 -> array header form-12 with len 13 (0x0D).136 */137 u8 blob[2 + 3 + 10];138 blob[0] = 0xCB; blob[1] = 0x0D; /* array header form-12, len 13 */139 blob[2] = 0xD7; blob[3] = 0x00; blob[4] = 0x0A; /* element header form-13 */140 for(int i=0;i<10;i++) blob[5+i] = (u8)('b' + (i%26));141 TEST_ASSERT_EQUAL_UINT32(1, run_count(blob, (u32)sizeof(blob), 0));142}143 144int main(void){145 UNITY_BEGIN();146 RUN_TEST(test_jsonbArrayCount_empty_array_minimal);147 RUN_TEST(test_jsonbArrayCount_single_true_minimal);148 RUN_TEST(test_jsonbArrayCount_two_elements_minimal);149 RUN_TEST(test_jsonbArrayCount_mixed_elements_minimal);150 RUN_TEST(test_jsonbArrayCount_array_header_form_12);151 RUN_TEST(test_jsonbArrayCount_array_header_form_13);152 RUN_TEST(test_jsonbArrayCount_array_header_form_14);153 RUN_TEST(test_jsonbArrayCount_array_at_nonzero_offset);154 RUN_TEST(test_jsonbArrayCount_invalid_array_header_overflow_returns_zero);155 RUN_TEST(test_jsonbArrayCount_invalid_element_counts_once_then_stops);156 RUN_TEST(test_jsonbArrayCount_element_header_form_12);157 RUN_TEST(test_jsonbArrayCount_element_header_form_13);158 return UNITY_END();159}