Skip to content

Commit b54761f

Browse files
broonieshuahkh
authored andcommitted
kselftest/seccomp: Report each expectation we assert as a KTAP test
The seccomp benchmark test makes a number of checks on the performance it measures and logs them to the output but does so in a custom format which none of the automated test runners understand meaning that the chances that anyone is paying attention are slim. Let's additionally log each result in KTAP format so that automated systems parsing the test output will see each comparison as a test case. The original logs are left in place since they provide the actual numbers for analysis. As part of this rework the flow for the main program so that when we skip tests we still log all the tests we skip, this is because the standard KTAP headers and footers include counts of the number of expected and run tests. Tested-by: Anders Roxell <[email protected]> Acked-by: Kees Cook <[email protected]> Signed-off-by: Mark Brown <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 5820cfe commit b54761f

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

tools/testing/selftests/seccomp/seccomp_benchmark.c

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,36 @@ bool le(int i_one, int i_two)
9898
}
9999

100100
long compare(const char *name_one, const char *name_eval, const char *name_two,
101-
unsigned long long one, bool (*eval)(int, int), unsigned long long two)
101+
unsigned long long one, bool (*eval)(int, int), unsigned long long two,
102+
bool skip)
102103
{
103104
bool good;
104105

106+
if (skip) {
107+
ksft_test_result_skip("%s %s %s\n", name_one, name_eval,
108+
name_two);
109+
return 0;
110+
}
111+
105112
ksft_print_msg("\t%s %s %s (%lld %s %lld): ", name_one, name_eval, name_two,
106113
(long long)one, name_eval, (long long)two);
107114
if (one > INT_MAX) {
108115
ksft_print_msg("Miscalculation! Measurement went negative: %lld\n", (long long)one);
109-
return 1;
116+
good = false;
117+
goto out;
110118
}
111119
if (two > INT_MAX) {
112120
ksft_print_msg("Miscalculation! Measurement went negative: %lld\n", (long long)two);
113-
return 1;
121+
good = false;
122+
goto out;
114123
}
115124

116125
good = eval(one, two);
117126
printf("%s\n", good ? "✔️" : "❌");
118127

128+
out:
129+
ksft_test_result(good, "%s %s %s\n", name_one, name_eval, name_two);
130+
119131
return good ? 0 : 1;
120132
}
121133

@@ -142,9 +154,13 @@ int main(int argc, char *argv[])
142154
unsigned long long samples, calc;
143155
unsigned long long native, filter1, filter2, bitmap1, bitmap2;
144156
unsigned long long entry, per_filter1, per_filter2;
157+
bool skip = false;
145158

146159
setbuf(stdout, NULL);
147160

161+
ksft_print_header();
162+
ksft_set_plan(7);
163+
148164
ksft_print_msg("Running on:\n");
149165
ksft_print_msg("");
150166
system("uname -a");
@@ -202,8 +218,10 @@ int main(int argc, char *argv[])
202218
#define ESTIMATE(fmt, var, what) do { \
203219
var = (what); \
204220
ksft_print_msg("Estimated " fmt ": %llu ns\n", var); \
205-
if (var > INT_MAX) \
206-
goto more_samples; \
221+
if (var > INT_MAX) { \
222+
skip = true; \
223+
ret |= 1; \
224+
} \
207225
} while (0)
208226

209227
ESTIMATE("total seccomp overhead for 1 bitmapped filter", calc,
@@ -222,30 +240,33 @@ int main(int argc, char *argv[])
222240
(filter2 - native - entry) / 4);
223241

224242
ksft_print_msg("Expectations:\n");
225-
ret |= compare("native", "≤", "1 bitmap", native, le, bitmap1);
226-
bits = compare("native", "≤", "1 filter", native, le, filter1);
243+
ret |= compare("native", "≤", "1 bitmap", native, le, bitmap1,
244+
skip);
245+
bits = compare("native", "≤", "1 filter", native, le, filter1,
246+
skip);
227247
if (bits)
228-
goto more_samples;
248+
skip = true;
229249

230250
ret |= compare("per-filter (last 2 diff)", "≈", "per-filter (filters / 4)",
231-
per_filter1, approx, per_filter2);
251+
per_filter1, approx, per_filter2, skip);
232252

233253
bits = compare("1 bitmapped", "≈", "2 bitmapped",
234-
bitmap1 - native, approx, bitmap2 - native);
254+
bitmap1 - native, approx, bitmap2 - native, skip);
235255
if (bits) {
236256
ksft_print_msg("Skipping constant action bitmap expectations: they appear unsupported.\n");
237-
goto out;
257+
skip = true;
238258
}
239259

240-
ret |= compare("entry", "≈", "1 bitmapped", entry, approx, bitmap1 - native);
241-
ret |= compare("entry", "≈", "2 bitmapped", entry, approx, bitmap2 - native);
260+
ret |= compare("entry", "≈", "1 bitmapped", entry, approx,
261+
bitmap1 - native, skip);
262+
ret |= compare("entry", "≈", "2 bitmapped", entry, approx,
263+
bitmap2 - native, skip);
242264
ret |= compare("native + entry + (per filter * 4)", "≈", "4 filters total",
243-
entry + (per_filter1 * 4) + native, approx, filter2);
244-
if (ret == 0)
245-
goto out;
265+
entry + (per_filter1 * 4) + native, approx, filter2,
266+
skip);
246267

247-
more_samples:
248-
ksft_print_msg("Saw unexpected benchmark result. Try running again with more samples?\n");
249-
out:
250-
return 0;
268+
if (ret)
269+
ksft_print_msg("Saw unexpected benchmark result. Try running again with more samples?\n");
270+
271+
ksft_finished();
251272
}

0 commit comments

Comments
 (0)