~kvik/lu9-p9

ref: 9ddcbf1be2343ebb4d7853fecf8528e862def846 lu9-p9/test/test.lua -rwxr-xr-x 6.2 KiB
9ddcbf1bkvik extra/lfs: add lfs._VERSION 1 year, 1 month ago
                                                                                
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/bin/lu9

local p9 = require "p9"
local dump = (function()
	local ok, inspect = pcall(require, "inspect")
	if ok then return function(v) print(inspect(v)) end end
	return print
end)()

local function tmp()
	return string.format("/tmp/lua.%x", math.random(1e10))
end

local function rc()
	os.execute("prompt='p9; ' rc -i")
end

local leak = false

p9.rfork("env name")
os.execute("ramfs")



-- File I/O
do
	local s = string.rep("ABCD", 2048*2) -- 16k > standard 8k buffer
	local f = tmp()
	local fd = p9.create(f, "w")
	fd:write(s)
	fd:close()
	
	fd = p9.open(f)
	assert(fd:slurp() == s)
	fd:close()
	
	fd = p9.open(f, "r")
	assert(fd:slurp(2048) == string.rep("ABCD", 512))
	fd:close()
	
	fd = p9.open(f, "r")
	assert(fd:slurp(16*1024 + 999) == s)
	fd:close()
	
	fd = assert(p9.open(f, "r"))
	assert(fd:seek(0, "end") == 16*1024)
	assert(fd:seek(8192, "set") == 8192
		and fd:slurp() == string.rep("ABCD", 2*1024))
	assert(fd:seek(0, "set"))
	assert(fd:seek(16*1024 - 4, "set") == 16*1024 - 4)
	assert(fd:slurp() == "ABCD")
	fd:close()
end

-- File objects
-- TODO: closing and collecting.

-- file:path()
do
	local f = p9.create("/tmp/fd2path")
	assert(f:path() == "/tmp/fd2path")
	f:close()
end

-- file:iounit()
do
	local f = assert(p9.open("/srv/slashmnt"))
	assert(f:iounit() ~= 0)
	f:close()
end

-- file:dup() and file:set()
do
	local a, b = p9.pipe()
	local c = assert(a:dup())
	a:write("hello")
	assert(b:read() == "hello")
	c:write("world")
	assert(b:read() == "world")
	a:close() b:close() c:close()

	a = assert(p9.open("/lib/glass"))
	b = assert(p9.open("/lib/bullshit"))
	local buf = a:read()
	assert(b:set(a))
	b:seek(0)
	assert(b:read() == buf)
end

-- access
do
	assert(p9.access("/dev/null"))
	assert(p9.access("/dev/null", "read write"))
	assert(p9.access("/bin/rc", "exec"))
end
	
-- pipe
do
	local p₀, p₁ = assert(p9.pipe())
	p₀:write("ABCD")
	assert(p₁:read() == "ABCD")
	p₀:close(); p₁:close()
end

-- wstat
do
	local name = tmp()
	assert(p9.create(name, nil, "644")):close()
	assert(p9.wstat(name, {name = "notyourbusiness", mode = "append 777"}))
	local st = assert(p9.stat("/tmp/notyourbusiness"))
	assert(st.mode.file and st.mode.append and st.mode.perm == "rwxrwxrwx")
end

-- Filesystem
do
	-- Create a test tree
	local function File(data) return {
		type = "file", perm = "644", data = data
	} end
	local function Dir(children) return {
		type = "dir", perm = "dir 755", children = children
	} end
	local function mkfs(path, d)
		assert(p9.create(path, nil, d.perm))
		for name, c in pairs(d.children) do
			local new = path .. "/" .. name
			if c.type == "dir" then
				mkfs(new, c)
			else
				local f <close> = assert(p9.create(new, "w", c.perm))
				f:write(c.data)
			end
		end
	end
	local fs = Dir {
		a = File "a",
		b = Dir {},
		c = Dir {
			ca = File "ca",
			cb = Dir {
				cba = File "cba",
			},
			cc = File "cc",
		},
		d = File "d",
	}
	local function walk(fs)
		dump(fs.perm)
		for _, e in pairs(fs.children) do
			if e.type == "dir" then
				walk(e)
			end
		end
	end
	local ok, err = pcall(mkfs, "/tmp/fs", fs)
	if not ok then print(err) end
	
	-- Stat a file
	assert(p9.stat("/tmp/fs/a").mode.file)
	
	-- Walking
	-- Walking a file (or any other error) must report an error
	local e = {}
	for w in p9.walk("/tmp/fs/a", e) do
		assert(false)
	end
	assert(e.error == "walk in a non-directory")
	-- Without error object an error must be thrown
	assert(false == pcall(function()
		for w in p9.walk("tmp/fs/a") do end
	end))
	-- Same should happen if the iterator function fails inside
	-- the loop because of dirread(2) failure, but this kind of
	-- failure is hard to simulate.
	
	-- Walking a directory
	local function compare(path, fs)
		assert(fs.type == "dir")
		for f in p9.walk(path) do
			local new = path .. "/" .. f.name
			if f.mode.dir then
				if compare(new, fs.children[f.name]) == false then
					return false
				end
			else
				if fs.children[f.name] == nil then
					error("file does not exist in proto")
				end
			end
		end
		return true
	end
	assert(compare("/tmp/fs", fs) == true)
end



-- Namespaces
-- bind and unmount work
do
	local f
	assert(p9.bind("#|", "/n/pipe"))
	f = assert(p9.open("/n/pipe/data"))
	assert(p9.unmount("/n/pipe"))
	assert(p9.open("/n/pipe/data") == nil)
end
-- mount works
do
	assert(p9.mount("/srv/cwfs", "/n/test"))
	assert(p9.open("/n/test/lib/glass"))
end

-- Process control
-- No idea how to test some of this
do
	assert(p9.sleep(0) == true)
end

-- wdir
do
	local cwd = assert(p9.wdir())
	assert(p9.wdir("/dev") and p9.wdir() == "/dev")
	assert(p9.wdir(cwd))
end

-- proc info
do
	assert(p9.user() and p9.sysname())
	assert(p9.pid() and p9.ppid())
end

-- Fork & Exec
do
	local us, them = p9.pipe()
	if p9.rfork("proc nowait fd") == 0 then
		us:close()
		p9.fd[0]:set(them)
		p9.fd[1]:set(them)
		p9.exec("cat")
	else
		them:close()
		us:write("HELLO CAT")
		us:write("")
		assert(us:slurp() == "HELLO CAT")
		us:close()
	end
	
	local ok = p9.exec("/dev/mordor")
	assert(not ok)
	
	-- Wait
	local N = 9
	for i = 1, N do
		if p9.rfork("proc") == 0 then
			p9.exec("sleep", "0")
		end
	end
	for i = 1, N do
		local ok, w = p9.wait()
	end
	local ok = p9.wait()
	assert(not ok)
	-- Wait (status)
	if p9.rfork("proc") == 0 then
		p9.sleep(0)
		p9.exits("i failed you")
	end
	local ok, w = p9.wait()
	assert(not ok and w.status:match("i failed you"))
end




-- Environment variables
do
	local e
	
	assert(p9.env["sure-is-empty"] == nil)
	-- Set and get a string variable
	p9.env.test = "ABC"; assert(p9.env.test == "ABC")
	-- Delete a variable
	p9.env.test = nil; assert(p9.env.test == nil)
	
	-- Set and get a list variable
	p9.env.test = {"a", "b", "c"}
	e = p9.env.test
	assert(type(e) == "table"
		and #e == 3 and e[1] == "a" and e[2] == "b" and e[3] == "c")
	-- Ensure it's understood by rc
	os.execute("echo -n $#test $test >/env/res")
	assert(p9.env.res == "3 a b c")
	-- Ensure we understand rc
	os.execute("test=(d e f)")
	e = p9.env.test
	assert(type(e) == "table"
		and #e == 3 and e[1] == "d" and e[2] == "e" and e[3] == "f")
	p9.env.test = nil
end




-- Misc.

-- cleanname
do
	assert(p9.cleanname("/usr///./glenda/.") == "/usr/glenda")
end




-- Check for leaks (might not come from us)
if leak then
	if p9.rfork("proc") == 0 then
		p9.exec("leak", p9.ppid())
	end
	p9.wait()
end