1 // Copyright 2019 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/common/public/testing/discovery_utils.h"
6 
7 #include <sstream>
8 #include <string>
9 #include <vector>
10 
11 #include "util/stringprintf.h"
12 
13 namespace openscreen {
14 namespace cast {
15 
16 const IPAddress kAddressV4 = {192, 168, 0, 0};
17 const IPAddress kAddressV6 = {1, 2, 3, 4, 5, 6, 7, 8};
18 const IPEndpoint kEndpointV4 = {kAddressV4, kPort};
19 const IPEndpoint kEndpointV6 = {kAddressV6, kPort};
20 
CreateValidTxt()21 discovery::DnsSdTxtRecord CreateValidTxt() {
22   discovery::DnsSdTxtRecord txt;
23   txt.SetValue(kUniqueIdKey, kTestUniqueId);
24   txt.SetValue(kVersionKey, std::to_string(kTestVersion));
25   txt.SetValue(kCapabilitiesKey, kCapabilitiesStringLong);
26   txt.SetValue(kStatusKey, std::to_string(kStatus));
27   txt.SetValue(kFriendlyNameKey, kFriendlyName);
28   txt.SetValue(kModelNameKey, kModelName);
29   return txt;
30 }
31 
CompareTxtString(const discovery::DnsSdTxtRecord & txt,const std::string & key,const std::string & expected)32 void CompareTxtString(const discovery::DnsSdTxtRecord& txt,
33                       const std::string& key,
34                       const std::string& expected) {
35   ErrorOr<discovery::DnsSdTxtRecord::ValueRef> value = txt.GetValue(key);
36   ASSERT_FALSE(value.is_error())
37       << "expected value: '" << expected << "' for key: '" << key
38       << "'; got error: " << value.error();
39   const std::vector<uint8_t>& data = value.value().get();
40   std::string parsed_value = std::string(data.begin(), data.end());
41   EXPECT_EQ(parsed_value, expected) << "expected value '"
42                                     << "' for key: '" << key << "'";
43 }
44 
CompareTxtInt(const discovery::DnsSdTxtRecord & txt,const std::string & key,int expected)45 void CompareTxtInt(const discovery::DnsSdTxtRecord& txt,
46                    const std::string& key,
47                    int expected) {
48   ErrorOr<discovery::DnsSdTxtRecord::ValueRef> value = txt.GetValue(key);
49   ASSERT_FALSE(value.is_error())
50       << "key: '" << key << "'' expected: '" << expected << "'";
51   const std::vector<uint8_t>& data = value.value().get();
52   EXPECT_EQ(std::string(data.begin(), data.end()), std::to_string(expected))
53       << "for key: '" << key << "'";
54 }
55 
56 }  // namespace cast
57 }  // namespace openscreen
58