blob: 6b568426b77d4c7aa7a43dc9056210f6ae562e04 [file] [log] [blame]
Jakub Czapigaf5c00212021-03-31 12:41:29 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <tests/test.h>
4#include <uuid.h>
5
6/* This test checks if parse_uuid() correctly parses UUID strings with correct format.
7 lib/uuid does not check UUID version nor variant.
8 If that changes tests should be updated as appropriate. */
9static void test_parse_uuid_correct(void **state)
10{
11 /* lib/uuid does not check UUID version nor variant.
12 If that changes tests should be updated as appropriate. */
13 uint8_t uuid_buf[UUID_LEN] = { 0 };
14 const char *uuid_correct = "00112233-4455-6677-8899-aabbccddeeff";
15 const uint8_t uuid_correct_bin[UUID_LEN] = {
16 0x33, 0x22, 0x11, 0x00, 0x55, 0x44, 0x77, 0x66,
17 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
18 };
19 const char *uuid_zereos = "00000000-0000-0000-0000-000000000000";
20 const uint8_t uuid_zeros_bin[UUID_LEN] = { 0 };
21 const char *uuid_ff = "ffffffff-ffff-ffff-ffff-ffffffffffff";
22 const uint8_t uuid_ff_bin[UUID_LEN] = {
23 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
24 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
25 };
26
27 assert_int_equal(0, parse_uuid(uuid_buf, uuid_correct));
28 assert_memory_equal(uuid_correct_bin, uuid_buf, UUID_LEN);
29
30 assert_int_equal(0, parse_uuid(uuid_buf, uuid_zereos));
31 assert_memory_equal(uuid_zeros_bin, uuid_buf, UUID_LEN);
32
33 assert_int_equal(0, parse_uuid(uuid_buf, uuid_ff));
34 assert_memory_equal(uuid_ff_bin, uuid_buf, UUID_LEN);
35}
36
37/* This test validates parse_uuid() feeding it with incorrectly-formatted UUID strings.
38 No version/variant tests are performed due to lack of handling these properties
39 by lib/uuid. If that changes tests should be updated as appropriate.*/
40static void test_parse_uuid_incorrect(void **state)
41{
42 uint8_t uuid_buf[UUID_LEN] = { 0 };
43 /* First separator at incorrect index */
44 const char *uuid_incorrect_1 = "ab372d0-34cc1-1337-688b-2211337777ee";
45 /* Incorrect characters */
46 const char *uuid_incorrect_2 = "abbce711-5@51-7777-ffff-ahchbh999911";
47 /* Too long uuid string */
48 const char *uuid_incorrect_3 = "a7a38502-7465-9cbd-98ea-73003906dad5dd";
49 /* Too short uuid string */
50 const char *uuid_incorrect_4 = "72abd839-7109-64bd-cfef-a8266";
51
52 assert_int_equal(-1, parse_uuid(uuid_buf, uuid_incorrect_1));
53 assert_int_equal(-1, parse_uuid(uuid_buf, uuid_incorrect_2));
54 assert_int_equal(-1, parse_uuid(uuid_buf, uuid_incorrect_3));
55 assert_int_equal(-1, parse_uuid(uuid_buf, uuid_incorrect_4));
56}
57
58static void test_guid(void **state)
59{
60 guid_t guid = GUID_INIT(0x1F4D66BB, 0xDEAD, 0xBEEF,
61 0x08, 0xAB, 0x21, 0x37, 0xBB, 0x07, 0xDD, 0x4C);
62 guid_t guid_copy;
63 const guid_t guid_bin = { {
64 0xBB, 0x66, 0x4D, 0x1F,
65 0xAD, 0xDE,
66 0xEF, 0xBE,
67 0x08, 0xAB, 0x21, 0x37, 0xBB, 0x07, 0xDD, 0x4C
68 } };
69
70 /* GUID produced using GUID_INIT() macro should be identical to guid_bin
71 Check it by comparing memory directly and using provided comparison function */
72 assert_memory_equal(guid.b, guid_bin.b, UUID_LEN);
73 assert_int_equal(0, guidcmp(&guid, &guid_bin));
74
75 /* Copy GUID and compare it with original */
76 guidcpy(&guid_copy, &guid);
77 assert_memory_equal(guid.b, guid_copy.b, UUID_LEN);
78 assert_int_equal(0, guidcmp(&guid, &guid_copy));
79
80 /* Binary negate bits of one of GUIDs bytes.
81 Expect comparison to fail as GUIDs should differ */
82 guid_copy.b[7] = ~guid_copy.b[7];
83 assert_int_not_equal(0, guidcmp(&guid, &guid_copy));
84}
85
86int main(void)
87{
88 const struct CMUnitTest tests[] = {
89 cmocka_unit_test(test_parse_uuid_correct),
90 cmocka_unit_test(test_parse_uuid_incorrect),
91 cmocka_unit_test(test_guid),
92 };
93
Jakub Czapiga7c6081e2021-08-25 16:27:35 +020094 return cb_run_group_tests(tests, NULL, NULL);
Jakub Czapigaf5c00212021-03-31 12:41:29 +020095}