MOS Source Code
Loading...
Searching...
No Matches
test_extensions.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 <pb_decode.h>
6#include "extensions.pb.h"
7#include "unittests.h"
8
9static bool write_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
10{
11 return pb_encode_tag_for_field(stream, field) &&
12 pb_encode_string(stream, (const void*)"abc", 3);
13}
14
15int main(int argc, char **argv)
16{
17 int status = 0;
18 uint8_t buffer[64];
19 pb_size_t msglen = 0;
20
21 {
22 pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
23 pb_callback_t callback_def = {{0}};
24 pb_extension_t ext = {0};
25 BaseMessage msg = {0};
26
27 callback_def.funcs.encode = &write_string;
28 ext.type = &string_extension;
29 ext.dest = &callback_def;
30 msg.extensions = &ext;
31
32 TEST(pb_encode(&stream, BaseMessage_fields, &msg));
33
34 msglen = stream.bytes_written;
35 TEST(msglen > 3);
36 }
37
38 {
39 pb_istream_t stream = pb_istream_from_buffer(buffer, msglen);
40 pb_extension_t ext = {0};
41 BaseMessage msg = {0};
42
43 ext.type = &string_extension;
44 /* Note: ext.dest remains null to trigger bug #342 */
45 msg.extensions = &ext;
46
47 TEST(pb_decode(&stream, BaseMessage_fields, &msg));
48 }
49
50 return status;
51}
52
static bool write_string(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
const char ** argv
Definition kmain.c:44
size_t argc
Definition kmain.c:43
uint_least16_t pb_size_t
Definition pb.h:310
pb_field_iter_t pb_field_t
Definition pb.h:359
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
bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_iter_t *field)
Definition pb_encode.c:681
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
bool pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size)
Definition pb_encode.c:716
unsigned char uint8_t
Definition pb_syshdr.h:20
int main()
Definition simple.cpp:6
#define TEST(x)
Definition test.h:3
static char buffer[2048]
Definition test_printf.c:7