test_sshbuf_misc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* $OpenBSD: test_sshbuf_misc.c,v 1.4 2019/07/16 22:16:49 djm Exp $ */
  2. /*
  3. * Regress test for sshbuf.h buffer API
  4. *
  5. * Placed in the public domain
  6. */
  7. #include "includes.h"
  8. #include <sys/types.h>
  9. #include <sys/param.h>
  10. #include <stdio.h>
  11. #ifdef HAVE_STDINT_H
  12. # include <stdint.h>
  13. #endif
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "../test_helper/test_helper.h"
  17. #include "sshbuf.h"
  18. #include "ssherr.h"
  19. void sshbuf_misc_tests(void);
  20. void
  21. sshbuf_misc_tests(void)
  22. {
  23. struct sshbuf *p1;
  24. char tmp[512], msg[] = "imploring ping silence ping over", *p;
  25. FILE *out;
  26. size_t sz;
  27. TEST_START("sshbuf_dump");
  28. out = tmpfile();
  29. ASSERT_PTR_NE(out, NULL);
  30. p1 = sshbuf_new();
  31. ASSERT_PTR_NE(p1, NULL);
  32. ASSERT_INT_EQ(sshbuf_put_u32(p1, 0x12345678), 0);
  33. sshbuf_dump(p1, out);
  34. fflush(out);
  35. rewind(out);
  36. sz = fread(tmp, 1, sizeof(tmp), out);
  37. ASSERT_INT_EQ(ferror(out), 0);
  38. ASSERT_INT_NE(feof(out), 0);
  39. ASSERT_SIZE_T_GT(sz, 0);
  40. tmp[sz] = '\0';
  41. ASSERT_PTR_NE(strstr(tmp, "12 34 56 78"), NULL);
  42. fclose(out);
  43. sshbuf_free(p1);
  44. TEST_DONE();
  45. TEST_START("sshbuf_dtob16");
  46. p1 = sshbuf_new();
  47. ASSERT_PTR_NE(p1, NULL);
  48. ASSERT_INT_EQ(sshbuf_put_u32(p1, 0x12345678), 0);
  49. p = sshbuf_dtob16(p1);
  50. ASSERT_PTR_NE(p, NULL);
  51. ASSERT_STRING_EQ(p, "12345678");
  52. free(p);
  53. sshbuf_free(p1);
  54. TEST_DONE();
  55. TEST_START("sshbuf_dtob64_string len 1");
  56. p1 = sshbuf_new();
  57. ASSERT_PTR_NE(p1, NULL);
  58. ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x11), 0);
  59. p = sshbuf_dtob64_string(p1, 0);
  60. ASSERT_PTR_NE(p, NULL);
  61. ASSERT_STRING_EQ(p, "EQ==");
  62. free(p);
  63. sshbuf_free(p1);
  64. TEST_DONE();
  65. TEST_START("sshbuf_dtob64_string len 2");
  66. p1 = sshbuf_new();
  67. ASSERT_PTR_NE(p1, NULL);
  68. ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x11), 0);
  69. ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x22), 0);
  70. p = sshbuf_dtob64_string(p1, 0);
  71. ASSERT_PTR_NE(p, NULL);
  72. ASSERT_STRING_EQ(p, "ESI=");
  73. free(p);
  74. sshbuf_free(p1);
  75. TEST_DONE();
  76. TEST_START("sshbuf_dtob64_string len 3");
  77. p1 = sshbuf_new();
  78. ASSERT_PTR_NE(p1, NULL);
  79. ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x11), 0);
  80. ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x22), 0);
  81. ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x33), 0);
  82. p = sshbuf_dtob64_string(p1, 0);
  83. ASSERT_PTR_NE(p, NULL);
  84. ASSERT_STRING_EQ(p, "ESIz");
  85. free(p);
  86. sshbuf_free(p1);
  87. TEST_DONE();
  88. TEST_START("sshbuf_dtob64_string len 8191");
  89. p1 = sshbuf_new();
  90. ASSERT_PTR_NE(p1, NULL);
  91. ASSERT_INT_EQ(sshbuf_reserve(p1, 8192, NULL), 0);
  92. bzero(sshbuf_mutable_ptr(p1), 8192);
  93. p = sshbuf_dtob64_string(p1, 0);
  94. ASSERT_PTR_NE(p, NULL);
  95. ASSERT_SIZE_T_EQ(strlen(p), ((8191 + 2) / 3) * 4);
  96. free(p);
  97. sshbuf_free(p1);
  98. TEST_DONE();
  99. TEST_START("sshbuf_b64tod len 1");
  100. p1 = sshbuf_new();
  101. ASSERT_PTR_NE(p1, NULL);
  102. ASSERT_INT_EQ(sshbuf_b64tod(p1, "0A=="), 0);
  103. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
  104. ASSERT_U8_EQ(*sshbuf_ptr(p1), 0xd0);
  105. sshbuf_free(p1);
  106. TEST_DONE();
  107. TEST_START("sshbuf_b64tod len 2");
  108. p1 = sshbuf_new();
  109. ASSERT_PTR_NE(p1, NULL);
  110. ASSERT_INT_EQ(sshbuf_b64tod(p1, "0A8="), 0);
  111. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
  112. ASSERT_U16_EQ(PEEK_U16(sshbuf_ptr(p1)), 0xd00f);
  113. sshbuf_free(p1);
  114. TEST_DONE();
  115. TEST_START("sshbuf_b64tod len 4");
  116. p1 = sshbuf_new();
  117. ASSERT_PTR_NE(p1, NULL);
  118. ASSERT_INT_EQ(sshbuf_b64tod(p1, "0A/QDw=="), 0);
  119. ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4);
  120. ASSERT_U32_EQ(PEEK_U32(sshbuf_ptr(p1)), 0xd00fd00f);
  121. sshbuf_free(p1);
  122. TEST_DONE();
  123. TEST_START("sshbuf_dup_string");
  124. p1 = sshbuf_new();
  125. ASSERT_PTR_NE(p1, NULL);
  126. /* Check empty buffer */
  127. p = sshbuf_dup_string(p1);
  128. ASSERT_PTR_NE(p, NULL);
  129. ASSERT_SIZE_T_EQ(strlen(p), 0);
  130. free(p);
  131. /* Check buffer with string */
  132. ASSERT_INT_EQ(sshbuf_put(p1, "quad1", strlen("quad1")), 0);
  133. p = sshbuf_dup_string(p1);
  134. ASSERT_PTR_NE(p, NULL);
  135. ASSERT_SIZE_T_EQ(strlen(p), strlen("quad1"));
  136. ASSERT_STRING_EQ(p, "quad1");
  137. free(p);
  138. /* Check buffer with terminating nul */
  139. ASSERT_INT_EQ(sshbuf_put(p1, "\0", 1), 0);
  140. p = sshbuf_dup_string(p1);
  141. ASSERT_PTR_NE(p, NULL);
  142. ASSERT_SIZE_T_EQ(strlen(p), strlen("quad1"));
  143. ASSERT_STRING_EQ(p, "quad1");
  144. free(p);
  145. /* Check buffer with data after nul (expect failure) */
  146. ASSERT_INT_EQ(sshbuf_put(p1, "quad2", strlen("quad2")), 0);
  147. p = sshbuf_dup_string(p1);
  148. ASSERT_PTR_EQ(p, NULL);
  149. sshbuf_free(p1);
  150. TEST_DONE();
  151. TEST_START("sshbuf_cmp");
  152. p1 = sshbuf_from(msg, sizeof(msg) - 1);
  153. ASSERT_PTR_NE(p1, NULL);
  154. ASSERT_INT_EQ(sshbuf_cmp(p1, 0, "i", 1), 0);
  155. ASSERT_INT_EQ(sshbuf_cmp(p1, 0, "j", 1), SSH_ERR_INVALID_FORMAT);
  156. ASSERT_INT_EQ(sshbuf_cmp(p1, 0, "imploring", 9), 0);
  157. ASSERT_INT_EQ(sshbuf_cmp(p1, 0, "implored", 9), SSH_ERR_INVALID_FORMAT);
  158. ASSERT_INT_EQ(sshbuf_cmp(p1, 10, "ping", 4), 0);
  159. ASSERT_INT_EQ(sshbuf_cmp(p1, 10, "ring", 4), SSH_ERR_INVALID_FORMAT);
  160. ASSERT_INT_EQ(sshbuf_cmp(p1, 28, "over", 4), 0);
  161. ASSERT_INT_EQ(sshbuf_cmp(p1, 28, "rove", 4), SSH_ERR_INVALID_FORMAT);
  162. ASSERT_INT_EQ(sshbuf_cmp(p1, 28, "overt", 5),
  163. SSH_ERR_MESSAGE_INCOMPLETE);
  164. ASSERT_INT_EQ(sshbuf_cmp(p1, 32, "ping", 4),
  165. SSH_ERR_MESSAGE_INCOMPLETE);
  166. ASSERT_INT_EQ(sshbuf_cmp(p1, 1000, "silence", 7),
  167. SSH_ERR_MESSAGE_INCOMPLETE);
  168. ASSERT_INT_EQ(sshbuf_cmp(p1, 0, msg, sizeof(msg) - 1), 0);
  169. TEST_DONE();
  170. TEST_START("sshbuf_find");
  171. p1 = sshbuf_from(msg, sizeof(msg) - 1);
  172. ASSERT_PTR_NE(p1, NULL);
  173. ASSERT_INT_EQ(sshbuf_find(p1, 0, "i", 1, &sz), 0);
  174. ASSERT_SIZE_T_EQ(sz, 0);
  175. ASSERT_INT_EQ(sshbuf_find(p1, 0, "j", 1, &sz), SSH_ERR_INVALID_FORMAT);
  176. ASSERT_INT_EQ(sshbuf_find(p1, 0, "imploring", 9, &sz), 0);
  177. ASSERT_SIZE_T_EQ(sz, 0);
  178. ASSERT_INT_EQ(sshbuf_find(p1, 0, "implored", 9, &sz),
  179. SSH_ERR_INVALID_FORMAT);
  180. ASSERT_INT_EQ(sshbuf_find(p1, 3, "ping", 4, &sz), 0);
  181. ASSERT_SIZE_T_EQ(sz, 10);
  182. ASSERT_INT_EQ(sshbuf_find(p1, 11, "ping", 4, &sz), 0);
  183. ASSERT_SIZE_T_EQ(sz, 23);
  184. ASSERT_INT_EQ(sshbuf_find(p1, 20, "over", 4, &sz), 0);
  185. ASSERT_SIZE_T_EQ(sz, 28);
  186. ASSERT_INT_EQ(sshbuf_find(p1, 28, "over", 4, &sz), 0);
  187. ASSERT_SIZE_T_EQ(sz, 28);
  188. ASSERT_INT_EQ(sshbuf_find(p1, 28, "rove", 4, &sz),
  189. SSH_ERR_INVALID_FORMAT);
  190. ASSERT_INT_EQ(sshbuf_find(p1, 28, "overt", 5, &sz),
  191. SSH_ERR_MESSAGE_INCOMPLETE);
  192. ASSERT_INT_EQ(sshbuf_find(p1, 32, "ping", 4, &sz),
  193. SSH_ERR_MESSAGE_INCOMPLETE);
  194. ASSERT_INT_EQ(sshbuf_find(p1, 1000, "silence", 7, &sz),
  195. SSH_ERR_MESSAGE_INCOMPLETE);
  196. ASSERT_INT_EQ(sshbuf_find(p1, 0, msg + 1, sizeof(msg) - 2, &sz), 0);
  197. ASSERT_SIZE_T_EQ(sz, 1);
  198. TEST_DONE();
  199. }