File: uuid_test.c

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (123 lines) | stat: -rw-r--r-- 4,137 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include <aws/common/uuid.h>

#include <aws/common/byte_buf.h>

#include <aws/testing/aws_test_harness.h>

static int s_uuid_string_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    struct aws_uuid uuid;
    ASSERT_SUCCESS(aws_uuid_init(&uuid));

    uint8_t uuid_array[AWS_UUID_STR_LEN] = {0};
    struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_array, sizeof(uuid_array));
    uuid_buf.len = 0;

    ASSERT_SUCCESS(aws_uuid_to_str(&uuid, &uuid_buf));
    uint8_t zerod_buf[AWS_UUID_STR_LEN] = {0};
    ASSERT_UINT_EQUALS(AWS_UUID_STR_LEN - 1, uuid_buf.len);
    ASSERT_FALSE(0 == memcmp(zerod_buf, uuid_array, sizeof(uuid_array)));

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(uuid_string, s_uuid_string_fn)

static int s_prefilled_uuid_string_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    struct aws_uuid uuid = {
        .uuid_data = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10},
    };

    uint8_t uuid_array[AWS_UUID_STR_LEN] = {0};
    struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_array, sizeof(uuid_array));
    uuid_buf.len = 0;

    ASSERT_SUCCESS(aws_uuid_to_str(&uuid, &uuid_buf));

    const char *expected_str = "01020304-0506-0708-090a-0b0c0d0e0f10";
    struct aws_byte_buf expected = aws_byte_buf_from_c_str(expected_str);
    ASSERT_BIN_ARRAYS_EQUALS(expected.buffer, expected.len, uuid_buf.buffer, uuid_buf.len);

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(prefilled_uuid_string, s_prefilled_uuid_string_fn)

static int s_uuid_string_short_buffer_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    struct aws_uuid uuid;
    ASSERT_SUCCESS(aws_uuid_init(&uuid));

    uint8_t uuid_array[AWS_UUID_STR_LEN - 2] = {0};
    struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_array, sizeof(uuid_array));
    uuid_buf.len = 0;

    ASSERT_ERROR(AWS_ERROR_SHORT_BUFFER, aws_uuid_to_str(&uuid, &uuid_buf));

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(uuid_string_short_buffer, s_uuid_string_short_buffer_fn)

static int s_uuid_string_parse_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    uint8_t expected_uuid[] = {
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10};

    const char *uuid_str = "01020304-0506-0708-090a-0b0c0d0e0f10";
    struct aws_byte_buf uuid_buf = aws_byte_buf_from_c_str(uuid_str);
    struct aws_byte_cursor uuid_cur = aws_byte_cursor_from_buf(&uuid_buf);

    struct aws_uuid uuid;
    ASSERT_SUCCESS(aws_uuid_init_from_str(&uuid, &uuid_cur));
    ASSERT_BIN_ARRAYS_EQUALS(expected_uuid, sizeof(expected_uuid), uuid.uuid_data, sizeof(uuid.uuid_data));

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(uuid_string_parse, s_uuid_string_parse_fn)

static int s_uuid_string_parse_too_short_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    const char *uuid_str = "01020304-0506-0708-090a-0b0c0d0e0f1";
    struct aws_byte_buf uuid_buf = aws_byte_buf_from_c_str(uuid_str);
    struct aws_byte_cursor uuid_cur = aws_byte_cursor_from_buf(&uuid_buf);

    struct aws_uuid uuid;
    ASSERT_ERROR(AWS_ERROR_INVALID_BUFFER_SIZE, aws_uuid_init_from_str(&uuid, &uuid_cur));

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(uuid_string_parse_too_short, s_uuid_string_parse_too_short_fn)

static int s_uuid_string_parse_malformed_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    const char *uuid_str = "010203-040506-0708-090a-0b0c0d0e0f10";
    struct aws_byte_buf uuid_buf = aws_byte_buf_from_c_str(uuid_str);
    struct aws_byte_cursor uuid_cur = aws_byte_cursor_from_buf(&uuid_buf);

    struct aws_uuid uuid;
    ASSERT_ERROR(AWS_ERROR_MALFORMED_INPUT_STRING, aws_uuid_init_from_str(&uuid, &uuid_cur));

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(uuid_string_parse_malformed, s_uuid_string_parse_malformed_fn)