File: test-mc-writer.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 (176 lines) | stat: -rw-r--r-- 5,898 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
/*
 * Copyright 2022-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 "mc-reader-private.h"
#include "mc-writer-private.h"
#include "test-mongocrypt-assert.h"
#include "test-mongocrypt.h"

static void _test_mc_writer_ints(_mongocrypt_tester_t *tester) {
    {
        mongocrypt_status_t *status;
        status = mongocrypt_status_new();

        _mongocrypt_buffer_t write_buffer;
        _mongocrypt_buffer_init_size(&write_buffer, sizeof(uint8_t));

        mc_writer_t writer;
        mc_writer_init_from_buffer(&writer, &write_buffer, __func__);

        uint8_t num = 4;
        ASSERT_OK_STATUS(mc_writer_write_u8(&writer, num, status), status);

        mc_reader_t reader;
        mc_reader_init_from_buffer(&reader, &write_buffer, __func__);

        uint8_t out;
        ASSERT_OK_STATUS(mc_reader_read_u8(&reader, &out, status), status);

        ASSERT_CMPUINT8(out, ==, num);

        _mongocrypt_buffer_cleanup(&write_buffer);
        mongocrypt_status_destroy(status);
    }

    {
        mongocrypt_status_t *status;
        status = mongocrypt_status_new();

        _mongocrypt_buffer_t write_buffer;
        _mongocrypt_buffer_init_size(&write_buffer, sizeof(uint32_t));

        mc_writer_t writer;
        mc_writer_init_from_buffer(&writer, &write_buffer, __func__);

        uint32_t num = 23832405;
        ASSERT_OK_STATUS(mc_writer_write_u32(&writer, num, status), status);

        mc_reader_t reader;
        mc_reader_init_from_buffer(&reader, &write_buffer, __func__);

        uint32_t out;
        ASSERT_OK_STATUS(mc_reader_read_u32(&reader, &out, status), status);

        ASSERT_CMPUINT32(out, ==, num);

        _mongocrypt_buffer_cleanup(&write_buffer);
        mongocrypt_status_destroy(status);
    }

    {
        mongocrypt_status_t *status;
        status = mongocrypt_status_new();

        _mongocrypt_buffer_t write_buffer;
        _mongocrypt_buffer_init_size(&write_buffer, sizeof(uint64_t));

        mc_writer_t writer;
        mc_writer_init_from_buffer(&writer, &write_buffer, __func__);

        uint64_t num = 23832405;
        ASSERT_OK_STATUS(mc_writer_write_u64(&writer, num, status), status);

        mc_reader_t reader;
        mc_reader_init_from_buffer(&reader, &write_buffer, __func__);

        uint64_t out;
        ASSERT_OK_STATUS(mc_reader_read_u64(&reader, &out, status), status);

        ASSERT_CMPUINT64(out, ==, num);

        _mongocrypt_buffer_cleanup(&write_buffer);
        mongocrypt_status_destroy(status);
    }
}

static void _test_mc_writer_buffer(_mongocrypt_tester_t *tester) {
    mongocrypt_status_t *status;
    status = mongocrypt_status_new();

    _mongocrypt_buffer_t input_buffer;

    _mongocrypt_buffer_copy_from_hex(&input_buffer,
                                     "07123456781234987612341234567890120243bba14ddf42da823c33569f4689f465a");

    _mongocrypt_buffer_t write_buffer;
    _mongocrypt_buffer_init_size(&write_buffer, input_buffer.len);

    mc_writer_t writer;
    mc_writer_init_from_buffer(&writer, &write_buffer, __func__);

    ASSERT_OK_STATUS(mc_writer_write_buffer(&writer, &input_buffer, input_buffer.len, status), status);

    _mongocrypt_buffer_t read_buffer;
    mc_reader_t reader;
    mc_reader_init_from_buffer(&reader, &write_buffer, __func__);
    ASSERT_OK_STATUS(mc_reader_read_buffer(&reader, &read_buffer, write_buffer.len, status), status);

    ASSERT_CMPBUF(input_buffer, read_buffer);

    _mongocrypt_buffer_cleanup(&input_buffer);
    _mongocrypt_buffer_cleanup(&write_buffer);
    _mongocrypt_buffer_cleanup(&read_buffer);
    mongocrypt_status_destroy(status);
}

static void _test_mc_writer_prf(_mongocrypt_tester_t *tester) {
    mongocrypt_status_t *status;
    status = mongocrypt_status_new();

    _mongocrypt_buffer_t input_buffer;
    _mongocrypt_buffer_copy_from_hex(&input_buffer, "c03cd1d0536aa07d4ffaa0301656222fc03cd1d0536aa07d4ffaa0301656222f");

    _mongocrypt_buffer_t write_buffer;
    _mongocrypt_buffer_init_size(&write_buffer, input_buffer.len);

    mc_writer_t writer;
    mc_writer_init_from_buffer(&writer, &write_buffer, __func__);

    ASSERT_OK_STATUS(mc_writer_write_prfblock_buffer(&writer, &input_buffer, status), status);
    ASSERT_CMPBUF(input_buffer, write_buffer);

    _mongocrypt_buffer_cleanup(&input_buffer);
    _mongocrypt_buffer_cleanup(&write_buffer);
    mongocrypt_status_destroy(status);
}

static void _test_mc_writer_uuid(_mongocrypt_tester_t *tester) {
    mongocrypt_status_t *status;
    status = mongocrypt_status_new();

    _mongocrypt_buffer_t input_buffer;
    _mongocrypt_buffer_copy_from_hex(&input_buffer, "c03cd1d0536aa07d4ffaa0301656222f");

    _mongocrypt_buffer_t write_buffer;
    _mongocrypt_buffer_init_size(&write_buffer, input_buffer.len);

    mc_writer_t writer;
    mc_writer_init_from_buffer(&writer, &write_buffer, __func__);

    ASSERT_OK_STATUS(mc_writer_write_uuid_buffer(&writer, &input_buffer, status), status);
    ASSERT_CMPBUF(input_buffer, write_buffer);

    _mongocrypt_buffer_cleanup(&input_buffer);
    _mongocrypt_buffer_cleanup(&write_buffer);
    mongocrypt_status_destroy(status);
}

void _mongocrypt_tester_install_mc_writer(_mongocrypt_tester_t *tester) {
    INSTALL_TEST(_test_mc_writer_ints);
    INSTALL_TEST(_test_mc_writer_buffer);
    INSTALL_TEST(_test_mc_writer_prf);
    INSTALL_TEST(_test_mc_writer_uuid);
}