unit1661.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curlcheck.h"
  25. #include "bufref.h"
  26. #include "memdebug.h"
  27. static struct bufref bufref;
  28. static int freecount = 0;
  29. static void test_free(void *p)
  30. {
  31. fail_unless(p, "pointer to free may not be NULL");
  32. freecount++;
  33. free(p);
  34. }
  35. static CURLcode unit_setup(void)
  36. {
  37. Curl_bufref_init(&bufref);
  38. return CURLE_OK;
  39. }
  40. static void unit_stop(void)
  41. {
  42. Curl_bufref_free(&bufref);
  43. }
  44. UNITTEST_START
  45. {
  46. char *buffer = NULL;
  47. CURLcode result = CURLE_OK;
  48. /**
  49. * testing Curl_bufref_init.
  50. * @assumptions:
  51. * 1: data size will be 0
  52. * 2: reference will be NULL
  53. * 3: destructor will be NULL
  54. */
  55. fail_unless(!bufref.ptr, "Initial reference must be NULL");
  56. fail_unless(!bufref.len, "Initial length must be NULL");
  57. fail_unless(!bufref.dtor, "Destructor must be NULL");
  58. /**
  59. * testing Curl_bufref_set
  60. */
  61. buffer = malloc(13);
  62. abort_unless(buffer, "Out of memory");
  63. Curl_bufref_set(&bufref, buffer, 13, test_free);
  64. fail_unless((char *) bufref.ptr == buffer, "Referenced data badly set");
  65. fail_unless(bufref.len == 13, "Data size badly set");
  66. fail_unless(bufref.dtor == test_free, "Destructor badly set");
  67. /**
  68. * testing Curl_bufref_ptr
  69. */
  70. fail_unless((char *) Curl_bufref_ptr(&bufref) == buffer,
  71. "Wrong pointer value returned");
  72. /**
  73. * testing Curl_bufref_len
  74. */
  75. fail_unless(Curl_bufref_len(&bufref) == 13, "Wrong data size returned");
  76. /**
  77. * testing Curl_bufref_memdup
  78. */
  79. result = Curl_bufref_memdup(&bufref, "1661", 3);
  80. abort_unless(result == CURLE_OK, curl_easy_strerror(result));
  81. fail_unless(freecount == 1, "Destructor not called");
  82. fail_unless((char *) bufref.ptr != buffer, "Returned pointer not set");
  83. buffer = (char *) Curl_bufref_ptr(&bufref);
  84. fail_unless(buffer, "Allocated pointer is NULL");
  85. fail_unless(bufref.len == 3, "Wrong data size stored");
  86. fail_unless(!buffer[3], "Duplicated data should have been truncated");
  87. fail_unless(!strcmp(buffer, "166"), "Bad duplicated data");
  88. /**
  89. * testing Curl_bufref_free
  90. */
  91. Curl_bufref_free(&bufref);
  92. fail_unless(freecount == 1, "Wrong destructor called");
  93. fail_unless(!bufref.ptr, "Initial reference must be NULL");
  94. fail_unless(!bufref.len, "Initial length must be NULL");
  95. fail_unless(!bufref.dtor, "Destructor must be NULL");
  96. }
  97. UNITTEST_STOP