17ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* Test decoding of extension fields. */ 27ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 37ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <stdio.h> 47ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <string.h> 57ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <stdlib.h> 67ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <pb_decode.h> 77ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include "alltypes.pb.h" 87ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include "extensions.pb.h" 97ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include "test_helpers.h" 107ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 117ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#define TEST(x) if (!(x)) { \ 127ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen printf("Test " #x " failed.\n"); \ 137ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return 2; \ 147ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 157ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 167ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenint main(int argc, char **argv) 177ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen{ 187ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen uint8_t buffer[1024]; 197ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t count; 207ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen pb_istream_t stream; 217ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 227ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen AllTypes alltypes = {0}; 237ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int32_t extensionfield1; 247ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen pb_extension_t ext1; 257ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ExtensionMessage extensionfield2; 267ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen pb_extension_t ext2; 277ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 287ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen /* Read the message data */ 297ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SET_BINARY_MODE(stdin); 307ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen count = fread(buffer, 1, sizeof(buffer), stdin); 317ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen stream = pb_istream_from_buffer(buffer, count); 327ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 337ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen /* Add the extensions */ 347ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen alltypes.extensions = &ext1; 357ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 367ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ext1.type = &AllTypes_extensionfield1; 377ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ext1.dest = &extensionfield1; 387ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ext1.next = &ext2; 397ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 407ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ext2.type = &ExtensionMessage_AllTypes_extensionfield2; 417ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ext2.dest = &extensionfield2; 427ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ext2.next = NULL; 437ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 447ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen /* Decode the message */ 457ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (!pb_decode(&stream, AllTypes_fields, &alltypes)) 467ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen { 477ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen printf("Parsing failed: %s\n", PB_GET_ERROR(&stream)); 487ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return 1; 497ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 507ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 517ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen /* Check that the extensions decoded properly */ 527ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen TEST(ext1.found) 537ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen TEST(extensionfield1 == 12345) 547ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen TEST(ext2.found) 557ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen TEST(strcmp(extensionfield2.test1, "test") == 0) 567ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen TEST(extensionfield2.test2 == 54321) 577ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 587ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return 0; 597ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 607ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 61