Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix problem with [default=xxxx, (nanopb).proto3=true] field option co…
…mbination (#558)
- Loading branch information
1 parent
d1be4fe
commit 4b455da
Showing
4 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Regression test for #558: | ||
# Manually defining proto3:true on a field with default value incorrectly omits it when encoding. | ||
|
||
Import("env") | ||
env.NanopbProto("mixed") | ||
|
||
test = env.Program(["mixed.c", "mixed.pb.c", "$COMMON/pb_encode.o", "$COMMON/pb_decode.o", "$COMMON/pb_common.o"]) | ||
env.RunTest(test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <pb_encode.h> | ||
#include <pb_decode.h> | ||
|
||
#include "mixed.pb.h" | ||
#include "unittests.h" | ||
|
||
int main() | ||
{ | ||
int status = 0; | ||
pb_byte_t buf[64]; | ||
size_t msglen; | ||
|
||
{ | ||
pb_ostream_t ostream = pb_ostream_from_buffer(buf, sizeof(buf)); | ||
MixedMessage msg = MixedMessage_init_default; | ||
|
||
msg.has_proto2_value = true; | ||
msg.proto2_value = 0; | ||
msg.proto3_value = 0; | ||
|
||
if (!pb_encode(&ostream, MixedMessage_fields, &msg)) | ||
{ | ||
fprintf(stderr, "pb_encode() failed: %s\n", PB_GET_ERROR(&ostream)); | ||
return 1; | ||
} | ||
|
||
msglen = ostream.bytes_written; | ||
TEST(msglen > 0); | ||
} | ||
|
||
{ | ||
pb_istream_t istream = pb_istream_from_buffer(buf, msglen); | ||
MixedMessage msg = MixedMessage_init_default; | ||
|
||
TEST(msg.proto2_value == 100); | ||
TEST(msg.proto3_value == 200); | ||
|
||
if (!pb_decode(&istream, MixedMessage_fields, &msg)) | ||
{ | ||
fprintf(stderr, "pb_decode() failed: %s\n", PB_GET_ERROR(&istream)); | ||
return 1; | ||
} | ||
|
||
TEST(msg.proto2_value == 0); | ||
TEST(msg.proto3_value == 0); | ||
} | ||
|
||
return status; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
syntax = "proto2"; | ||
|
||
import "nanopb.proto"; | ||
|
||
message MixedMessage { | ||
optional int32 proto2_value = 1 [default = 100]; | ||
optional int32 proto3_value = 2 [default = 200, (nanopb).proto3 = true]; | ||
} |