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 static const char* test_bytes = "4c68384139f574d836bcf34e9dfb01bf\0";
26 static uint8_t test_data[16] = {
27     0x4c, 0x68, 0x38, 0x41, 0x39, 0xf5, 0x74, 0xd8, 0x36, 0xbc, 0xf3, 0x4e, 0x9d, 0xfb, 0x01, 0xbf};
28 static uint8_t data[16] = {
29     0x4c, 0x87, 0x49, 0xe1, 0x2e, 0x55, 0x0f, 0x7f, 0x60, 0x8b, 0x4f, 0x96, 0xd7, 0xc5, 0xbc, 0x2a};
30 
TEST(ByteArrayTest,test_constructor_array)31 TEST(ByteArrayTest, test_constructor_array) {
32   ByteArray<16> byte_array(data);
33 
34   for (int i = 0; i < ByteArray<16>::kLength; i++) {
35     ASSERT_EQ(data[i], byte_array.bytes[i]);
36   }
37 }
38 
TEST(ByteArrayTest,test_from_str)39 TEST(ByteArrayTest, test_from_str) {
40   auto byte_array = ByteArray<16>::FromString(test_bytes);
41   ASSERT_TRUE(byte_array);
42 
43   for (int i = 0; i < ByteArray<16>::kLength; i++) {
44     ASSERT_EQ(test_data[i], byte_array->bytes[i]);
45   }
46 }
47 
TEST(ByteArrayTest,test_to_str)48 TEST(ByteArrayTest, test_to_str) {
49   ByteArray<16> byte_array = {
50       {0x4C, 0x68, 0x38, 0x41, 0x39, 0xf5, 0x74, 0xd8, 0x36, 0xbc, 0xf3, 0x4e, 0x9d, 0xfb, 0x01, 0xbf}};
51   std::string str = byte_array.ToString();
52   ASSERT_STREQ(str.c_str(), test_bytes);
53 }