xref: /aosp_15_r20/external/nanopb-c/tests/regression/issue_504/test.c (revision c8d645cafcee3f91213d30caa0fe303887010b9b)
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 
main(int argc,char ** argv)11 int main(int argc, char **argv)
12 {
13     int status = 0;
14     uint8_t buffer[512] = {0};
15     int i;
16     pb_ostream_t ostream;
17     MyMessage msg = MyMessage_init_zero;
18     char *pStr, *pStrAligned;
19     ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
20 
21     /* copy STR to a malloced 0x100 aligned address */
22     pStr = malloc(sizeof(STR) + ALIGN);
23     pStrAligned = (char*)((uintptr_t)(pStr + ALIGN) & ~(ALIGN - 1));
24     memcpy(pStrAligned, STR, sizeof(STR));
25 
26     msg.submessage.somestring = pStrAligned;
27     printf("%p: '%s'\n", msg.submessage.somestring, msg.submessage.somestring);
28 
29     if (!pb_encode(&ostream, MyMessage_fields, &msg)) {
30         fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&ostream));
31         return 1;
32     }
33 
34     free(pStr);
35     msg.submessage.somestring = NULL;
36 
37     printf("response payload (%d):", (int)ostream.bytes_written);
38     for (i = 0; i < ostream.bytes_written; i++) {
39         printf("%02X", buffer[i]);
40     }
41     printf("\n");
42 
43     TEST(ostream.bytes_written != 0);
44 
45     return status;
46 }
47 
48