/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2026 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 "test.h"

#include "sab.h"

void register_sab_tests(int argc, char **argv, CxTestSuite *suite) {
    cx_test_register(suite, test_sab_url_match);
    cx_test_register(suite, test_sab_has_cookie);
}

CX_TEST(test_sab_url_match) {
    CX_TEST_DO {
        pool_handle_t *pool = pool_create();
        pblock *pb = pblock_create_pool(pool, 4);
        pblock_nvinsert("match", "/hg/.*/rev.*", pb);
        
        // test1: match
        Request rq;
        rq.headers = pblock_create_pool(pool, 4);
        pblock_nvinsert("uri", "/hg/webserver/rev/13432432/test", rq.headers);
        
        int error;
        int test = sab_url_match(pb, &rq, &error);
        CX_TEST_ASSERT(test == 1);
        
        // test2: no match
        rq.headers = pblock_create_pool(pool, 4);
        pblock_nvinsert("uri", "/hello/world", rq.headers);
        
        test = sab_url_match(pb, &rq, &error);
        CX_TEST_ASSERT(test == 0);
        
        pool_destroy(pool);
    }
}

CX_TEST(test_sab_has_cookie) {
    CX_TEST_DO {
        pool_handle_t *pool = pool_create();

        pblock *pb = pblock_create_pool(pool, 4);
        pblock_nvinsert("cookie", "session", pb);

        Request rq;
        rq.headers = pblock_create_pool(pool, 4);
        rq.vars = pblock_create_pool(pool, 4);

        // Test 1: cookie is present
        pblock_nvinsert("cookie", "foo=bar; session=abc123; hello=world", rq.headers);

        int ret = sab_has_cookie(pb, NULL, &rq);

        CX_TEST_ASSERT(ret == REQ_NOACTION);
        CX_TEST_ASSERT(pblock_findval("is-bot", rq.vars) == NULL);

        // Test 2: cookie is missing
        rq.headers = pblock_create_pool(pool, 4);
        rq.vars = pblock_create_pool(pool, 4);
        pblock_nvinsert("cookie", "foo=bar; hello=world", rq.headers);

        ret = sab_has_cookie(pb, NULL, &rq);

        CX_TEST_ASSERT(ret == REQ_NOACTION);
        CX_TEST_ASSERT(strcmp(pblock_findval("is-bot", rq.vars), "1") == 0);

        // Test 3: cookie exists, but value is wrong
        pblock_nvinsert("value", "expected", pb);

        rq.headers = pblock_create_pool(pool, 4);
        rq.vars = pblock_create_pool(pool, 4);
        pblock_nvinsert("cookie", "foo=bar; session=abc123; hello=world", rq.headers);

        // Clear vars for this test
        rq.vars = pblock_create_pool(pool, 4);

        ret = sab_has_cookie(pb, NULL, &rq);

        CX_TEST_ASSERT(ret == REQ_NOACTION);
        CX_TEST_ASSERT(strcmp(pblock_findval("is-bot", rq.vars), "1") == 0);

        pool_destroy(pool);
    }
}
