1/****************************************************************************** 2 * 3 * Copyright (C) 2014 Google, Inc. 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#include "osi/test/AllocationTestHarness.h" 21 22extern "C" { 23#include "btcore/include/uuid.h" 24} 25 26static const char *UUID_EMPTY = "00000000-0000-0000-0000-000000000000"; 27static const char *UUID_ONES = "11111111-1111-1111-1111-111111111111"; 28static const char *UUID_SEQUENTIAL = "01234567-89ab-cdef-ABCD-EF0123456789"; 29static const char *UUID_BASE = "00000000-0000-1000-8000-00805f9b34fb"; 30 31class UuidTest : public AllocationTestHarness { 32 protected: 33 virtual void SetUp() { 34 } 35 36 virtual void TearDown() { 37 } 38}; 39 40TEST_F(UuidTest, new_from_string) { 41 bt_uuid_t *uuid; 42 43 uuid = uuid_new("incorrect length"); 44 EXPECT_EQ(NULL, uuid); 45 46 uuid = uuid_new("correct length but missing dashes --"); 47 EXPECT_EQ(NULL, uuid); 48 49 uuid = uuid_new(UUID_ONES); 50 ASSERT_TRUE(uuid != NULL); 51 for (int i = 0; i < 16; i++) { 52 EXPECT_EQ(0x11, uuid->uu[i]); 53 } 54 uuid_free(uuid); 55 56 uuid = uuid_new(UUID_SEQUENTIAL); 57 EXPECT_EQ(0x01, uuid->uu[0]); 58 EXPECT_EQ(0x23, uuid->uu[1]); 59 EXPECT_EQ(0x45, uuid->uu[2]); 60 EXPECT_EQ(0x67, uuid->uu[3]); 61 EXPECT_EQ(0x89, uuid->uu[4]); 62 EXPECT_EQ(0xAB, uuid->uu[5]); 63 EXPECT_EQ(0xCD, uuid->uu[6]); 64 EXPECT_EQ(0xEF, uuid->uu[7]); 65 EXPECT_EQ(0xab, uuid->uu[8]); 66 EXPECT_EQ(0xcd, uuid->uu[9]); 67 EXPECT_EQ(0xef, uuid->uu[10]); 68 EXPECT_EQ(0x01, uuid->uu[11]); 69 EXPECT_EQ(0x23, uuid->uu[12]); 70 EXPECT_EQ(0x45, uuid->uu[13]); 71 EXPECT_EQ(0x67, uuid->uu[14]); 72 EXPECT_EQ(0x89, uuid->uu[15]); 73 uuid_free(uuid); 74 75 uuid = uuid_new(UUID_BASE); 76 EXPECT_EQ(0x00, uuid->uu[0]); 77 EXPECT_EQ(0x00, uuid->uu[1]); 78 EXPECT_EQ(0x00, uuid->uu[2]); 79 EXPECT_EQ(0x00, uuid->uu[3]); 80 EXPECT_EQ(0x00, uuid->uu[4]); 81 EXPECT_EQ(0x00, uuid->uu[5]); 82 EXPECT_EQ(0x10, uuid->uu[6]); 83 EXPECT_EQ(0x00, uuid->uu[7]); 84 EXPECT_EQ(0x80, uuid->uu[8]); 85 EXPECT_EQ(0x00, uuid->uu[9]); 86 EXPECT_EQ(0x00, uuid->uu[10]); 87 EXPECT_EQ(0x80, uuid->uu[11]); 88 EXPECT_EQ(0x5f, uuid->uu[12]); 89 EXPECT_EQ(0x9b, uuid->uu[13]); 90 EXPECT_EQ(0x34, uuid->uu[14]); 91 EXPECT_EQ(0xfb, uuid->uu[15]); 92 uuid_free(uuid); 93} 94 95TEST_F(UuidTest, uuid_is_empty) { 96 bt_uuid_t *uuid = NULL; 97 98 uuid = uuid_new(UUID_EMPTY); 99 ASSERT_TRUE(uuid != NULL); 100 EXPECT_TRUE(uuid_is_empty(uuid)); 101 uuid_free(uuid); 102 103 uuid = uuid_new(UUID_BASE); 104 ASSERT_TRUE(uuid != NULL); 105 EXPECT_FALSE(uuid_is_empty(uuid)); 106 uuid_free(uuid); 107} 108 109TEST_F(UuidTest, uuid_128_to_16) { 110 bt_uuid_t *uuid = NULL; 111 uint16_t uuid16 = 0xffff; 112 113 uuid = uuid_new(UUID_ONES); 114 EXPECT_FALSE(uuid_128_to_16(uuid, &uuid16)); 115 uuid_free(uuid); 116 EXPECT_EQ((uint16_t)0xffff, uuid16); 117 118 uuid = uuid_new(UUID_BASE); 119 EXPECT_TRUE(uuid_128_to_16(uuid, &uuid16)); 120 uuid_free(uuid); 121 EXPECT_NE((uint16_t)0xffff, uuid16); 122 EXPECT_EQ((uint16_t)0, uuid16); 123} 124 125TEST_F(UuidTest, uuid_128_to_32) { 126 bt_uuid_t *uuid = NULL; 127 uint32_t uuid32 = 0xffffffff; 128 129 uuid = uuid_new(UUID_ONES); 130 EXPECT_FALSE(uuid_128_to_32(uuid, &uuid32)); 131 uuid_free(uuid); 132 EXPECT_EQ((uint32_t)0xffffffff, uuid32); 133 134 uuid = uuid_new(UUID_BASE); 135 EXPECT_TRUE(uuid_128_to_32(uuid, &uuid32)); 136 uuid_free(uuid); 137 EXPECT_NE((uint32_t)0xffffffff, uuid32); 138 EXPECT_EQ((uint32_t)0, uuid32); 139} 140 141TEST_F(UuidTest, uuid_to_string) { 142 bt_uuid_t *uuid = NULL; 143 144 uuid_string_t *uuid_string = uuid_string_new(); 145 EXPECT_TRUE(uuid_string != NULL); 146 147 uuid = uuid_new(UUID_BASE); 148 EXPECT_TRUE(uuid != NULL); 149 uuid_to_string(uuid, uuid_string); 150 uuid_free(uuid); 151 152 EXPECT_TRUE(!strcmp(UUID_BASE, uuid_string_data(uuid_string))); 153 154 uuid = uuid_new(UUID_SEQUENTIAL); 155 EXPECT_TRUE(uuid != NULL); 156 157 uuid_to_string(uuid, uuid_string); 158 uuid_free(uuid); 159 160 char lower_case_buf[36+1]; 161 for (int i = 0; i < 36+1; i++) { 162 lower_case_buf[i] = tolower(UUID_SEQUENTIAL[i]); 163 } 164 EXPECT_TRUE(!strcmp(lower_case_buf, uuid_string_data(uuid_string))); 165 uuid_string_free(uuid_string); 166} 167