Skip to content

Commit 25296c6

Browse files
rprobainapcmoore
authored andcommitted
tests/field_compare: add field_compare test
Issue: #5 Simple test case to test UID/GID event filtering. This test case adds full coverage to the following auditsc.c functions, which are not exercised by any other test case of the test suite: - audit_field_compare() - audit_compare_gid() - audit_compare_uid() Signed-off-by: Ricardo Robaina <[email protected]> Signed-off-by: Paul Moore <[email protected]>
1 parent 5a10451 commit 25296c6

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

tests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ TESTS := \
1919
exec_execve \
2020
exec_name \
2121
fanotify \
22+
field_compare \
2223
file_create \
2324
file_delete \
2425
file_permission \

tests/field_compare/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
TARGETS=$(patsubst %.c,%,$(wildcard *.c))
2+
3+
LDLIBS += -lpthread
4+
5+
all: $(TARGETS)
6+
clean:
7+
rm -f $(TARGETS)
8+

tests/field_compare/test

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
5+
use Test;
6+
BEGIN { plan tests => 50 }
7+
8+
use File::Temp qw/ tempdir tempfile /;
9+
10+
###
11+
# functions
12+
13+
sub key_gen {
14+
my @chars = ( "A" .. "Z", "a" .. "z" );
15+
my $key = "testsuite-" . time . "-";
16+
$key .= $chars[ rand @chars ] for 1 .. 8;
17+
return $key;
18+
}
19+
20+
###
21+
# setup
22+
23+
# reset audit
24+
system("auditctl -D >& /dev/null");
25+
26+
# create temp directory
27+
my $dir = tempdir( TEMPLATE => '/tmp/audit-testsuite-XXXX', CLEANUP => 1 );
28+
29+
# create stdout/stderr sinks
30+
( my $fh_out, my $stdout ) = tempfile(
31+
TEMPLATE => '/tmp/audit-testsuite-out-XXXX',
32+
UNLINK => 1
33+
);
34+
( my $fh_err, my $stderr ) = tempfile(
35+
TEMPLATE => '/tmp/audit-testsuite-err-XXXX',
36+
UNLINK => 1
37+
);
38+
39+
###
40+
# tests
41+
42+
# uid fields tests
43+
my @fields = ( "auid", "uid", "euid", "suid", "fsuid", "obj_uid" );
44+
45+
# equal operator
46+
for my $i ( 0 .. $#fields ) {
47+
for ( my $j = $i + 1 ; $j <= $#fields ; $j++ ) {
48+
my $key = key_gen();
49+
system(
50+
"auditctl -a always,exit -S openat -C $fields[$i]=$fields[$j] -k $key > $stdout 2> $stderr"
51+
);
52+
my $filename = tempfile( TEMPLATE => $dir . "/file-XXXX", UNLINK => 1 );
53+
unlink($filename);
54+
my $result = system("ausearch -i -k $key > $stdout 2> $stderr");
55+
ok( $result, 0 );
56+
system("auditctl -D >& /dev/null");
57+
}
58+
}
59+
60+
# not equal opeator
61+
for my $i ( 0 .. $#fields ) {
62+
for ( my $j = $i + 1 ; $j <= $#fields ; $j++ ) {
63+
my $key = key_gen();
64+
system(
65+
"auditctl -a always,exit -S openat -C $fields[$i]!=$fields[$j] -k $key > $stdout 2> $stderr"
66+
);
67+
my $filename = tempfile( TEMPLATE => $dir . "/file-XXXX", UNLINK => 1 );
68+
unlink($filename);
69+
my $result = system("ausearch -i -k $key > $stdout 2> $stderr");
70+
ok( $result, 0 );
71+
system("auditctl -D >& /dev/null");
72+
}
73+
}
74+
75+
# gid field tests
76+
@fields = ( "gid", "egid", "sgid", "fsgid", "obj_gid" );
77+
78+
# equal tests
79+
for my $i ( 0 .. $#fields ) {
80+
for ( my $j = $i + 1 ; $j <= $#fields ; $j++ ) {
81+
my $key = key_gen();
82+
system(
83+
"auditctl -a always,exit -S openat -C $fields[$i]=$fields[$j] -k $key > $stdout 2> $stderr"
84+
);
85+
my $filename = tempfile( TEMPLATE => $dir . "/file-XXXX", UNLINK => 1 );
86+
unlink($filename);
87+
my $result = system("ausearch -i -k $key > $stdout 2> $stderr");
88+
ok( $result, 0 );
89+
system("auditctl -D >& /dev/null");
90+
}
91+
}
92+
93+
# not equal tests
94+
for my $i ( 0 .. $#fields ) {
95+
for ( my $j = $i + 1 ; $j <= $#fields ; $j++ ) {
96+
my $key = key_gen();
97+
system(
98+
"auditctl -a always,exit -S openat -C $fields[$i]!=$fields[$j] -k $key > $stdout 2> $stderr"
99+
);
100+
my $filename = tempfile( TEMPLATE => $dir . "/file-XXXX", UNLINK => 1 );
101+
unlink($filename);
102+
my $result = system("ausearch -i -k $key > $stdout 2> $stderr");
103+
ok( $result, 0 );
104+
system("auditctl -D >& /dev/null");
105+
}
106+
}
107+

0 commit comments

Comments
 (0)