1*10ff414cSEd Maste #include "stream_expectations.h" 2*10ff414cSEd Maste 3*10ff414cSEd Maste // TODO: The saved keystrokes are not worth the complexity. Get rid of this 4*10ff414cSEd Maste // file to prevent confusion, the fundamental structure is unlikely to change 5*10ff414cSEd Maste // in the future. 6*10ff414cSEd Maste 7*10ff414cSEd Maste /* Ordered from 0 to queue_size - 1 */ 8*10ff414cSEd Maste struct test_assertion assertions_queue[MAX_QUEUE_ITEMS]; 9*10ff414cSEd Maste int queue_size = 0; 10*10ff414cSEd Maste int current_expectation = 0; 11*10ff414cSEd Maste decoder_t *decoder; 12*10ff414cSEd Maste 13*10ff414cSEd Maste void set_decoder(decoder_t *dec) { decoder = dec; } 14*10ff414cSEd Maste 15*10ff414cSEd Maste int clear_stream_assertions(void **state) { 16*10ff414cSEd Maste if (queue_size != current_expectation) { 17*10ff414cSEd Maste return 1; // We have not matched all expectations correctly 18*10ff414cSEd Maste } 19*10ff414cSEd Maste queue_size = current_expectation = 0; 20*10ff414cSEd Maste free(*state); 21*10ff414cSEd Maste return 0; 22*10ff414cSEd Maste } 23*10ff414cSEd Maste 24*10ff414cSEd Maste /* Callbacks */ 25*10ff414cSEd Maste struct test_assertion current() { 26*10ff414cSEd Maste return assertions_queue[current_expectation]; 27*10ff414cSEd Maste } 28*10ff414cSEd Maste 29*10ff414cSEd Maste /* Assertions builders and matcher callbacks */ 30*10ff414cSEd Maste 31*10ff414cSEd Maste void assert_uint8_eq(uint8_t actual) { 32*10ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){ 33*10ff414cSEd Maste UINT8_EQ, (union test_expectation_data){.int8 = actual}}; 34*10ff414cSEd Maste } 35*10ff414cSEd Maste 36*10ff414cSEd Maste void uint8_callback(void *context, uint8_t actual) { 37*10ff414cSEd Maste assert_true(current().expectation == UINT8_EQ); 38*10ff414cSEd Maste assert_true(current().data.int8 == actual); 39*10ff414cSEd Maste current_expectation++; 40*10ff414cSEd Maste } 41*10ff414cSEd Maste 42*10ff414cSEd Maste void assert_uint16_eq(uint16_t actual) { 43*10ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){ 44*10ff414cSEd Maste UINT16_EQ, (union test_expectation_data){.int16 = actual}}; 45*10ff414cSEd Maste } 46*10ff414cSEd Maste 47*10ff414cSEd Maste void uint16_callback(void *context, uint16_t actual) { 48*10ff414cSEd Maste assert_true(current().expectation == UINT16_EQ); 49*10ff414cSEd Maste assert_true(current().data.int16 == actual); 50*10ff414cSEd Maste current_expectation++; 51*10ff414cSEd Maste } 52*10ff414cSEd Maste 53*10ff414cSEd Maste void assert_uint32_eq(uint32_t actual) { 54*10ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){ 55*10ff414cSEd Maste UINT32_EQ, (union test_expectation_data){.int32 = actual}}; 56*10ff414cSEd Maste } 57*10ff414cSEd Maste 58*10ff414cSEd Maste void uint32_callback(void *context, uint32_t actual) { 59*10ff414cSEd Maste assert_true(current().expectation == UINT32_EQ); 60*10ff414cSEd Maste assert_true(current().data.int32 == actual); 61*10ff414cSEd Maste current_expectation++; 62*10ff414cSEd Maste } 63*10ff414cSEd Maste 64*10ff414cSEd Maste void assert_uint64_eq(uint64_t actual) { 65*10ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){ 66*10ff414cSEd Maste UINT64_EQ, (union test_expectation_data){.int64 = actual}}; 67*10ff414cSEd Maste } 68*10ff414cSEd Maste 69*10ff414cSEd Maste void uint64_callback(void *context, uint64_t actual) { 70*10ff414cSEd Maste assert_true(current().expectation == UINT64_EQ); 71*10ff414cSEd Maste assert_true(current().data.int64 == actual); 72*10ff414cSEd Maste current_expectation++; 73*10ff414cSEd Maste } 74*10ff414cSEd Maste 75*10ff414cSEd Maste void assert_negint8_eq(uint8_t actual) { 76*10ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){ 77*10ff414cSEd Maste NEGINT8_EQ, (union test_expectation_data){.int8 = actual}}; 78*10ff414cSEd Maste } 79*10ff414cSEd Maste 80*10ff414cSEd Maste void negint8_callback(void *context, uint8_t actual) { 81*10ff414cSEd Maste assert_true(current().expectation == NEGINT8_EQ); 82*10ff414cSEd Maste assert_true(current().data.int8 == actual); 83*10ff414cSEd Maste current_expectation++; 84*10ff414cSEd Maste } 85*10ff414cSEd Maste 86*10ff414cSEd Maste void assert_negint16_eq(uint16_t actual) { 87*10ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){ 88*10ff414cSEd Maste NEGINT16_EQ, (union test_expectation_data){.int16 = actual}}; 89*10ff414cSEd Maste } 90*10ff414cSEd Maste 91*10ff414cSEd Maste void negint16_callback(void *context, uint16_t actual) { 92*10ff414cSEd Maste assert_true(current().expectation == NEGINT16_EQ); 93*10ff414cSEd Maste assert_true(current().data.int16 == actual); 94*10ff414cSEd Maste current_expectation++; 95*10ff414cSEd Maste } 96*10ff414cSEd Maste 97*10ff414cSEd Maste void assert_negint32_eq(uint32_t actual) { 98*10ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){ 99*10ff414cSEd Maste NEGINT32_EQ, (union test_expectation_data){.int32 = actual}}; 100*10ff414cSEd Maste } 101*10ff414cSEd Maste 102*10ff414cSEd Maste void negint32_callback(void *context, uint32_t actual) { 103*10ff414cSEd Maste assert_true(current().expectation == NEGINT32_EQ); 104*10ff414cSEd Maste assert_true(current().data.int32 == actual); 105*10ff414cSEd Maste current_expectation++; 106*10ff414cSEd Maste } 107*10ff414cSEd Maste 108*10ff414cSEd Maste void assert_negint64_eq(uint64_t actual) { 109*10ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){ 110*10ff414cSEd Maste NEGINT64_EQ, (union test_expectation_data){.int64 = actual}}; 111*10ff414cSEd Maste } 112*10ff414cSEd Maste 113*10ff414cSEd Maste void negint64_callback(void *context, uint64_t actual) { 114*10ff414cSEd Maste assert_true(current().expectation == NEGINT64_EQ); 115*10ff414cSEd Maste assert_true(current().data.int64 == actual); 116*10ff414cSEd Maste current_expectation++; 117*10ff414cSEd Maste } 118*10ff414cSEd Maste 119*10ff414cSEd Maste void assert_bstring_mem_eq(cbor_data address, size_t length) { 120*10ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){ 121*10ff414cSEd Maste BSTRING_MEM_EQ, 122*10ff414cSEd Maste (union test_expectation_data){.string = {address, length}}}; 123*10ff414cSEd Maste } 124*10ff414cSEd Maste 125*10ff414cSEd Maste void byte_string_callback(void *context, cbor_data address, size_t length) { 126*10ff414cSEd Maste assert_true(current().expectation == BSTRING_MEM_EQ); 127*10ff414cSEd Maste assert_true(current().data.string.address == address); 128*10ff414cSEd Maste assert_true(current().data.string.length == length); 129*10ff414cSEd Maste current_expectation++; 130*10ff414cSEd Maste } 131*10ff414cSEd Maste 132*10ff414cSEd Maste void assert_bstring_indef_start() { 133*10ff414cSEd Maste assertions_queue[queue_size++] = 134*10ff414cSEd Maste (struct test_assertion){.expectation = BSTRING_INDEF_START}; 135*10ff414cSEd Maste } 136*10ff414cSEd Maste 137*10ff414cSEd Maste void byte_string_start_callback(void *context) { 138*10ff414cSEd Maste assert_true(current().expectation == BSTRING_INDEF_START); 139*10ff414cSEd Maste current_expectation++; 140*10ff414cSEd Maste } 141*10ff414cSEd Maste 142*10ff414cSEd Maste void assert_indef_break() { 143*10ff414cSEd Maste assertions_queue[queue_size++] = 144*10ff414cSEd Maste (struct test_assertion){.expectation = INDEF_BREAK}; 145*10ff414cSEd Maste } 146*10ff414cSEd Maste 147*10ff414cSEd Maste void indef_break_callback(void *context) { 148*10ff414cSEd Maste assert_true(current().expectation == INDEF_BREAK); 149*10ff414cSEd Maste current_expectation++; 150*10ff414cSEd Maste } 151*10ff414cSEd Maste 152*10ff414cSEd Maste void assert_array_start(size_t length) { 153*10ff414cSEd Maste assertions_queue[queue_size++] = 154*10ff414cSEd Maste (struct test_assertion){ARRAY_START, {.length = length}}; 155*10ff414cSEd Maste } 156*10ff414cSEd Maste 157*10ff414cSEd Maste void array_start_callback(void *context, size_t length) { 158*10ff414cSEd Maste assert_true(current().expectation == ARRAY_START); 159*10ff414cSEd Maste assert_true(current().data.length == length); 160*10ff414cSEd Maste current_expectation++; 161*10ff414cSEd Maste } 162*10ff414cSEd Maste 163*10ff414cSEd Maste void assert_indef_array_start() { 164*10ff414cSEd Maste assertions_queue[queue_size++] = 165*10ff414cSEd Maste (struct test_assertion){.expectation = ARRAY_INDEF_START}; 166*10ff414cSEd Maste } 167*10ff414cSEd Maste 168*10ff414cSEd Maste void indef_array_start_callback(void *context) { 169*10ff414cSEd Maste assert_true(current().expectation == ARRAY_INDEF_START); 170*10ff414cSEd Maste current_expectation++; 171*10ff414cSEd Maste } 172*10ff414cSEd Maste 173*10ff414cSEd Maste void assert_map_start(size_t length) { 174*10ff414cSEd Maste assertions_queue[queue_size++] = 175*10ff414cSEd Maste (struct test_assertion){MAP_START, {.length = length}}; 176*10ff414cSEd Maste } 177*10ff414cSEd Maste 178*10ff414cSEd Maste void map_start_callback(void *context, size_t length) { 179*10ff414cSEd Maste assert_true(current().expectation == MAP_START); 180*10ff414cSEd Maste assert_true(current().data.length == length); 181*10ff414cSEd Maste current_expectation++; 182*10ff414cSEd Maste } 183*10ff414cSEd Maste 184*10ff414cSEd Maste void assert_indef_map_start() { 185*10ff414cSEd Maste assertions_queue[queue_size++] = 186*10ff414cSEd Maste (struct test_assertion){.expectation = MAP_INDEF_START}; 187*10ff414cSEd Maste } 188*10ff414cSEd Maste 189*10ff414cSEd Maste void indef_map_start_callback(void *context) { 190*10ff414cSEd Maste assert_true(current().expectation == MAP_INDEF_START); 191*10ff414cSEd Maste current_expectation++; 192*10ff414cSEd Maste } 193*10ff414cSEd Maste 194*10ff414cSEd Maste void assert_tag_eq(uint64_t value) { 195*10ff414cSEd Maste assertions_queue[queue_size++] = 196*10ff414cSEd Maste (struct test_assertion){TAG_EQ, {.int64 = value}}; 197*10ff414cSEd Maste } 198*10ff414cSEd Maste 199*10ff414cSEd Maste void tag_callback(void *context, uint64_t value) { 200*10ff414cSEd Maste assert_true(current().expectation == TAG_EQ); 201*10ff414cSEd Maste assert_true(current().data.int64 == value); 202*10ff414cSEd Maste current_expectation++; 203*10ff414cSEd Maste } 204*10ff414cSEd Maste 205*10ff414cSEd Maste void assert_half(float value) { 206*10ff414cSEd Maste assertions_queue[queue_size++] = 207*10ff414cSEd Maste (struct test_assertion){HALF_EQ, {.float2 = value}}; 208*10ff414cSEd Maste } 209*10ff414cSEd Maste 210*10ff414cSEd Maste void half_callback(void *context, float actual) { 211*10ff414cSEd Maste assert_true(current().expectation == HALF_EQ); 212*10ff414cSEd Maste assert_true(current().data.float2 == actual); 213*10ff414cSEd Maste current_expectation++; 214*10ff414cSEd Maste } 215*10ff414cSEd Maste 216*10ff414cSEd Maste void assert_float(float value) { 217*10ff414cSEd Maste assertions_queue[queue_size++] = 218*10ff414cSEd Maste (struct test_assertion){FLOAT_EQ, {.float4 = value}}; 219*10ff414cSEd Maste } 220*10ff414cSEd Maste 221*10ff414cSEd Maste void float_callback(void *context, float actual) { 222*10ff414cSEd Maste assert_true(current().expectation == FLOAT_EQ); 223*10ff414cSEd Maste assert_true(current().data.float4 == actual); 224*10ff414cSEd Maste current_expectation++; 225*10ff414cSEd Maste } 226*10ff414cSEd Maste 227*10ff414cSEd Maste void assert_double(double value) { 228*10ff414cSEd Maste assertions_queue[queue_size++] = 229*10ff414cSEd Maste (struct test_assertion){DOUBLE_EQ, {.float8 = value}}; 230*10ff414cSEd Maste } 231*10ff414cSEd Maste 232*10ff414cSEd Maste void double_callback(void *context, double actual) { 233*10ff414cSEd Maste assert_true(current().expectation == DOUBLE_EQ); 234*10ff414cSEd Maste assert_true(current().data.float8 == actual); 235*10ff414cSEd Maste current_expectation++; 236*10ff414cSEd Maste } 237*10ff414cSEd Maste 238*10ff414cSEd Maste void assert_bool(bool value) { 239*10ff414cSEd Maste assertions_queue[queue_size++] = 240*10ff414cSEd Maste (struct test_assertion){BOOL_EQ, {.boolean = value}}; 241*10ff414cSEd Maste } 242*10ff414cSEd Maste 243*10ff414cSEd Maste void assert_nil() { 244*10ff414cSEd Maste assertions_queue[queue_size++] = (struct test_assertion){.expectation = NIL}; 245*10ff414cSEd Maste } 246*10ff414cSEd Maste 247*10ff414cSEd Maste void assert_undef() { 248*10ff414cSEd Maste assertions_queue[queue_size++] = 249*10ff414cSEd Maste (struct test_assertion){.expectation = UNDEF}; 250*10ff414cSEd Maste } 251*10ff414cSEd Maste 252*10ff414cSEd Maste void bool_callback(void *context, bool actual) { 253*10ff414cSEd Maste assert_true(current().expectation == BOOL_EQ); 254*10ff414cSEd Maste assert_true(current().data.boolean == actual); 255*10ff414cSEd Maste current_expectation++; 256*10ff414cSEd Maste } 257*10ff414cSEd Maste 258*10ff414cSEd Maste void null_callback(void *context) { 259*10ff414cSEd Maste assert_true(current().expectation == NIL); 260*10ff414cSEd Maste current_expectation++; 261*10ff414cSEd Maste } 262*10ff414cSEd Maste 263*10ff414cSEd Maste void undef_callback(void *context) { 264*10ff414cSEd Maste assert_true(current().expectation == UNDEF); 265*10ff414cSEd Maste current_expectation++; 266*10ff414cSEd Maste } 267*10ff414cSEd Maste 268*10ff414cSEd Maste const struct cbor_callbacks asserting_callbacks = { 269*10ff414cSEd Maste 270*10ff414cSEd Maste .uint8 = &uint8_callback, 271*10ff414cSEd Maste 272*10ff414cSEd Maste .uint16 = &uint16_callback, 273*10ff414cSEd Maste 274*10ff414cSEd Maste .uint32 = &uint32_callback, 275*10ff414cSEd Maste 276*10ff414cSEd Maste .uint64 = &uint64_callback, 277*10ff414cSEd Maste 278*10ff414cSEd Maste .negint8 = &negint8_callback, 279*10ff414cSEd Maste 280*10ff414cSEd Maste .negint16 = &negint16_callback, 281*10ff414cSEd Maste 282*10ff414cSEd Maste .negint32 = &negint32_callback, 283*10ff414cSEd Maste 284*10ff414cSEd Maste .negint64 = &negint64_callback, 285*10ff414cSEd Maste 286*10ff414cSEd Maste .byte_string = &byte_string_callback, 287*10ff414cSEd Maste .byte_string_start = &byte_string_start_callback, 288*10ff414cSEd Maste 289*10ff414cSEd Maste .array_start = &array_start_callback, 290*10ff414cSEd Maste .indef_array_start = &indef_array_start_callback, 291*10ff414cSEd Maste 292*10ff414cSEd Maste .map_start = &map_start_callback, 293*10ff414cSEd Maste .indef_map_start = &indef_map_start_callback, 294*10ff414cSEd Maste 295*10ff414cSEd Maste .tag = &tag_callback, 296*10ff414cSEd Maste 297*10ff414cSEd Maste .float2 = &half_callback, 298*10ff414cSEd Maste 299*10ff414cSEd Maste .float4 = &float_callback, 300*10ff414cSEd Maste 301*10ff414cSEd Maste .float8 = &double_callback, 302*10ff414cSEd Maste 303*10ff414cSEd Maste .undefined = &undef_callback, 304*10ff414cSEd Maste .boolean = &bool_callback, 305*10ff414cSEd Maste .null = &null_callback, 306*10ff414cSEd Maste .indef_break = &indef_break_callback}; 307*10ff414cSEd Maste 308*10ff414cSEd Maste struct cbor_decoder_result decode(cbor_data source, size_t source_size) { 309*10ff414cSEd Maste int last_expectation = current_expectation; 310*10ff414cSEd Maste struct cbor_decoder_result result = 311*10ff414cSEd Maste decoder(source, source_size, &asserting_callbacks, NULL); 312*10ff414cSEd Maste if (result.status == CBOR_DECODER_FINISHED) { 313*10ff414cSEd Maste // Check that we have matched an expectation from the queue 314*10ff414cSEd Maste assert_true(last_expectation + 1 == current_expectation); 315*10ff414cSEd Maste } 316*10ff414cSEd Maste return result; 317*10ff414cSEd Maste } 318