summaryrefslogtreecommitdiff
path: root/regress/unittests/sshbuf/test_sshbuf_misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'regress/unittests/sshbuf/test_sshbuf_misc.c')
-rw-r--r--regress/unittests/sshbuf/test_sshbuf_misc.c134
1 files changed, 134 insertions, 0 deletions
diff --git a/regress/unittests/sshbuf/test_sshbuf_misc.c b/regress/unittests/sshbuf/test_sshbuf_misc.c
new file mode 100644
index 000000000..a5b1ab2c9
--- /dev/null
+++ b/regress/unittests/sshbuf/test_sshbuf_misc.c
@@ -0,0 +1,134 @@
1/* $OpenBSD: test_sshbuf_misc.c,v 1.1 2014/04/30 05:32:00 djm Exp $ */
2/*
3 * Regress test for sshbuf.h buffer API
4 *
5 * Placed in the public domain
6 */
7
8#include <sys/types.h>
9#include <sys/param.h>
10#include <stdio.h>
11#include <stdint.h>
12#include <stdlib.h>
13#include <string.h>
14
15#include "test_helper.h"
16
17#include "sshbuf.h"
18
19void sshbuf_misc_tests(void);
20
21void
22sshbuf_misc_tests(void)
23{
24 struct sshbuf *p1;
25 char tmp[512], *p;
26 FILE *out;
27 size_t sz;
28
29 TEST_START("sshbuf_dump");
30 out = tmpfile();
31 ASSERT_PTR_NE(out, NULL);
32 p1 = sshbuf_new();
33 ASSERT_PTR_NE(p1, NULL);
34 ASSERT_INT_EQ(sshbuf_put_u32(p1, 0x12345678), 0);
35 sshbuf_dump(p1, out);
36 fflush(out);
37 rewind(out);
38 sz = fread(tmp, 1, sizeof(tmp), out);
39 ASSERT_INT_EQ(ferror(out), 0);
40 ASSERT_INT_NE(feof(out), 0);
41 ASSERT_SIZE_T_GT(sz, 0);
42 tmp[sz] = '\0';
43 ASSERT_PTR_NE(strstr(tmp, "12 34 56 78"), NULL);
44 fclose(out);
45 sshbuf_free(p1);
46 TEST_DONE();
47
48 TEST_START("sshbuf_dtob16");
49 p1 = sshbuf_new();
50 ASSERT_PTR_NE(p1, NULL);
51 ASSERT_INT_EQ(sshbuf_put_u32(p1, 0x12345678), 0);
52 p = sshbuf_dtob16(p1);
53 ASSERT_PTR_NE(p, NULL);
54 ASSERT_STRING_EQ(p, "12345678");
55 free(p);
56 sshbuf_free(p1);
57 TEST_DONE();
58
59 TEST_START("sshbuf_dtob64 len 1");
60 p1 = sshbuf_new();
61 ASSERT_PTR_NE(p1, NULL);
62 ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x11), 0);
63 p = sshbuf_dtob64(p1);
64 ASSERT_PTR_NE(p, NULL);
65 ASSERT_STRING_EQ(p, "EQ==");
66 free(p);
67 sshbuf_free(p1);
68 TEST_DONE();
69
70 TEST_START("sshbuf_dtob64 len 2");
71 p1 = sshbuf_new();
72 ASSERT_PTR_NE(p1, NULL);
73 ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x11), 0);
74 ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x22), 0);
75 p = sshbuf_dtob64(p1);
76 ASSERT_PTR_NE(p, NULL);
77 ASSERT_STRING_EQ(p, "ESI=");
78 free(p);
79 sshbuf_free(p1);
80 TEST_DONE();
81
82 TEST_START("sshbuf_dtob64 len 3");
83 p1 = sshbuf_new();
84 ASSERT_PTR_NE(p1, NULL);
85 ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x11), 0);
86 ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x22), 0);
87 ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x33), 0);
88 p = sshbuf_dtob64(p1);
89 ASSERT_PTR_NE(p, NULL);
90 ASSERT_STRING_EQ(p, "ESIz");
91 free(p);
92 sshbuf_free(p1);
93 TEST_DONE();
94
95 TEST_START("sshbuf_dtob64 len 8191");
96 p1 = sshbuf_new();
97 ASSERT_PTR_NE(p1, NULL);
98 ASSERT_INT_EQ(sshbuf_reserve(p1, 8192, NULL), 0);
99 bzero(sshbuf_mutable_ptr(p1), 8192);
100 p = sshbuf_dtob64(p1);
101 ASSERT_PTR_NE(p, NULL);
102 ASSERT_SIZE_T_EQ(strlen(p), ((8191 + 2) / 3) * 4);
103 free(p);
104 sshbuf_free(p1);
105 TEST_DONE();
106
107 TEST_START("sshbuf_b64tod len 1");
108 p1 = sshbuf_new();
109 ASSERT_PTR_NE(p1, NULL);
110 ASSERT_INT_EQ(sshbuf_b64tod(p1, "0A=="), 0);
111 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
112 ASSERT_U8_EQ(*sshbuf_ptr(p1), 0xd0);
113 sshbuf_free(p1);
114 TEST_DONE();
115
116 TEST_START("sshbuf_b64tod len 2");
117 p1 = sshbuf_new();
118 ASSERT_PTR_NE(p1, NULL);
119 ASSERT_INT_EQ(sshbuf_b64tod(p1, "0A8="), 0);
120 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
121 ASSERT_U16_EQ(PEEK_U16(sshbuf_ptr(p1)), 0xd00f);
122 sshbuf_free(p1);
123 TEST_DONE();
124
125 TEST_START("sshbuf_b64tod len 4");
126 p1 = sshbuf_new();
127 ASSERT_PTR_NE(p1, NULL);
128 ASSERT_INT_EQ(sshbuf_b64tod(p1, "0A/QDw=="), 0);
129 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
130 ASSERT_U32_EQ(PEEK_U32(sshbuf_ptr(p1)), 0xd00fd00f);
131 sshbuf_free(p1);
132 TEST_DONE();
133}
134