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