aboutsummaryrefslogtreecommitdiff
path: root/tests/regression/issue_504/test.c
blob: 1ca86db7449e2ffacaa3ebb2ed190e4a8dd5c61e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pb_encode.h>
#include "test.pb.h"
#include "unittests.h"

const char STR[] = "test str";
#define ALIGN 0x100

int main(int argc, char **argv)
{
    int status = 0;
    uint8_t buffer[512] = {0};
    int i;
    pb_ostream_t ostream;
    MyMessage msg = MyMessage_init_zero;
    char *pStr, *pStrAligned;
    ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));

    /* copy STR to a malloced 0x100 aligned address */
    pStr = malloc(sizeof(STR) + ALIGN);
    pStrAligned = (char*)((uintptr_t)(pStr + ALIGN) & ~(ALIGN - 1));
    memcpy(pStrAligned, STR, sizeof(STR));

    msg.submessage.somestring = pStrAligned;
    printf("%p: '%s'\n", msg.submessage.somestring, msg.submessage.somestring);

    if (!pb_encode(&ostream, MyMessage_fields, &msg)) {
        fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&ostream));
        return 1;
    }

    free(pStr);
    msg.submessage.somestring = NULL;

    printf("response payload (%d):", (int)ostream.bytes_written);
    for (i = 0; i < ostream.bytes_written; i++) {
        printf("%02X", buffer[i]);
    }
    printf("\n");

    TEST(ostream.bytes_written != 0);

    return status;
}