Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 1 | #include "aos/uuid.h" |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 2 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 3 | #include "absl/log/check.h" |
| 4 | #include "absl/log/log.h" |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 5 | #include "gtest/gtest.h" |
| 6 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 7 | namespace aos::testing { |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 8 | |
| 9 | // Tests that random UUIDs are actually random, and we can convert them to a |
| 10 | // string. Not very exhaustive, but it is a good smoke test. |
| 11 | TEST(UUIDTest, GetOne) { |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 12 | LOG(INFO) << UUID::Random(); |
| 13 | |
| 14 | UUID r = UUID::Random(); |
| 15 | |
| 16 | std::stringstream ss; |
| 17 | ss << r; |
| 18 | |
| 19 | UUID r2 = UUID::FromString(ss.str()); |
| 20 | EXPECT_EQ(r2, r); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 21 | |
| 22 | EXPECT_NE(UUID::Random(), UUID::Random()); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 23 | EXPECT_NE(UUID::Random(), UUID::Zero()); |
| 24 | EXPECT_EQ(UUID::Zero(), UUID::Zero()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 27 | // Tests that converting to and from various formats produces the same UUID. |
Alexei Strots | 72060d6 | 2022-10-10 19:23:53 -0700 | [diff] [blame] | 28 | TEST(UUIDTest, FromStringOrSpan) { |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 29 | std::string_view str = "4b88ab00-556a-455b-a395-17d1a0c6f906"; |
| 30 | std::array<uint8_t, UUID::kDataSize> data = { |
| 31 | 0x4b, 0x88, 0xab, 0x00, 0x55, 0x6a, 0x45, 0x5b, |
| 32 | 0xa3, 0x95, 0x17, 0xd1, 0xa0, 0xc6, 0xf9, 0x06}; |
| 33 | |
Alexei Strots | 72060d6 | 2022-10-10 19:23:53 -0700 | [diff] [blame] | 34 | const UUID u_str = UUID::FromString(str); |
| 35 | const UUID u_span = UUID::FromSpan({data.data(), data.size()}); |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 36 | |
Alexei Strots | 72060d6 | 2022-10-10 19:23:53 -0700 | [diff] [blame] | 37 | EXPECT_EQ(u_str.span(), absl::Span<uint8_t>(data.data(), data.size())); |
| 38 | EXPECT_EQ(u_span.span(), absl::Span<uint8_t>(data.data(), data.size())); |
| 39 | EXPECT_EQ(u_str.ToString(), str); |
| 40 | EXPECT_EQ(u_span.ToString(), str); |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 41 | |
| 42 | flatbuffers::FlatBufferBuilder fbb; |
| 43 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data_offset = |
| 44 | fbb.CreateVector(data.data(), data.size()); |
| 45 | |
| 46 | const flatbuffers::Vector<uint8_t> *data_vector = |
| 47 | flatbuffers::GetTemporaryPointer(fbb, data_offset); |
| 48 | |
| 49 | const UUID u2 = UUID::FromVector(data_vector); |
| 50 | |
Alexei Strots | 72060d6 | 2022-10-10 19:23:53 -0700 | [diff] [blame] | 51 | EXPECT_EQ(u_str, u2); |
| 52 | EXPECT_EQ(u_span, u2); |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 55 | } // namespace aos::testing |