]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add a unit test for isc_url_t
authorAram Sargsyan <aram@isc.org>
Tue, 16 Jun 2026 11:10:21 +0000 (11:10 +0000)
committerArаm Sаrgsyаn <aram@isc.org>
Tue, 7 Jul 2026 10:08:04 +0000 (10:08 +0000)
The test has some basic checks for the URL/URI parser.

tests/isc/meson.build
tests/isc/url_test.c [new file with mode: 0644]

index ca9933de3912758c13d60391278ac5a5f20d8c49..4e18e345ee4f45474484b30cf9dbfa872730e34b 100644 (file)
@@ -53,6 +53,7 @@ isc_test = [
     'tls',
     'tlsdns',
     'udp',
+    'url',
     'work',
 ]
 
diff --git a/tests/isc/url_test.c b/tests/isc/url_test.c
new file mode 100644 (file)
index 0000000..5c261f0
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
+ *
+ * SPDX-License-Identifier: MPL-2.0
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at https://mozilla.org/MPL/2.0/.
+ *
+ * See the COPYRIGHT file distributed with this work for additional
+ * information regarding copyright ownership.
+ */
+
+/*! \file */
+
+#include <inttypes.h>
+#include <sched.h> /* IWYU pragma: keep */
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define UNIT_TESTING
+#include <cmocka.h>
+
+#include <isc/lib.h>
+#include <isc/mem.h>
+#include <isc/url.h>
+#include <isc/util.h>
+
+#include <tests/isc.h>
+
+ISC_RUN_TEST_IMPL(parse) {
+       isc_result_t result;
+       isc_url_parser_t up = { 0 };
+       const char *url = NULL;
+
+       /* Test an empty buffer. */
+       url = "";
+       result = isc_url_parse(url, strlen(url), 0, &up);
+       assert_int_equal(ISC_R_RANGE, result);
+
+       /* Test a buffer with a valid URI. */
+       url = "http://user:pass@example.com:8080/test?a=b&c=d";
+       result = isc_url_parse(url, strlen(url), false, &up);
+       assert_int_equal(ISC_R_SUCCESS, result);
+       assert_int_equal(0, up.field_data[ISC_UF_SCHEMA].off);
+       assert_int_equal(4, up.field_data[ISC_UF_SCHEMA].len);
+       assert_int_equal(17, up.field_data[ISC_UF_HOST].off);
+       assert_int_equal(11, up.field_data[ISC_UF_HOST].len);
+       assert_int_equal(29, up.field_data[ISC_UF_PORT].off);
+       assert_int_equal(4, up.field_data[ISC_UF_PORT].len);
+       assert_int_equal(33, up.field_data[ISC_UF_PATH].off);
+       assert_int_equal(5, up.field_data[ISC_UF_PATH].len);
+       assert_int_equal(39, up.field_data[ISC_UF_QUERY].off);
+       assert_int_equal(7, up.field_data[ISC_UF_QUERY].len);
+       assert_int_equal(0, up.field_data[ISC_UF_FRAGMENT].off);
+       assert_int_equal(0, up.field_data[ISC_UF_FRAGMENT].len);
+       assert_int_equal(7, up.field_data[ISC_UF_USERINFO].off);
+       assert_int_equal(9, up.field_data[ISC_UF_USERINFO].len);
+
+       /* Test a too big buffer. */
+       url = "https://localhost/";
+       size_t buf_len = UINT16_MAX + 2;
+       char *buf = isc_mem_get(isc_g_mctx, buf_len);
+       snprintf(buf, buf_len - 1, "%-65535s", url); /* Pad with spaces */
+       buf[buf_len - 1] = '\0'; /* Ensure ending with Null-byte */
+       result = isc_url_parse(buf, buf_len - 1, 0, &up);
+       isc_mem_put(isc_g_mctx, buf, buf_len);
+       assert_int_equal(ISC_R_RANGE, result);
+}
+
+ISC_TEST_LIST_START
+
+ISC_TEST_ENTRY(parse)
+
+ISC_TEST_LIST_END
+
+ISC_TEST_MAIN