Skip to content

Commit

Permalink
Add unit test for srs2 and fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
roehling committed Aug 7, 2023
1 parent 5998fdc commit 07b4190
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/srs2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ target_link_libraries(
test_database_executable PRIVATE $<$<BOOL:${WITH_SQLITE}>:sqlite3::sqlite3>
$<$<BOOL:${WITH_REDIS}>:${HIREDIS_TARGET}>
)
add_postsrsd_test(test_srs2 ${SRCDIR}/srs2.c ${SRCDIR}/sha1.c)
77 changes: 77 additions & 0 deletions tests/unit/test_srs2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* PostSRSd - Sender Rewriting Scheme daemon for Postfix
* Copyright 2012-2022 Timo Röhling <[email protected]>
* 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 <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include "srs2.h"

#include <check.h>

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, "[email protected]", "example.com");
ck_assert_int_eq(result, SRS_SUCCESS);
ck_assert_str_eq(output, "[email protected]");
free(output);

result =
srs_forward_alloc(srs, &output, "[email protected]", "example.com");
ck_assert_int_eq(result, SRS_SUCCESS);
ck_assert_str_eq(output, "[email protected]");
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, "[email protected]");
ck_assert_int_eq(result, SRS_ENOTSRSADDRESS);

result = srs_reverse_alloc(srs, &output,
"[email protected]");
ck_assert_int_eq(result, SRS_SUCCESS);
ck_assert_str_eq(output, "[email protected]");
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)

0 comments on commit 07b4190

Please sign in to comment.