MOS Source Code
Loading...
Searching...
No Matches
test.c
Go to the documentation of this file.
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
8const char STR[] = "test str";
9#define ALIGN 0x100
10
11int main(int argc, char **argv)
12{
13 int status = 0;
14 uint8_t buffer[512] = {0};
15 int i;
16
17 {
18 pb_ostream_t ostream;
19 MyMessage msg = MyMessage_init_zero;
20 char *pStr, *pStrAligned;
21
22 COMMENT("Test for false negatives with pointer value low byte 0x00")
23 ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
24
25 /* copy STR to a malloced 0x100 aligned address */
26 pStr = malloc(sizeof(STR) + ALIGN);
27 pStrAligned = (char*)((uintptr_t)(pStr + ALIGN) & ~(ALIGN - 1));
28 memcpy(pStrAligned, STR, sizeof(STR));
29
30 msg.submessage.somestring = pStrAligned;
31 printf("%p: '%s'\n", msg.submessage.somestring, msg.submessage.somestring);
32
33 if (!pb_encode(&ostream, MyMessage_fields, &msg)) {
34 fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&ostream));
35 return 1;
36 }
37
38 free(pStr);
39 msg.submessage.somestring = NULL;
40
41 printf("response payload (%d):", (int)ostream.bytes_written);
42 for (i = 0; i < ostream.bytes_written; i++) {
43 printf("%02X", buffer[i]);
44 }
45 printf("\n");
46
47 TEST(ostream.bytes_written != 0);
48 }
49
50 {
51 pb_ostream_t ostream;
52 struct {
53 MyMessage msg;
54 uint32_t bar;
55 } msg = {MyMessage_init_zero, 0};
56
57 COMMENT("Test for false positives with data after end of struct")
58 ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
59
60 msg.bar = 0xFFFFFFFF;
61
62 if (!pb_encode(&ostream, MyMessage_fields, &msg)) {
63 fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&ostream));
64 return 1;
65 }
66
67 printf("response payload (%d):", (int)ostream.bytes_written);
68 for (i = 0; i < ostream.bytes_written; i++) {
69 printf("%02X", buffer[i]);
70 }
71 printf("\n");
72
73 TEST(ostream.bytes_written == 0);
74 }
75
76 return status;
77}
78
int main()
Definition test.c:24
#define stderr
Definition mos_stdio.h:32
#define ALIGN
Definition test.c:9
const char STR[]
Definition test.c:8
const char ** argv
Definition kmain.c:44
size_t argc
Definition kmain.c:43
#define PB_GET_ERROR(stream)
Definition pb.h:891
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
Definition pb_encode.c:63
bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
Definition pb_encode.c:512
static void * memcpy(void *s1, const void *s2, size_t n)
Definition pb_syshdr.h:90
#define NULL
Definition pb_syshdr.h:46
unsigned int uint32_t
Definition pb_syshdr.h:24
unsigned char uint8_t
Definition pb_syshdr.h:20
#define TEST(x)
Definition test.h:3
static char buffer[2048]
Definition test_printf.c:7
#define COMMENT(x)
Definition unittests.h:17