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