1 /******************************************************************************
2  *
3  *  Copyright 2018 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include <gtest/gtest.h>
20 
21 #include "class_of_device.h"
22 
23 static const char* test_class = "efc-d-ab";
24 static const uint8_t test_bytes[]{0xab, 0xcd, 0xef};
25 
TEST(ClassOfDeviceUnittest,test_constructor_array)26 TEST(ClassOfDeviceUnittest, test_constructor_array) {
27   ClassOfDevice cod(test_bytes);
28 
29   ASSERT_EQ(test_bytes[0], cod.cod[0]);
30   ASSERT_EQ(test_bytes[1], cod.cod[1]);
31   ASSERT_EQ(test_bytes[2], cod.cod[2]);
32 
33   std::string ret = cod.ToString();
34 
35   ASSERT_STREQ(test_class, ret.c_str());
36 }
37 
TEST(ClassOfDeviceUnittest,test_to_from_str)38 TEST(ClassOfDeviceUnittest, test_to_from_str) {
39   ClassOfDevice cod;
40   ClassOfDevice::FromString(test_class, cod);
41 
42   ASSERT_EQ(test_bytes[0], cod.cod[0]);
43   ASSERT_EQ(test_bytes[1], cod.cod[1]);
44   ASSERT_EQ(test_bytes[2], cod.cod[2]);
45 
46   std::string ret = cod.ToString();
47 
48   ASSERT_STREQ(test_class, ret.c_str());
49 }
50 
TEST(ClassOfDeviceUnittest,test_from_octets)51 TEST(ClassOfDeviceUnittest, test_from_octets) {
52   ClassOfDevice cod;
53   size_t expected_result = ClassOfDevice::kLength;
54   ASSERT_EQ(expected_result, cod.FromOctets(test_bytes));
55 
56   ASSERT_EQ(test_bytes[0], cod.cod[0]);
57   ASSERT_EQ(test_bytes[1], cod.cod[1]);
58   ASSERT_EQ(test_bytes[2], cod.cod[2]);
59 
60   std::string ret = cod.ToString();
61 
62   ASSERT_STREQ(test_class, ret.c_str());
63 }
64 
TEST(ClassOfDeviceTest,test_copy)65 TEST(ClassOfDeviceTest, test_copy) {
66   ClassOfDevice cod1;
67   ClassOfDevice cod2;
68   ClassOfDevice::FromString(test_class, cod1);
69   cod2 = cod1;
70 
71   ASSERT_EQ(cod1.cod[0], cod2.cod[0]);
72   ASSERT_EQ(cod1.cod[1], cod2.cod[1]);
73   ASSERT_EQ(cod1.cod[2], cod2.cod[2]);
74 }
75 
TEST(ClassOfDeviceTest,IsValid)76 TEST(ClassOfDeviceTest, IsValid) {
77   EXPECT_FALSE(ClassOfDevice::IsValid(""));
78   EXPECT_FALSE(ClassOfDevice::IsValid("000000"));
79   EXPECT_FALSE(ClassOfDevice::IsValid("00-00-00"));
80   EXPECT_FALSE(ClassOfDevice::IsValid("000-0-0"));
81   EXPECT_TRUE(ClassOfDevice::IsValid("000-0-00"));
82   EXPECT_TRUE(ClassOfDevice::IsValid("ABc-d-00"));
83   EXPECT_TRUE(ClassOfDevice::IsValid("aBc-D-eF"));
84 }
85 
TEST(ClassOfDeviceTest,classOfDeviceFromString)86 TEST(ClassOfDeviceTest, classOfDeviceFromString) {
87   ClassOfDevice cod;
88 
89   EXPECT_TRUE(ClassOfDevice::FromString("000-0-00", cod));
90   const ClassOfDevice result0 = {{0x00, 0x00, 0x00}};
91   EXPECT_EQ(0, memcmp(&cod, &result0, sizeof(cod)));
92 
93   EXPECT_TRUE(ClassOfDevice::FromString("ab2-1-4C", cod));
94   const ClassOfDevice result1 = {{0x4c, 0x21, 0xab}};
95   EXPECT_EQ(0, memcmp(&cod, &result1, sizeof(cod)));
96 }
97