1 /****************************************************************************** 2 * 3 * Copyright 2019 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 "hci/class_of_device.h" 22 23 using bluetooth::hci::ClassOfDevice; 24 25 static const char* test_class = "efc-d-ab"; 26 static const uint8_t test_bytes[]{0xab, 0xcd, 0xef}; 27 28 TEST(ClassOfDeviceUnittest, test_constructor_array) { 29 ClassOfDevice cod(test_bytes); 30 31 ASSERT_EQ(test_bytes[0], cod.cod[0]); 32 ASSERT_EQ(test_bytes[1], cod.cod[1]); 33 ASSERT_EQ(test_bytes[2], cod.cod[2]); 34 35 std::string ret = cod.ToString(); 36 37 ASSERT_STREQ(test_class, ret.c_str()); 38 } 39 40 TEST(ClassOfDeviceUnittest, test_to_from_str) { 41 ClassOfDevice cod; 42 ClassOfDevice::FromString(test_class, cod); 43 44 ASSERT_EQ(test_bytes[0], cod.cod[0]); 45 ASSERT_EQ(test_bytes[1], cod.cod[1]); 46 ASSERT_EQ(test_bytes[2], cod.cod[2]); 47 48 std::string ret = cod.ToString(); 49 50 ASSERT_STREQ(test_class, ret.c_str()); 51 } 52 53 TEST(ClassOfDeviceUnittest, test_from_octets) { 54 ClassOfDevice cod; 55 size_t expected_result = ClassOfDevice::kLength; 56 ASSERT_EQ(expected_result, cod.FromOctets(test_bytes)); 57 58 ASSERT_EQ(test_bytes[0], cod.cod[0]); 59 ASSERT_EQ(test_bytes[1], cod.cod[1]); 60 ASSERT_EQ(test_bytes[2], cod.cod[2]); 61 62 std::string ret = cod.ToString(); 63 64 ASSERT_STREQ(test_class, ret.c_str()); 65 } 66 67 TEST(ClassOfDeviceTest, test_copy) { 68 ClassOfDevice cod1; 69 ClassOfDevice cod2; 70 ClassOfDevice::FromString(test_class, cod1); 71 cod2 = cod1; 72 73 ASSERT_EQ(cod1.cod[0], cod2.cod[0]); 74 ASSERT_EQ(cod1.cod[1], cod2.cod[1]); 75 ASSERT_EQ(cod1.cod[2], cod2.cod[2]); 76 } 77 78 TEST(ClassOfDeviceTest, IsValid) { 79 EXPECT_FALSE(ClassOfDevice::IsValid("")); 80 EXPECT_FALSE(ClassOfDevice::IsValid("000000")); 81 EXPECT_FALSE(ClassOfDevice::IsValid("00-00-00")); 82 EXPECT_FALSE(ClassOfDevice::IsValid("000-0-0")); 83 EXPECT_TRUE(ClassOfDevice::IsValid("000-0-00")); 84 EXPECT_TRUE(ClassOfDevice::IsValid("ABc-d-00")); 85 EXPECT_TRUE(ClassOfDevice::IsValid("aBc-D-eF")); 86 } 87 88 TEST(ClassOfDeviceTest, classOfDeviceFromString) { 89 ClassOfDevice cod; 90 91 EXPECT_TRUE(ClassOfDevice::FromString("000-0-00", cod)); 92 const ClassOfDevice result0 = {{0x00, 0x00, 0x00}}; 93 EXPECT_EQ(0, memcmp(&cod, &result0, sizeof(cod))); 94 95 EXPECT_TRUE(ClassOfDevice::FromString("ab2-1-4C", cod)); 96 const ClassOfDevice result1 = {{0x4c, 0x21, 0xab}}; 97 EXPECT_EQ(0, memcmp(&cod, &result1, sizeof(cod))); 98 } 99