• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "discovery/dnssd/public/dns_sd_txt_record.h"
6 
7 #include "gmock/gmock.h"
8 #include "gtest/gtest.h"
9 
10 namespace openscreen {
11 namespace discovery {
12 namespace dnssd {
13 
TEST(TxtRecordTest,TestCaseInsensitivity)14 TEST(TxtRecordTest, TestCaseInsensitivity) {
15   DnsSdTxtRecord txt;
16   std::vector<uint8_t> data{'a', 'b', 'c'};
17   EXPECT_TRUE(txt.SetValue("key", data).ok());
18   EXPECT_TRUE(txt.GetValue("KEY").is_value());
19 
20   EXPECT_TRUE(txt.SetFlag("KEY2", true).ok());
21   ASSERT_TRUE(txt.GetFlag("key2").is_value());
22   EXPECT_TRUE(txt.GetFlag("key2").value());
23 }
24 
TEST(TxtRecordTest,TestEmptyValue)25 TEST(TxtRecordTest, TestEmptyValue) {
26   DnsSdTxtRecord txt;
27   EXPECT_TRUE(txt.SetValue("key", std::vector<uint8_t>{}).ok());
28   ASSERT_TRUE(txt.GetValue("key").is_value());
29   EXPECT_EQ(txt.GetValue("key").value().get().size(), size_t{0});
30 
31   EXPECT_TRUE(txt.SetValue("key2", "").ok());
32   ASSERT_TRUE(txt.GetValue("key2").is_value());
33   EXPECT_EQ(txt.GetValue("key2").value().get().size(), size_t{0});
34 }
35 
TEST(TxtRecordTest,TestSetAndGetValue)36 TEST(TxtRecordTest, TestSetAndGetValue) {
37   DnsSdTxtRecord txt;
38   std::vector<uint8_t> data{'a', 'b', 'c'};
39   EXPECT_TRUE(txt.SetValue("key", data).ok());
40   ASSERT_TRUE(txt.GetValue("key").is_value());
41   const std::vector<uint8_t>& value = txt.GetValue("key").value();
42   ASSERT_EQ(value.size(), size_t{3});
43   EXPECT_EQ(value[0], 'a');
44   EXPECT_EQ(value[1], 'b');
45   EXPECT_EQ(value[2], 'c');
46 
47   std::vector<uint8_t> data2{'a', 'b'};
48   EXPECT_TRUE(txt.SetValue("key", data2).ok());
49   ASSERT_TRUE(txt.GetValue("key").is_value());
50   const std::vector<uint8_t>& value2 = txt.GetValue("key").value();
51   EXPECT_EQ(value2.size(), size_t{2});
52   EXPECT_EQ(value2[0], 'a');
53   EXPECT_EQ(value2[1], 'b');
54 
55   EXPECT_TRUE(txt.SetValue("key", "abc").ok());
56   ASSERT_TRUE(txt.GetValue("key").is_value());
57   const std::vector<uint8_t>& value3 = txt.GetValue("key").value();
58   ASSERT_EQ(value.size(), size_t{3});
59   EXPECT_EQ(value3[0], 'a');
60   EXPECT_EQ(value3[1], 'b');
61   EXPECT_EQ(value3[2], 'c');
62 
63   EXPECT_TRUE(txt.SetValue("key", "ab").ok());
64   ASSERT_TRUE(txt.GetValue("key").is_value());
65   const std::vector<uint8_t>& value4 = txt.GetValue("key").value();
66   EXPECT_EQ(value4.size(), size_t{2});
67   EXPECT_EQ(value4[0], 'a');
68   EXPECT_EQ(value4[1], 'b');
69 }
70 
TEST(TxtRecordTest,TestClearValue)71 TEST(TxtRecordTest, TestClearValue) {
72   DnsSdTxtRecord txt;
73   std::vector<uint8_t> data{'a', 'b', 'c'};
74   EXPECT_TRUE(txt.SetValue("key", data).ok());
75   txt.ClearValue("key");
76 
77   EXPECT_FALSE(txt.GetValue("key").is_value());
78 }
79 
TEST(TxtRecordTest,TestSetAndGetFlag)80 TEST(TxtRecordTest, TestSetAndGetFlag) {
81   DnsSdTxtRecord txt;
82   EXPECT_TRUE(txt.SetFlag("key", true).ok());
83   ASSERT_TRUE(txt.GetFlag("key").is_value());
84   EXPECT_TRUE(txt.GetFlag("key").value());
85 
86   EXPECT_TRUE(txt.SetFlag("key", false).ok());
87   ASSERT_TRUE(txt.GetFlag("key").is_value());
88   EXPECT_FALSE(txt.GetFlag("key").value());
89 }
90 
TEST(TxtRecordTest,TestClearFlag)91 TEST(TxtRecordTest, TestClearFlag) {
92   DnsSdTxtRecord txt;
93   EXPECT_TRUE(txt.SetFlag("key", true).ok());
94   txt.ClearFlag("key");
95 
96   ASSERT_TRUE(txt.GetFlag("key").is_value());
97   EXPECT_FALSE(txt.GetFlag("key").value());
98 }
99 
TEST(TxtRecordTest,TestGettingWrongRecordTypeFails)100 TEST(TxtRecordTest, TestGettingWrongRecordTypeFails) {
101   DnsSdTxtRecord txt;
102   std::vector<uint8_t> data{'a', 'b', 'c'};
103   EXPECT_TRUE(txt.SetValue("key", data).ok());
104   EXPECT_TRUE(txt.SetFlag("key2", true).ok());
105   EXPECT_FALSE(txt.GetValue("key2").is_value());
106 }
107 
TEST(TxtRecordTest,TestClearWrongRecordTypeFails)108 TEST(TxtRecordTest, TestClearWrongRecordTypeFails) {
109   DnsSdTxtRecord txt;
110   std::vector<uint8_t> data{'a', 'b', 'c'};
111   EXPECT_TRUE(txt.SetValue("key", data).ok());
112   EXPECT_TRUE(txt.SetFlag("key2", true).ok());
113 }
114 
TEST(TxtRecordTest,TestGetDataWorks)115 TEST(TxtRecordTest, TestGetDataWorks) {
116   DnsSdTxtRecord txt;
117   std::vector<uint8_t> data{'a', 'b', 'c'};
118   EXPECT_TRUE(txt.SetValue("key", data).ok());
119   EXPECT_TRUE(txt.SetFlag("bool", true).ok());
120   std::vector<std::vector<uint8_t>> results = txt.GetData();
121   ASSERT_EQ(results.size(), size_t{2});
122   bool seen_flag = false;
123   bool seen_kv_pair = false;
124   for (const std::vector<uint8_t>& entry : results) {
125     std::string flag = "bool";
126     std::string kv_pair = "key=abc";
127     for (size_t i = 0; i < flag.size(); i++) {
128       if (entry[i] != flag[i]) {
129         break;
130       }
131       if (i == flag.size() - 1) {
132         seen_flag = true;
133       }
134     }
135     for (size_t i = 0; i < kv_pair.size(); i++) {
136       if (entry[i] != kv_pair[i]) {
137         break;
138       }
139       if (i == kv_pair.size() - 1) {
140         seen_kv_pair = true;
141       }
142     }
143   }
144 
145   EXPECT_TRUE(seen_flag);
146   EXPECT_TRUE(seen_kv_pair);
147 }
148 
149 }  // namespace dnssd
150 }  // namespace discovery
151 }  // namespace openscreen
152