~ubuntu-branches/ubuntu/wily/apparmor/wily

« back to all changes in this revision

Viewing changes to tests/regression/apparmor/netdomain/lib/netdomain_init.exp

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2011-04-27 10:38:07 UTC
  • mfrom: (5.1.118 natty)
  • Revision ID: james.westby@ubuntu.com-20110427103807-ym3rhwys6o84ith0
Tags: 2.6.1-2
debian/copyright: clarify for some full organization names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Adapted from the limal testsuite
 
2
 
 
3
proc multi-run { send_program } {
 
4
        set user_id [exec sh -c "/usr/bin/whoami"]
 
5
        if {$user_id != "root"} {
 
6
                puts "In order to actually load profiles, you need to run this test as root."
 
7
                return 0
 
8
        }
 
9
        set errorOccurred 0
 
10
        set path [split $send_program "/"]
 
11
        set filename [lindex $path [expr [llength $path]-1]]
 
12
        set cwd [pwd]
 
13
 
 
14
        # extract basename and check extension
 
15
 
 
16
        set fname [split $filename "."]
 
17
 
 
18
        if {[llength $fname] < 2} {
 
19
                fail "Bad filename syntax '$send_program'"
 
20
                return -1
 
21
        }
 
22
 
 
23
        if {[lindex $fname [expr [llength $fname]-1]] != "send"} {
 
24
                fail "Not .send extension '$send_program'"
 
25
                return -1
 
26
        }
 
27
 
 
28
        # Set the name the multi run test
 
29
        set base_name [lindex $fname 0]
 
30
        puts "Running $base_name..."
 
31
 
 
32
        set testcases [glob $base_name/*.testcase ]
 
33
 
 
34
        foreach testcase $testcases {
 
35
                set testPath [split $testcase "/"]
 
36
                set testFilename [lindex $testPath [expr [llength $testPath]-1]]
 
37
                set testFname [split $testFilename "."]
 
38
                set testBase_name [lindex $testFname 0]
 
39
 
 
40
                puts "   ... $testBase_name"
 
41
 
 
42
                # Generate the executable output directory
 
43
                exec rm -rf $cwd/$base_name/.out
 
44
                exec mkdir -p $cwd/$base_name/.out
 
45
                set send_output_file $cwd/$base_name/.out/$testBase_name.sent
 
46
                set receive_output_file $cwd/$base_name/.out/$testBase_name.received
 
47
 
 
48
                # Set the testcase expected output files
 
49
                set send_expected_out $cwd/$base_name/output/$testBase_name.send.out
 
50
                set receive_expected_out $cwd/$base_name/output/$testBase_name.receive.out
 
51
 
 
52
                # A little ways down I use sed to turn a generic profile into a testcase
 
53
                # specific one.  Since it uses sed, and we look for three variables in the
 
54
                # generic testcase, we need a couple of staging files.
 
55
                set profile $cwd/$base_name/$testBase_name.profile
 
56
                set generic_profile $cwd/$base_name/$base_name.generic-profile
 
57
                set pathname_profile $cwd/$base_name/.out/$testBase_name.profile.pathnames
 
58
                set netdomain_profile $cwd/$base_name/.out/$testBase_name.profile.netdomain
 
59
                set generated_profile $cwd/$base_name/.out/$testBase_name.profile
 
60
 
 
61
                # The name of the server program
 
62
                set receive_program "./$base_name.receive"
 
63
                # This is used to check whether or not the receive daemon is still running
 
64
                set receive_in_process ./$base_name/$testBase_name.rec.in.process
 
65
 
 
66
                # Read in the test case
 
67
                set testcaseFileId [open $testcase r]
 
68
                set contents [read $testcaseFileId]
 
69
                close $testcaseFileId
 
70
                set line_list [split $contents "\n"]
 
71
 
 
72
                foreach line $line_list {
 
73
                        set listlist [split $line ":"]
 
74
                        set listvalue [string trimleft [lindex $listlist 1]]
 
75
                        set testcase_array([lindex $listlist 0]) $listvalue
 
76
                }
 
77
 
 
78
                # Set up the sed commands to generate the profile
 
79
                set sed_cmd "/bin/sed \"s|\\\$\\\$PWD|[pwd]|\" < $generic_profile > $pathname_profile"
 
80
                set send_netdomain_sed_cmd "/bin/sed \"s|\\\$\\\$SENDNETDOMAIN|$testcase_array(send_profile)|\" < $pathname_profile > $netdomain_profile"
 
81
                set receive_netdomain_sed_cmd "/bin/sed \"s|\\\$\\\$RECEIVENETDOMAIN|$testcase_array(receive_profile)|\" < $netdomain_profile > $generated_profile"
 
82
 
 
83
                # Run sed
 
84
                exec sh -c $sed_cmd
 
85
                exec sh -c $send_netdomain_sed_cmd
 
86
                exec sh -c $receive_netdomain_sed_cmd
 
87
 
 
88
                # Load the profile
 
89
                set apparmor_cmd "/sbin/apparmor_parser --replace < $generated_profile > /dev/null"
 
90
                exec sh -c $apparmor_cmd
 
91
 
 
92
                # fork off the server process
 
93
                if {[fork] == 0} {
 
94
                        # Make a note of our running process
 
95
                        set fileId [open $receive_in_process w]
 
96
                        puts -nonewline $fileId "[pid]"
 
97
                        close $fileId
 
98
                        set rec_cmd "$receive_program $testcase_array(receive_ip) $testcase_array(receive_port) $testcase_array(proto) >$receive_output_file 2>$receive_output_file"
 
99
                        set rec_status [catch { exec sh -c $rec_cmd } rec_result]
 
100
                        file delete $receive_in_process
 
101
                        if { $rec_status != 0 } {
 
102
                                exit 1
 
103
                        }
 
104
                        exit 0
 
105
                }
 
106
                # Give the server a little time to get settled
 
107
                sleep 2
 
108
 
 
109
                set send_command "$send_program $testcase_array(send_ip) $testcase_array(send_port) $testcase_array(receive_ip) $testcase_array(receive_port) $testcase_array(proto) \"$testcase_array(message)\" >$send_output_file 2>$send_output_file"
 
110
                set result ""
 
111
                set oops [catch { set result [exec sh -c $send_command] } caught]
 
112
                if {$oops != 0} {
 
113
                        fail "Test case failed for $testBase_name: $caught"
 
114
                        set errorOccurred 0
 
115
                        continue
 
116
                }
 
117
 
 
118
                if {$result != ""} {
 
119
                        warning "Run of $testBase_name results in '$result'"
 
120
                        set errorOccurred 0
 
121
                        continue
 
122
                }
 
123
 
 
124
                # Wait for the process that we forked earlier to be done
 
125
                while { [file exists $receive_in_process ] != 0 } { }
 
126
 
 
127
                # Now compare the expect output with the actual output from the two programs
 
128
                if { [ file exists $send_output_file ] == 0 } {
 
129
                        perror "Missing file $send_output_file" 0
 
130
                } else {
 
131
                        if { [ diff $send_output_file $send_expected_out ] != 1 } {
 
132
                                puts "Send program output doesn't match expected data:"
 
133
                                puts [ exec sh -c "diff -u $send_output_file $send_expected_out || true" ]
 
134
                                fail $testBase_name
 
135
                                set errorOccurred 0
 
136
                                continue
 
137
                        }
 
138
                }
 
139
 
 
140
                if { [ file exists $receive_output_file ] == 0 } {
 
141
                        perror "Missing file $receive_output_file" 0
 
142
                } else {
 
143
                        if { [ diff $receive_output_file $receive_expected_out ] != 1 } {
 
144
                                puts "Receive program output doesn't match expected data:"
 
145
                                puts [ exec sh -c "diff -u $receive_output_file $receive_expected_out || true" ]
 
146
                                fail $testBase_name
 
147
                                set errorOccurred 0
 
148
                                continue
 
149
                        }
 
150
                }
 
151
                # If we got this far, then everything is fine
 
152
                pass $testBase_name
 
153
        }
 
154
 
 
155
        if { $errorOccurred == 0 } {
 
156
                return 0
 
157
        } else {
 
158
                return -1
 
159
        }
 
160
}
 
161