unit1661.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. static struct bufref bufref;
  27. static int freecount = 0;
  28. static void test_free(void *p)
  29. {
  30. fail_unless(p, "pointer to free may not be NULL");
  31. freecount++;
  32. free(p);
  33. }
  34. static CURLcode unit_setup(void)
  35. {
  36. Curl_bufref_init(&bufref);
  37. return CURLE_OK;
  38. }
  39. static void unit_stop(void)
  40. {
  41. }
  42. UNITTEST_START
  43. {
  44. char *buffer = NULL;
  45. CURLcode result = CURLE_OK;
  46. /**
  47. * testing Curl_bufref_init.
  48. * @assumptions:
  49. * 1: data size will be 0
  50. * 2: reference will be NULL
  51. * 3: destructor will be NULL
  52. */
  53. fail_unless(!bufref.ptr, "Initial reference must be NULL");
  54. fail_unless(!bufref.len, "Initial length must be NULL");
  55. fail_unless(!bufref.dtor, "Destructor must be NULL");
  56. /**
  57. * testing Curl_bufref_set
  58. */
  59. buffer = malloc(13);
  60. abort_unless(buffer, "Out of memory");
  61. Curl_bufref_set(&bufref, buffer, 13, test_free);
  62. fail_unless((char *) bufref.ptr == buffer, "Referenced data badly set");
  63. fail_unless(bufref.len == 13, "Data size badly set");
  64. fail_unless(bufref.dtor == test_free, "Destructor badly set");
  65. /**
  66. * testing Curl_bufref_ptr
  67. */
  68. fail_unless((char *) Curl_bufref_ptr(&bufref) == buffer,
  69. "Wrong pointer value returned");
  70. /**
  71. * testing Curl_bufref_len
  72. */
  73. fail_unless(Curl_bufref_len(&bufref) == 13, "Wrong data size returned");
  74. /**
  75. * testing Curl_bufref_memdup
  76. */
  77. result = Curl_bufref_memdup(&bufref, "1661", 3);
  78. abort_unless(result == CURLE_OK, curl_easy_strerror(result));
  79. fail_unless(freecount == 1, "Destructor not called");
  80. fail_unless((char *) bufref.ptr != buffer, "Returned pointer not set");
  81. buffer = (char *) Curl_bufref_ptr(&bufref);
  82. fail_unless(buffer, "Allocated pointer is NULL");
  83. fail_unless(bufref.len == 3, "Wrong data size stored");
  84. fail_unless(!buffer[3], "Duplicated data should have been truncated");
  85. fail_unless(!strcmp(buffer, "166"), "Bad duplicated data");
  86. /**
  87. * testing Curl_bufref_free
  88. */
  89. Curl_bufref_free(&bufref);
  90. fail_unless(freecount == 1, "Wrong destructor called");
  91. fail_unless(!bufref.ptr, "Initial reference must be NULL");
  92. fail_unless(!bufref.len, "Initial length must be NULL");
  93. fail_unless(!bufref.dtor, "Destructor must be NULL");
  94. }
  95. UNITTEST_STOP