test_sshbuf_fixed.c 3.8 KB

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