]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fido-id-desc.c
meson: correct man page deps
[thirdparty/systemd.git] / src / test / test-fido-id-desc.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <stdint.h>
4 #include <stdlib.h>
5
6 #include "fido_id/fido_id_desc.h"
7 #include "macro.h"
8
9 static void test_is_fido_security_token_desc__fido(void) {
10 static const uint8_t FIDO_HID_DESC_1[] = {
11 0x06, 0xd0, 0xf1, 0x09, 0x01, 0xa1, 0x01, 0x09, 0x20, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75,
12 0x08, 0x95, 0x40, 0x81, 0x02, 0x09, 0x21, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95,
13 0x40, 0x91, 0x02, 0xc0,
14 };
15 assert_se(is_fido_security_token_desc(FIDO_HID_DESC_1, sizeof(FIDO_HID_DESC_1)) > 0);
16
17 static const uint8_t FIDO_HID_DESC_2[] = {
18 0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 0x25,
19 0x01, 0x75, 0x01, 0x95, 0x08, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x01, 0x95, 0x05,
20 0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 0x91, 0x02, 0x95, 0x01, 0x75, 0x03, 0x91,
21 0x01, 0x95, 0x06, 0x75, 0x08, 0x15, 0x00, 0x25, 0x65, 0x05, 0x07, 0x19, 0x00, 0x29, 0x65,
22 0x81, 0x00, 0x09, 0x03, 0x75, 0x08, 0x95, 0x08, 0xb1, 0x02, 0xc0,
23 0x06, 0xd0, 0xf1, 0x09, 0x01, 0xa1, 0x01, 0x09, 0x20, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75,
24 0x08, 0x95, 0x40, 0x81, 0x02, 0x09, 0x21, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95,
25 0x40, 0x91, 0x02, 0xc0,
26 };
27 assert_se(is_fido_security_token_desc(FIDO_HID_DESC_2, sizeof(FIDO_HID_DESC_2)) > 0);
28 }
29
30 static void test_is_fido_security_token_desc__non_fido(void) {
31 /* Wrong usage page */
32 static const uint8_t NON_FIDO_HID_DESC_1[] = {
33 0x06, 0xd0, 0xf0, 0x09, 0x01, 0xa1, 0x01, 0x09, 0x20, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75,
34 0x08, 0x95, 0x40, 0x81, 0x02, 0x09, 0x21, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95,
35 0x40, 0x91, 0x02, 0xc0,
36 };
37 assert_se(is_fido_security_token_desc(NON_FIDO_HID_DESC_1, sizeof(NON_FIDO_HID_DESC_1)) == 0);
38
39 /* Wrong usage */
40 static const uint8_t NON_FIDO_HID_DESC_2[] = {
41 0x06, 0xd0, 0xf1, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x20, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75,
42 0x08, 0x95, 0x40, 0x81, 0x02, 0x09, 0x21, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95,
43 0x40, 0x91, 0x02, 0xc0,
44 };
45 assert_se(is_fido_security_token_desc(NON_FIDO_HID_DESC_2, sizeof(NON_FIDO_HID_DESC_2)) == 0);
46
47 static const uint8_t NON_FIDO_HID_DESC_3[] = {
48 0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 0x25,
49 0x01, 0x75, 0x01, 0x95, 0x08, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x01, 0x95, 0x05,
50 0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 0x91, 0x02, 0x95, 0x01, 0x75, 0x03, 0x91,
51 0x01, 0x95, 0x06, 0x75, 0x08, 0x15, 0x00, 0x25, 0x65, 0x05, 0x07, 0x19, 0x00, 0x29, 0x65,
52 0x81, 0x00, 0x09, 0x03, 0x75, 0x08, 0x95, 0x08, 0xb1, 0x02, 0xc0,
53 };
54 assert_se(is_fido_security_token_desc(NON_FIDO_HID_DESC_3, sizeof(NON_FIDO_HID_DESC_3)) == 0);
55 }
56
57 static void test_is_fido_security_token_desc__invalid(void) {
58 /* Size coded on 1 byte, but no byte given */
59 static const uint8_t INVALID_HID_DESC_1[] = { 0x01 };
60 assert_se(is_fido_security_token_desc(INVALID_HID_DESC_1, sizeof(INVALID_HID_DESC_1)) < 0);
61
62 /* Size coded on 2 bytes, but only 1 byte given */
63 static const uint8_t INVALID_HID_DESC_2[] = { 0x02, 0x01 };
64 assert_se(is_fido_security_token_desc(INVALID_HID_DESC_2, sizeof(INVALID_HID_DESC_2)) < 0);
65
66 /* Size coded on 4 bytes, but only 3 bytes given */
67 static const uint8_t INVALID_HID_DESC_3[] = { 0x03, 0x01, 0x02, 0x03 };
68 assert_se(is_fido_security_token_desc(INVALID_HID_DESC_3, sizeof(INVALID_HID_DESC_3)) < 0);
69
70 /* Long item without a size byte */
71 static const uint8_t INVALID_HID_DESC_4[] = { 0xfe };
72 assert_se(is_fido_security_token_desc(INVALID_HID_DESC_4, sizeof(INVALID_HID_DESC_4)) < 0);
73
74 /* Usage pages are coded on at most 2 bytes */
75 static const uint8_t INVALID_HID_DESC_5[] = { 0x07, 0x01, 0x02, 0x03, 0x04 };
76 assert_se(is_fido_security_token_desc(INVALID_HID_DESC_5, sizeof(INVALID_HID_DESC_5)) < 0);
77 }
78
79 int main(int argc, char *argv[]) {
80 test_is_fido_security_token_desc__fido();
81 test_is_fido_security_token_desc__non_fido();
82 test_is_fido_security_token_desc__invalid();
83
84 return EXIT_SUCCESS;
85 }