|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/util/uuid.h" |
| 21 | + |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +#include <gtest/gtest.h> |
| 25 | + |
| 26 | +#include "matchers.h" |
| 27 | + |
| 28 | +namespace iceberg { |
| 29 | + |
| 30 | +TEST(UUIDUtilTest, GenerateV4) { |
| 31 | + auto uuid = Uuid::GenerateV4(); |
| 32 | + // just ensure it runs and produces a value |
| 33 | + EXPECT_EQ(uuid.bytes().size(), Uuid::kLength); |
| 34 | + // Version 4 UUIDs have the version number (4) in the 7th byte |
| 35 | + EXPECT_EQ((uuid[6] >> 4) & 0x0F, 4); |
| 36 | + // Variant is in the 9th byte, the two most significant bits should be 10 |
| 37 | + EXPECT_EQ((uuid[8] >> 6) & 0x03, 0b10); |
| 38 | +} |
| 39 | + |
| 40 | +TEST(UUIDUtilTest, GenerateV7) { |
| 41 | + auto uuid = Uuid::GenerateV7(); |
| 42 | + // just ensure it runs and produces a value |
| 43 | + EXPECT_EQ(uuid.bytes().size(), 16); |
| 44 | + // Version 7 UUIDs have the version number (7) in the 7th byte |
| 45 | + EXPECT_EQ((uuid[6] >> 4) & 0x0F, 7); |
| 46 | + // Variant is in the 9th byte, the two most significant bits should be 10 |
| 47 | + EXPECT_EQ((uuid[8] >> 6) & 0x03, 0b10); |
| 48 | +} |
| 49 | + |
| 50 | +TEST(UUIDUtilTest, FromString) { |
| 51 | + std::vector<std::string> uuid_strings = { |
| 52 | + "123e4567-e89b-12d3-a456-426614174000", |
| 53 | + "550e8400-e29b-41d4-a716-446655440000", |
| 54 | + "f47ac10b-58cc-4372-a567-0e02b2c3d479", |
| 55 | + }; |
| 56 | + |
| 57 | + for (const auto& uuid_str : uuid_strings) { |
| 58 | + auto result = Uuid::FromString(uuid_str); |
| 59 | + EXPECT_THAT(result, IsOk()); |
| 60 | + auto uuid = result.value(); |
| 61 | + EXPECT_EQ(uuid.ToString(), uuid_str); |
| 62 | + } |
| 63 | + |
| 64 | + std::vector<std::pair<std::string, std::string>> uuid_string_pairs = { |
| 65 | + {"123e4567e89b12d3a456426614174000", "123e4567-e89b-12d3-a456-426614174000"}, |
| 66 | + {"550E8400E29B41D4A716446655440000", "550e8400-e29b-41d4-a716-446655440000"}, |
| 67 | + {"F47AC10B58CC4372A5670E02B2C3D479", "f47ac10b-58cc-4372-a567-0e02b2c3d479"}, |
| 68 | + }; |
| 69 | + |
| 70 | + for (const auto& [input_str, expected_str] : uuid_string_pairs) { |
| 71 | + auto result = Uuid::FromString(input_str); |
| 72 | + EXPECT_THAT(result, IsOk()); |
| 73 | + auto uuid = result.value(); |
| 74 | + EXPECT_EQ(uuid.ToString(), expected_str); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +TEST(UUIDUtilTest, FromStringInvalid) { |
| 79 | + std::vector<std::string> invalid_uuid_strings = { |
| 80 | + "123e4567-e89b-12d3-a456-42661417400", // too short |
| 81 | + "123e4567-e89b-12d3-a456-4266141740000", // too long |
| 82 | + "g23e4567-e89b-12d3-a456-426614174000", // invalid character |
| 83 | + "123e4567e89b12d3a45642661417400", // too short without dashes |
| 84 | + "123e4567e89b12d3a4564266141740000", // too long without dashes |
| 85 | + "550e8400-e29b-41d4-a716-44665544000Z", // invalid character at end |
| 86 | + "550e8400-e29b-41d4-a716-44665544000-", // invalid character at end |
| 87 | + "550e8400-e29b-41d4-a716-4466554400", // too short |
| 88 | + }; |
| 89 | + |
| 90 | + for (const auto& uuid_str : invalid_uuid_strings) { |
| 91 | + auto result = Uuid::FromString(uuid_str); |
| 92 | + EXPECT_THAT(result, IsError(ErrorKind::kInvalidArgument)); |
| 93 | + EXPECT_THAT(result, HasErrorMessage("Invalid UUID string")); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +TEST(UUIDUtilTest, FromBytes) { |
| 98 | + std::array<uint8_t, Uuid::kLength> bytes = {0x12, 0x3e, 0x45, 0x67, 0xe8, 0x9b, |
| 99 | + 0x12, 0xd3, 0xa4, 0x56, 0x42, 0x66, |
| 100 | + 0x14, 0x17, 0x40, 0x00}; |
| 101 | + auto result = Uuid::FromBytes(bytes); |
| 102 | + EXPECT_THAT(result, IsOk()); |
| 103 | + auto uuid = result.value(); |
| 104 | + EXPECT_EQ(uuid.ToString(), "123e4567-e89b-12d3-a456-426614174000"); |
| 105 | + EXPECT_EQ(uuid, Uuid(bytes)); |
| 106 | +} |
| 107 | + |
| 108 | +TEST(UUIDUtilTest, FromBytesInvalid) { |
| 109 | + std::array<uint8_t, Uuid::kLength - 1> short_bytes = {0x12, 0x3e, 0x45, 0x67, 0xe8, |
| 110 | + 0x9b, 0x12, 0xd3, 0xa4, 0x56, |
| 111 | + 0x42, 0x66, 0x14, 0x17, 0x40}; |
| 112 | + auto result = Uuid::FromBytes(short_bytes); |
| 113 | + EXPECT_THAT(result, IsError(ErrorKind::kInvalidArgument)); |
| 114 | + EXPECT_THAT(result, HasErrorMessage("UUID byte array must be exactly 16 bytes")); |
| 115 | +} |
| 116 | + |
| 117 | +} // namespace iceberg |
0 commit comments