Julian Hall | a7e89b0 | 2020-11-23 17:33:31 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <cstring> |
| 8 | #include <common/uuid/uuid.h> |
| 9 | #include <CppUTest/TestHarness.h> |
| 10 | |
| 11 | TEST_GROUP(UuidTests) { |
| 12 | |
| 13 | }; |
| 14 | |
| 15 | TEST(UuidTests, parseValidUuidLowerCase) { |
| 16 | |
| 17 | /* A valid UUID unsing lower-case */ |
| 18 | const char *uuid_text = "d9df52d5-16a2-4bb2-9aa4-d26d3b84e8c0"; |
| 19 | CHECK(uuid_is_valid(uuid_text)); |
| 20 | |
| 21 | const uint8_t expected_bytes[] = |
| 22 | {0xd9,0xdf,0x52,0xd5, |
| 23 | 0x16,0xa2, |
| 24 | 0x4b,0xb2, |
| 25 | 0x9a,0xa4, |
| 26 | 0xd2,0x6d,0x3b,0x84,0xe8,0xc0}; |
| 27 | |
| 28 | uint8_t byte_array[UUID_OCTETS_LEN]; |
| 29 | memset(byte_array, 0, sizeof(byte_array)); |
| 30 | |
| 31 | CHECK(uuid_parse_to_octets(uuid_text, byte_array, sizeof(byte_array))); |
| 32 | CHECK(memcmp(byte_array, expected_bytes, UUID_OCTETS_LEN) == 0); |
| 33 | } |
| 34 | |
| 35 | TEST(UuidTests, parseValidUuidMixedCase) { |
| 36 | |
| 37 | /* A valid UUID unsing mixed-case */ |
| 38 | const char *uuid_text = "D9df52d5-16a2-4bB2-9aa4-d26d3b84E8c0"; |
| 39 | CHECK(uuid_is_valid(uuid_text)); |
| 40 | |
| 41 | const uint8_t expected_bytes[] = |
| 42 | {0xd9,0xdf,0x52,0xd5, |
| 43 | 0x16,0xa2, |
| 44 | 0x4b,0xb2, |
| 45 | 0x9a,0xa4, |
| 46 | 0xd2,0x6d,0x3b,0x84,0xe8,0xc0}; |
| 47 | |
| 48 | uint8_t byte_array[UUID_OCTETS_LEN]; |
| 49 | memset(byte_array, 0, sizeof(byte_array)); |
| 50 | |
| 51 | CHECK(uuid_parse_to_octets(uuid_text, byte_array, sizeof(byte_array))); |
| 52 | CHECK(memcmp(byte_array, expected_bytes, UUID_OCTETS_LEN) == 0); |
| 53 | } |
| 54 | |
| 55 | TEST(UuidTests, parseUuidInUrn) { |
| 56 | |
| 57 | /* A valid UUID embedded in a urn */ |
| 58 | const char *urn_text = "urn:uuid:6e8bc430-9c3a-11d9-9669-0800200c9a66:0"; |
| 59 | CHECK(uuid_is_valid(&urn_text[9])); |
| 60 | |
| 61 | const uint8_t expected_bytes[] = |
| 62 | {0x6e,0x8b,0xc4,0x30, |
| 63 | 0x9c,0x3a, |
| 64 | 0x11,0xd9, |
| 65 | 0x96,0x69, |
| 66 | 0x08,0x00,0x20,0x0c,0x9a,0x66}; |
| 67 | |
| 68 | uint8_t byte_array[UUID_OCTETS_LEN]; |
| 69 | memset(byte_array, 0, sizeof(byte_array)); |
| 70 | |
| 71 | CHECK(uuid_parse_to_octets(&urn_text[9], byte_array, sizeof(byte_array))); |
| 72 | CHECK(memcmp(byte_array, expected_bytes, UUID_OCTETS_LEN) == 0); |
| 73 | } |
| 74 | |
| 75 | TEST(UuidTests, parseError) { |
| 76 | |
| 77 | /* Invalid digit */ |
| 78 | const char *broken1 = "d9df52d5-16a2-4bb2-9aa4-d26d3b84e8X0"; |
| 79 | CHECK(!uuid_is_valid(broken1)); |
| 80 | |
| 81 | /* Invalid separator */ |
| 82 | const char *broken2 = "d9df52d5-16a2-4bb2-9aa4_d26d3b84e8c0"; |
| 83 | CHECK(!uuid_is_valid(broken2)); |
| 84 | |
| 85 | /* Too short */ |
| 86 | const char *broken3 = "d9df52d5-16a2-4bb2-9aa4-d26d3b84e8c"; |
| 87 | CHECK(!uuid_is_valid(broken3)); |
| 88 | |
| 89 | /* Zero length */ |
| 90 | const char *broken4 = ""; |
| 91 | CHECK(!uuid_is_valid(broken4)); |
| 92 | } |
| 93 | |
| 94 | TEST(UuidTests, parseValidUuidToReversed) { |
| 95 | |
| 96 | /* A valid UUID unsing lower-case */ |
| 97 | const char *uuid_text = "d9df52d5-16a2-4bb2-9aa4-d26d3b84e8c0"; |
| 98 | CHECK(uuid_is_valid(uuid_text)); |
| 99 | |
| 100 | /* Reversed ouput is expected to be */ |
| 101 | const uint8_t expected_bytes[] = |
| 102 | {0xd5,0x52,0xdf,0xd9, |
| 103 | 0xa2,0x16, |
| 104 | 0xb2,0x4b, |
| 105 | 0x9a,0xa4, |
| 106 | 0xd2,0x6d,0x3b,0x84,0xe8,0xc0}; |
| 107 | |
| 108 | uint8_t byte_array[UUID_OCTETS_LEN]; |
| 109 | memset(byte_array, 0, sizeof(byte_array)); |
| 110 | |
| 111 | CHECK(uuid_parse_to_octets_reversed(uuid_text, byte_array, sizeof(byte_array))); |
| 112 | CHECK(memcmp(byte_array, expected_bytes, UUID_OCTETS_LEN) == 0); |
| 113 | } |