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