Merged 0004-Added-missing-trailing-newlines.patch by Matt Kern.
[apps/agl-service-can-low-level.git] / tests / test_decode1.c
index dc0831a..b412ea8 100644 (file)
-#include <stdio.h>
-#include <string.h>
-#include <stddef.h>
-#include "pb_decode.h"
-
-/* Structures for "Person" message */
-
-typedef enum {
-    Person_PhoneType_MOBILE = 0,
-    Person_PhoneType_HOME = 1,
-    Person_PhoneType_WORK = 2,
-    
-    _Person_PhoneType_size = 0xFFFFFFFF // Force 32-bit enum
-} Person_PhoneType;
-
-typedef struct {
-    char number[40];
-    bool has_type;
-    Person_PhoneType type;
-} Person_PhoneNumber;
-
-typedef struct {
-    char name[40];
-    int32_t id;
-    bool has_email;
-    char email[40];
-
-    pb_callback_t phone;
-} Person;
-
-/* Field descriptions */
-#define membersize(st, m) (sizeof ((st*)0)->m)
+/* A very simple decoding test case, using person.proto.
+ * Produces output compatible with protoc --decode.
+ * Reads the encoded data from stdin and prints the values
+ * to stdout as text.
+ *
+ * Run e.g. ./test_encode1 | ./test_decode1
+ */
 
-const pb_field_t Person_PhoneNumber_fields[] = {
-    {1, offsetof(Person_PhoneNumber, number), PB_ACT_STRING, membersize(Person_PhoneNumber, number)},
-    {2, offsetof(Person_PhoneNumber, has_type), PB_ACT_HAS, membersize(Person_PhoneNumber, has_type)},
-    {2, offsetof(Person_PhoneNumber, type), PB_ACT_UINT32, membersize(Person_PhoneNumber, type)},
-    PB_LAST_FIELD
-};
-
-const pb_field_t Person_fields[] = {
-    {1, offsetof(Person, name), PB_ACT_STRING, membersize(Person, name)},
-    {2, offsetof(Person, id), PB_ACT_INT32, membersize(Person, id)},
-    {3, offsetof(Person, email), PB_ACT_STRING, membersize(Person, email)},
-    {4, offsetof(Person, phone), PB_ACT_SUBMESSAGE, membersize(Person, phone)}
-};
-
-/* Default value descriptions */
-#define Person_PhoneNumber_default {"", false, Person_PhoneType_HOME};
-#define Person_default {"", 0, false, "", {{0},0}};
-
-/* And now, the actual test program */
+#include <stdio.h>
+#include <pb_decode.h>
+#include "person.pb.h"
 
-bool print_phonenumber(pb_istream_t *stream, const pb_field_t *field, void *arg)
+/* This function is called once from main(), it handles
+   the decoding and printing. */
+bool print_person(pb_istream_t *stream)
 {
-    Person_PhoneNumber x = Person_PhoneNumber_default;
-    if (!pb_decode(stream, Person_PhoneNumber_fields, &x))
+    int i;
+    Person person;
+    
+    if (!pb_decode(stream, Person_fields, &person))
         return false;
     
-    printf("PhoneNumber: number '%s' type '%d'\n", x.number, x.type);
-    return true;
-}
+    /* Now the decoding is done, rest is just to print stuff out. */
 
-bool print_person(pb_istream_t *stream)
-{
-    Person x = Person_default;
-    x.phone.funcs.decode = &print_phonenumber;
+    printf("name: \"%s\"\n", person.name);
+    printf("id: %ld\n", (long)person.id);
     
-    if (!pb_decode(stream, Person_fields, &x))
-        return false;
+    if (person.has_email)
+        printf("email: \"%s\"\n", person.email);
+    
+    for (i = 0; i < person.phone_count; i++)
+    {
+        Person_PhoneNumber *phone = &person.phone[i];
+        printf("phone {\n");
+        printf("  number: \"%s\"\n", phone->number);
+        
+        switch (phone->type)
+        {
+            case Person_PhoneType_WORK:
+                printf("  type: WORK\n");
+                break;
+            
+            case Person_PhoneType_HOME:
+                printf("  type: HOME\n");
+                break;
+            
+            case Person_PhoneType_MOBILE:
+                printf("  type: MOBILE\n");
+                break;
+        }
+        printf("}\n");
+    }
     
-    printf("Person: name '%s' id '%d' email '%s'\n", x.name, x.id, x.email);
     return true;
 }
 
-bool my_read(pb_istream_t *stream, char *buf, size_t count)
+/* This binds the pb_istream_t to stdin */
+bool callback(pb_istream_t *stream, uint8_t *buf, size_t count)
 {
-    char *source = (char*)stream->state;
-    
-    if (!stream->bytes_left)
-        return false;
+    FILE *file = (FILE*)stream->state;
+    bool status;
     
-    if (buf != NULL)
+    if (buf == NULL)
     {
-        memcpy(buf, source, count);
+       /* Skipping data */
+        while (count-- && fgetc(file) != EOF);
+        return count == 0;
     }
     
-    stream->state = source + count;
-    return true;
+    status = (fread(buf, 1, count, file) == count);
+    
+    if (feof(file))
+        stream->bytes_left = 0;
+    
+    return status;
 }
 
 int main()
 {
-    char buffer[512];
-    size_t size = fread(buffer, 1, 512, stdin);
-    
-    pb_istream_t stream = {&my_read, buffer, size};
+    /* Maximum size is specified to prevent infinite length messages from
+     * hanging this in the fuzz test.
+     */
+    pb_istream_t stream = {&callback, stdin, 10000};
     if (!print_person(&stream))
+    {
         printf("Parsing failed.\n");
-    
-    return 0;
+        return 1;
+    } else {
+        return 0;
+    }
 }