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