From 07b4190532b8d60cbbc23e535cb8b0ed781d28d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20R=C3=B6hling?= Date: Mon, 7 Aug 2023 18:13:06 +0200 Subject: [PATCH] Add unit test for srs2 and fix memory leak --- src/srs2.c | 1 + tests/unit/CMakeLists.txt | 1 + tests/unit/test_srs2.c | 77 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 tests/unit/test_srs2.c diff --git a/src/srs2.c b/src/srs2.c index a620bfc..024b261 100644 --- a/src/srs2.c +++ b/src/srs2.c @@ -136,6 +136,7 @@ void srs_free(srs_t* srs) srs_f_free(srs->secrets[i]); srs->secrets[i] = 0; } + srs_f_free(srs->secrets); srs_f_free(srs); } diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index f0c1f3d..7d7003b 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -49,3 +49,4 @@ target_link_libraries( test_database_executable PRIVATE $<$:sqlite3::sqlite3> $<$:${HIREDIS_TARGET}> ) +add_postsrsd_test(test_srs2 ${SRCDIR}/srs2.c ${SRCDIR}/sha1.c) diff --git a/tests/unit/test_srs2.c b/tests/unit/test_srs2.c new file mode 100644 index 0000000..8123244 --- /dev/null +++ b/tests/unit/test_srs2.c @@ -0,0 +1,77 @@ +/* PostSRSd - Sender Rewriting Scheme daemon for Postfix + * Copyright 2012-2022 Timo Röhling + * SPDX-License-Identifier: GPL-3.0-only + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "common.h" +#include "srs2.h" + +#include + +srs_t* create_srs_t() +{ + srs_t* srs = srs_new(); + srs->faketime = 1577836860; /* 2020-01-01 00:01:00 UTC */ + srs_add_secret(srs, "tops3cr3t"); + return srs; +} + +START_TEST(srs2_forwarding) +{ + srs_t* srs = create_srs_t(); + char* output = NULL; + int result; + + result = srs_forward_alloc(srs, &output, "test@example.com", "example.com"); + ck_assert_int_eq(result, SRS_SUCCESS); + ck_assert_str_eq(output, "test@example.com"); + free(output); + + result = + srs_forward_alloc(srs, &output, "test@otherdomain.com", "example.com"); + ck_assert_int_eq(result, SRS_SUCCESS); + ck_assert_str_eq(output, "SRS0=vmyz=2W=otherdomain.com=test@example.com"); + free(output); + + result = srs_forward_alloc(srs, &output, "foo", "example.com"); + ck_assert_int_eq(result, SRS_ENOSENDERATSIGN); + + srs_free(srs); +} +END_TEST + +START_TEST(srs2_reversing) +{ + srs_t* srs = create_srs_t(); + char* output = NULL; + int result; + + result = srs_reverse_alloc(srs, &output, "test@example.com"); + ck_assert_int_eq(result, SRS_ENOTSRSADDRESS); + + result = srs_reverse_alloc(srs, &output, + "SRS0=vmyz=2W=otherdomain.com=test@example.com"); + ck_assert_int_eq(result, SRS_SUCCESS); + ck_assert_str_eq(output, "test@otherdomain.com"); + free(output); + + srs_free(srs); +} +END_TEST + +BEGIN_TEST_SUITE(srs2) +ADD_TEST(srs2_forwarding); +ADD_TEST(srs2_reversing); +END_TEST_SUITE() +TEST_MAIN(srs2)