| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <string.h> |
| 4 |
|
| 5 |
#include "recovery-key.h" |
| 6 |
#include "tests.h" |
| 7 |
|
| 8 |
TEST(make_recovery_key) { |
| 9 |
_cleanup_(erase_and_freep) char *recovery_key = NULL; |
| 10 |
size_t length; |
| 11 |
const size_t num_test = 10; |
| 12 |
char *generated_keys[num_test]; |
| 13 |
int r; |
| 14 |
|
| 15 |
/* Check for successful recovery-key creation */ |
| 16 |
r = make_recovery_key(&recovery_key); |
| 17 |
assert_se(r == 0); |
| 18 |
ASSERT_NOT_NULL(recovery_key); |
| 19 |
|
| 20 |
/* Check that length of formatted key is 72 with 64 modhex characters */ |
| 21 |
length = strlen(recovery_key); |
| 22 |
assert_se(length == RECOVERY_KEY_MODHEX_FORMATTED_LENGTH - 1); |
| 23 |
/* Check modhex characters in formatted key with dashes */ |
| 24 |
for (size_t i = 0; i < length; i++) { |
| 25 |
assert_se((recovery_key[i] >= 'a' && recovery_key[i] <= 'v') || recovery_key[i] == '-'); |
| 26 |
if (i % 9 == 8) |
| 27 |
/* confirm '-' is after every 8 characters */ |
| 28 |
assert_se(recovery_key[i] == '-'); |
| 29 |
} |
| 30 |
/* Repeat tests to determine randomness of generated keys */ |
| 31 |
for (size_t test = 0; test < num_test; ++test) { |
| 32 |
r = make_recovery_key(&generated_keys[test]); |
| 33 |
assert_se(r == 0); |
| 34 |
length = strlen(generated_keys[test]); |
| 35 |
assert_se(length == RECOVERY_KEY_MODHEX_FORMATTED_LENGTH - 1); |
| 36 |
for (size_t i = 0; i < length; i++) { |
| 37 |
assert_se((generated_keys[test][i] >= 'a' && generated_keys[test][i] <= 'v') |
| 38 |
|| generated_keys[test][i] == '-'); |
| 39 |
if (i % 9 == 8) |
| 40 |
assert_se(generated_keys[test][i] == '-'); |
| 41 |
} |
| 42 |
/* Check for uniqueness of each generated recovery key */ |
| 43 |
for (size_t prev = 0; prev < test; ++prev) |
| 44 |
assert_se(!streq(generated_keys[test], generated_keys[prev])); |
| 45 |
} |
| 46 |
for (size_t i = 0; i < num_test; i++) |
| 47 |
free(generated_keys[i]); |
| 48 |
} |
| 49 |
|
| 50 |
TEST(decode_modhex_char) { |
| 51 |
|
| 52 |
assert_se(decode_modhex_char('c') == 0); |
| 53 |
assert_se(decode_modhex_char('C') == 0); |
| 54 |
assert_se(decode_modhex_char('b') == 1); |
| 55 |
assert_se(decode_modhex_char('B') == 1); |
| 56 |
assert_se(decode_modhex_char('d') == 2); |
| 57 |
assert_se(decode_modhex_char('D') == 2); |
| 58 |
assert_se(decode_modhex_char('e') == 3); |
| 59 |
assert_se(decode_modhex_char('E') == 3); |
| 60 |
assert_se(decode_modhex_char('f') == 4); |
| 61 |
assert_se(decode_modhex_char('F') == 4); |
| 62 |
assert_se(decode_modhex_char('g') == 5); |
| 63 |
assert_se(decode_modhex_char('G') == 5); |
| 64 |
assert_se(decode_modhex_char('h') == 6); |
| 65 |
assert_se(decode_modhex_char('H') == 6); |
| 66 |
assert_se(decode_modhex_char('i') == 7); |
| 67 |
assert_se(decode_modhex_char('I') == 7); |
| 68 |
assert_se(decode_modhex_char('j') == 8); |
| 69 |
assert_se(decode_modhex_char('J') == 8); |
| 70 |
assert_se(decode_modhex_char('k') == 9); |
| 71 |
assert_se(decode_modhex_char('K') == 9); |
| 72 |
assert_se(decode_modhex_char('l') == 10); |
| 73 |
assert_se(decode_modhex_char('L') == 10); |
| 74 |
assert_se(decode_modhex_char('n') == 11); |
| 75 |
assert_se(decode_modhex_char('N') == 11); |
| 76 |
assert_se(decode_modhex_char('r') == 12); |
| 77 |
assert_se(decode_modhex_char('R') == 12); |
| 78 |
assert_se(decode_modhex_char('t') == 13); |
| 79 |
assert_se(decode_modhex_char('T') == 13); |
| 80 |
assert_se(decode_modhex_char('u') == 14); |
| 81 |
assert_se(decode_modhex_char('U') == 14); |
| 82 |
assert_se(decode_modhex_char('v') == 15); |
| 83 |
assert_se(decode_modhex_char('V') == 15); |
| 84 |
assert_se(decode_modhex_char('a') == -EINVAL); |
| 85 |
assert_se(decode_modhex_char('A') == -EINVAL); |
| 86 |
assert_se(decode_modhex_char('x') == -EINVAL); |
| 87 |
assert_se(decode_modhex_char('.') == -EINVAL); |
| 88 |
assert_se(decode_modhex_char('/') == -EINVAL); |
| 89 |
assert_se(decode_modhex_char('\0') == -EINVAL); |
| 90 |
} |
| 91 |
|
| 92 |
TEST(normalize_recovery_key) { |
| 93 |
_cleanup_(erase_and_freep) char *normalized_key1 = NULL; |
| 94 |
_cleanup_(erase_and_freep) char *normalized_key2 = NULL; |
| 95 |
_cleanup_(erase_and_freep) char *normalized_key3 = NULL; |
| 96 |
int r; |
| 97 |
|
| 98 |
/* Case 1: Normalization without dashes */ |
| 99 |
r = normalize_recovery_key("cdefghijcdefghijcdefghijcdefghijcdefghijcdefghijcdefghijcdefghij", |
| 100 |
&normalized_key1); |
| 101 |
assert(r == 0); |
| 102 |
assert(streq(normalized_key1, "cdefghij-cdefghij-cdefghij-cdefghij-cdefghij-cdefghij-cdefghij-cdefghij")); |
| 103 |
|
| 104 |
/* Case 2: Normalization with dashes */ |
| 105 |
r = normalize_recovery_key("cdefVhij-cDefghij-cdefkhij-cdufghij-cdefgdij-cidefIhj-cdefNijR-cdVfguij", |
| 106 |
&normalized_key2); |
| 107 |
assert_se(r == 0); |
| 108 |
ASSERT_STREQ(normalized_key2, "cdefvhij-cdefghij-cdefkhij-cdufghij-cdefgdij-cidefihj-cdefnijr-cdvfguij"); |
| 109 |
|
| 110 |
/* Case 3: Invalid password length */ |
| 111 |
r = normalize_recovery_key("1234-5678-90AB-CDEF-1234-5678-90AB-CDEF", &normalized_key1); |
| 112 |
assert(r == -EINVAL); |
| 113 |
|
| 114 |
/* Case 4: Invalid password format(missing dash) */ |
| 115 |
r = normalize_recovery_key("cdefghij-cdefghij-cdefghij-cdefghij-cdefghij-cdefghij-cdefghijcdefghij", |
| 116 |
&normalized_key1); |
| 117 |
assert_se(r == -EINVAL); |
| 118 |
|
| 119 |
/* Case 5: Normalization of Upper cases password without dashes */ |
| 120 |
r = normalize_recovery_key("BFGHICEHHIUVLKJIHFHEDlntruvcdefjiTUVKLNIJVTUTKJIHDFBCBGHIJHHFDBC", |
| 121 |
&normalized_key3); |
| 122 |
assert(r == 0); |
| 123 |
ASSERT_STREQ(normalized_key3, "bfghiceh-hiuvlkji-hfhedlnt-ruvcdefj-ituvklni-jvtutkji-hdfbcbgh-ijhhfdbc"); |
| 124 |
|
| 125 |
/* Case 6: Minimum password length */ |
| 126 |
r = normalize_recovery_key("", &normalized_key1); |
| 127 |
assert_se(r == -EINVAL); |
| 128 |
|
| 129 |
/* Case 7: Invalid characters and numbers in password */ |
| 130 |
r = normalize_recovery_key("cde123hi-cdefgzij-cdefghij-cdefghij-cdefghij-cdefghij-cdefghijcdefghij", |
| 131 |
&normalized_key1); |
| 132 |
assert_se(r == -EINVAL); |
| 133 |
} |
| 134 |
|
| 135 |
DEFINE_TEST_MAIN(LOG_INFO); |