MOS Source Code
Loading...
Searching...
No Matches
decode_extensions.c
Go to the documentation of this file.
1/* Test decoding of extension fields. */
2
3#include <stdio.h>
4#include <string.h>
5#include <stdlib.h>
6#include <pb_decode.h>
7#include "alltypes.pb.h"
8#include "extensions.pb.h"
9#include "test_helpers.h"
10#include "unittests.h"
11
12int main(int argc, char **argv)
13{
14 uint8_t buffer[1024];
15 size_t count;
16 pb_istream_t stream;
17 int status = 0;
18
20 int32_t extensionfield1;
21 pb_extension_t ext1 = pb_extension_init_zero;
22 ExtensionMessage extensionfield2 = ExtensionMessage_init_zero;
23 pb_extension_t ext2 = pb_extension_init_zero;
24
25 /* Read the message data */
27 count = fread(buffer, 1, sizeof(buffer), stdin);
28 stream = pb_istream_from_buffer(buffer, count);
29
30 /* Add the extensions */
31 alltypes.extensions = &ext1;
32
33 ext1.type = &AllTypes_extensionfield1;
34 ext1.dest = &extensionfield1;
35 ext1.next = &ext2;
36
37 ext2.type = &ExtensionMessage_AllTypes_extensionfield2;
38 ext2.dest = &extensionfield2;
39 ext2.next = NULL;
40
41 /* Decode the message */
42 if (!pb_decode(&stream, AllTypes_fields, &alltypes))
43 {
44 printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
45 return 1;
46 }
47
48 /* Check that the extensions decoded properly */
49 TEST(ext1.found)
50 TEST(extensionfield1 == 12345)
51 TEST(ext2.found)
52 TEST(strcmp(extensionfield2.test1, "test") == 0)
53 TEST(extensionfield2.test2 == 54321)
54
55 return status;
56}
57
#define AllTypes_fields
#define AllTypes_init_zero
MOSAPI s32 strcmp(const char *str1, const char *str2)
Definition mos_string.c:24
size_t fread(void *__restrict ptr, size_t size, size_t nmemb, FILE *__restrict stream)
#define stdin
Definition mos_stdio.h:30
const char ** argv
Definition kmain.c:44
size_t argc
Definition kmain.c:43
static pb_extension_t ext2
Definition mem_release.c:17
static pb_extension_t ext1
Definition mem_release.c:17
#define PB_GET_ERROR(stream)
Definition pb.h:891
#define pb_extension_init_zero
Definition pb.h:477
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1182
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
Definition pb_decode.c:143
#define NULL
Definition pb_syshdr.h:46
signed int int32_t
Definition pb_syshdr.h:23
unsigned char uint8_t
Definition pb_syshdr.h:20
int main()
Definition simple.cpp:6
pb_extension_t * extensions
#define TEST(x)
Definition test.h:3
#define SET_BINARY_MODE(file)
static char buffer[2048]
Definition test_printf.c:7