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