Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
268
269
270
271
272
273
274
275
276
/***********************************************************************************************************************************
Test Binary to String Encode/Decode
***********************************************************************************************************************************/
 
/***********************************************************************************************************************************
Test Run
***********************************************************************************************************************************/
static void
testRun(void)
{
    FUNCTION_HARNESS_VOID();
 
    // *****************************************************************************************************************************
    if (testBegin("base64"))
    {
        const uint8_t *encode = (const uint8_t *)"string_to_encode\r\n";
        char destinationEncode[256];
 
        TEST_RESULT_UINT(encodeToStrSize(encodingBase64, 0), 0, "check zero size");
 
        encodeToStr(encodingBase64, encode, 1, destinationEncode);
        TEST_RESULT_Z(destinationEncode, "cw==", "1 character encode");
        TEST_RESULT_UINT(encodeToStrSize(encodingBase64, 1), strlen(destinationEncode), "check size");
 
        encodeToStr(encodingBase64, encode, 2, destinationEncode);
        TEST_RESULT_Z(destinationEncode, "c3Q=", "2 character encode");
        TEST_RESULT_UINT(encodeToStrSize(encodingBase64, 2), strlen(destinationEncode), "check size");
 
        encodeToStr(encodingBase64, encode, 3, destinationEncode);
        TEST_RESULT_Z(destinationEncode, "c3Ry", "3 character encode");
        TEST_RESULT_UINT(encodeToStrSize(encodingBase64, 3), strlen(destinationEncode), "check size");
 
        encodeToStr(encodingBase64, encode, strlen((const char *)encode) - 2, destinationEncode);
        TEST_RESULT_Z(destinationEncode, "c3RyaW5nX3RvX2VuY29kZQ==", "encode full string");
        TEST_RESULT_UINT(
            encodeToStrSize(encodingBase64, strlen((const char *)encode) - 2), strlen(destinationEncode), "check size");
 
        encodeToStr(encodingBase64, encode, strlen((const char *)encode), destinationEncode);
        TEST_RESULT_Z(destinationEncode, "c3RyaW5nX3RvX2VuY29kZQ0K", "encode full string with \\r\\n");
        TEST_RESULT_UINT(encodeToStrSize(encodingBase64, strlen((const char *)encode)), strlen(destinationEncode), "check size");
 
        encodeToStr(encodingBase64, encode, strlen((const char *)encode) + 1, destinationEncode);
        TEST_RESULT_Z(destinationEncode, "c3RyaW5nX3RvX2VuY29kZQ0KAA==", "encode full string with \\r\\n and null");
        TEST_RESULT_UINT(
            encodeToStrSize(encodingBase64, strlen((const char *)encode) + 1), strlen(destinationEncode), "check size");
 
        // -------------------------------------------------------------------------------------------------------------------------
        uint8_t destinationDecode[256];
 
        memset(destinationDecode, 0xFF, sizeof(destinationDecode));
        const char *decode = "c3RyaW5nX3RvX2VuY29kZQ0KAA==";
        decodeToBin(encodingBase64, decode, destinationDecode);
        TEST_RESULT_Z((char *)destinationDecode, (const char *)encode, "full string with \\r\\n and null decode");
        TEST_RESULT_INT(destinationDecode[strlen((const char *)encode) + 1], 0xFF, "check for overrun");
        TEST_RESULT_UINT(decodeToBinSize(encodingBase64, decode), strlen((const char *)encode) + 1, "check size");
 
        memset(destinationDecode, 0xFF, sizeof(destinationDecode));
        decode = "c3RyaW5nX3RvX2VuY29kZQ0K";
        decodeToBin(encodingBase64, decode, destinationDecode);
        TEST_RESULT_INT(memcmp(destinationDecode, encode, strlen((const char *)encode)), 0, "full string with \\r\\n decode");
        TEST_RESULT_INT(destinationDecode[strlen((const char *)encode)], 0xFF, "check for overrun");
        TEST_RESULT_UINT(decodeToBinSize(encodingBase64, decode), strlen((const char *)encode), "check size");
 
        memset(destinationDecode, 0xFF, sizeof(destinationDecode));
        decode = "c3RyaW5nX3RvX2VuY29kZQ==";
        decodeToBin(encodingBase64, decode, destinationDecode);
        TEST_RESULT_INT(memcmp(destinationDecode, encode, strlen((const char *)encode) - 2), 0, "full string decode");
        TEST_RESULT_INT(destinationDecode[strlen((const char *)encode) - 2], 0xFF, "check for overrun");
        TEST_RESULT_UINT(decodeToBinSize(encodingBase64, decode), strlen((const char *)encode) - 2, "check size");
 
        memset(destinationDecode, 0xFF, sizeof(destinationDecode));
        decode = "c3Ry";
        decodeToBin(encodingBase64, decode, destinationDecode);
        TEST_RESULT_INT(memcmp(destinationDecode, encode, 3), 0, "3 character decode");
        TEST_RESULT_INT(destinationDecode[3], 0xFF, "check for overrun");
        TEST_RESULT_UINT(decodeToBinSize(encodingBase64, decode), 3, "check size");
 
        memset(destinationDecode, 0xFF, sizeof(destinationDecode));
        decode = "c3Q=";
        decodeToBin(encodingBase64, decode, destinationDecode);
        TEST_RESULT_INT(memcmp(destinationDecode, encode, 2), 0, "2 character decode");
        TEST_RESULT_INT(destinationDecode[2], 0xFF, "check for overrun");
        TEST_RESULT_UINT(decodeToBinSize(encodingBase64, decode), 2, "check size");
 
        memset(destinationDecode, 0xFF, sizeof(destinationDecode));
        decode = "cw==";
        decodeToBin(encodingBase64, decode, destinationDecode);
        TEST_RESULT_INT(memcmp(destinationDecode, encode, 1), 0, "1 character decode");
        TEST_RESULT_INT(destinationDecode[1], 0xFF, "check for overrun");
        TEST_RESULT_UINT(decodeToBinSize(encodingBase64, decode), 1, "check size");
 
        // An empty string is valid base64 since zero is evenly divisible by four, and it has no last character to check
        memset(destinationDecode, 0xFF, sizeof(destinationDecode));
        decode = "";
        decodeToBin(encodingBase64, decode, destinationDecode);
        TEST_RESULT_INT(destinationDecode[0], 0xFF, "check for overrun");
        TEST_RESULT_UINT(decodeToBinSize(encodingBase64, decode), 0, "check zero size");
 
        TEST_ERROR(
            decodeToBin(encodingBase64, "cc$=", destinationDecode), FormatError, "base64 invalid character found at position 2");
        TEST_ERROR(
            decodeToBin(encodingBase64, "c\xc3\xa9=", destinationDecode), FormatError,
            "base64 invalid character found at position 1");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_ERROR(decodeToBin(encodingBase64, "c3", destinationDecode), FormatError, "base64 size 2 is not evenly divisible by 4");
        TEST_ERROR(
            decodeToBin(encodingBase64, "c===", destinationDecode), FormatError,
            "base64 '=' character may only appear in last two positions");
        TEST_ERROR(
            decodeToBin(encodingBase64, "cc=c", destinationDecode), FormatError,
            "base64 last character must be '=' if second to last is");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("base64url"))
    {
        TEST_TITLE("encode");
 
        const uint8_t *encode = (const uint8_t *)"string_to_encode\r\n";
        char destinationEncode[256];
 
        TEST_RESULT_UINT(encodeToStrSize(encodingBase64Url, 0), 0, "check zero size");
 
        encodeToStr(encodingBase64Url, encode, 1, destinationEncode);
        TEST_RESULT_Z(destinationEncode, "cw", "1 character encode");
        TEST_RESULT_UINT(encodeToStrSize(encodingBase64Url, 1), strlen(destinationEncode), "check size");
 
        encodeToStr(encodingBase64Url, encode, 2, destinationEncode);
        TEST_RESULT_Z(destinationEncode, "c3Q", "2 character encode");
        TEST_RESULT_UINT(encodeToStrSize(encodingBase64Url, 2), strlen(destinationEncode), "check size");
 
        encodeToStr(encodingBase64Url, encode, 3, destinationEncode);
        TEST_RESULT_Z(destinationEncode, "c3Ry", "3 character encode");
        TEST_RESULT_UINT(encodeToStrSize(encodingBase64Url, 3), strlen(destinationEncode), "check size");
 
        encodeToStr(encodingBase64Url, encode, strlen((const char *)encode) - 2, destinationEncode);
        TEST_RESULT_Z(destinationEncode, "c3RyaW5nX3RvX2VuY29kZQ", "encode full string");
        TEST_RESULT_UINT(
            encodeToStrSize(encodingBase64Url, strlen((const char *)encode) - 2), strlen(destinationEncode), "check size");
 
        encodeToStr(encodingBase64Url, encode, strlen((const char *)encode), destinationEncode);
        TEST_RESULT_Z(destinationEncode, "c3RyaW5nX3RvX2VuY29kZQ0K", "encode full string with \\r\\n");
        TEST_RESULT_UINT(encodeToStrSize(encodingBase64Url, strlen((const char *)encode)), strlen(destinationEncode), "check size");
 
        encodeToStr(encodingBase64Url, encode, strlen((const char *)encode) + 1, destinationEncode);
        TEST_RESULT_Z(destinationEncode, "c3RyaW5nX3RvX2VuY29kZQ0KAA", "encode full string with \\r\\n and null");
        TEST_RESULT_UINT(
            encodeToStrSize(encodingBase64Url, strlen((const char *)encode) + 1), strlen(destinationEncode), "check size");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("decode");
 
        uint8_t destinationDecode[256];
 
        memset(destinationDecode, 0xFF, sizeof(destinationDecode));
        const char *decode = "c3RyaW5nX3RvX2VuY29kZQ0KAA";
        decodeToBin(encodingBase64Url, decode, destinationDecode);
        TEST_RESULT_Z((char *)destinationDecode, (const char *)encode, "full string with \\r\\n and null decode");
        TEST_RESULT_INT(destinationDecode[strlen((const char *)encode) + 1], 0xFF, "check for overrun");
        TEST_RESULT_UINT(decodeToBinSize(encodingBase64Url, decode), strlen((const char *)encode) + 1, "check size");
 
        memset(destinationDecode, 0xFF, sizeof(destinationDecode));
        decode = "c3RyaW5nX3RvX2VuY29kZQ0K";
        decodeToBin(encodingBase64Url, decode, destinationDecode);
        TEST_RESULT_INT(memcmp(destinationDecode, encode, strlen((const char *)encode)), 0, "full string with \\r\\n decode");
        TEST_RESULT_INT(destinationDecode[strlen((const char *)encode)], 0xFF, "check for overrun");
        TEST_RESULT_UINT(decodeToBinSize(encodingBase64Url, decode), strlen((const char *)encode), "check size");
 
        memset(destinationDecode, 0xFF, sizeof(destinationDecode));
        decode = "c3Q";
        decodeToBin(encodingBase64Url, decode, destinationDecode);
        TEST_RESULT_INT(memcmp(destinationDecode, encode, 2), 0, "2 character decode");
        TEST_RESULT_INT(destinationDecode[2], 0xFF, "check for overrun");
        TEST_RESULT_UINT(decodeToBinSize(encodingBase64Url, decode), 2, "check size");
 
        memset(destinationDecode, 0xFF, sizeof(destinationDecode));
        decode = "cw";
        decodeToBin(encodingBase64Url, decode, destinationDecode);
        TEST_RESULT_INT(memcmp(destinationDecode, encode, 1), 0, "1 character decode");
        TEST_RESULT_INT(destinationDecode[1], 0xFF, "check for overrun");
        TEST_RESULT_UINT(decodeToBinSize(encodingBase64Url, decode), 1, "check size");
 
        memset(destinationDecode, 0xFF, sizeof(destinationDecode));
        decode = "";
        decodeToBin(encodingBase64Url, decode, destinationDecode);
        TEST_RESULT_INT(destinationDecode[0], 0xFF, "check for overrun");
        TEST_RESULT_UINT(decodeToBinSize(encodingBase64Url, decode), 0, "check zero size");
 
        decode = "-_-_";
        decodeToBin(encodingBase64Url, decode, destinationDecode);
        TEST_RESULT_INT(memcmp(destinationDecode, "\xfb\xff\xbf", 3), 0, "url characters decode");
 
        TEST_ERROR(decodeToBin(encodingBase64Url, "c3Ryc", destinationDecode), FormatError, "base64url size 5 is not valid");
        TEST_ERROR(
            decodeToBin(encodingBase64Url, "cw==", destinationDecode), FormatError,
            "base64url invalid character found at position 2");
        TEST_ERROR(
            decodeToBin(encodingBase64Url, "c+/", destinationDecode), FormatError,
            "base64url invalid character found at position 1");
        TEST_ERROR(
            decodeToBin(encodingBase64Url, "c\xc3\xa9", destinationDecode), FormatError,
            "base64url invalid character found at position 1");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("hex"))
    {
        TEST_TITLE("encode");
 
        const uint8_t *encode = (const uint8_t *)"string_to_encode\r\n";
        char destinationEncode[513];
 
        encodeToStr(encodingHex, encode, 1, destinationEncode);
        TEST_RESULT_Z(destinationEncode, "73", "1 character encode");
        TEST_RESULT_UINT(encodeToStrSize(encodingHex, 1), strlen(destinationEncode), "check size");
 
        encodeToStr(encodingHex, encode, strlen((const char *)encode), destinationEncode);
        TEST_RESULT_Z(destinationEncode, "737472696e675f746f5f656e636f64650d0a", "encode full string with \\r\\n");
        TEST_RESULT_UINT(encodeToStrSize(encodingHex, strlen((const char *)encode)), strlen(destinationEncode), "check size");
 
        encodeToStr(encodingHex, encode, strlen((const char *)encode) + 1, destinationEncode);
        TEST_RESULT_Z(destinationEncode, "737472696e675f746f5f656e636f64650d0a00", "encode full string with \\r\\n and null");
        TEST_RESULT_UINT(encodeToStrSize(encodingHex, strlen((const char *)encode) + 1), strlen(destinationEncode), "check size");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("decode");
 
        uint8_t destinationDecode[256];
 
        memset(destinationDecode, 0xFF, sizeof(destinationDecode));
        const char *decode = "737472696e675f746f5f656e636f64650d0a00";
        decodeToBin(encodingHex, decode, destinationDecode);
        TEST_RESULT_Z((char *)destinationDecode, (const char *)encode, "full string with \\r\\n and null decode");
        TEST_RESULT_INT(destinationDecode[strlen((const char *)encode) + 1], 0xFF, "check for overrun");
        TEST_RESULT_UINT(decodeToBinSize(encodingHex, decode), strlen((const char *)encode) + 1, "check size");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("decode/encode with mixed case");
 
        const char *decodeMixed = "0123456789AaBbCcDdEeFf";
 
        TEST_RESULT_VOID(decodeToBin(encodingHex, decodeMixed, destinationDecode), "decode");
        TEST_RESULT_VOID(
            encodeToStr(encodingHex, destinationDecode, decodeToBinSize(encodingHex, decodeMixed), destinationEncode), "encode");
        TEST_RESULT_Z(destinationEncode, "0123456789aabbccddeeff", "check encoded hex");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("decode/encode all values");
 
        uint8_t decodedAll[256];
 
        for (unsigned int decodeIdx = 0; decodeIdx < sizeof(decodedAll); decodeIdx++)
            decodedAll[decodeIdx] = (uint8_t)decodeIdx;
 
        encodeToStr(encodingHex, decodedAll, sizeof(decodedAll), destinationEncode);
        TEST_RESULT_UINT(strlen(destinationEncode), 512, "all values encoded size");
        TEST_RESULT_INT(memcmp(destinationEncode, encodeHexLookup, 512), 0, "all values encoded");
        decodeToBin(encodingHex, destinationEncode, destinationDecode);
        TEST_RESULT_INT(memcmp(decodedAll, destinationDecode, sizeof(decodedAll)), 0, "all values decoded");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("decode errors");
 
        TEST_ERROR(decodeToBin(encodingHex, "c", destinationDecode), FormatError, "hex size 1 is not evenly divisible by 2");
 
        TEST_ERROR(
            decodeToBin(encodingHex, "hh", destinationDecode), FormatError,
            "hex invalid character found at position 0");
        TEST_ERROR(
            decodeToBin(encodingHex, "c\xc3", destinationDecode), FormatError,
            "hex invalid character found at position 1");
    }
 
    FUNCTION_HARNESS_RETURN_VOID();
}