-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.rb
executable file
·250 lines (197 loc) · 5.46 KB
/
test.rb
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env ruby
require "forwardable"
require "minitest/autorun"
require "open3"
require "rubygems"
require "rubygems/package"
require "set"
def execute(command, *args)
stdout, status = Open3.capture2(command, *args)
raise "Failed to executed command: #{command} #{args.join(' ')}" unless status.success?
stdout
end
def qemu_path(architecture)
"qemu/build/qemu-system-#{architecture}"
end
def assert_qemu_system(architecture, firmwares:)
validator = QemuSystemValidator.new(architecture, firmwares)
assert validator.valid?, validator.message
end
def assert_only_system_dependencies(architecture)
return unless QemuSystemValidator.host_os == "macos"
allowed_prefixes = Set.new ["/System/Library/Frameworks", "/usr/lib"]
qemu_path = qemu_path(architecture)
result = execute "otool", "-L", qemu_path
non_system_dependecies = result
.split("\n")
.drop(1)
.map(&:strip)
.reject { |path| allowed_prefixes.any? { path.start_with?(_1) } }
.map { _1.split.first }
assert non_system_dependecies.empty?, %("#{qemu_path}" is linked with the following non-system dependencies:\n#{non_system_dependecies.join("\n")})
end
def assert_statically_linked(architecture)
return unless QemuSystemValidator.host_os == "linux"
qemu_path = qemu_path(architecture)
result = execute "file", qemu_path
statically_linked = result.include?("static-pie linked") || result.include?("statically linked")
assert statically_linked, %("#{qemu_path}" is not statically linked:\n#{result})
end
describe "resources" do
describe "qemu-system" do
describe "x86_64" do
it "contains the correct file structure for x86_64" do
assert_qemu_system "x86_64", firmwares: %w[
bios-256k.bin
efi-e1000.rom
efi-virtio.rom
kvmvapic.bin
vgabios-stdvga.bin
uefi.fd
]
end
it "is only linked with system dependencies" do
assert_only_system_dependencies "x86_64"
end
it "is statically linked" do
assert_statically_linked "x86_64"
end
end
describe "arm64" do
it "contains the correct file structure for arm64" do
assert_qemu_system "aarch64", firmwares: %w[
efi-e1000.rom
efi-virtio.rom
uefi.fd
linaro_uefi.fd
]
end
it "is only linked with system dependencies" do
assert_only_system_dependencies "aarch64"
end
it "is statically linked" do
assert_statically_linked "aarch64"
end
end
end
end
class QemuSystemValidator
attr_reader :firmwares
def initialize(architecture, firmwares)
@architecture = architecture
@firmwares = firmwares.sort
end
def self.host_os
@host_os ||= case Gem::Platform.local.os
when "darwin"
"macos"
when "linux"
"linux"
else
raise "Unsupported platform: #{Gem::Platform.local.os}"
end
end
def valid?
@valid ||= qemu_binary? && firmware_matching?
end
def message
message_formatter.format
end
def tar_file
@tar_file ||= TarFile.for(architecture: architecture, host_os: host_os)
end
def extra
@extra ||= tar_file.firmwares - firmwares
end
def missing
@missing ||= firmwares - tar_file.firmwares
end
private
attr_reader :architecture
def qemu_binary?
tar_file.qemu_binary.any?
end
def firmware_matching?
extra.empty? && missing.empty?
end
def message_formatter
@message_formatter ||= MessageFormatter.new(self)
end
def host_os
self.class.host_os
end
class TarFile
def self.for(architecture:, host_os:)
new("qemu-system-#{architecture}-#{host_os}.tar")
end
def initialize(filename)
@filename = filename
end
attr_reader :filename
def paths
@paths ||= File.open(filename) do |io|
tar_files = []
Gem::Package::TarReader.new(io) do |tar|
tar_files = tar
.filter(&:file?)
.map(&:full_name)
.map { _1.delete_prefix("./") }
.sort
end
tar_files
end
end
def firmware_paths
@firmware_paths ||= paths.filter { _1.start_with?(firmware_directory) }
end
def qemu_binary
@qemu_binary ||= paths.filter { _1.start_with?("bin/qemu") }
end
def firmwares
@firmwares ||= firmware_paths.map { _1.delete_prefix(firmware_directory) }
end
def firmware_directory
"share/qemu/"
end
end
class MessageFormatter
extend Forwardable
def initialize(validator)
@validator = validator
end
def format
expected.concat([""], actual, [""], diff).join("\n")
end
private
def_delegators :@validator, :tar_file, :firmwares, :missing, :extra
def expected
[
"Expected '#{tar_file.filename}' to contain:",
binary_message,
firmware_message,
]
end
def actual
["Actual:"] + tar_file.paths
end
def diff
missing = to_full_path(self.missing)
extra = to_full_path(self.extra)
diff = to_full_path(firmwares)
.concat(to_full_path(tar_file.firmwares))
.uniq
.map { missing.include?(_1) ? "-#{_1}" : _1 }
.map { extra.include?(_1) ? "+#{_1}" : _1 }
["Diff:"] + diff
end
def binary_message
tar_file.qemu_binary
end
def firmware_message
to_full_path(firmwares).join("\n")
end
def to_full_path(array)
array.map { File.join(tar_file.firmware_directory, _1) }
end
end
end