more unittests
authorPetteri Aimonen <jpa@npb.mail.kapsi.fi>
Tue, 23 Aug 2011 15:57:47 +0000 (15:57 +0000)
committerPetteri Aimonen <jpa@npb.mail.kapsi.fi>
Tue, 23 Aug 2011 15:57:47 +0000 (15:57 +0000)
git-svn-id: https://svn.kapsi.fi/jpa/nanopb@964 e3a754e5-d11d-0410-8d38-ebb782a927b9

pb_decode.c
tests/decode_unittests.c

index 0e55cbf..6c6b1d9 100644 (file)
@@ -337,17 +337,14 @@ bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void
     pb_field_iterator_t iter;
     int i;
     
-    if (fields[0].tag == 0)
-    {
-        /* No fields -> nothing to do */
-        return pb_read(stream, NULL, stream->bytes_left);
-    }
-    
     pb_field_init(&iter, fields, dest_struct);
     
     /* Initialize size/has fields and apply default values */
     do
     {
+        if (iter.current->tag == 0)
+            continue;
+        
         if (PB_HTYPE(iter.current->type) == PB_HTYPE_OPTIONAL)
         {
             *(bool*)iter.pSize = false;
index 85dd8d5..6f2b77e 100644 (file)
@@ -282,6 +282,18 @@ int main()
         TEST((s = S("\x08\x55"), !pb_decode(&s, CallbackArray_fields, &dest)))
     }
     
+    {
+        pb_istream_t s;
+        IntegerArray dest;
+        
+        COMMENT("Testing pb_decode message termination")
+        TEST((s = S(""), pb_decode(&s, IntegerArray_fields, &dest)))
+        TEST((s = S("\x00"), pb_decode(&s, IntegerArray_fields, &dest)))
+        TEST((s = S("\x08\x01"), pb_decode(&s, IntegerArray_fields, &dest)))
+        TEST((s = S("\x08\x01\x00"), pb_decode(&s, IntegerArray_fields, &dest)))
+        TEST((s = S("\x08"), !pb_decode(&s, IntegerArray_fields, &dest)))
+    }
+    
     if (status != 0)
         fprintf(stdout, "\n\nSome tests FAILED!\n");