Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antongrbin committed Feb 23, 2022
1 parent 160a2c9 commit f0e0866
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 46 deletions.
53 changes: 19 additions & 34 deletions conformance/binary_json_conformance_suite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -501,19 +501,11 @@ void BinaryAndJsonConformanceSuite::RunValidJsonTestWithProtobufInput(
void BinaryAndJsonConformanceSuite::RunValidJsonIgnoreUnknownTest(
const string& test_name, ConformanceLevel level, const string& input_json,
const string& equivalent_text_format) {
RunValidJsonIgnoreUnknownTestWithProtoVersion(
test_name, level, input_json, equivalent_text_format, /*is_proto3=*/true);
}

void BinaryAndJsonConformanceSuite::RunValidJsonIgnoreUnknownTestWithProtoVersion(
const string& test_name, ConformanceLevel level, const string& input_json,
const string& equivalent_text_format, bool is_proto3) {
std::unique_ptr<Message> prototype = NewTestMessage(is_proto3);

TestAllTypesProto3 prototype;
ConformanceRequestSetting setting(
level, conformance::JSON, conformance::PROTOBUF,
conformance::JSON_IGNORE_UNKNOWN_PARSING_TEST,
*prototype, test_name, input_json);
prototype, test_name, input_json);
RunValidInputTest(setting, equivalent_text_format);
}

Expand Down Expand Up @@ -614,26 +606,17 @@ void BinaryAndJsonConformanceSuite::RunValidJsonTestWithValidator(

void BinaryAndJsonConformanceSuite::ExpectParseFailureForJson(
const string& test_name, ConformanceLevel level, const string& input_json) {
ExpectParseFailureForJsonWithProtoVersion(test_name, level, input_json, /*is_proto3=*/true);
}

void BinaryAndJsonConformanceSuite::ExpectParseFailureForJsonWithProtoVersion(
const string& test_name, ConformanceLevel level, const string& input_json,
bool is_proto3) {
std::unique_ptr<Message> prototype = NewTestMessage(is_proto3);

TestAllTypesProto3 prototype;
// We don't expect output, but if the program erroneously accepts the protobuf
// we let it send its response as this. We must not leave it unspecified.
ConformanceRequestSetting setting(
level, conformance::JSON, conformance::JSON,
conformance::JSON_TEST,
*prototype, test_name, input_json);
prototype, test_name, input_json);
const ConformanceRequest& request = setting.GetRequest();
ConformanceResponse response;
string effective_test_name = StrCat(
setting.ConformanceLevelToString(level), ".Proto",
is_proto3 ? 3 : 2,
".JsonInput.", test_name);
setting.ConformanceLevelToString(level), ".Proto3.JsonInput.", test_name);

RunTest(effective_test_name, request, &response);
if (response.result_case() == ConformanceResponse::kParseError) {
Expand Down Expand Up @@ -1611,32 +1594,34 @@ void BinaryAndJsonConformanceSuite::RunJsonTests() {
}

void BinaryAndJsonConformanceSuite::RunJsonTestsForUnknownEnumStringValues() {
// The tests here are taking an input message which only contains an unknown enum value represented as string.
// Depending on whether unknown fields are ignored the implementation should either
// reject the message or parse it as an empty message.
// Tests the handling of unknown enum values when encoded as string labels.
// The expected behavior depends on whether unknown fields are ignored:
// * when ignored, the parser should ignore the unknown enum string value.
// * when not ignored, the parser should fail.
struct TestCase {
string enum_location;
// JSON input which will contain the unknown field.
string input_json;
// Used in the test name.
string enum_location;
};
const std::vector<TestCase> test_cases = {
{"InOptionalField", R"({ "optional_nested_enum": "UNKNOWN_ENUM_VALUE" })"},
{"InRepeatedField", R"({ "repeated_nested_enum": ["UNKNOWN_ENUM_VALUE"] })"},
{"InMap", R"({ "map_string_nested_enum": {"key": "UNKNOWN_ENUM_VALUE"} })"},
{"InMapValue", R"({ "map_string_nested_enum": {"key": "UNKNOWN_ENUM_VALUE"} })"},
};
for (const TestCase& test_case : test_cases) {
for (int is_proto3 = 0; is_proto3 < 2; ++is_proto3) {
ExpectParseFailureForJsonWithProtoVersion(
// Unknown enum string value is a parse failure when not ignoring unknown fields.
ExpectParseFailureForJson(
StrCat("RejectUnknownEnumStringValue", test_case.enum_location),
RECOMMENDED,
test_case.input_json,
is_proto3);
test_case.input_json);

RunValidJsonIgnoreUnknownTestWithProtoVersion(
// Unknown enum string value is ignored when ignoring unknown fields.
RunValidJsonIgnoreUnknownTest(
StrCat("IgnoreUnknownEnumStringValue", test_case.enum_location),
RECOMMENDED,
test_case.input_json,
"",
is_proto3);
"");
}
}
}
Expand Down
12 changes: 0 additions & 12 deletions conformance/binary_json_conformance_suite.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,10 @@ class BinaryAndJsonConformanceSuite : public ConformanceTestSuite {
const std::string& test_name, ConformanceLevel level,
const protobuf_test_messages::proto3::TestAllTypesProto3& input,
const std::string& equivalent_text_format);
// Runs in proto3 only.
void RunValidJsonIgnoreUnknownTest(const std::string& test_name,
ConformanceLevel level,
const std::string& input_json,
const std::string& equivalent_text_format);
void RunValidJsonIgnoreUnknownTestWithProtoVersion(const std::string& test_name,
ConformanceLevel level,
const std::string& input_json,
const std::string& equivalent_text_format,
bool is_proto3);
void RunValidProtobufTest(const std::string& test_name,
ConformanceLevel level,
const std::string& input_protobuf,
Expand Down Expand Up @@ -104,15 +98,9 @@ class BinaryAndJsonConformanceSuite : public ConformanceTestSuite {
const std::string& input_json,
const Validator& validator,
bool is_proto3);
// Runs in proto3 only.
void ExpectParseFailureForJson(const std::string& test_name,
ConformanceLevel level,
const std::string& input_json);
void ExpectParseFailureForJsonWithProtoVersion(const std::string& test_name,
ConformanceLevel level,
const std::string& input_json,
bool is_proto3);
// Runs for both proto2 and proto3.
void ExpectSerializeFailureForJson(const std::string& test_name,
ConformanceLevel level,
const std::string& text_format);
Expand Down

0 comments on commit f0e0866

Please sign in to comment.