1c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton/******************************************************************************
2c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton *
3c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton *  Copyright (C) 2014 Google, Inc.
4c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton *
5c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton *  Licensed under the Apache License, Version 2.0 (the "License");
6c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton *  you may not use this file except in compliance with the License.
7c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton *  You may obtain a copy of the License at:
8c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton *
9c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton *  http://www.apache.org/licenses/LICENSE-2.0
10c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton *
11c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton *  Unless required by applicable law or agreed to in writing, software
12c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton *  distributed under the License is distributed on an "AS IS" BASIS,
13c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton *  See the License for the specific language governing permissions and
15c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton *  limitations under the License.
16c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton *
17c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton ******************************************************************************/
18c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
19c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton#include <gtest/gtest.h>
20c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton#include "osi/test/AllocationTestHarness.h"
21c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
22c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Mantonextern "C" {
23c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton#include "btcore/include/uuid.h"
24c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton}
25c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
26c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Mantonstatic const char *UUID_EMPTY = "00000000-0000-0000-0000-000000000000";
27c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Mantonstatic const char *UUID_ONES = "11111111-1111-1111-1111-111111111111";
28c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Mantonstatic const char *UUID_SEQUENTIAL = "01234567-89ab-cdef-ABCD-EF0123456789";
29c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Mantonstatic const char *UUID_BASE = "00000000-0000-1000-8000-00805f9b34fb";
30c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
31c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Mantonclass UuidTest : public AllocationTestHarness {
32c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  protected:
33c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton    virtual void SetUp() {
34c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton    }
35c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
36c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton    virtual void TearDown() {
37c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton    }
38c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton};
39c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
40c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris MantonTEST_F(UuidTest, new_from_string) {
41c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  bt_uuid_t *uuid;
42c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
43c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid = uuid_new("incorrect length");
44c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(NULL, uuid);
45c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
46c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid = uuid_new("correct length but missing dashes --");
47c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(NULL, uuid);
48c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
49c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid = uuid_new(UUID_ONES);
50c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  ASSERT_TRUE(uuid != NULL);
51c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  for (int i = 0; i < 16; i++) {
52c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton    EXPECT_EQ(0x11, uuid->uu[i]);
53c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  }
54c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid_free(uuid);
55c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
56c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid = uuid_new(UUID_SEQUENTIAL);
57c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x01, uuid->uu[0]);
58c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x23, uuid->uu[1]);
59c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x45, uuid->uu[2]);
60c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x67, uuid->uu[3]);
61c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x89, uuid->uu[4]);
62c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0xAB, uuid->uu[5]);
63c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0xCD, uuid->uu[6]);
64c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0xEF, uuid->uu[7]);
65c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0xab, uuid->uu[8]);
66c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0xcd, uuid->uu[9]);
67c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0xef, uuid->uu[10]);
68c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x01, uuid->uu[11]);
69c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x23, uuid->uu[12]);
70c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x45, uuid->uu[13]);
71c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x67, uuid->uu[14]);
72c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x89, uuid->uu[15]);
73c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid_free(uuid);
74c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
75c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid = uuid_new(UUID_BASE);
76c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x00, uuid->uu[0]);
77c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x00, uuid->uu[1]);
78c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x00, uuid->uu[2]);
79c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x00, uuid->uu[3]);
80c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x00, uuid->uu[4]);
81c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x00, uuid->uu[5]);
82c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x10, uuid->uu[6]);
83c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x00, uuid->uu[7]);
84c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x80, uuid->uu[8]);
85c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x00, uuid->uu[9]);
86c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x00, uuid->uu[10]);
87c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x80, uuid->uu[11]);
88c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x5f, uuid->uu[12]);
89c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x9b, uuid->uu[13]);
90c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0x34, uuid->uu[14]);
91c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ(0xfb, uuid->uu[15]);
92c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid_free(uuid);
93c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton}
94c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
95c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris MantonTEST_F(UuidTest, uuid_is_empty) {
96c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  bt_uuid_t *uuid = NULL;
97c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
98c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid = uuid_new(UUID_EMPTY);
99c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  ASSERT_TRUE(uuid != NULL);
100c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_TRUE(uuid_is_empty(uuid));
101c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid_free(uuid);
102c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
103c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid = uuid_new(UUID_BASE);
104c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  ASSERT_TRUE(uuid != NULL);
105c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_FALSE(uuid_is_empty(uuid));
106c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid_free(uuid);
107c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton}
108c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
109c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris MantonTEST_F(UuidTest, uuid_128_to_16) {
110c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  bt_uuid_t *uuid = NULL;
111c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uint16_t uuid16 = 0xffff;
112c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
113c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid = uuid_new(UUID_ONES);
114c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_FALSE(uuid_128_to_16(uuid, &uuid16));
115c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid_free(uuid);
116c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ((uint16_t)0xffff, uuid16);
117c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
118c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid = uuid_new(UUID_BASE);
119c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_TRUE(uuid_128_to_16(uuid, &uuid16));
120c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid_free(uuid);
121c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_NE((uint16_t)0xffff, uuid16);
122c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ((uint16_t)0, uuid16);
123c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton}
124c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
125c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris MantonTEST_F(UuidTest, uuid_128_to_32) {
126c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  bt_uuid_t *uuid = NULL;
127c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uint32_t uuid32 = 0xffffffff;
128c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
129c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid = uuid_new(UUID_ONES);
130c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_FALSE(uuid_128_to_32(uuid, &uuid32));
131c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid_free(uuid);
132c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ((uint32_t)0xffffffff, uuid32);
133c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
134c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid = uuid_new(UUID_BASE);
135c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_TRUE(uuid_128_to_32(uuid, &uuid32));
136c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid_free(uuid);
137c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_NE((uint32_t)0xffffffff, uuid32);
138c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_EQ((uint32_t)0, uuid32);
139c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton}
140c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
141c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris MantonTEST_F(UuidTest, uuid_to_string) {
142c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  bt_uuid_t *uuid = NULL;
143c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
144c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid_string_t *uuid_string = uuid_string_new();
145c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_TRUE(uuid_string != NULL);
146c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
147c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid = uuid_new(UUID_BASE);
148c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_TRUE(uuid != NULL);
149c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid_to_string(uuid, uuid_string);
150c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid_free(uuid);
151c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
152c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_TRUE(!strcmp(UUID_BASE, uuid_string_data(uuid_string)));
153c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
154c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid = uuid_new(UUID_SEQUENTIAL);
155c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_TRUE(uuid != NULL);
156c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
157c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid_to_string(uuid, uuid_string);
158c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid_free(uuid);
159c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton
160c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  char lower_case_buf[36+1];
161c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  for (int i = 0; i < 36+1; i++) {
162c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton    lower_case_buf[i] = tolower(UUID_SEQUENTIAL[i]);
163c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  }
164c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  EXPECT_TRUE(!strcmp(lower_case_buf, uuid_string_data(uuid_string)));
165c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton  uuid_string_free(uuid_string);
166c6092cd79e757cd2c805f8bdb7e71a3cd38543bcChris Manton}
167