UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2026 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "test.h" 30 31 #include "sab.h" 32 33 void register_sab_tests(int argc, char **argv, CxTestSuite *suite) { 34 cx_test_register(suite, test_sab_url_match); 35 cx_test_register(suite, test_sab_has_cookie); 36 } 37 38 CX_TEST(test_sab_url_match) { 39 CX_TEST_DO { 40 pool_handle_t *pool = pool_create(); 41 pblock *pb = pblock_create_pool(pool, 4); 42 pblock_nvinsert("match", "/hg/.*/rev.*", pb); 43 44 // test1: match 45 Request rq; 46 rq.headers = pblock_create_pool(pool, 4); 47 pblock_nvinsert("uri", "/hg/webserver/rev/13432432/test", rq.headers); 48 49 int error; 50 int test = sab_url_match(pb, &rq, &error); 51 CX_TEST_ASSERT(test == 1); 52 53 // test2: no match 54 rq.headers = pblock_create_pool(pool, 4); 55 pblock_nvinsert("uri", "/hello/world", rq.headers); 56 57 test = sab_url_match(pb, &rq, &error); 58 CX_TEST_ASSERT(test == 0); 59 60 pool_destroy(pool); 61 } 62 } 63 64 CX_TEST(test_sab_has_cookie) { 65 CX_TEST_DO { 66 pool_handle_t *pool = pool_create(); 67 68 pblock *pb = pblock_create_pool(pool, 4); 69 pblock_nvinsert("cookie", "session", pb); 70 71 Request rq; 72 rq.headers = pblock_create_pool(pool, 4); 73 rq.vars = pblock_create_pool(pool, 4); 74 75 // Test 1: cookie is present 76 pblock_nvinsert("cookie", "foo=bar; session=abc123; hello=world", rq.headers); 77 78 int ret = sab_has_cookie(pb, NULL, &rq); 79 80 CX_TEST_ASSERT(ret == REQ_NOACTION); 81 CX_TEST_ASSERT(pblock_findval("is-bot", rq.vars) == NULL); 82 83 // Test 2: cookie is missing 84 rq.headers = pblock_create_pool(pool, 4); 85 rq.vars = pblock_create_pool(pool, 4); 86 pblock_nvinsert("cookie", "foo=bar; hello=world", rq.headers); 87 88 ret = sab_has_cookie(pb, NULL, &rq); 89 90 CX_TEST_ASSERT(ret == REQ_NOACTION); 91 CX_TEST_ASSERT(strcmp(pblock_findval("is-bot", rq.vars), "1") == 0); 92 93 // Test 3: cookie exists, but value is wrong 94 pblock_nvinsert("value", "expected", pb); 95 96 rq.headers = pblock_create_pool(pool, 4); 97 rq.vars = pblock_create_pool(pool, 4); 98 pblock_nvinsert("cookie", "foo=bar; session=abc123; hello=world", rq.headers); 99 100 // Clear vars for this test 101 rq.vars = pblock_create_pool(pool, 4); 102 103 ret = sab_has_cookie(pb, NULL, &rq); 104 105 CX_TEST_ASSERT(ret == REQ_NOACTION); 106 CX_TEST_ASSERT(strcmp(pblock_findval("is-bot", rq.vars), "1") == 0); 107 108 pool_destroy(pool); 109 } 110 } 111