1 /* $OpenBSD: test_sshbuf_fixed.c,v 1.2 2021/12/14 21:25:27 deraadt Exp $ */
2 /*
3 * Regress test for sshbuf.h buffer API
4 *
5 * Placed in the public domain
6 */
7
8 #define SSHBUF_INTERNAL 1 /* access internals for testing */
9 #include "includes.h"
10
11 #include <sys/types.h>
12 #include <stdio.h>
13 #ifdef HAVE_STDINT_H
14 # include <stdint.h>
15 #endif
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include "../test_helper/test_helper.h"
20
21 #include "sshbuf.h"
22 #include "ssherr.h"
23
24 void sshbuf_fixed(void);
25
26 const u_char test_buf[] = "\x01\x12\x34\x56\x78\x00\x00\x00\x05hello";
27
28 void
sshbuf_fixed(void)29 sshbuf_fixed(void)
30 {
31 struct sshbuf *p1, *p2, *p3;
32 u_char c;
33 char *s;
34 u_int i;
35 size_t l;
36
37 TEST_START("sshbuf_from");
38 p1 = sshbuf_from(test_buf, sizeof(test_buf));
39 ASSERT_PTR_NE(p1, NULL);
40 ASSERT_PTR_EQ(sshbuf_mutable_ptr(p1), NULL);
41 ASSERT_INT_EQ(sshbuf_check_reserve(p1, 1), SSH_ERR_BUFFER_READ_ONLY);
42 ASSERT_INT_EQ(sshbuf_reserve(p1, 1, NULL), SSH_ERR_BUFFER_READ_ONLY);
43 ASSERT_INT_EQ(sshbuf_set_max_size(p1, 200), SSH_ERR_BUFFER_READ_ONLY);
44 ASSERT_INT_EQ(sshbuf_put_u32(p1, 0x12345678), SSH_ERR_BUFFER_READ_ONLY);
45 ASSERT_SIZE_T_EQ(sshbuf_avail(p1), 0);
46 ASSERT_PTR_EQ(sshbuf_ptr(p1), test_buf);
47 sshbuf_free(p1);
48 TEST_DONE();
49
50 TEST_START("sshbuf_from data");
51 p1 = sshbuf_from(test_buf, sizeof(test_buf) - 1);
52 ASSERT_PTR_NE(p1, NULL);
53 ASSERT_PTR_EQ(sshbuf_ptr(p1), test_buf);
54 ASSERT_INT_EQ(sshbuf_get_u8(p1, &c), 0);
55 ASSERT_PTR_EQ(sshbuf_ptr(p1), test_buf + 1);
56 ASSERT_U8_EQ(c, 1);
57 ASSERT_INT_EQ(sshbuf_get_u32(p1, &i), 0);
58 ASSERT_PTR_EQ(sshbuf_ptr(p1), test_buf + 5);
59 ASSERT_U32_EQ(i, 0x12345678);
60 ASSERT_INT_EQ(sshbuf_get_cstring(p1, &s, &l), 0);
61 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
62 ASSERT_STRING_EQ(s, "hello");
63 ASSERT_SIZE_T_EQ(l, 5);
64 sshbuf_free(p1);
65 free(s);
66 TEST_DONE();
67
68 TEST_START("sshbuf_fromb ");
69 p1 = sshbuf_new();
70 ASSERT_PTR_NE(p1, NULL);
71 ASSERT_U_INT_EQ(sshbuf_refcount(p1), 1);
72 ASSERT_PTR_EQ(sshbuf_parent(p1), NULL);
73 ASSERT_INT_EQ(sshbuf_put(p1, test_buf, sizeof(test_buf) - 1), 0);
74 p2 = sshbuf_fromb(p1);
75 ASSERT_PTR_NE(p2, NULL);
76 ASSERT_U_INT_EQ(sshbuf_refcount(p1), 2);
77 ASSERT_PTR_EQ(sshbuf_parent(p1), NULL);
78 ASSERT_PTR_EQ(sshbuf_parent(p2), p1);
79 ASSERT_PTR_EQ(sshbuf_ptr(p2), sshbuf_ptr(p1));
80 ASSERT_PTR_NE(sshbuf_ptr(p1), NULL);
81 ASSERT_PTR_NE(sshbuf_ptr(p2), NULL);
82 ASSERT_PTR_EQ(sshbuf_mutable_ptr(p1), NULL);
83 ASSERT_PTR_EQ(sshbuf_mutable_ptr(p2), NULL);
84 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sshbuf_len(p2));
85 ASSERT_INT_EQ(sshbuf_get_u8(p2, &c), 0);
86 ASSERT_PTR_EQ(sshbuf_ptr(p2), sshbuf_ptr(p1) + 1);
87 ASSERT_U8_EQ(c, 1);
88 ASSERT_INT_EQ(sshbuf_get_u32(p2, &i), 0);
89 ASSERT_PTR_EQ(sshbuf_ptr(p2), sshbuf_ptr(p1) + 5);
90 ASSERT_U32_EQ(i, 0x12345678);
91 ASSERT_INT_EQ(sshbuf_get_cstring(p2, &s, &l), 0);
92 ASSERT_SIZE_T_EQ(sshbuf_len(p2), 0);
93 ASSERT_STRING_EQ(s, "hello");
94 ASSERT_SIZE_T_EQ(l, 5);
95 sshbuf_free(p1);
96 ASSERT_U_INT_EQ(sshbuf_refcount(p1), 1);
97 sshbuf_free(p2);
98 free(s);
99 TEST_DONE();
100
101 TEST_START("sshbuf_froms");
102 p1 = sshbuf_new();
103 ASSERT_PTR_NE(p1, NULL);
104 ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x01), 0);
105 ASSERT_INT_EQ(sshbuf_put_u32(p1, 0x12345678), 0);
106 ASSERT_INT_EQ(sshbuf_put_cstring(p1, "hello"), 0);
107 p2 = sshbuf_new();
108 ASSERT_PTR_NE(p2, NULL);
109 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(test_buf) - 1);
110 ASSERT_INT_EQ(sshbuf_put_stringb(p2, p1), 0);
111 ASSERT_SIZE_T_EQ(sshbuf_len(p2), sizeof(test_buf) + 4 - 1);
112 ASSERT_INT_EQ(sshbuf_froms(p2, &p3), 0);
113 ASSERT_SIZE_T_EQ(sshbuf_len(p2), 0);
114 ASSERT_PTR_NE(p3, NULL);
115 ASSERT_PTR_NE(sshbuf_ptr(p3), NULL);
116 ASSERT_SIZE_T_EQ(sshbuf_len(p3), sizeof(test_buf) - 1);
117 ASSERT_MEM_EQ(sshbuf_ptr(p3), test_buf, sizeof(test_buf) - 1);
118 sshbuf_free(p3);
119 ASSERT_INT_EQ(sshbuf_put_stringb(p2, p1), 0);
120 ASSERT_INT_EQ(sshbuf_consume_end(p2, 1), 0);
121 ASSERT_INT_EQ(sshbuf_froms(p2, &p3), SSH_ERR_MESSAGE_INCOMPLETE);
122 ASSERT_PTR_EQ(p3, NULL);
123 sshbuf_free(p2);
124 sshbuf_free(p1);
125 }
126