-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·234 lines (190 loc) · 7.96 KB
/
test.py
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
#!/usr/bin/python3
# -*- coding:utf-8 -*-
import argparse
import os
import select
import subprocess
import sys
import string
import traceback
import random
import time
import hashlib
class QemuTerminate(Exception):
pass
class Qemu:
def __init__(self, command:str, history:str):
self.proc = subprocess.Popen("exec " + command, shell=True,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
self.output = ""
self.outbytes = bytearray()
self.history = history
with open(history, "w") as f:
f.truncate()
f.write("#!/bin/bash\n")
def _read(self) -> None:
rset, _, _ = select.select([self.proc.stdout.fileno()], [], [], 1)
if (rset == []):
self.proc.poll()
if (self.proc.returncode != None):
raise QemuTerminate
return
self.outbytes += os.read(self.proc.stdout.fileno(), 4096)
res = self.outbytes.decode("utf-8", "ignore")
self.output += res
self.outbytes = self.outbytes[len(res.encode("utf-8")):]
def runtil(self, string:str, timeout:int) -> None:
cursor = 0
cur = time.time()
while(True):
self._read()
if (string in self.output):
break
print(self.output[cursor:], end="", flush=True)
if (time.time() - cur > timeout):
raise TimeoutError
cursor = len(self.output)
idx = self.output.find(string) + len(string)
print(self.output[cursor:idx], end="", flush=True)
self.output = self.output[idx:]
def write(self, buf:str) -> None:
buf:bytes = buf.encode("utf-8")
self.proc.stdin.write(buf)
self.proc.stdin.flush()
def execute(self, command:str) -> None:
with open(self.history, "a") as f:
f.write(command + "\n")
self.runtil(":~#", timeout=60)
self.write(command + "\n")
def kill(self) -> None:
self.proc.kill()
self.proc.wait()
if __name__ == "__main__":
ret = 0
qemu:Qemu = None
parser = argparse.ArgumentParser(description="yaf test script")
parser.add_argument("--command", action="store",
type=str, required=True,
help="command to boot up qemu")
parser.add_argument("--history", action="store",
type=str, required=True,
help="path to store executed commands")
parser.add_argument("--timeout", action="store",
type=int, default=10,
help="max timeout for receiving from guest")
args = parser.parse_args()
try:
# boot up the Qemu
qemu = Qemu(command=args.command, history=args.history)
dirs = []
files = []
qemu.runtil("login:", timeout=args.timeout)
qemu.write("root\n")
# insmod the yaf module
qemu.execute("insmod /mnt/shares/yaf.ko")
# format the disk device
qemu.execute("/mnt/shares/mkfs /dev/vda")
qemu.execute("mkdir -p test")
# mount the device
qemu.execute("mount -t yaf /dev/vda test")
def check_directory():
qemu.execute("ls -al test")
# check entrys number
qemu.execute("ls -al test | wc -l")
qemu.runtil(str(len(dirs) + len(files) + 3), timeout=args.timeout)
# check '.'
qemu.execute('''ls -al test | grep " \.$" | wc -l''')
qemu.runtil("1", timeout=args.timeout)
# check '..'
qemu.execute('''ls -al test | grep " \.\.$"''')
qemu.runtil("1", timeout=args.timeout)
# check each entrys
for name in dirs + files:
qemu.execute('''ls -al test | grep " %s$" | wc -l'''%(name))
qemu.runtil("1", timeout=args.timeout)
# add random subdirectorys
for i in range(64 + random.randint(1, 32)):
name = "dir%d"%(i)
dirs.append(name)
qemu.execute("mkdir -p test/%s"%(name))
check_directory()
# add random files
for i in range(64 + random.randint(1, 32)):
name = "file%d"%(i)
files.append(name)
qemu.execute("touch test/%s"%(name))
check_directory()
# write random files
contents = {}
def check_files():
qemu.execute('''ls -al test/file*''')
for name in files:
if name in contents:
qemu.execute("cat test/%s | wc -c"%(name))
qemu.runtil(str(len(contents[name])), timeout=args.timeout)
qemu.execute("md5sum test/%s"%(name))
qemu.runtil(hashlib.md5(contents[name].encode("ascii")).hexdigest(), timeout=args.timeout)
qemu.execute("sha256sum test/%s"%(name))
qemu.runtil(hashlib.sha256(contents[name].encode("ascii")).hexdigest(), timeout=args.timeout)
qemu.execute("sha512sum test/%s"%(name))
qemu.runtil(hashlib.sha512(contents[name].encode("ascii")).hexdigest(), timeout=args.timeout)
else:
qemu.execute("cat test/%s | wc -c"%(name))
qemu.runtil(str(0), timeout=args.timeout)
max_filesize = 4096 * 8
step = 64
# write *wnumber* times making each file *max_filesize / 2* bytes
wnumber = int(max_filesize / 2 * len(files) / step)
for i in range(wnumber):
name = files[random.randint(0, len(files) - 1)]
content = ''.join(random.choice(string.digits) for _ in range(step))
if name in contents:
if (len(contents[name] + content) >= max_filesize):
continue
contents[name] += content
qemu.execute('''echo -n "%s" >> test/%s'''%(content, name))
else:
contents[name] = content
qemu.execute('''echo -n "%s" > test/%s'''%(content, name))
check_files()
for name, content in contents.items():
offset = random.randint(2, len(content))
qemu.execute("tail --bytes=+%d test/%s | md5sum -"%(offset, name))
qemu.runtil(hashlib.md5(content[offset-1:].encode("ascii")).hexdigest(), timeout=args.timeout)
qemu.execute("tail --bytes=+%d test/%s | sha256sum -"%(offset, name))
qemu.runtil(hashlib.sha256(content[offset-1:].encode("ascii")).hexdigest(), timeout=args.timeout)
qemu.execute("tail --bytes=+%d test/%s | sha512sum -"%(offset, name))
qemu.runtil(hashlib.sha512(content[offset-1:].encode("ascii")).hexdigest(), timeout=args.timeout)
# delete test
qemu.execute("rmdir test")
qemu.runtil("rmdir: failed to remove 'test': Device or resource busy", timeout=args.timeout)
# delete random entrys
for i in range(32 + random.randint(1, 16) + 32 + random.randint(1, 16)):
if ((random.randint(0, 1) == 0 and len(files)) or len(dirs) == 0):
# delete the random dir
qemu.execute("rmdir test/%s"%(dirs.pop(random.randint(0, len(dirs) - 1))))
else:
# delete the random file
name = random.randint(0, len(files) - 1)
if (name in contents):
contents.pop(name)
qemu.execute("rm test/%s"%(files.pop(name)))
# umount the device
qemu.execute("umount test")
# mount the device again
qemu.execute("mount -t yaf /dev/vda test")
check_directory()
check_files()
# umount the device
qemu.execute("umount test")
# remove the yaf module
qemu.execute("rmmod yaf")
qemu.runtil("cleanup filesystem", timeout=args.timeout)
except:
traceback.print_exc()
ret = -1
finally:
if (qemu != None):
qemu.kill()
sys.exit(ret)