]> FriiDump Source - friidump.git/blob - tests/test_linux_rawio.c
FriiDump 0.5.3.16: finalize release identity and documentation
[friidump.git] / tests / test_linux_rawio.c
1 #include "linux_rawio.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7
8 static int create_status(char *path, size_t path_size, const char *cap_eff) {
9     int fd;
10     FILE *f;
11
12     if (path_size < sizeof("/tmp/friidump-rawio-XXXXXX"))
13         return 0;
14
15     snprintf(path, path_size, "/tmp/friidump-rawio-XXXXXX");
16     fd = mkstemp(path);
17     if (fd < 0)
18         return 0;
19
20     f = fdopen(fd, "wb");
21     if (!f) {
22         close(fd);
23         remove(path);
24         return 0;
25     }
26
27     fprintf(f, "Name:\ttest\nCapInh:\t0000000000000000\nCapEff:\t%s\n", cap_eff);
28     fclose(f);
29     return 1;
30 }
31
32 static int expect_state(
33     const char *label,
34     const char *path,
35     friidump_linux_rawio_state expected,
36     unsigned long long expected_mask) {
37     unsigned long long mask = 0;
38     friidump_linux_rawio_state actual =
39         friidump_linux_rawio_state_from_status_file(path, &mask);
40
41     if (actual != expected || mask != expected_mask) {
42         fprintf(stderr,
43             "FAIL: %s state=%d expected=%d mask=0x%llx expected=0x%llx\n",
44             label, (int) actual, (int) expected,
45             mask, expected_mask);
46         return 0;
47     }
48
49     printf("PASS: %s\n", label);
50     return 1;
51 }
52
53 int main(void) {
54     char present_path[64];
55     char missing_path[64];
56     char malformed_path[64];
57     unsigned long long rawio_mask = 1ULL << FRIIDUMP_LINUX_CAP_SYS_RAWIO;
58     char present_value[32];
59     int ok = 1;
60
61     snprintf(present_value, sizeof(present_value), "%016llx", rawio_mask);
62
63     ok &= create_status(present_path, sizeof(present_path), present_value);
64     ok &= create_status(missing_path, sizeof(missing_path), "0000000000000000");
65     ok &= create_status(malformed_path, sizeof(malformed_path), "not-hex");
66
67     if (!ok) {
68         fprintf(stderr, "FAIL: fixture creation\n");
69         return 1;
70     }
71
72     ok &= expect_state(
73         "CAP_SYS_RAWIO present",
74         present_path,
75         FRIIDUMP_LINUX_RAWIO_PRESENT,
76         rawio_mask);
77     ok &= expect_state(
78         "CAP_SYS_RAWIO missing",
79         missing_path,
80         FRIIDUMP_LINUX_RAWIO_MISSING,
81         0);
82     ok &= expect_state(
83         "malformed CapEff",
84         malformed_path,
85         FRIIDUMP_LINUX_RAWIO_UNKNOWN,
86         0);
87     ok &= expect_state(
88         "missing status file",
89         "/definitely/not/a/real/status/file",
90         FRIIDUMP_LINUX_RAWIO_UNKNOWN,
91         0);
92
93     remove(present_path);
94     remove(missing_path);
95     remove(malformed_path);
96
97     if (!ok)
98         return 1;
99
100     printf("LINUX RAW-I/O CAPABILITY PARSER: PASS\n");
101     return 0;
102 }