| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "bootspec-util.h" |
| 5 |
#include "tests.h" |
| 6 |
|
| 7 |
static void test_one( |
| 8 |
const char *entry_token, |
| 9 |
uint64_t entry_commit, |
| 10 |
const char *version, |
| 11 |
unsigned profile_nr, |
| 12 |
unsigned tries_left, |
| 13 |
const char *expected) { |
| 14 |
|
| 15 |
_cleanup_free_ char *fn = NULL; |
| 16 |
ASSERT_OK(boot_entry_make_commit_filename(entry_token, entry_commit, version, profile_nr, tries_left, &fn)); |
| 17 |
ASSERT_STREQ(fn, expected); |
| 18 |
|
| 19 |
_cleanup_free_ char *token = NULL; |
| 20 |
uint64_t commit = 0; |
| 21 |
ASSERT_OK(boot_entry_parse_commit_filename(fn, &token, &commit)); |
| 22 |
ASSERT_STREQ(token, entry_token); |
| 23 |
ASSERT_EQ(commit, entry_commit); |
| 24 |
} |
| 25 |
|
| 26 |
TEST(boot_entry_commit_filename) { |
| 27 |
test_one("foo", 1, NULL, 0, UINT_MAX, "foo-commit_1.conf"); |
| 28 |
test_one("foo", 42, "1.0", 0, UINT_MAX, "foo-commit_42.1.0.conf"); |
| 29 |
test_one("foo", 42, "1.0", 3, UINT_MAX, "[email protected]"); |
| 30 |
test_one("foo", 42, "1.0", 3, 5, "foo-commit_42.1.0@3+5.conf"); |
| 31 |
test_one("foo", 42, NULL, 3, UINT_MAX, "[email protected]"); |
| 32 |
test_one("foo", 42, NULL, 3, 7, "foo-commit_42@3+7.conf"); |
| 33 |
test_one("foo", 42, NULL, 0, 9, "foo-commit_42+9.conf"); |
| 34 |
test_one("my-token", 123456, "v2", 0, UINT_MAX, "my-token-commit_123456.v2.conf"); |
| 35 |
|
| 36 |
/* Invalid inputs for make */ |
| 37 |
_cleanup_free_ char *fn = NULL; |
| 38 |
ASSERT_ERROR(boot_entry_make_commit_filename("foo/bar", 1, NULL, 0, UINT_MAX, &fn), EINVAL); |
| 39 |
ASSERT_ERROR(boot_entry_make_commit_filename("foo", 0, NULL, 0, UINT_MAX, &fn), EINVAL); |
| 40 |
ASSERT_ERROR(boot_entry_make_commit_filename("foo", UINT64_MAX, NULL, 0, UINT_MAX, &fn), EINVAL); |
| 41 |
|
| 42 |
/* Invalid inputs for parse */ |
| 43 |
_cleanup_free_ char *token = NULL; |
| 44 |
uint64_t commit = 0; |
| 45 |
ASSERT_ERROR(boot_entry_parse_commit_filename("foo.conf", &token, &commit), EBADMSG); |
| 46 |
ASSERT_ERROR(boot_entry_parse_commit_filename("foo-commit_.conf", &token, &commit), EBADMSG); |
| 47 |
ASSERT_ERROR(boot_entry_parse_commit_filename("foo-commit_abc.conf", &token, &commit), EBADMSG); |
| 48 |
ASSERT_ERROR(boot_entry_parse_commit_filename("foo-commit_0.conf", &token, &commit), EBADMSG); |
| 49 |
} |
| 50 |
|
| 51 |
DEFINE_TEST_MAIN(LOG_INFO); |