Debian Codegraph @

crt/s2n/tests/unit/s2n_blob_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "utils/s2n_blob.h"
17
18 #include "api/s2n.h"
19 #include "s2n_test.h"
20 #include "utils/s2n_mem.h"
21
22 int main(int argc, char **argv)
23 {
24 BEGIN_TEST();
25 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
26
27 /* Null blob is not valid */
28 EXPECT_ERROR(s2n_blob_validate(NULL));
29
30 #ifndef NDEBUG
31 /* Invalid blob is not valid */
32 struct s2n_blob b1 = { .data = 0, .size = 101 };
33 EXPECT_ERROR(s2n_blob_validate(&b1));
34 #endif
35
36 /* Size of 0 is OK if data is null */
37 struct s2n_blob b2 = { 0 };
38 EXPECT_SUCCESS(s2n_blob_init(&b2, 0, 0));
39 EXPECT_OK(s2n_blob_validate(&b2));
40
41 /* Valid blob is valid */
42 uint8_t array[12];
43 struct s2n_blob b3 = { 0 };
44 EXPECT_SUCCESS(s2n_blob_init(&b3, array, sizeof(array)));
45 EXPECT_OK(s2n_blob_validate(&b3));
46
47 /* Null blob is not growable */
48 EXPECT_FALSE(s2n_blob_is_growable(NULL));
49 EXPECT_FAILURE(s2n_realloc(NULL, 24));
50 EXPECT_FAILURE(s2n_free(NULL));
51
52 /* Static blob is not growable or freeable */
53 struct s2n_blob g1 = { 0 };
54 EXPECT_SUCCESS(s2n_blob_init(&g1, array, 12));
55 EXPECT_FALSE(s2n_blob_is_growable(&g1));
56 EXPECT_FAILURE(s2n_realloc(&g1, 24));
57 EXPECT_FAILURE(s2n_free(&g1));
58
59 /* Empty blob is freeable */
60 struct s2n_blob g2 = { 0 };
61 EXPECT_TRUE(s2n_blob_is_growable(&g2));
62 EXPECT_SUCCESS(s2n_free(&g2));
63
64 /* Empty blob is growable */
65 struct s2n_blob g3 = { 0 };
66 EXPECT_TRUE(s2n_blob_is_growable(&g3));
67 EXPECT_SUCCESS(s2n_realloc(&g3, 24));
68 EXPECT_SUCCESS(s2n_free(&g3));
69
70 /* Alloced blob can be freed */
71 struct s2n_blob g4 = { 0 };
72 EXPECT_SUCCESS(s2n_alloc(&g4, 12));
73 EXPECT_TRUE(s2n_blob_is_growable(&g4));
74 EXPECT_SUCCESS(s2n_free(&g4));
75
76 /* Alloced blob can be realloced and data preserved */
77 struct s2n_blob g5 = { 0 };
78 uint8_t hello_world[] = "HELLO WORLD";
79 EXPECT_SUCCESS(s2n_alloc(&g5, 12));
80 EXPECT_TRUE(s2n_blob_is_growable(&g5));
81 EXPECT_MEMCPY_SUCCESS(g5.data, hello_world, sizeof(hello_world));
82 EXPECT_SUCCESS(s2n_realloc(&g5, 24));
83 EXPECT_EQUAL(memcmp(g5.data, hello_world, sizeof(hello_world)), 0);
84 EXPECT_SUCCESS(s2n_free(&g5));
85
86 /* Alloced blob can be reallocated without leaking memory */
87 struct s2n_blob g6 = { 0 };
88 EXPECT_SUCCESS(s2n_alloc(&g6, 12));
89 g6.size = 0;
90 EXPECT_SUCCESS(s2n_realloc(&g6, g6.allocated + 12));
91 EXPECT_SUCCESS(s2n_free(&g6));
92
93 /* Down-casing works */
94 struct s2n_blob g7 = { 0 };
95 EXPECT_SUCCESS(s2n_blob_init(&g7, hello_world, sizeof(hello_world)));
96 EXPECT_SUCCESS(s2n_blob_char_to_lower(&g7));
97 EXPECT_SUCCESS(memcmp(g7.data, "hello world", sizeof(hello_world)));
98
99 /* Slicing works */
100 struct s2n_blob g8 = { 0 };
101 uint8_t hello[] = "hello ";
102 uint8_t world[] = "world";
103 EXPECT_SUCCESS(s2n_blob_slice(&g7, &g8, strlen((char *) hello), sizeof(world)));
104 EXPECT_EQUAL(memcmp(g8.data, world, sizeof(world)), 0);
105 EXPECT_EQUAL(g8.size, sizeof(world));
106
107 END_TEST();
108 }