unit1661.c 3.2 KB

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