File: test-mongocrypt-buffer.c

package info (click to toggle)
libmongocrypt 1.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,572 kB
  • sloc: ansic: 70,067; python: 4,547; cpp: 615; sh: 460; makefile: 44; awk: 8
file content (267 lines) | stat: -rw-r--r-- 10,785 bytes parent folder | download
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright 2019-present MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <mongocrypt-marking-private.h>

#include "mongocrypt-buffer-private.h"
#include "test-mongocrypt-assert.h"
#include "test-mongocrypt.h"

#include "mongocrypt-endian-private.h"

#define TEST_STRING "?????" /* 3F 3F 3F 3F 3F */
#define TEST_INT 5555555    /* 54 C5 63 */

static void _get_bytes(const void *in, char *out, int len) {
    const unsigned char *src = in;
    char *dest = out;

    for (int i = 0; i < len; i++, dest += 3) {
        sprintf(dest, "%02X ", src[i]);
    }
    dest[-1] = '\0';
}

static bool assert_excess_bytes_removed(char *key, char *wrapped, char *unwrapped, uint32_t type, bson_value_t *out) {
    _mongocrypt_buffer_t plaintext = {0};
    _mongocrypt_marking_t marking = {0};
    bson_iter_t iter;
    bson_t wrapper = BSON_INITIALIZER;
    char actual[100] = {0};
    bool ret = false;
    bson_t bson = BSON_INITIALIZER;

    BSON_APPEND_UTF8(&bson, "str_key", TEST_STRING);
    BSON_APPEND_INT32(&bson, "int_key", TEST_INT);

    bson_iter_init_find(&iter, &bson, key);
    memcpy(&marking.u.fle1.v_iter, &iter, sizeof(bson_iter_t));

    bson_append_iter(&wrapper, "", 0, &marking.u.fle1.v_iter);
    _get_bytes(bson_get_data(&wrapper), actual, wrapper.len);
    BSON_ASSERT(0 == strcmp(wrapped, actual));

    _mongocrypt_buffer_from_iter(&plaintext, &(&marking)->u.fle1.v_iter);
    _get_bytes(plaintext.data, actual, plaintext.len);
    BSON_ASSERT(0 == strcmp(unwrapped, actual));

    _mongocrypt_marking_cleanup(&marking);
    bson_destroy(&wrapper);

    ret = _mongocrypt_buffer_to_bson_value(&plaintext, type, out);

    _mongocrypt_buffer_cleanup(&plaintext);
    bson_destroy(&bson);
    return ret;
}

static void _test_mongocrypt_buffer_from_iter(_mongocrypt_tester_t *tester) {
    /*
     * This section explains the purpose of each byte in a BSON document. This is
     * used to extract only the value of a BSON document for later storage. Below
     * is an example of the leftmost derivation of one of the BSON documents
     * used for this test.
     *
     * NOTES:
     * - When used as a unary operator, * means that the repetition can occur 0
     *   or more times.
     *
     * - int32     4 bytes (32-bit signed integer, two's complement)
     * - (byte*)   Zero or more modified UTF-8 encoded characters followed by
     *             '\x00'. The (byte*) MUST NOT contain '\x00', hence it is
     *             not full UTF-8.
     *
     * RULES:
     * 1. document ::=  int32 e_list "\x00"     int32 is the total number of
     *                                          bytes comprising the doc.
     * 2. e_list   ::=  element e_list
     *              |   ""
     * 3. element  ::=  "\x02" e_name string    UTF-8 string
     *              |   "\x10" e_name int32 	  32-bit integer
     * 4. e_name   ::=  cstring                 Key name
     * 5. string   ::=  int32 (byte*) "\x00"
     * 6. cstring  ::=  (byte*) "\x00"
     *
     * BELOW IS A LEFTMOST DERIVATION:
     * Let doc = { "" : "?????" }
     *
     * -  doc  ::= int32 e_list "\x00"
     *
     * -- rule2 -> int32 element e_list "\x00"
     * -- rule3 -> int32 "\x02" e_name string e_list "\x00"
     * -- rule4 -> int32 "\x02" cstring string e_list "\x00"
     * -- rule6 -> int32 "\x02" (byte*) "\x00" string e_list "\x00"
     * -- key   -> int32 "\x02" "" "\x00" string e_list "\x00"
     ** The key is an empty string, i.e. 0 bytes **
     * -- rule5 -> int32 "\x02" "" "\x00" int32 (byte*) "\x00" e_list "\x00"
     * -- value -> int32 "\x02" "" "\x00" int32=6 "?????" "\x00" e_list "\x00"
     ** Above, the value is set. The int32 before the value is the size of the **
     ** value in bytes, plus one for the the null char. **
     * -- rule2 -> int32=17 "\x02" "" "\x00" int32=6 "?????" "\x00" "" "\x00"
     *
     * Finally, we have the byte sequence:
     *    "11000000 02 "" 00 06000000 "?????" 00 00"
     *
     * Note, the hexcode for '?' is '3F'. Grouping the sequence by byte for
     * readability, more precisely we have:
     *    "11 00 00 00 02 00 06 00 00 00 3F 3F 3F 3F 3F 00 00"
     *
     * with the value, including its length and null terminator being:
     *    "06 00 00 00 3F 3F 3F 3F 3F 00".
     * This is what we will store.
     */

    bson_t wrapper = BSON_INITIALIZER;
    bson_value_t out;

    BSON_ASSERT(assert_excess_bytes_removed("str_key",
                                            "11 00 00 00 02 00 06 00 00 00 3F 3F 3F 3F 3F 00 00",
                                            /** no prefix **/ "06 00 00 00 3F 3F 3F 3F 3F 00",
                                            0x02, /* string type */
                                            &out));

    BSON_ASSERT(out.value_type == BSON_TYPE_UTF8);
    BSON_ASSERT(0 == strcmp(TEST_STRING, out.value.v_utf8.str));
    BSON_ASSERT(5 == out.value.v_utf8.len);
    bson_value_destroy(&out);

    BSON_ASSERT(assert_excess_bytes_removed("int_key",
                                            "0B 00 00 00 10 00 63 C5 54 00 00",
                                            /** no prefix **/ "63 C5 54 00",
                                            0x10, /* int type */
                                            &out));

    BSON_ASSERT(out.value_type == BSON_TYPE_INT32);
    BSON_ASSERT(TEST_INT == out.value.v_int32);

    BSON_ASSERT(!assert_excess_bytes_removed("int_key",
                                             "0B 00 00 00 10 00 63 C5 54 00 00",
                                             /** no prefix **/ "63 C5 54 00",
                                             0x99, /* invalid type */
                                             &out));
    bson_destroy(&wrapper);
}

static void _test_mongocrypt_buffer_copy_from_data_and_size(_mongocrypt_tester_t *tester) {
    _mongocrypt_buffer_t buf;
    const uint8_t data[] = {0, 1, 2};

    ASSERT(_mongocrypt_buffer_copy_from_data_and_size(&buf, data, 3));
    ASSERT_CMPBYTES(data, sizeof(data), buf.data, buf.len);
    _mongocrypt_buffer_cleanup(&buf);
}

static void _test_mongocrypt_buffer_steal_from_data_and_size(_mongocrypt_tester_t *tester) {
    _mongocrypt_buffer_t buf;
    uint8_t *data = bson_malloc0(3);

    data[0] = 0;
    data[1] = 1;
    data[2] = 2;
    ASSERT(_mongocrypt_buffer_steal_from_data_and_size(&buf, data, 3));
    ASSERT_CMPBYTES(data, 3, buf.data, buf.len);
    _mongocrypt_buffer_cleanup(&buf);
}

static void _test_mongocrypt_buffer_steal_from_string(_mongocrypt_tester_t *tester) {
    _mongocrypt_buffer_t buf;
    char *str = bson_strdup("foo");

    ASSERT(_mongocrypt_buffer_steal_from_string(&buf, str));
    ASSERT_STREQUAL((const char *)buf.data, str);
    _mongocrypt_buffer_cleanup(&buf);
}

static void _test_mongocrypt_buffer_copy_from_uint64_le(_mongocrypt_tester_t *tester) {
    _mongocrypt_buffer_t expect;
    _mongocrypt_buffer_t got;

    _mongocrypt_buffer_copy_from_hex(&expect, "0100000000000000");
    _mongocrypt_buffer_copy_from_uint64_le(&got, 0x0000000000000001ULL);
    ASSERT_CMPBYTES(expect.data, expect.len, got.data, got.len);
    _mongocrypt_buffer_cleanup(&expect);
    _mongocrypt_buffer_cleanup(&got);

    _mongocrypt_buffer_copy_from_hex(&expect, "1122334455667788");
    _mongocrypt_buffer_copy_from_uint64_le(&got, 0x8877665544332211ULL);
    ASSERT_CMPBYTES(expect.data, expect.len, got.data, got.len);
    _mongocrypt_buffer_cleanup(&expect);
    _mongocrypt_buffer_cleanup(&got);
}

static void _test_mongocrypt_buffer_from_subrange(_mongocrypt_tester_t *tester) {
    _mongocrypt_buffer_t input;
    _mongocrypt_buffer_t expect;
    _mongocrypt_buffer_t got;

    _mongocrypt_buffer_copy_from_hex(&input, "010203");
    _mongocrypt_buffer_copy_from_hex(&expect, "01");
    ASSERT(_mongocrypt_buffer_from_subrange(&got, &input, 0, 1));
    ASSERT_CMPBYTES(expect.data, expect.len, got.data, got.len);
    _mongocrypt_buffer_cleanup(&expect);
    _mongocrypt_buffer_cleanup(&input);

    _mongocrypt_buffer_copy_from_hex(&input, "010203");
    _mongocrypt_buffer_copy_from_hex(&expect, "010203");
    ASSERT(_mongocrypt_buffer_from_subrange(&got, &input, 0, 3));
    ASSERT_CMPBYTES(expect.data, expect.len, got.data, got.len);
    _mongocrypt_buffer_cleanup(&expect);
    _mongocrypt_buffer_cleanup(&input);

    _mongocrypt_buffer_copy_from_hex(&input, "010203");
    _mongocrypt_buffer_copy_from_hex(&expect, "0203");
    ASSERT(_mongocrypt_buffer_from_subrange(&got, &input, 1, 2));
    ASSERT_CMPBYTES(expect.data, expect.len, got.data, got.len);
    _mongocrypt_buffer_cleanup(&expect);
    _mongocrypt_buffer_cleanup(&input);

    _mongocrypt_buffer_copy_from_hex(&input, "010203");
    ASSERT(!_mongocrypt_buffer_from_subrange(&got, &input, 0, 4));
    _mongocrypt_buffer_cleanup(&input);
}

static void _test_mongocrypt_buffer_copy_from_string_as_bson_value(_mongocrypt_tester_t *tester) {
    _mongocrypt_buffer_t buf;
    _mongocrypt_buffer_t expectedLenBuf;
    const char *data = "foobar";

    // expect output to contain 4-byte length + data + null string terminator
    size_t expectedLen = sizeof(int32_t) + strlen(data) + sizeof(uint8_t);
    _mongocrypt_buffer_copy_from_hex(&expectedLenBuf, "07000000");

    _mongocrypt_buffer_copy_from_string_as_bson_value(&buf, data, (int)strlen(data));
    ASSERT(buf.len == expectedLen);

    // check 4-byte length
    ASSERT_CMPBYTES(expectedLenBuf.data, expectedLenBuf.len, buf.data, expectedLenBuf.len);
    // check data + null byte
    ASSERT_CMPBYTES((const uint8_t *)data,
                    strlen(data) + 1,
                    buf.data + expectedLenBuf.len,
                    buf.len - expectedLenBuf.len);
    _mongocrypt_buffer_cleanup(&buf);
    _mongocrypt_buffer_cleanup(&expectedLenBuf);
}

void _mongocrypt_tester_install_buffer(_mongocrypt_tester_t *tester) {
    INSTALL_TEST(_test_mongocrypt_buffer_from_iter);
    INSTALL_TEST(_test_mongocrypt_buffer_copy_from_data_and_size);
    INSTALL_TEST(_test_mongocrypt_buffer_steal_from_data_and_size);
    INSTALL_TEST(_test_mongocrypt_buffer_steal_from_string);
    INSTALL_TEST(_test_mongocrypt_buffer_copy_from_uint64_le);
    INSTALL_TEST(_test_mongocrypt_buffer_from_subrange);
    INSTALL_TEST(_test_mongocrypt_buffer_copy_from_string_as_bson_value);
}