test/utils.c

Wed, 07 Jan 2026 22:31:05 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 07 Jan 2026 22:31:05 +0100
branch
dav-2
changeset 896
2c765c286536
parent 895
87638f19f6dd
child 897
d684ba9e2da0
permissions
-rw-r--r--

add test for util_path_isrelated

/*
 * 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_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/", "/"));
    }
}

mercurial