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