test_sshbuf.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* $OpenBSD: test_sshbuf.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. #define SSHBUF_INTERNAL 1 /* access internals for testing */
  8. #include "includes.h"
  9. #include <sys/types.h>
  10. #include <sys/param.h>
  11. #include <stdio.h>
  12. #ifdef HAVE_STDINT_H
  13. # include <stdint.h>
  14. #endif
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "../test_helper/test_helper.h"
  18. #include "ssherr.h"
  19. #include "sshbuf.h"
  20. void sshbuf_tests(void);
  21. void
  22. sshbuf_tests(void)
  23. {
  24. struct sshbuf *p1;
  25. const u_char *cdp;
  26. u_char *dp;
  27. size_t sz;
  28. int r;
  29. TEST_START("allocate sshbuf");
  30. p1 = sshbuf_new();
  31. ASSERT_PTR_NE(p1, NULL);
  32. TEST_DONE();
  33. TEST_START("max size on fresh buffer");
  34. ASSERT_SIZE_T_GT(sshbuf_max_size(p1), 0);
  35. TEST_DONE();
  36. TEST_START("available on fresh buffer");
  37. ASSERT_SIZE_T_GT(sshbuf_avail(p1), 0);
  38. TEST_DONE();
  39. TEST_START("len = 0 on empty buffer");
  40. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
  41. TEST_DONE();
  42. TEST_START("set valid max size");
  43. ASSERT_INT_EQ(sshbuf_set_max_size(p1, 65536), 0);
  44. ASSERT_SIZE_T_EQ(sshbuf_max_size(p1), 65536);
  45. TEST_DONE();
  46. TEST_START("available on limited buffer");
  47. ASSERT_SIZE_T_EQ(sshbuf_avail(p1), 65536);
  48. TEST_DONE();
  49. TEST_START("free");
  50. sshbuf_free(p1);
  51. TEST_DONE();
  52. TEST_START("consume on empty buffer");
  53. p1 = sshbuf_new();
  54. ASSERT_PTR_NE(p1, NULL);
  55. ASSERT_INT_EQ(sshbuf_consume(p1, 0), 0);
  56. ASSERT_INT_EQ(sshbuf_consume(p1, 1), SSH_ERR_MESSAGE_INCOMPLETE);
  57. sshbuf_free(p1);
  58. TEST_DONE();
  59. TEST_START("consume_end on empty buffer");
  60. p1 = sshbuf_new();
  61. ASSERT_PTR_NE(p1, NULL);
  62. ASSERT_INT_EQ(sshbuf_consume_end(p1, 0), 0);
  63. ASSERT_INT_EQ(sshbuf_consume_end(p1, 1), SSH_ERR_MESSAGE_INCOMPLETE);
  64. sshbuf_free(p1);
  65. TEST_DONE();
  66. TEST_START("reserve space");
  67. p1 = sshbuf_new();
  68. ASSERT_PTR_NE(p1, NULL);
  69. r = sshbuf_reserve(p1, 1, &dp);
  70. ASSERT_INT_EQ(r, 0);
  71. ASSERT_PTR_NE(dp, NULL);
  72. *dp = 0x11;
  73. r = sshbuf_reserve(p1, 3, &dp);
  74. ASSERT_INT_EQ(r, 0);
  75. ASSERT_PTR_NE(dp, NULL);
  76. *dp++ = 0x22;
  77. *dp++ = 0x33;
  78. *dp++ = 0x44;
  79. TEST_DONE();
  80. TEST_START("sshbuf_len on filled buffer");
  81. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
  82. TEST_DONE();
  83. TEST_START("sshbuf_ptr on filled buffer");
  84. cdp = sshbuf_ptr(p1);
  85. ASSERT_PTR_NE(cdp, NULL);
  86. ASSERT_U8_EQ(cdp[0], 0x11);
  87. ASSERT_U8_EQ(cdp[1], 0x22);
  88. ASSERT_U8_EQ(cdp[2], 0x33);
  89. ASSERT_U8_EQ(cdp[3], 0x44);
  90. TEST_DONE();
  91. TEST_START("consume on filled buffer");
  92. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
  93. ASSERT_INT_EQ(sshbuf_consume(p1, 0), 0);
  94. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
  95. r = sshbuf_consume(p1, 64);
  96. ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
  97. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
  98. ASSERT_INT_EQ(sshbuf_consume(p1, 1), 0);
  99. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 3);
  100. cdp = sshbuf_ptr(p1);
  101. ASSERT_PTR_NE(p1, NULL);
  102. ASSERT_U8_EQ(cdp[0], 0x22);
  103. ASSERT_INT_EQ(sshbuf_consume(p1, 2), 0);
  104. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
  105. cdp = sshbuf_ptr(p1);
  106. ASSERT_PTR_NE(p1, NULL);
  107. ASSERT_U8_EQ(cdp[0], 0x44);
  108. r = sshbuf_consume(p1, 2);
  109. ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
  110. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
  111. ASSERT_INT_EQ(sshbuf_consume(p1, 1), 0);
  112. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
  113. r = sshbuf_consume(p1, 1);
  114. ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
  115. sshbuf_free(p1);
  116. TEST_DONE();
  117. TEST_START("consume_end on filled buffer");
  118. p1 = sshbuf_new();
  119. ASSERT_PTR_NE(p1, NULL);
  120. r = sshbuf_reserve(p1, 4, &dp);
  121. ASSERT_INT_EQ(r, 0);
  122. ASSERT_PTR_NE(dp, NULL);
  123. *dp++ = 0x11;
  124. *dp++ = 0x22;
  125. *dp++ = 0x33;
  126. *dp++ = 0x44;
  127. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
  128. r = sshbuf_consume_end(p1, 5);
  129. ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
  130. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
  131. ASSERT_INT_EQ(sshbuf_consume_end(p1, 3), 0);
  132. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
  133. cdp = sshbuf_ptr(p1);
  134. ASSERT_PTR_NE(cdp, NULL);
  135. ASSERT_U8_EQ(*cdp, 0x11);
  136. r = sshbuf_consume_end(p1, 2);
  137. ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
  138. ASSERT_INT_EQ(sshbuf_consume_end(p1, 1), 0);
  139. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
  140. sshbuf_free(p1);
  141. TEST_DONE();
  142. TEST_START("fill limited buffer");
  143. p1 = sshbuf_new();
  144. ASSERT_PTR_NE(p1, NULL);
  145. ASSERT_INT_EQ(sshbuf_set_max_size(p1, 1223), 0);
  146. ASSERT_SIZE_T_EQ(sshbuf_max_size(p1), 1223);
  147. ASSERT_SIZE_T_EQ(sshbuf_avail(p1), 1223);
  148. r = sshbuf_reserve(p1, 1223, &dp);
  149. ASSERT_INT_EQ(r, 0);
  150. ASSERT_PTR_NE(dp, NULL);
  151. memset(dp, 0xd7, 1223);
  152. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1223);
  153. ASSERT_SIZE_T_EQ(sshbuf_avail(p1), 0);
  154. r = sshbuf_reserve(p1, 1, &dp);
  155. ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
  156. ASSERT_PTR_EQ(dp, NULL);
  157. TEST_DONE();
  158. TEST_START("consume and force compaction");
  159. ASSERT_INT_EQ(sshbuf_consume(p1, 223), 0);
  160. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1000);
  161. ASSERT_SIZE_T_EQ(sshbuf_avail(p1), 223);
  162. r = sshbuf_reserve(p1, 224, &dp);
  163. ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
  164. ASSERT_PTR_EQ(dp, NULL);
  165. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1000);
  166. ASSERT_SIZE_T_EQ(sshbuf_avail(p1), 223);
  167. r = sshbuf_reserve(p1, 223, &dp);
  168. ASSERT_INT_EQ(r, 0);
  169. ASSERT_PTR_NE(dp, NULL);
  170. memset(dp, 0x7d, 223);
  171. cdp = sshbuf_ptr(p1);
  172. ASSERT_PTR_NE(cdp, NULL);
  173. ASSERT_MEM_FILLED_EQ(cdp, 0xd7, 1000);
  174. ASSERT_MEM_FILLED_EQ(cdp + 1000, 0x7d, 223);
  175. TEST_DONE();
  176. TEST_START("resize full buffer");
  177. r = sshbuf_set_max_size(p1, 1000);
  178. ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
  179. sz = roundup(1223 + SSHBUF_SIZE_INC * 3, SSHBUF_SIZE_INC);
  180. ASSERT_INT_EQ(sshbuf_set_max_size(p1, sz), 0);
  181. ASSERT_SIZE_T_EQ(sshbuf_max_size(p1), sz);
  182. ASSERT_SIZE_T_EQ(sshbuf_avail(p1), sz - 1223);
  183. ASSERT_INT_EQ(sshbuf_len(p1), 1223);
  184. TEST_DONE();
  185. /* NB. uses sshbuf internals */
  186. TEST_START("alloc chunking");
  187. r = sshbuf_reserve(p1, 1, &dp);
  188. ASSERT_INT_EQ(r, 0);
  189. ASSERT_PTR_NE(dp, NULL);
  190. *dp = 0xff;
  191. cdp = sshbuf_ptr(p1);
  192. ASSERT_PTR_NE(cdp, NULL);
  193. ASSERT_MEM_FILLED_EQ(cdp, 0xd7, 1000);
  194. ASSERT_MEM_FILLED_EQ(cdp + 1000, 0x7d, 223);
  195. ASSERT_MEM_FILLED_EQ(cdp + 1223, 0xff, 1);
  196. ASSERT_SIZE_T_EQ(sshbuf_alloc(p1) % SSHBUF_SIZE_INC, 0);
  197. sshbuf_free(p1);
  198. TEST_DONE();
  199. TEST_START("reset buffer");
  200. p1 = sshbuf_new();
  201. ASSERT_PTR_NE(p1, NULL);
  202. ASSERT_INT_EQ(sshbuf_set_max_size(p1, 1223), 0);
  203. ASSERT_SIZE_T_EQ(sshbuf_max_size(p1), 1223);
  204. r = sshbuf_reserve(p1, 1223, &dp);
  205. ASSERT_INT_EQ(r, 0);
  206. ASSERT_PTR_NE(dp, NULL);
  207. memset(dp, 0xd7, 1223);
  208. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1223);
  209. sshbuf_reset(p1);
  210. ASSERT_SIZE_T_EQ(sshbuf_max_size(p1), 1223);
  211. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
  212. ASSERT_SIZE_T_EQ(sshbuf_avail(p1), 1223);
  213. sshbuf_free(p1);
  214. TEST_DONE();
  215. }