-
Notifications
You must be signed in to change notification settings - Fork 9
/
Rakefile
168 lines (150 loc) · 4.37 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
task :default => ["task_vaccine_tests:bootstrap",
"task_vaccine_tests:run_tests", "task_vaccine_tests:cleanup",
"task_vaccine_tests:exit"]
namespace :task_vaccine_tests do
task :default => [:bootstrap, :run_tests, :cleanup, :exit]
# # # # # # # # # # # # # # #
# SPECS
# # # # # # # # # # # # # # #
if (File.basename(Dir.pwd) == "tests") then
TESTS_SOURCES = "tests.c ../task_vaccine.c \
../submodules/liblorgnette/lorgnette.c"
PAYLOAD_SOURCES = "demo_payload.c"
TARGET_SOURCES = "demo_target.c"
else
# In case we execute tasks from within the project's main Rakefile
TESTS_SOURCES = "./tests/tests.c ./task_vaccine.c \
./submodules/liblorgnette/lorgnette.c"
PAYLOAD_SOURCES = "./tests/demo_payload.c"
TARGET_SOURCES = "./tests/demo_target.c"
end
TESTS_SHARED_CCFLAGS = ""
TESTS_NEED_SUDO = true
test_specs = [
{:arch => "i386", :ccflags => "-O0" },
{:arch => "x86_64", :ccflags => "-O0" },
]
payload_specs = [
# i386
{ :arch => ["i386"], :ccflags => "" },
# x86_64
{ :arch => ["x86_64"], :ccflags => "" },
# Fat (both i386 and x86_64)
{ :arch => ["i386", "x86_64"], :ccflags => "" },
]
target_specs = [
{ :arch => "i386", :ccflags => "" },
{ :arch => "x86_64", :ccflags => "" },
]
# # # # # # # # # # # # # # #
# TASKS
# # # # # # # # # # # # # # #
desc "Prepare the environment"
task :bootstrap do
system("mkdir build_tests") if (!File.exists?(Dir.getwd + "/build_tests"))
end
desc "Build tests cases"
task :build_tests do
test_specs.each {|entry|
system("clang -arch #{entry[:arch]} \
#{TESTS_SHARED_CCFLAGS} \
#{entry[:ccflags]} \
-o ./build_tests/#{filename_for_test_spec(entry)} \
#{TESTS_SOURCES}")
if $?.exitstatus == 0 then
entry[:result] = true;
else
entry[:result] = false;
puts "Fail to compile test suit: '#{outfile}'. Aborting...".red
end
}
end
desc "Build demo payloads"
task :build_payloads do
payload_specs.each {|entry|
arch_string = "-arch " + entry[:arch].join(" -arch ")
system("clang #{arch_string} \
-dynamiclib -single_module \
-o ./build_tests/#{filename_for_payload_spec(entry)} \
#{PAYLOAD_SOURCES}")
if $?.exitstatus != 0 then
puts "Fail to compile payload: '#{outfile}'. Aborting...".red
exit (-1)
end
}
end
desc "Build demo targets"
task :build_targets do
target_specs.each {|entry|
system("clang -arch #{entry[:arch]} \
#{entry[:ccflags]} \
-o ./build_tests/#{filename_for_target_spec(entry)} \
#{TARGET_SOURCES}")
if $?.exitstatus == 0 then
entry[:result] = true;
else
entry[:result] = false;
puts "Fail to compile target: '#{outfile}'. Aborting...".red
end
}
end
desc "Run test cases"
task :run_tests => [:build_tests, :build_payloads, :build_targets] do
test_specs.each {|entry|
if entry[:result] != false then
sudo_prefix = "sudo " if TESTS_NEED_SUDO
system("#{sudo_prefix}./build_tests/#{filename_for_test_spec(entry)}")
entry[:result] = false if $?.exitstatus != 0
status = (entry[:result] == true) ? "OK".green : "FAIL".red
printf "<test suit: %s> %s\n",
"#{entry[:arch]}/#{entry[:ccflags].sub(' ', ',')}".brown,
status
end
}
end
desc "Run the tests again and again until somebody hits ^C"
task :inferno do
Rake::Task["task_vaccine_tests:bootstrap"].invoke
Rake::Task["task_vaccine_tests:build_tests"].invoke
Rake::Task["task_vaccine_tests:build_targets"].invoke
Rake::Task["task_vaccine_tests:build_payloads"].invoke
i = 1
while true do
Rake::Task["task_vaccine_tests:run_tests"].execute
puts "test n. #{i}"
i = i+1
end
# won't be reached anyway
Rake::Task["task_vaccine_tests:cleanup"].invoke
end
desc "Clean up /build/ directory"
task :cleanup do
system("rm -Rf ./build_tests")
end
desc "Finish testing"
task :exit => [:run_tests] do
if test_specs.index {|e| e[:result] == false} != nil then
exit (-1)
else
exit (0)
end
end
# # # # # # # # # # # # # # #
# MISC
# # # # # # # # # # # # # # #
def filename_for_test_spec(s)
"tests.#{s[:arch]}#{s[:ccflags].delete(' ')}"
end
def filename_for_target_spec(s)
"target.#{s[:arch]}"
end
def filename_for_payload_spec(s)
"payload.#{s[:arch].join('.')}"
end
# Colors, Rainbows, Unicorns!
class String
def red; "\033[1;31m#{self}\033[0m" end
def green; "\033[1;32m#{self}\033[0m" end
def brown; "\033[0;33m#{self}\033[0m" end
end
end