Modify the alltypes test to check re-encoding through protoc.
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>
Sat, 21 Dec 2013 10:14:20 +0000 (12:14 +0200)
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>
Sat, 21 Dec 2013 10:14:20 +0000 (12:14 +0200)
This way we can verify that the message is encoded exactly the same way
as the official protobuf implementation would do it.

tests/alltypes/SConscript
tests/alltypes/alltypes.proto
tests/site_scons/site_init.py

index 1dc6f87..9c9072b 100644 (file)
@@ -7,6 +7,29 @@ env.NanopbProto(["alltypes", "alltypes.options"])
 enc = env.Program(["encode_alltypes.c", "alltypes.pb.c", "$COMMON/pb_encode.o"])
 dec = env.Program(["decode_alltypes.c", "alltypes.pb.c", "$COMMON/pb_decode.o"])
 
+# Test the round-trip from nanopb encoder to nanopb decoder
 env.RunTest(enc)
 env.RunTest([dec, "encode_alltypes.output"])
 
+# Re-encode the data using protoc, and check that the results from nanopb
+# match byte-per-byte to the protoc output.
+env.Decode("encode_alltypes.output.decoded",
+           ["encode_alltypes.output", "alltypes.proto"],
+           MESSAGE='AllTypes')
+env.Encode("encode_alltypes.output.recoded",
+           ["encode_alltypes.output.decoded", "alltypes.proto"],
+           MESSAGE='AllTypes')
+env.Compare(["encode_alltypes.output", "encode_alltypes.output.recoded"])
+
+# Do the same checks with the optional fields present.
+env.RunTest("optionals.output", enc, ARGS = ['1'])
+env.RunTest("optionals.decout", [dec, "optionals.output"], ARGS = ['1'])
+env.Decode("optionals.output.decoded",
+           ["optionals.output", "alltypes.proto"],
+           MESSAGE='AllTypes')
+env.Encode("optionals.output.recoded",
+           ["optionals.output.decoded", "alltypes.proto"],
+           MESSAGE='AllTypes')
+env.Compare(["optionals.output", "optionals.output.recoded"])
+
+
index a2cf8bb..2bc8efc 100644 (file)
@@ -39,26 +39,26 @@ message AllTypes {
     required EmptyMessage req_emptymsg = 18;
     
     
-    repeated int32      rep_int32   = 21;
-    repeated int64      rep_int64   = 22;
-    repeated uint32     rep_uint32  = 23;
-    repeated uint64     rep_uint64  = 24;
-    repeated sint32     rep_sint32  = 25;
-    repeated sint64     rep_sint64  = 26;
-    repeated bool       rep_bool    = 27;
+    repeated int32      rep_int32   = 21 [packed = true];
+    repeated int64      rep_int64   = 22 [packed = true];
+    repeated uint32     rep_uint32  = 23 [packed = true];
+    repeated uint64     rep_uint64  = 24 [packed = true];
+    repeated sint32     rep_sint32  = 25 [packed = true];
+    repeated sint64     rep_sint64  = 26 [packed = true];
+    repeated bool       rep_bool    = 27 [packed = true];
     
-    repeated fixed32    rep_fixed32 = 28;
-    repeated sfixed32   rep_sfixed32= 29;
-    repeated float      rep_float   = 30;
+    repeated fixed32    rep_fixed32 = 28 [packed = true];
+    repeated sfixed32   rep_sfixed32= 29 [packed = true];
+    repeated float      rep_float   = 30 [packed = true];
     
-    repeated fixed64    rep_fixed64 = 31;
-    repeated sfixed64   rep_sfixed64= 32;
-    repeated double     rep_double  = 33;
+    repeated fixed64    rep_fixed64 = 31 [packed = true];
+    repeated sfixed64   rep_sfixed64= 32 [packed = true];
+    repeated double     rep_double  = 33 [packed = true];
     
     repeated string     rep_string  = 34;
     repeated bytes      rep_bytes   = 35;
     repeated SubMessage rep_submsg  = 36;
-    repeated MyEnum     rep_enum    = 37;
+    repeated MyEnum     rep_enum    = 37 [packed = true];
     repeated EmptyMessage rep_emptymsg = 38;
     
     optional int32      opt_int32   = 41 [default = 4041];
index 1f81115..38aa1a4 100644 (file)
@@ -55,7 +55,11 @@ def add_nanopb_builders(env):
         else:
             infile = None
         
-        pipe = subprocess.Popen(str(source[0]),
+        args = [str(source[0])]
+        if env.has_key('ARGS'):
+            args.extend(env['ARGS'])
+        
+        pipe = subprocess.Popen(args,
                                 stdin = infile,
                                 stdout = open(str(target[0]), 'w'),
                                 stderr = sys.stderr)
@@ -81,6 +85,17 @@ def add_nanopb_builders(env):
                              suffix = '.decoded')
     env.Append(BUILDERS = {'Decode': decode_builder})    
 
+    # Build command that encodes a message using protoc
+    def encode_actions(source, target, env, for_signature):
+        esc = env['ESCAPE']
+        dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']])
+        return '$PROTOC $PROTOCFLAGS %s --encode=%s %s <%s >%s' % (
+            dirs, env['MESSAGE'], esc(str(source[1])), esc(str(source[0])), esc(str(target[0])))
+
+    encode_builder = Builder(generator = encode_actions,
+                             suffix = '.encoded')
+    env.Append(BUILDERS = {'Encode': encode_builder})    
+
     # Build command that asserts that two files be equal
     def compare_files(target, source, env):
         data1 = open(str(source[0]), 'rb').read()