1276707Sdes/* $OpenBSD: test_sshbuf_fixed.c,v 1.1 2014/04/30 05:32:00 djm Exp $ */ 2276707Sdes/* 3276707Sdes * Regress test for sshbuf.h buffer API 4276707Sdes * 5276707Sdes * Placed in the public domain 6276707Sdes */ 7276707Sdes 8276707Sdes#define SSHBUF_INTERNAL 1 /* access internals for testing */ 9276707Sdes#include "includes.h" 10276707Sdes 11276707Sdes#include <sys/types.h> 12276707Sdes#include <sys/param.h> 13276707Sdes#include <stdio.h> 14276707Sdes#ifdef HAVE_STDINT_H 15276707Sdes# include <stdint.h> 16276707Sdes#endif 17276707Sdes#include <stdlib.h> 18276707Sdes#include <string.h> 19276707Sdes 20276707Sdes#include "../test_helper/test_helper.h" 21276707Sdes 22276707Sdes#include "sshbuf.h" 23276707Sdes#include "ssherr.h" 24276707Sdes 25276707Sdesvoid sshbuf_fixed(void); 26276707Sdes 27276707Sdesconst u_char test_buf[] = "\x01\x12\x34\x56\x78\x00\x00\x00\x05hello"; 28276707Sdes 29276707Sdesvoid 30276707Sdessshbuf_fixed(void) 31276707Sdes{ 32276707Sdes struct sshbuf *p1, *p2, *p3; 33276707Sdes u_char c; 34276707Sdes char *s; 35276707Sdes u_int i; 36276707Sdes size_t l; 37276707Sdes 38276707Sdes TEST_START("sshbuf_from"); 39276707Sdes p1 = sshbuf_from(test_buf, sizeof(test_buf)); 40276707Sdes ASSERT_PTR_NE(p1, NULL); 41276707Sdes ASSERT_PTR_EQ(sshbuf_mutable_ptr(p1), NULL); 42276707Sdes ASSERT_INT_EQ(sshbuf_check_reserve(p1, 1), SSH_ERR_BUFFER_READ_ONLY); 43276707Sdes ASSERT_INT_EQ(sshbuf_reserve(p1, 1, NULL), SSH_ERR_BUFFER_READ_ONLY); 44276707Sdes ASSERT_INT_EQ(sshbuf_set_max_size(p1, 200), SSH_ERR_BUFFER_READ_ONLY); 45276707Sdes ASSERT_INT_EQ(sshbuf_put_u32(p1, 0x12345678), SSH_ERR_BUFFER_READ_ONLY); 46276707Sdes ASSERT_SIZE_T_EQ(sshbuf_avail(p1), 0); 47276707Sdes ASSERT_PTR_EQ(sshbuf_ptr(p1), test_buf); 48276707Sdes sshbuf_free(p1); 49276707Sdes TEST_DONE(); 50276707Sdes 51276707Sdes TEST_START("sshbuf_from data"); 52276707Sdes p1 = sshbuf_from(test_buf, sizeof(test_buf) - 1); 53276707Sdes ASSERT_PTR_NE(p1, NULL); 54276707Sdes ASSERT_PTR_EQ(sshbuf_ptr(p1), test_buf); 55276707Sdes ASSERT_INT_EQ(sshbuf_get_u8(p1, &c), 0); 56276707Sdes ASSERT_PTR_EQ(sshbuf_ptr(p1), test_buf + 1); 57276707Sdes ASSERT_U8_EQ(c, 1); 58276707Sdes ASSERT_INT_EQ(sshbuf_get_u32(p1, &i), 0); 59276707Sdes ASSERT_PTR_EQ(sshbuf_ptr(p1), test_buf + 5); 60276707Sdes ASSERT_U32_EQ(i, 0x12345678); 61276707Sdes ASSERT_INT_EQ(sshbuf_get_cstring(p1, &s, &l), 0); 62276707Sdes ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0); 63276707Sdes ASSERT_STRING_EQ(s, "hello"); 64276707Sdes ASSERT_SIZE_T_EQ(l, 5); 65276707Sdes sshbuf_free(p1); 66276707Sdes free(s); 67276707Sdes TEST_DONE(); 68276707Sdes 69276707Sdes TEST_START("sshbuf_fromb "); 70276707Sdes p1 = sshbuf_new(); 71276707Sdes ASSERT_PTR_NE(p1, NULL); 72276707Sdes ASSERT_U_INT_EQ(sshbuf_refcount(p1), 1); 73276707Sdes ASSERT_PTR_EQ(sshbuf_parent(p1), NULL); 74276707Sdes ASSERT_INT_EQ(sshbuf_put(p1, test_buf, sizeof(test_buf) - 1), 0); 75276707Sdes p2 = sshbuf_fromb(p1); 76276707Sdes ASSERT_PTR_NE(p2, NULL); 77276707Sdes ASSERT_U_INT_EQ(sshbuf_refcount(p1), 2); 78276707Sdes ASSERT_PTR_EQ(sshbuf_parent(p1), NULL); 79276707Sdes ASSERT_PTR_EQ(sshbuf_parent(p2), p1); 80276707Sdes ASSERT_PTR_EQ(sshbuf_ptr(p2), sshbuf_ptr(p1)); 81276707Sdes ASSERT_PTR_NE(sshbuf_ptr(p1), NULL); 82276707Sdes ASSERT_PTR_NE(sshbuf_ptr(p2), NULL); 83276707Sdes ASSERT_PTR_EQ(sshbuf_mutable_ptr(p1), NULL); 84276707Sdes ASSERT_PTR_EQ(sshbuf_mutable_ptr(p2), NULL); 85276707Sdes ASSERT_SIZE_T_EQ(sshbuf_len(p1), sshbuf_len(p2)); 86276707Sdes ASSERT_INT_EQ(sshbuf_get_u8(p2, &c), 0); 87276707Sdes ASSERT_PTR_EQ(sshbuf_ptr(p2), sshbuf_ptr(p1) + 1); 88276707Sdes ASSERT_U8_EQ(c, 1); 89276707Sdes ASSERT_INT_EQ(sshbuf_get_u32(p2, &i), 0); 90276707Sdes ASSERT_PTR_EQ(sshbuf_ptr(p2), sshbuf_ptr(p1) + 5); 91276707Sdes ASSERT_U32_EQ(i, 0x12345678); 92276707Sdes ASSERT_INT_EQ(sshbuf_get_cstring(p2, &s, &l), 0); 93276707Sdes ASSERT_SIZE_T_EQ(sshbuf_len(p2), 0); 94276707Sdes ASSERT_STRING_EQ(s, "hello"); 95276707Sdes ASSERT_SIZE_T_EQ(l, 5); 96276707Sdes sshbuf_free(p1); 97276707Sdes ASSERT_U_INT_EQ(sshbuf_refcount(p1), 1); 98276707Sdes sshbuf_free(p2); 99276707Sdes free(s); 100276707Sdes TEST_DONE(); 101276707Sdes 102276707Sdes TEST_START("sshbuf_froms"); 103276707Sdes p1 = sshbuf_new(); 104276707Sdes ASSERT_PTR_NE(p1, NULL); 105276707Sdes ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x01), 0); 106276707Sdes ASSERT_INT_EQ(sshbuf_put_u32(p1, 0x12345678), 0); 107276707Sdes ASSERT_INT_EQ(sshbuf_put_cstring(p1, "hello"), 0); 108276707Sdes p2 = sshbuf_new(); 109276707Sdes ASSERT_PTR_NE(p2, NULL); 110276707Sdes ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(test_buf) - 1); 111276707Sdes ASSERT_INT_EQ(sshbuf_put_stringb(p2, p1), 0); 112276707Sdes ASSERT_SIZE_T_EQ(sshbuf_len(p2), sizeof(test_buf) + 4 - 1); 113276707Sdes ASSERT_INT_EQ(sshbuf_froms(p2, &p3), 0); 114276707Sdes ASSERT_SIZE_T_EQ(sshbuf_len(p2), 0); 115276707Sdes ASSERT_PTR_NE(p3, NULL); 116276707Sdes ASSERT_PTR_NE(sshbuf_ptr(p3), NULL); 117276707Sdes ASSERT_SIZE_T_EQ(sshbuf_len(p3), sizeof(test_buf) - 1); 118276707Sdes ASSERT_MEM_EQ(sshbuf_ptr(p3), test_buf, sizeof(test_buf) - 1); 119276707Sdes sshbuf_free(p3); 120276707Sdes ASSERT_INT_EQ(sshbuf_put_stringb(p2, p1), 0); 121276707Sdes ASSERT_INT_EQ(sshbuf_consume_end(p2, 1), 0); 122276707Sdes ASSERT_INT_EQ(sshbuf_froms(p2, &p3), SSH_ERR_MESSAGE_INCOMPLETE); 123276707Sdes ASSERT_PTR_EQ(p3, NULL); 124276707Sdes sshbuf_free(p2); 125276707Sdes sshbuf_free(p1); 126276707Sdes} 127