1 /*
2  * Copyright 2020 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 "common/byte_array.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include "os/log.h"
22 
23 using bluetooth::common::ByteArray;
24 
25 namespace {
26 const char* byte_string16 = "4c68384139f574d836bcf34e9dfb01bf\0";
27 const uint8_t byte_data16[16] = {
28     0x4c, 0x68, 0x38, 0x41, 0x39, 0xf5, 0x74, 0xd8, 0x36, 0xbc, 0xf3, 0x4e, 0x9d, 0xfb, 0x01, 0xbf};
29 const char* byte_string21 = "4c68384139f574d836bcf34e9dfb01bf0011223344\0";
30 const uint8_t byte_data21[21] = {0x4c, 0x68, 0x38, 0x41, 0x39, 0xf5, 0x74, 0xd8, 0x36, 0xbc, 0xf3,
31                                  0x4e, 0x9d, 0xfb, 0x01, 0xbf, 0x00, 0x11, 0x22, 0x33, 0x44};
32 const char* byte_string23 = "4c68384139f574d836bcf34e9dfb01bf00112233445566\0";
33 const uint8_t byte_data23[23] = {0x4c, 0x68, 0x38, 0x41, 0x39, 0xf5, 0x74, 0xd8, 0x36, 0xbc, 0xf3, 0x4e,
34                                  0x9d, 0xfb, 0x01, 0xbf, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
35 const char* byte_string28 = "4c68384139f574d836bcf34e9dfb01bf00112233445566778899aabb\0";
36 const uint8_t byte_data28[28] = {0x4c, 0x68, 0x38, 0x41, 0x39, 0xf5, 0x74, 0xd8, 0x36, 0xbc, 0xf3, 0x4e, 0x9d, 0xfb,
37                                  0x01, 0xbf, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb};
38 
39 template <typename T, size_t N>
simple_constructor_test(const T (& data)[N])40 void simple_constructor_test(const T (&data)[N]) {
41   ByteArray<N> byte_array(data);
42   for (size_t i = 0; i < ByteArray<N>::kLength; i++) {
43     ASSERT_EQ(data[i], byte_array.bytes[i]);
44   }
45 }
46 
47 template <typename T, size_t N>
simple_const_constructor_test(const T (& data)[N])48 void simple_const_constructor_test(const T (&data)[N]) {
49   const ByteArray<N> byte_array(data);
50   for (size_t i = 0; i < ByteArray<N>::kLength; i++) {
51     ASSERT_EQ(data[i], byte_array.data()[i]);
52   }
53 }
54 
55 template <typename T, size_t N>
simple_array_constructor_test(const T (& data)[N])56 void simple_array_constructor_test(const T (&data)[N]) {
57   std::array<uint8_t, N> array_of_bytes;
58   std::copy(data, data + N, std::begin(array_of_bytes));
59 
60   ByteArray<N> byte_array(array_of_bytes);
61   for (size_t i = 0; i < ByteArray<N>::kLength; i++) {
62     ASSERT_EQ(data[i], byte_array.data()[i]);
63   }
64 }
65 
66 template <typename T, size_t N>
simple_from_string_test(const char * byte_string,const T (& data)[N])67 void simple_from_string_test(const char* byte_string, const T (&data)[N]) {
68   auto byte_array = ByteArray<N>::FromString(byte_string);
69   ASSERT_TRUE(byte_array);
70 
71   for (size_t i = 0; i < ByteArray<N>::kLength; i++) {
72     ASSERT_EQ(data[i], byte_array->bytes[i]);
73   }
74 }
75 
76 template <typename T, size_t N>
simple_to_string_test(const char * byte_string,const T (& data)[N])77 void simple_to_string_test(const char* byte_string, const T (&data)[N]) {
78   const ByteArray<N> byte_array(data);
79   std::string str = byte_array.ToString();
80   ASSERT_STREQ(str.c_str(), byte_string);
81 }
82 
83 template <typename T, size_t N>
simple_from_legacy_string_test(const char * byte_string,const T (& data)[N])84 void simple_from_legacy_string_test(const char* byte_string, const T (&data)[N]) {
85   auto byte_array = ByteArray<N>::FromLegacyConfigString(byte_string);
86   ASSERT_TRUE(byte_array);
87 
88   for (size_t i = 0; i < ByteArray<N>::kLength; i++) {
89     ASSERT_EQ(data[i], byte_array->bytes[i]);
90   }
91 }
92 
93 template <typename T, size_t N>
simple_to_legacy_string_test(const char * byte_string,const T (& data)[N])94 void simple_to_legacy_string_test(const char* byte_string, const T (&data)[N]) {
95   const ByteArray<N> byte_array(data);
96   std::string str = byte_array.ToLegacyConfigString();
97   ASSERT_STREQ(str.c_str(), byte_string);
98 }
99 
100 }  // namespace
101 
TEST(ByteArrayTest,test_simple_constructor)102 TEST(ByteArrayTest, test_simple_constructor) {
103   simple_constructor_test<const uint8_t, 16>(byte_data16);
104   simple_constructor_test<const uint8_t, 21>(byte_data21);
105   simple_constructor_test<const uint8_t, 23>(byte_data23);
106   simple_constructor_test<const uint8_t, 28>(byte_data28);
107 }
108 
TEST(ByteArrayTest,test_simple_const_constructor)109 TEST(ByteArrayTest, test_simple_const_constructor) {
110   simple_const_constructor_test<const uint8_t, 16>(byte_data16);
111   simple_const_constructor_test<const uint8_t, 21>(byte_data21);
112   simple_const_constructor_test<const uint8_t, 23>(byte_data23);
113   simple_const_constructor_test<const uint8_t, 28>(byte_data28);
114 }
115 
TEST(ByteArrayTest,test_simple_array_constructor)116 TEST(ByteArrayTest, test_simple_array_constructor) {
117   simple_array_constructor_test<const uint8_t, 16>(byte_data16);
118   simple_array_constructor_test<const uint8_t, 21>(byte_data21);
119   simple_array_constructor_test<const uint8_t, 23>(byte_data23);
120   simple_array_constructor_test<const uint8_t, 28>(byte_data28);
121 }
122 
TEST(ByteArrayTest,test_from_str)123 TEST(ByteArrayTest, test_from_str) {
124   simple_from_string_test<const uint8_t, 16>(byte_string16, byte_data16);
125   simple_from_string_test<const uint8_t, 21>(byte_string21, byte_data21);
126   simple_from_string_test<const uint8_t, 23>(byte_string23, byte_data23);
127   simple_from_string_test<const uint8_t, 28>(byte_string28, byte_data28);
128 }
129 
TEST(ByteArrayTest,test_from_legacy_str)130 TEST(ByteArrayTest, test_from_legacy_str) {
131   simple_from_legacy_string_test<const uint8_t, 16>(byte_string16, byte_data16);
132   simple_from_legacy_string_test<const uint8_t, 21>(byte_string21, byte_data21);
133   simple_from_legacy_string_test<const uint8_t, 23>(byte_string23, byte_data23);
134   simple_from_legacy_string_test<const uint8_t, 28>(byte_string28, byte_data28);
135 }
136 
TEST(ByteArrayTest,test_to_str)137 TEST(ByteArrayTest, test_to_str) {
138   simple_to_string_test<const uint8_t, 16>(byte_string16, byte_data16);
139   simple_to_string_test<const uint8_t, 21>(byte_string21, byte_data21);
140   simple_to_string_test<const uint8_t, 23>(byte_string23, byte_data23);
141   simple_to_string_test<const uint8_t, 28>(byte_string28, byte_data28);
142 }
143 
TEST(ByteArrayTest,test_to_legacy_str)144 TEST(ByteArrayTest, test_to_legacy_str) {
145   simple_to_legacy_string_test<const uint8_t, 16>(byte_string16, byte_data16);
146   simple_to_legacy_string_test<const uint8_t, 21>(byte_string21, byte_data21);
147   simple_to_legacy_string_test<const uint8_t, 23>(byte_string23, byte_data23);
148   simple_to_legacy_string_test<const uint8_t, 28>(byte_string28, byte_data28);
149 }
150