1*3f982cf4SFabien Sanglard // Copyright 2020 The Chromium Authors. All rights reserved.
2*3f982cf4SFabien Sanglard // Use of this source code is governed by a BSD-style license that can be
3*3f982cf4SFabien Sanglard // found in the LICENSE file.
4*3f982cf4SFabien Sanglard
5*3f982cf4SFabien Sanglard #include "cast/sender/testing/test_helpers.h"
6*3f982cf4SFabien Sanglard
7*3f982cf4SFabien Sanglard #include "cast/common/channel/message_util.h"
8*3f982cf4SFabien Sanglard #include "cast/receiver/channel/message_util.h"
9*3f982cf4SFabien Sanglard #include "cast/sender/channel/message_util.h"
10*3f982cf4SFabien Sanglard #include "gtest/gtest.h"
11*3f982cf4SFabien Sanglard #include "util/json/json_serialization.h"
12*3f982cf4SFabien Sanglard #include "util/json/json_value.h"
13*3f982cf4SFabien Sanglard #include "util/osp_logging.h"
14*3f982cf4SFabien Sanglard
15*3f982cf4SFabien Sanglard namespace openscreen {
16*3f982cf4SFabien Sanglard namespace cast {
17*3f982cf4SFabien Sanglard
18*3f982cf4SFabien Sanglard using ::cast::channel::CastMessage;
19*3f982cf4SFabien Sanglard
VerifyAppAvailabilityRequest(const CastMessage & message,const std::string & expected_app_id,int * request_id_out,std::string * sender_id_out)20*3f982cf4SFabien Sanglard void VerifyAppAvailabilityRequest(const CastMessage& message,
21*3f982cf4SFabien Sanglard const std::string& expected_app_id,
22*3f982cf4SFabien Sanglard int* request_id_out,
23*3f982cf4SFabien Sanglard std::string* sender_id_out) {
24*3f982cf4SFabien Sanglard std::string app_id_out;
25*3f982cf4SFabien Sanglard VerifyAppAvailabilityRequest(message, &app_id_out, request_id_out,
26*3f982cf4SFabien Sanglard sender_id_out);
27*3f982cf4SFabien Sanglard EXPECT_EQ(app_id_out, expected_app_id);
28*3f982cf4SFabien Sanglard }
29*3f982cf4SFabien Sanglard
VerifyAppAvailabilityRequest(const CastMessage & message,std::string * app_id_out,int * request_id_out,std::string * sender_id_out)30*3f982cf4SFabien Sanglard void VerifyAppAvailabilityRequest(const CastMessage& message,
31*3f982cf4SFabien Sanglard std::string* app_id_out,
32*3f982cf4SFabien Sanglard int* request_id_out,
33*3f982cf4SFabien Sanglard std::string* sender_id_out) {
34*3f982cf4SFabien Sanglard EXPECT_EQ(message.namespace_(), kReceiverNamespace);
35*3f982cf4SFabien Sanglard EXPECT_EQ(message.destination_id(), kPlatformReceiverId);
36*3f982cf4SFabien Sanglard EXPECT_EQ(message.payload_type(),
37*3f982cf4SFabien Sanglard ::cast::channel::CastMessage_PayloadType_STRING);
38*3f982cf4SFabien Sanglard EXPECT_NE(message.source_id(), kPlatformSenderId);
39*3f982cf4SFabien Sanglard *sender_id_out = message.source_id();
40*3f982cf4SFabien Sanglard
41*3f982cf4SFabien Sanglard ErrorOr<Json::Value> maybe_value = json::Parse(message.payload_utf8());
42*3f982cf4SFabien Sanglard ASSERT_TRUE(maybe_value);
43*3f982cf4SFabien Sanglard Json::Value& value = maybe_value.value();
44*3f982cf4SFabien Sanglard
45*3f982cf4SFabien Sanglard absl::optional<absl::string_view> maybe_type =
46*3f982cf4SFabien Sanglard MaybeGetString(value, JSON_EXPAND_FIND_CONSTANT_ARGS(kMessageKeyType));
47*3f982cf4SFabien Sanglard ASSERT_TRUE(maybe_type);
48*3f982cf4SFabien Sanglard EXPECT_EQ(maybe_type.value(),
49*3f982cf4SFabien Sanglard CastMessageTypeToString(CastMessageType::kGetAppAvailability));
50*3f982cf4SFabien Sanglard
51*3f982cf4SFabien Sanglard absl::optional<int> maybe_id =
52*3f982cf4SFabien Sanglard MaybeGetInt(value, JSON_EXPAND_FIND_CONSTANT_ARGS(kMessageKeyRequestId));
53*3f982cf4SFabien Sanglard ASSERT_TRUE(maybe_id);
54*3f982cf4SFabien Sanglard *request_id_out = maybe_id.value();
55*3f982cf4SFabien Sanglard
56*3f982cf4SFabien Sanglard const Json::Value* maybe_app_ids =
57*3f982cf4SFabien Sanglard value.find(JSON_EXPAND_FIND_CONSTANT_ARGS(kMessageKeyAppId));
58*3f982cf4SFabien Sanglard ASSERT_TRUE(maybe_app_ids);
59*3f982cf4SFabien Sanglard ASSERT_TRUE(maybe_app_ids->isArray());
60*3f982cf4SFabien Sanglard ASSERT_EQ(maybe_app_ids->size(), 1u);
61*3f982cf4SFabien Sanglard Json::Value app_id_value = maybe_app_ids->get(0u, Json::Value(""));
62*3f982cf4SFabien Sanglard absl::optional<absl::string_view> maybe_app_id = MaybeGetString(app_id_value);
63*3f982cf4SFabien Sanglard ASSERT_TRUE(maybe_app_id);
64*3f982cf4SFabien Sanglard *app_id_out =
65*3f982cf4SFabien Sanglard std::string(maybe_app_id.value().begin(), maybe_app_id.value().end());
66*3f982cf4SFabien Sanglard }
67*3f982cf4SFabien Sanglard
CreateAppAvailableResponseChecked(int request_id,const std::string & sender_id,const std::string & app_id)68*3f982cf4SFabien Sanglard CastMessage CreateAppAvailableResponseChecked(int request_id,
69*3f982cf4SFabien Sanglard const std::string& sender_id,
70*3f982cf4SFabien Sanglard const std::string& app_id) {
71*3f982cf4SFabien Sanglard ErrorOr<CastMessage> message =
72*3f982cf4SFabien Sanglard CreateAppAvailableResponse(request_id, sender_id, app_id);
73*3f982cf4SFabien Sanglard OSP_CHECK(message);
74*3f982cf4SFabien Sanglard return std::move(message.value());
75*3f982cf4SFabien Sanglard }
76*3f982cf4SFabien Sanglard
CreateAppUnavailableResponseChecked(int request_id,const std::string & sender_id,const std::string & app_id)77*3f982cf4SFabien Sanglard CastMessage CreateAppUnavailableResponseChecked(int request_id,
78*3f982cf4SFabien Sanglard const std::string& sender_id,
79*3f982cf4SFabien Sanglard const std::string& app_id) {
80*3f982cf4SFabien Sanglard ErrorOr<CastMessage> message =
81*3f982cf4SFabien Sanglard CreateAppUnavailableResponse(request_id, sender_id, app_id);
82*3f982cf4SFabien Sanglard OSP_CHECK(message);
83*3f982cf4SFabien Sanglard return std::move(message.value());
84*3f982cf4SFabien Sanglard }
85*3f982cf4SFabien Sanglard
86*3f982cf4SFabien Sanglard } // namespace cast
87*3f982cf4SFabien Sanglard } // namespace openscreen
88