summaryrefslogtreecommitdiff
path: root/tests/unit
diff options
context:
space:
mode:
authorPatrick Monnerat <patrick@monnerat.net>2021-03-23 02:02:18 +0100
committerDaniel Stenberg <daniel@haxx.se>2021-04-22 09:05:53 +0200
commit34cf40321c3cfa5160e34a87a816b7c6030c0296 (patch)
tree5b9e2844d85a620027c2aa18f87d446b00875090 /tests/unit
parenta2a8f9d7300f3a3773cb046005e0ecbc9a7080db (diff)
downloadcurl-34cf40321c3cfa5160e34a87a816b7c6030c0296.tar.gz
bufref: buffer reference support
A struct bufref holds a buffer pointer, a data size and a destructor. When freed or its contents are changed, the previous buffer is implicitly released by the associated destructor. The data size, although not used internally, allows binary data support. A unit test checks its handling methods: test 1661 Closes #6654
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/Makefile.inc7
-rw-r--r--tests/unit/unit1661.c113
2 files changed, 118 insertions, 2 deletions
diff --git a/tests/unit/Makefile.inc b/tests/unit/Makefile.inc
index 0b03b4e69..854e3af38 100644
--- a/tests/unit/Makefile.inc
+++ b/tests/unit/Makefile.inc
@@ -5,7 +5,7 @@
# | (__| |_| | _ <| |___
# \___|\___/|_| \_\_____|
#
-# Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
+# Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
@@ -35,7 +35,7 @@ UNITPROGS = unit1300 unit1301 unit1302 unit1303 unit1304 unit1305 unit1307 \
unit1608 unit1609 unit1610 unit1611 unit1612 \
unit1620 unit1621 \
unit1650 unit1651 unit1652 unit1653 unit1654 unit1655 \
- unit1660
+ unit1660 unit1661
unit1300_SOURCES = unit1300.c $(UNITFILES)
unit1300_CPPFLAGS = $(AM_CPPFLAGS)
@@ -157,3 +157,6 @@ unit1655_CPPFLAGS = $(AM_CPPFLAGS)
unit1660_SOURCES = unit1660.c $(UNITFILES)
unit1660_CPPFLAGS = $(AM_CPPFLAGS)
+
+unit1661_SOURCES = unit1661.c $(UNITFILES)
+unit1661_CPPFLAGS = $(AM_CPPFLAGS)
diff --git a/tests/unit/unit1661.c b/tests/unit/unit1661.c
new file mode 100644
index 000000000..c32d6f164
--- /dev/null
+++ b/tests/unit/unit1661.c
@@ -0,0 +1,113 @@
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at https://curl.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+#include "curlcheck.h"
+
+#include "bufref.h"
+
+static struct bufref bufref;
+
+static int freecount = 0;
+
+static void test_free(void *p)
+{
+ fail_unless(p, "pointer to free may not be NULL");
+ freecount++;
+ free(p);
+}
+
+static CURLcode unit_setup(void)
+{
+ Curl_bufref_init(&bufref);
+ return CURLE_OK;
+}
+
+static void unit_stop(void)
+{
+}
+
+UNITTEST_START
+{
+ char *buffer = NULL;
+ CURLcode result = CURLE_OK;
+
+ /**
+ * testing Curl_bufref_init.
+ * @assumptions:
+ * 1: data size will be 0
+ * 2: reference will be NULL
+ * 3: destructor will be NULL
+ */
+
+ fail_unless(!bufref.ptr, "Initial reference must be NULL");
+ fail_unless(!bufref.len, "Initial length must be NULL");
+ fail_unless(!bufref.dtor, "Destructor must be NULL");
+
+ /**
+ * testing Curl_bufref_set
+ */
+
+ buffer = malloc(13);
+ abort_unless(buffer, "Out of memory");
+ Curl_bufref_set(&bufref, buffer, 13, test_free);
+
+ fail_unless((char *) bufref.ptr == buffer, "Referenced data badly set");
+ fail_unless(bufref.len == 13, "Data size badly set");
+ fail_unless(bufref.dtor == test_free, "Destructor badly set");
+
+ /**
+ * testing Curl_bufref_ptr
+ */
+
+ fail_unless((char *) Curl_bufref_ptr(&bufref) == buffer,
+ "Wrong pointer value returned");
+
+ /**
+ * testing Curl_bufref_len
+ */
+
+ fail_unless(Curl_bufref_len(&bufref) == 13, "Wrong data size returned");
+
+ /**
+ * testing Curl_bufref_memdup
+ */
+
+ result = Curl_bufref_memdup(&bufref, "1661", 3);
+ abort_unless(result == CURLE_OK, curl_easy_strerror(result));
+ fail_unless(freecount == 1, "Destructor not called");
+ fail_unless((char *) bufref.ptr != buffer, "Returned pointer not set");
+ buffer = (char *) Curl_bufref_ptr(&bufref);
+ fail_unless(buffer, "Allocated pointer is NULL");
+ fail_unless(bufref.len == 3, "Wrong data size stored");
+ fail_unless(!buffer[3], "Duplicated data should have been truncated");
+ fail_unless(!strcmp(buffer, "166"), "Bad duplicated data");
+
+ /**
+ * testing Curl_bufref_free
+ */
+
+ Curl_bufref_free(&bufref);
+ fail_unless(freecount == 1, "Wrong destructor called");
+ fail_unless(!bufref.ptr, "Initial reference must be NULL");
+ fail_unless(!bufref.len, "Initial length must be NULL");
+ fail_unless(!bufref.dtor, "Destructor must be NULL");
+}
+UNITTEST_STOP