xref: /aosp_15_r20/external/openscreen/cast/protocol/castv2/validation_unittest.cc (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "cast/protocol/castv2/validation.h"
6 
7 #include <numeric>
8 #include <string>
9 
10 #include "absl/strings/string_view.h"
11 #include "cast/protocol/castv2/receiver_examples/get_app_availability_data.h"
12 #include "cast/protocol/castv2/receiver_examples/get_app_availability_response_data.h"
13 #include "cast/protocol/castv2/receiver_examples/launch_data.h"
14 #include "cast/protocol/castv2/receiver_examples/stop_data.h"
15 #include "cast/protocol/castv2/receiver_schema_data.h"
16 #include "cast/protocol/castv2/streaming_examples/answer_data.h"
17 #include "cast/protocol/castv2/streaming_examples/capabilities_response_data.h"
18 #include "cast/protocol/castv2/streaming_examples/get_capabilities_data.h"
19 #include "cast/protocol/castv2/streaming_examples/get_status_data.h"
20 #include "cast/protocol/castv2/streaming_examples/offer_data.h"
21 #include "cast/protocol/castv2/streaming_examples/rpc_data.h"
22 #include "cast/protocol/castv2/streaming_examples/status_response_data.h"
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25 #include "json/value.h"
26 #include "platform/base/error.h"
27 #include "util/json/json_serialization.h"
28 #include "util/osp_logging.h"
29 #include "util/std_util.h"
30 #include "util/stringprintf.h"
31 
32 namespace openscreen {
33 namespace cast {
34 
35 namespace {
36 
37 constexpr char kEmptyJson[] = "{}";
38 
39 // Schema format string, that allows for specifying definitions,
40 // properties, and required fields.
41 constexpr char kSchemaFormat[] = R"({
42   "$schema": "http://json-schema.org/draft-07/schema#",
43   "$id": "https://something/app_schema_data.h",
44   "definitions": {
45     %s
46   },
47   "type": "object",
48   "properties": {
49     %s
50   },
51   "required": [%s]
52 })";
53 
54 // Fields used for an appId containing schema
55 constexpr char kAppIdDefinition[] = R"("app_id": {
56     "type": "string",
57     "enum": ["0F5096E8", "85CDB22F"]
58   })";
59 constexpr char kAppIdName[] = "\"appId\"";
60 constexpr char kAppIdProperty[] =
61     R"(  "appId": {"$ref": "#/definitions/app_id"})";
62 
63 // Teest documents containing an appId.
64 constexpr char kValidAppIdDocument[] = R"({ "appId": "0F5096E8" })";
65 constexpr char kInvalidAppIdDocument[] = R"({ "appId": "FooBar" })";
66 
BuildSchema(const char * definitions,const char * properties,const char * required)67 std::string BuildSchema(const char* definitions,
68                         const char* properties,
69                         const char* required) {
70   return StringPrintf(kSchemaFormat, definitions, properties, required);
71 }
72 
TestValidate(absl::string_view document,absl::string_view schema)73 bool TestValidate(absl::string_view document, absl::string_view schema) {
74   ErrorOr<Json::Value> document_root = json::Parse(document);
75   EXPECT_TRUE(document_root.is_value());
76   ErrorOr<Json::Value> schema_root = json::Parse(schema);
77   EXPECT_TRUE(schema_root.is_value());
78 
79   std::vector<Error> errors =
80       Validate(document_root.value(), schema_root.value());
81   return errors.empty();
82 }
83 
GetEmptySchema()84 const std::string& GetEmptySchema() {
85   static const std::string kEmptySchema = BuildSchema("", "", "");
86   return kEmptySchema;
87 }
88 
GetAppSchema()89 const std::string& GetAppSchema() {
90   static const std::string kAppIdSchema =
91       BuildSchema(kAppIdDefinition, kAppIdProperty, kAppIdName);
92   return kAppIdSchema;
93 }
94 
95 class StreamingValidationTest : public testing::TestWithParam<const char*> {};
96 class ReceiverValidationTest : public testing::TestWithParam<const char*> {};
97 
98 }  // namespace
99 
TEST(ValidationTest,EmptyPassesEmpty)100 TEST(ValidationTest, EmptyPassesEmpty) {
101   EXPECT_TRUE(TestValidate(kEmptyJson, kEmptyJson));
102 }
103 
TEST(ValidationTest,EmptyPassesBasicSchema)104 TEST(ValidationTest, EmptyPassesBasicSchema) {
105   EXPECT_TRUE(TestValidate(kEmptyJson, GetEmptySchema()));
106 }
107 
TEST(ValidationTest,EmptyFailsAppIdSchema)108 TEST(ValidationTest, EmptyFailsAppIdSchema) {
109   EXPECT_FALSE(TestValidate(kEmptyJson, GetAppSchema()));
110 }
111 
TEST(ValidationTest,InvalidAppIdFailsAppIdSchema)112 TEST(ValidationTest, InvalidAppIdFailsAppIdSchema) {
113   EXPECT_FALSE(TestValidate(kInvalidAppIdDocument, GetAppSchema()));
114 }
115 
TEST(ValidationTest,ValidAppIdPassesAppIdSchema)116 TEST(ValidationTest, ValidAppIdPassesAppIdSchema) {
117   EXPECT_TRUE(TestValidate(kValidAppIdDocument, GetAppSchema()));
118 }
119 
TEST(ValidationTest,InvalidAppIdPassesEmptySchema)120 TEST(ValidationTest, InvalidAppIdPassesEmptySchema) {
121   EXPECT_TRUE(TestValidate(kInvalidAppIdDocument, GetEmptySchema()));
122 }
123 
TEST(ValidationTest,ValidAppIdPassesEmptySchema)124 TEST(ValidationTest, ValidAppIdPassesEmptySchema) {
125   EXPECT_TRUE(TestValidate(kValidAppIdDocument, GetEmptySchema()));
126 }
127 
128 INSTANTIATE_TEST_SUITE_P(StreamingValidations,
129                          StreamingValidationTest,
130                          testing::Values(kAnswer,
131                                          kCapabilitiesResponse,
132                                          kGetCapabilities,
133                                          kGetStatus,
134                                          kOffer,
135                                          kRpc,
136                                          kStatusResponse));
137 
TEST_P(StreamingValidationTest,ExampleStreamingMessages)138 TEST_P(StreamingValidationTest, ExampleStreamingMessages) {
139   ErrorOr<Json::Value> message_root = json::Parse(GetParam());
140   EXPECT_TRUE(message_root.is_value());
141   EXPECT_TRUE(ValidateStreamingMessage(message_root.value()).empty());
142 }
143 
ExpectReceiverMessageValid(const char * message)144 void ExpectReceiverMessageValid(const char* message) {}
145 
146 INSTANTIATE_TEST_SUITE_P(ReceiverValidations,
147                          ReceiverValidationTest,
148                          testing::Values(kGetAppAvailability,
149                                          kGetAppAvailabilityResponse,
150                                          kLaunch,
151                                          kStop));
152 
TEST_P(ReceiverValidationTest,ExampleReceiverMessages)153 TEST_P(ReceiverValidationTest, ExampleReceiverMessages) {
154   ErrorOr<Json::Value> message_root = json::Parse(GetParam());
155   EXPECT_TRUE(message_root.is_value());
156   EXPECT_TRUE(ValidateReceiverMessage(message_root.value()).empty());
157 }
158 }  // namespace cast
159 }  // namespace openscreen
160