Skip to content

Commit a056ee2

Browse files
committed
Add a WPACKET test for the new DER capability
Reviewed-by: Richard Levitte <[email protected]> (Merged from openssl#11462)
1 parent d3ba391 commit a056ee2

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

test/wpackettest.c

+67
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <string.h>
1111
#include <openssl/buffer.h>
12+
#include <openssl/rand.h>
1213
#include "internal/packet.h"
1314
#include "testutil.h"
1415

@@ -21,6 +22,9 @@ static const unsigned char empty[] = { 0x00 };
2122
static const unsigned char alloc[] = { 0x02, 0xfe, 0xff };
2223
static const unsigned char submem[] = { 0x03, 0x02, 0xfe, 0xff };
2324
static const unsigned char fixed[] = { 0xff, 0xff, 0xff };
25+
static const unsigned char simpleder[] = {
26+
0xfc, 0x04, 0x00, 0x01, 0x02, 0x03, 0xff, 0xfe, 0xfd
27+
};
2428

2529
static BUF_MEM *buf;
2630

@@ -349,6 +353,68 @@ static int test_WPACKET_memcpy(void)
349353
return 1;
350354
}
351355

356+
static int test_WPACKET_init_der(void)
357+
{
358+
WPACKET pkt;
359+
unsigned char sbuf[1024];
360+
unsigned char testdata[] = { 0x00, 0x01, 0x02, 0x03 };
361+
unsigned char testdata2[259] = { 0x82, 0x01, 0x00 };
362+
size_t written[2];
363+
int i;
364+
365+
/* Test initialising for writing DER */
366+
if (!TEST_true(WPACKET_init_der(&pkt, sbuf, sizeof(sbuf)))
367+
|| !TEST_true(WPACKET_put_bytes_u24(&pkt, 0xfffefd))
368+
/* Test writing data in a length prefixed sub-packet */
369+
|| !TEST_true(WPACKET_start_sub_packet(&pkt))
370+
|| !TEST_true(WPACKET_memcpy(&pkt, testdata, sizeof(testdata)))
371+
|| !TEST_true(WPACKET_close(&pkt))
372+
|| !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xfc))
373+
|| !TEST_true(WPACKET_finish(&pkt))
374+
|| !TEST_true(WPACKET_get_total_written(&pkt, &written[0]))
375+
|| !TEST_mem_eq(WPACKET_get_curr(&pkt), written[0], simpleder,
376+
sizeof(simpleder)))
377+
return cleanup(&pkt);
378+
379+
/* Generate random packet data for test */
380+
if (!TEST_true(RAND_bytes(&testdata2[3], sizeof(testdata2) - 3)))
381+
return 0;
382+
383+
/*
384+
* Test with a sub-packet that has 2 length bytes. We do 2 passes - first
385+
* with a NULL buffer, just to calculate lengths, and a second pass with a
386+
* real buffer to actually generate a packet
387+
*/
388+
for (i = 0; i < 2; i++) {
389+
if (i == 0) {
390+
if (!TEST_true(WPACKET_init_null_der(&pkt)))
391+
return 0;
392+
} else {
393+
if (!TEST_true(WPACKET_init_der(&pkt, sbuf, sizeof(sbuf))))
394+
return 0;
395+
}
396+
if (!TEST_true(WPACKET_start_sub_packet(&pkt))
397+
|| !TEST_true(WPACKET_memcpy(&pkt, &testdata2[3],
398+
sizeof(testdata2) - 3))
399+
|| !TEST_true(WPACKET_close(&pkt))
400+
|| !TEST_true(WPACKET_finish(&pkt))
401+
|| !TEST_true(WPACKET_get_total_written(&pkt, &written[i])))
402+
return cleanup(&pkt);
403+
}
404+
405+
/*
406+
* Check that the size calculated in the first pass equals the size of the
407+
* packet actually generated in the second pass. Also check the generated
408+
* packet looks as we expect it to.
409+
*/
410+
if (!TEST_size_t_eq(written[0], written[1])
411+
|| !TEST_mem_eq(WPACKET_get_curr(&pkt), written[1], testdata2,
412+
sizeof(testdata2)))
413+
return 0;
414+
415+
return 1;
416+
}
417+
352418
int setup_tests(void)
353419
{
354420
if (!TEST_ptr(buf = BUF_MEM_new()))
@@ -360,6 +426,7 @@ int setup_tests(void)
360426
ADD_TEST(test_WPACKET_set_flags);
361427
ADD_TEST(test_WPACKET_allocate_bytes);
362428
ADD_TEST(test_WPACKET_memcpy);
429+
ADD_TEST(test_WPACKET_init_der);
363430
return 1;
364431
}
365432

0 commit comments

Comments
 (0)