Fri, 09 Jan 2026 14:52:34 +0100
add tests for some path util functions
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2023 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <cx/test.h> #include <libidav/utils.h> CX_TEST(test_util_parse_creationdate) { CX_TEST_DO { time_t t1 = util_parse_creationdate("2012-11-29T21:35:36Z"); time_t t2 = util_parse_creationdate("2023-09-17T13:03:00+02:00"); struct tm *tm = gmtime(&t1); CX_TEST_ASSERTM(tm->tm_year == 112, "t1: wrong year"); CX_TEST_ASSERTM(tm->tm_mon == 10, "t1: wrong month"); CX_TEST_ASSERTM(tm->tm_mday == 29, "t1: wrong day"); CX_TEST_ASSERTM(tm->tm_hour == 21, "t1: wrong hour"); CX_TEST_ASSERTM(tm->tm_min == 35, "t1: wrong minute"); CX_TEST_ASSERTM(tm->tm_sec == 36, "t1: wrong second"); tm = gmtime(&t2); CX_TEST_ASSERTM(tm->tm_year == 123, "t2: wrong year"); CX_TEST_ASSERTM(tm->tm_mon == 8, "t2: wrong month"); CX_TEST_ASSERTM(tm->tm_mday == 17, "t2: wrong day"); CX_TEST_ASSERTM(tm->tm_hour == 11, "t2: wrong hour"); CX_TEST_ASSERTM(tm->tm_min == 03, "t2: wrong minute"); CX_TEST_ASSERTM(tm->tm_sec == 0, "t2: wrong second"); } } CX_TEST(test_util_url_base) { CX_TEST_DO { char *path = util_url_base("http://example.com/basepath/123/"); CX_TEST_ASSERT(path && !strcmp(path, "http://example.com/")); free(path); path = util_url_base("http://example.com/"); CX_TEST_ASSERT(path && !strcmp(path, "http://example.com/")); free(path); path = util_url_base("https://example.com/basepath/123/"); CX_TEST_ASSERT(path && !strcmp(path, "https://example.com/")); free(path); path = util_url_base("https://example.com/"); CX_TEST_ASSERT(path && !strcmp(path, "https://example.com/")); free(path); path = util_url_base("http://example.com:8080/basepath/123/"); CX_TEST_ASSERT(path && !strcmp(path, "http://example.com:8080/")); free(path); path = util_url_base("http://user@example.com/"); CX_TEST_ASSERT(path && !strcmp(path, "http://user@example.com/")); free(path); //path = util_url_base("http://example.com"); //CX_TEST_ASSERT(path && !strcmp(path, "http://example.com/")); //free(path); } } CX_TEST(test_util_concat_path) { CX_TEST_DO { char *str = util_concat_path("", ""); CX_TEST_ASSERT(str); CX_TEST_ASSERT(!strcmp(str, "/")); free(str); str = util_concat_path("/test", "abc"); CX_TEST_ASSERT(str); CX_TEST_ASSERT(!cx_strcmp(str, "/test/abc")); free(str); str = util_concat_path("/test/", "abc"); CX_TEST_ASSERT(str); CX_TEST_ASSERT(!cx_strcmp(str, "/test/abc")); free(str); str = util_concat_path("/test", "/abc"); CX_TEST_ASSERT(str); CX_TEST_ASSERT(!cx_strcmp(str, "/test/abc")); free(str); str = util_concat_path("/test/", "/abc"); CX_TEST_ASSERT(str); CX_TEST_ASSERT(!cx_strcmp(str, "/test/abc")); free(str); str = util_concat_path("/test/123/long", "/dir1/dir2/dir3"); CX_TEST_ASSERT(str); CX_TEST_ASSERT(!cx_strcmp(str, "/test/123/long/dir1/dir2/dir3")); free(str); str = util_concat_path("relative", "path"); CX_TEST_ASSERT(str); CX_TEST_ASSERT(!cx_strcmp(str, "relative/path")); free(str); } } CX_TEST(test_util_path_isrelated) { CX_TEST_DO { CX_TEST_ASSERT(util_path_isrelated("/", "/")); CX_TEST_ASSERT(util_path_isrelated("/", "/test")); CX_TEST_ASSERT(util_path_isrelated("/", "/test/")); CX_TEST_ASSERT(util_path_isrelated("/", "/test/sub")); CX_TEST_ASSERT(util_path_isrelated("/", "/test/sub/")); CX_TEST_ASSERT(util_path_isrelated("/dir1/", "/dir1")); CX_TEST_ASSERT(util_path_isrelated("/dir1/", "/dir1/")); CX_TEST_ASSERT(util_path_isrelated("/abc/test/123/", "/abc/test/123")); CX_TEST_ASSERT(util_path_isrelated("/abc/test/123/", "/abc/test/123/test/sub/")); CX_TEST_ASSERT(!util_path_isrelated("/dir1/", "/dir2/")); CX_TEST_ASSERT(!util_path_isrelated("/dir1/", "/dir2")); CX_TEST_ASSERT(!util_path_isrelated("/dir1", "/dir2/")); CX_TEST_ASSERT(!util_path_isrelated("/abc/123/test1", "/abc/123/xxx")); CX_TEST_ASSERT(!util_path_isrelated("/abc/123/test1", "/abc/zzz/123/")); CX_TEST_ASSERT(!util_path_isrelated("/dir1/", "/")); } }