Line data Source code
1 : #include <stdio.h> 2 : #include <stdlib.h> 3 : #include <string.h> 4 : #include <pb_encode.h> 5 : #include "test.pb.h" 6 : #include "unittests.h" 7 : 8 : const char STR[] = "test str"; 9 : #define ALIGN 0x100 10 : 11 1 : int main(int argc, char **argv) 12 : { 13 1 : int status = 0; 14 1 : uint8_t buffer[512] = {0}; 15 : int i; 16 : 17 : { 18 : pb_ostream_t ostream; 19 1 : MyMessage msg = MyMessage_init_zero; 20 : char *pStr, *pStrAligned; 21 : 22 1 : COMMENT("Test for false negatives with pointer value low byte 0x00") 23 1 : ostream = pb_ostream_from_buffer(buffer, sizeof(buffer)); 24 : 25 : /* copy STR to a malloced 0x100 aligned address */ 26 1 : pStr = malloc(sizeof(STR) + ALIGN); 27 1 : pStrAligned = (char*)((uintptr_t)(pStr + ALIGN) & ~(ALIGN - 1)); 28 1 : memcpy(pStrAligned, STR, sizeof(STR)); 29 : 30 1 : msg.submessage.somestring = pStrAligned; 31 1 : printf("%p: '%s'\n", msg.submessage.somestring, msg.submessage.somestring); 32 : 33 1 : if (!pb_encode(&ostream, MyMessage_fields, &msg)) { 34 0 : fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&ostream)); 35 0 : return 1; 36 : } 37 : 38 1 : free(pStr); 39 1 : msg.submessage.somestring = NULL; 40 : 41 1 : printf("response payload (%d):", (int)ostream.bytes_written); 42 13 : for (i = 0; i < ostream.bytes_written; i++) { 43 12 : printf("%02X", buffer[i]); 44 : } 45 1 : printf("\n"); 46 : 47 1 : TEST(ostream.bytes_written != 0); 48 : } 49 : 50 : { 51 : pb_ostream_t ostream; 52 : struct { 53 : MyMessage msg; 54 : uint32_t bar; 55 1 : } msg = {MyMessage_init_zero, 0}; 56 : 57 1 : COMMENT("Test for false positives with data after end of struct") 58 1 : ostream = pb_ostream_from_buffer(buffer, sizeof(buffer)); 59 : 60 1 : msg.bar = 0xFFFFFFFF; 61 : 62 1 : if (!pb_encode(&ostream, MyMessage_fields, &msg)) { 63 0 : fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&ostream)); 64 0 : return 1; 65 : } 66 : 67 1 : printf("response payload (%d):", (int)ostream.bytes_written); 68 1 : for (i = 0; i < ostream.bytes_written; i++) { 69 0 : printf("%02X", buffer[i]); 70 : } 71 1 : printf("\n"); 72 : 73 1 : TEST(ostream.bytes_written == 0); 74 : } 75 : 76 1 : return status; 77 : } 78 :