1//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#include "shill/net/byte_string.h"
18
19#include <arpa/inet.h>
20#include <endian.h>
21
22#include <gtest/gtest.h>
23
24#include <string>
25
26using testing::Test;
27using std::string;
28
29namespace shill {
30
31namespace {
32const unsigned char kTest1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
33const char kTest1HexString[] = "00010203040506070809";
34const char kTest1HexSubstring[] = "0001020304050607";
35const char kTest1HexSubstringReordered[] = "0302010007060504";
36const unsigned char kTest2[] = { 1, 2, 3, 0xa };
37const char kTest2HexString[] = "0102030A";
38const unsigned int kTest2Uint32 = 0x0102030a;
39const unsigned char kTest3[] = { 0, 0, 0, 0 };
40const char kTest4[] = "Hello world";
41const unsigned char kTest5[] = { 1, 2, 3 };
42const unsigned char kTest6[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
43}  // namespace
44
45class ByteStringTest : public Test {
46 public:
47  bool IsCPUSameAsNetOrder() {
48    const uint32_t kTestValue = 0x12345678;
49    return htonl(kTestValue) == kTestValue;
50  }
51};
52
53TEST_F(ByteStringTest, Empty) {
54  uint32_t val;
55
56  ByteString bs1(0);
57  EXPECT_TRUE(bs1.IsEmpty());
58  EXPECT_EQ(0, bs1.GetLength());
59  EXPECT_EQ(nullptr, bs1.GetData());
60  EXPECT_FALSE(bs1.ConvertToNetUInt32(&val));
61  EXPECT_TRUE(bs1.IsZero());
62}
63
64TEST_F(ByteStringTest, NonEmpty) {
65  ByteString bs1(kTest1, sizeof(kTest1));
66  uint32_t val;
67
68  EXPECT_FALSE(bs1.IsEmpty());
69  ASSERT_NE(nullptr, bs1.GetData());
70  EXPECT_EQ(sizeof(kTest1), bs1.GetLength());
71  for (unsigned int i = 0; i < sizeof(kTest1); i++) {
72    EXPECT_EQ(bs1.GetData()[i], kTest1[i]);
73  }
74  EXPECT_FALSE(bs1.ConvertToNetUInt32(&val));
75  EXPECT_FALSE(bs1.IsZero());
76
77  // Build a ByteString (different to bs1), verify that the new ByteString
78  // looks as expected, verify that it's different to bs1.
79  ByteString bs2(kTest2, sizeof(kTest2));
80  ASSERT_NE(nullptr, bs2.GetData());
81  EXPECT_EQ(sizeof(kTest2), bs2.GetLength());
82  for (unsigned int i = 0; i < sizeof(kTest2); i++) {
83    EXPECT_EQ(bs2.GetData()[i], kTest2[i]);
84  }
85  EXPECT_FALSE(bs2.IsZero());
86  EXPECT_FALSE(bs2.Equals(bs1));
87
88  // Build _another_ ByteString (different to bs1 and bs2), verify that the
89  // new ByteString looks as expected, verify that it's different to bs1 and
90  // bs2.
91  ByteString bs3(kTest3, sizeof(kTest3));
92  ASSERT_NE(nullptr, bs3.GetData());
93  EXPECT_EQ(sizeof(kTest3), bs3.GetLength());
94  for (unsigned int i = 0; i < sizeof(kTest3); i++) {
95    EXPECT_EQ(bs3.GetData()[i], kTest3[i]);
96  }
97  EXPECT_TRUE(bs3.IsZero());
98  EXPECT_FALSE(bs2.Equals(bs1));
99  EXPECT_FALSE(bs3.Equals(bs1));
100
101  // Check two equal ByteStrings.
102  ByteString bs6(kTest1, sizeof(kTest1));
103  EXPECT_TRUE(bs6.Equals(bs1));
104}
105
106TEST_F(ByteStringTest, CopyTerminator) {
107  ByteString bs4(string(kTest4), false);
108  EXPECT_EQ(strlen(kTest4), bs4.GetLength());
109  EXPECT_EQ(0, memcmp(kTest4, bs4.GetData(), bs4.GetLength()));
110
111  ByteString bs5(string(kTest4), true);
112  EXPECT_EQ(strlen(kTest4) + 1, bs5.GetLength());
113  EXPECT_EQ(0, memcmp(kTest4, bs5.GetData(), bs5.GetLength()));
114}
115
116TEST_F(ByteStringTest, SubString) {
117  ByteString bs1(kTest1, sizeof(kTest1));
118  ByteString fragment(kTest1 + 3, 4);
119  EXPECT_TRUE(fragment.Equals(bs1.GetSubstring(3, 4)));
120  const int kMargin = sizeof(kTest1) - 3;
121  ByteString end_fragment(kTest1 + kMargin, sizeof(kTest1) - kMargin);
122  EXPECT_TRUE(end_fragment.Equals(bs1.GetSubstring(kMargin, sizeof(kTest1))));
123
124  // Verify that the ByteString correctly handles accessing a substring
125  // outside the range of the ByteString.
126  const size_t kBogusOffset = 10;
127  EXPECT_TRUE(bs1.GetSubstring(sizeof(kTest1), kBogusOffset).IsEmpty());
128}
129
130TEST_F(ByteStringTest, UInt32) {
131  ByteString bs1 = ByteString::CreateFromNetUInt32(kTest2Uint32);
132  uint32_t val;
133
134  EXPECT_EQ(4, bs1.GetLength());
135  ASSERT_NE(nullptr, bs1.GetData());
136  EXPECT_TRUE(bs1.ConvertToNetUInt32(&val));
137  EXPECT_EQ(kTest2Uint32, val);
138  EXPECT_FALSE(bs1.IsZero());
139
140  ByteString bs2(kTest2, sizeof(kTest2));
141  EXPECT_TRUE(bs1.Equals(bs2));
142  EXPECT_TRUE(bs2.ConvertToNetUInt32(&val));
143  EXPECT_EQ(kTest2Uint32, val);
144
145  ByteString bs3 = ByteString::CreateFromCPUUInt32(0x1020304);
146  EXPECT_EQ(4, bs1.GetLength());
147  ASSERT_NE(nullptr, bs3.GetData());
148  EXPECT_TRUE(bs3.ConvertToCPUUInt32(&val));
149  EXPECT_EQ(0x1020304, val);
150  EXPECT_FALSE(bs3.IsZero());
151
152#if __BYTE_ORDER == __LITTLE_ENDIAN
153  EXPECT_FALSE(bs1.Equals(bs3));
154#else
155  EXPECT_TRUE(bs1.Equals(bs3));
156#endif
157}
158
159TEST_F(ByteStringTest, Resize) {
160  ByteString bs(kTest2, sizeof(kTest2));
161
162  const size_t kSizeExtension = 10;
163  bs.Resize(sizeof(kTest2) + kSizeExtension);
164  EXPECT_EQ(sizeof(kTest2) + kSizeExtension, bs.GetLength());
165  ASSERT_NE(nullptr, bs.GetData());
166  EXPECT_EQ(0, memcmp(bs.GetData(), kTest2, sizeof(kTest2)));
167  for (size_t i = sizeof(kTest2); i < sizeof(kTest2) + kSizeExtension; ++i) {
168    EXPECT_EQ(0, bs.GetData()[i]);
169  }
170
171  const size_t kSizeReduction = 2;
172  bs.Resize(sizeof(kTest2) - kSizeReduction);
173  EXPECT_EQ(sizeof(kTest2) - kSizeReduction, bs.GetLength());
174  EXPECT_EQ(0, memcmp(bs.GetData(), kTest2, sizeof(kTest2) - kSizeReduction));
175}
176
177TEST_F(ByteStringTest, HexEncode) {
178  ByteString bs(kTest2, sizeof(kTest2));
179  EXPECT_EQ(kTest2HexString, bs.HexEncode());
180}
181
182TEST_F(ByteStringTest, BitwiseAnd) {
183  ByteString bs1(kTest1, sizeof(kTest1));
184
185  // Unequal sizes should fail and not modify bs1.
186  EXPECT_FALSE(bs1.BitwiseAnd(ByteString(kTest2, sizeof(kTest2))));
187  EXPECT_TRUE(bs1.Equals(ByteString(kTest1, sizeof(kTest1))));
188
189  const ByteString bs6(kTest6, sizeof(kTest6));
190  EXPECT_TRUE(bs1.BitwiseAnd(bs6));
191
192  const unsigned char kAndResult[] = { 0, 0, 2, 2, 4, 4, 2, 2, 0, 0 };
193  const ByteString expected_result(kAndResult, sizeof(kAndResult));
194  EXPECT_TRUE(bs1.Equals(expected_result));
195}
196
197TEST_F(ByteStringTest, BitwiseOr) {
198  ByteString bs1(kTest1, sizeof(kTest1));
199
200  // Unequal sizes should fail and not modify bs1.
201  EXPECT_FALSE(bs1.BitwiseOr(ByteString(kTest2, sizeof(kTest2))));
202  EXPECT_TRUE(bs1.Equals(ByteString(kTest1, sizeof(kTest1))));
203
204  const ByteString bs6(kTest6, sizeof(kTest6));
205  EXPECT_TRUE(bs1.BitwiseOr(bs6));
206
207  const unsigned char kOrResult[] = { 9, 9, 7, 7, 5, 5, 7, 7, 9, 9 };
208  const ByteString expected_result(kOrResult, sizeof(kOrResult));
209  EXPECT_TRUE(bs1.Equals(expected_result));
210}
211
212TEST_F(ByteStringTest, BitwiseInvert) {
213  ByteString bs(kTest1, sizeof(kTest1));
214  ByteString invert;
215  for (size_t i = 0; i < sizeof(kTest1); i++) {
216    unsigned char val = kTest1[i] ^ 0xff;
217    invert.Append(ByteString(&val, 1));
218  }
219  bs.BitwiseInvert();
220  EXPECT_TRUE(bs.Equals(invert));
221}
222
223TEST_F(ByteStringTest, CreateFromHexString) {
224  ByteString bs = ByteString::CreateFromHexString("");
225  EXPECT_TRUE(bs.IsEmpty());
226
227  ByteString bs1 = ByteString::CreateFromHexString("0");
228  EXPECT_TRUE(bs1.IsEmpty());
229
230  ByteString bs2 = ByteString::CreateFromHexString("0y");
231  EXPECT_TRUE(bs2.IsEmpty());
232
233  ByteString bs3 = ByteString::CreateFromHexString("ab");
234  EXPECT_EQ(1, bs3.GetLength());
235  EXPECT_EQ(0xab, bs3.GetData()[0]);
236
237  ByteString bs4 = ByteString::CreateFromHexString(kTest1HexString);
238  EXPECT_EQ(kTest1HexString, bs4.HexEncode());
239}
240
241TEST_F(ByteStringTest, ConvertFromNetToCPUUInt32Array) {
242  ByteString bs1;
243  EXPECT_TRUE(bs1.ConvertFromNetToCPUUInt32Array());
244  EXPECT_TRUE(bs1.IsEmpty());
245
246  // Conversion should fail when the length of ByteString is not a
247  // multiple of 4.
248  ByteString bs2(kTest1, sizeof(kTest1));
249  EXPECT_EQ(kTest1HexString, bs2.HexEncode());
250  EXPECT_FALSE(bs2.ConvertFromNetToCPUUInt32Array());
251  EXPECT_EQ(kTest1HexString, bs2.HexEncode());
252
253  // Conversion should succeed when the length of ByteString is a
254  // multiple of 4.
255  bs2.Resize(8);
256  EXPECT_EQ(kTest1HexSubstring, bs2.HexEncode());
257  EXPECT_TRUE(bs2.ConvertFromNetToCPUUInt32Array());
258  if (IsCPUSameAsNetOrder()) {
259    EXPECT_EQ(kTest1HexSubstring, bs2.HexEncode());
260  } else {
261    EXPECT_EQ(kTest1HexSubstringReordered, bs2.HexEncode());
262  }
263}
264
265TEST_F(ByteStringTest, ConvertFromCPUToNetUInt32Array) {
266  ByteString bs1;
267  EXPECT_TRUE(bs1.ConvertFromCPUToNetUInt32Array());
268  EXPECT_TRUE(bs1.IsEmpty());
269
270  // Conversion should fail when the length of ByteString is not a
271  // multiple of 4.
272  ByteString bs2(kTest1, sizeof(kTest1));
273  EXPECT_EQ(kTest1HexString, bs2.HexEncode());
274  EXPECT_FALSE(bs2.ConvertFromCPUToNetUInt32Array());
275  EXPECT_EQ(kTest1HexString, bs2.HexEncode());
276
277  // Conversion should succeed when the length of ByteString is a
278  // multiple of 4.
279  bs2.Resize(8);
280  EXPECT_EQ(kTest1HexSubstring, bs2.HexEncode());
281  EXPECT_TRUE(bs2.ConvertFromCPUToNetUInt32Array());
282  if (IsCPUSameAsNetOrder()) {
283    EXPECT_EQ(kTest1HexSubstring, bs2.HexEncode());
284  } else {
285    EXPECT_EQ(kTest1HexSubstringReordered, bs2.HexEncode());
286  }
287}
288
289TEST_F(ByteStringTest, LessThan) {
290  ByteString bs1(kTest1, sizeof(kTest1));
291  ByteString bs2(kTest2, sizeof(kTest2));
292  ByteString bs3(kTest3, sizeof(kTest3));
293  ByteString bs5(kTest5, sizeof(kTest5));
294
295  // bs2 is shorter, but the first four bytes of bs1 are less than those in bs2.
296  EXPECT_TRUE(ByteString::IsLessThan(bs1, bs2));
297
298  // bs2 and bs3 are the same length, but bs3 has less byte values.
299  EXPECT_TRUE(ByteString::IsLessThan(bs3, bs2));
300
301  // bs3 is shorter than bs1 and the first four bytes of bs3 are less than
302  // the first four bytes of bs1.
303  EXPECT_TRUE(ByteString::IsLessThan(bs3, bs1));
304
305  // The first three bytes of bs5 are equal to the first three bytes of bs2,
306  // but bs5 is shorter than bs2.
307  EXPECT_TRUE(ByteString::IsLessThan(bs5, bs2));
308
309  // A Bytestring is not less than another identical one.
310  EXPECT_FALSE(ByteString::IsLessThan(bs5, bs5));
311}
312
313}  // namespace shill
314