Debian Codegraph @

crt/aws-c-common/tests/uuid_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/common/uuid.h>
6
7 #include <aws/common/byte_buf.h>
8
9 #include <aws/testing/aws_test_harness.h>
10
11 static int s_uuid_string_fn(struct aws_allocator *allocator, void *ctx) {
12 (void)allocator;
13 (void)ctx;
14
15 struct aws_uuid uuid;
16 ASSERT_SUCCESS(aws_uuid_init(&uuid));
17
18 uint8_t uuid_array[AWS_UUID_STR_LEN] = {0};
19 struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_array, sizeof(uuid_array));
20 uuid_buf.len = 0;
21
22 ASSERT_SUCCESS(aws_uuid_to_str(&uuid, &uuid_buf));
23 uint8_t zerod_buf[AWS_UUID_STR_LEN] = {0};
24 ASSERT_UINT_EQUALS(AWS_UUID_STR_LEN - 1, uuid_buf.len);
25 ASSERT_FALSE(0 == memcmp(zerod_buf, uuid_array, sizeof(uuid_array)));
26
27 return AWS_OP_SUCCESS;
28 }
29
30 AWS_TEST_CASE(uuid_string, s_uuid_string_fn)
31
32 static int s_uuid_string_compact_fn(struct aws_allocator *allocator, void *ctx) {
33 (void)allocator;
34 (void)ctx;
35
36 struct aws_uuid uuid;
37 ASSERT_SUCCESS(aws_uuid_init(&uuid));
38
39 uint8_t uuid_array[AWS_UUID_STR_COMPACT_LEN] = {0};
40 struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_array, sizeof(uuid_array));
41 uuid_buf.len = 0;
42
43 ASSERT_SUCCESS(aws_uuid_to_str_compact(&uuid, &uuid_buf));
44 uint8_t zerod_buf[AWS_UUID_STR_COMPACT_LEN] = {0};
45 ASSERT_UINT_EQUALS(AWS_UUID_STR_COMPACT_LEN - 1, uuid_buf.len);
46 ASSERT_FALSE(0 == memcmp(zerod_buf, uuid_array, sizeof(uuid_array)));
47
48 return AWS_OP_SUCCESS;
49 }
50
51 AWS_TEST_CASE(uuid_string_compact, s_uuid_string_compact_fn)
52
53 static int s_prefilled_uuid_string_fn(struct aws_allocator *allocator, void *ctx) {
54 (void)allocator;
55 (void)ctx;
56
57 struct aws_uuid uuid = {
58 .uuid_data = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10},
59 };
60
61 uint8_t uuid_array[AWS_UUID_STR_LEN] = {0};
62 struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_array, sizeof(uuid_array));
63 uuid_buf.len = 0;
64
65 ASSERT_SUCCESS(aws_uuid_to_str(&uuid, &uuid_buf));
66
67 const char *expected_str = "01020304-0506-0708-090a-0b0c0d0e0f10";
68 struct aws_byte_buf expected = aws_byte_buf_from_c_str(expected_str);
69 ASSERT_BIN_ARRAYS_EQUALS(expected.buffer, expected.len, uuid_buf.buffer, uuid_buf.len);
70
71 return AWS_OP_SUCCESS;
72 }
73
74 AWS_TEST_CASE(prefilled_uuid_string, s_prefilled_uuid_string_fn)
75
76 static int s_prefilled_uuid_string_compact_fn(struct aws_allocator *allocator, void *ctx) {
77 (void)allocator;
78 (void)ctx;
79
80 struct aws_uuid uuid = {
81 .uuid_data = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10},
82 };
83
84 uint8_t uuid_array[AWS_UUID_STR_COMPACT_LEN] = {0};
85 struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_array, sizeof(uuid_array));
86 uuid_buf.len = 0;
87
88 ASSERT_SUCCESS(aws_uuid_to_str_compact(&uuid, &uuid_buf));
89
90 const char *expected_str = "0102030405060708090a0b0c0d0e0f10";
91 struct aws_byte_buf expected = aws_byte_buf_from_c_str(expected_str);
92 ASSERT_BIN_ARRAYS_EQUALS(expected.buffer, expected.len, uuid_buf.buffer, uuid_buf.len);
93
94 return AWS_OP_SUCCESS;
95 }
96
97 AWS_TEST_CASE(prefilled_uuid_string_compact, s_prefilled_uuid_string_compact_fn)
98
99 static int s_uuid_string_short_buffer_fn(struct aws_allocator *allocator, void *ctx) {
100 (void)allocator;
101 (void)ctx;
102
103 struct aws_uuid uuid;
104 ASSERT_SUCCESS(aws_uuid_init(&uuid));
105
106 uint8_t uuid_array[AWS_UUID_STR_LEN - 2] = {0};
107 struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_array, sizeof(uuid_array));
108 uuid_buf.len = 0;
109
110 ASSERT_ERROR(AWS_ERROR_SHORT_BUFFER, aws_uuid_to_str(&uuid, &uuid_buf));
111
112 return AWS_OP_SUCCESS;
113 }
114
115 AWS_TEST_CASE(uuid_string_short_buffer, s_uuid_string_short_buffer_fn)
116
117 static int s_uuid_string_compact_short_buffer_fn(struct aws_allocator *allocator, void *ctx) {
118 (void)allocator;
119 (void)ctx;
120
121 struct aws_uuid uuid;
122 ASSERT_SUCCESS(aws_uuid_init(&uuid));
123
124 uint8_t uuid_array[AWS_UUID_STR_COMPACT_LEN - 2] = {0};
125 struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_array, sizeof(uuid_array));
126 uuid_buf.len = 0;
127
128 ASSERT_ERROR(AWS_ERROR_SHORT_BUFFER, aws_uuid_to_str_compact(&uuid, &uuid_buf));
129
130 return AWS_OP_SUCCESS;
131 }
132
133 AWS_TEST_CASE(uuid_string_compact_short_buffer, s_uuid_string_compact_short_buffer_fn)
134
135 static int s_uuid_string_parse_fn(struct aws_allocator *allocator, void *ctx) {
136 (void)allocator;
137 (void)ctx;
138
139 uint8_t expected_uuid[] = {
140 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10};
141
142 const char *uuid_str = "01020304-0506-0708-090a-0b0c0d0e0f10";
143 struct aws_byte_buf uuid_buf = aws_byte_buf_from_c_str(uuid_str);
144 struct aws_byte_cursor uuid_cur = aws_byte_cursor_from_buf(&uuid_buf);
145
146 struct aws_uuid uuid;
147 ASSERT_SUCCESS(aws_uuid_init_from_str(&uuid, &uuid_cur));
148 ASSERT_BIN_ARRAYS_EQUALS(expected_uuid, sizeof(expected_uuid), uuid.uuid_data, sizeof(uuid.uuid_data));
149
150 return AWS_OP_SUCCESS;
151 }
152
153 AWS_TEST_CASE(uuid_string_parse, s_uuid_string_parse_fn)
154
155 static int s_uuid_string_parse_too_short_fn(struct aws_allocator *allocator, void *ctx) {
156 (void)allocator;
157 (void)ctx;
158
159 const char *uuid_str = "01020304-0506-0708-090a-0b0c0d0e0f1";
160 struct aws_byte_buf uuid_buf = aws_byte_buf_from_c_str(uuid_str);
161 struct aws_byte_cursor uuid_cur = aws_byte_cursor_from_buf(&uuid_buf);
162
163 struct aws_uuid uuid;
164 ASSERT_ERROR(AWS_ERROR_INVALID_BUFFER_SIZE, aws_uuid_init_from_str(&uuid, &uuid_cur));
165
166 return AWS_OP_SUCCESS;
167 }
168
169 AWS_TEST_CASE(uuid_string_parse_too_short, s_uuid_string_parse_too_short_fn)
170
171 static int s_uuid_string_parse_malformed_fn(struct aws_allocator *allocator, void *ctx) {
172 (void)allocator;
173 (void)ctx;
174
175 const char *uuid_str = "010203-040506-0708-090a-0b0c0d0e0f10";
176 struct aws_byte_buf uuid_buf = aws_byte_buf_from_c_str(uuid_str);
177 struct aws_byte_cursor uuid_cur = aws_byte_cursor_from_buf(&uuid_buf);
178
179 struct aws_uuid uuid;
180 ASSERT_ERROR(AWS_ERROR_MALFORMED_INPUT_STRING, aws_uuid_init_from_str(&uuid, &uuid_cur));
181
182 return AWS_OP_SUCCESS;
183 }
184
185 AWS_TEST_CASE(uuid_string_parse_malformed, s_uuid_string_parse_malformed_fn)