File: lptytest.lua

package info (click to toggle)
lua-lpty 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 168 kB
  • sloc: ansic: 546; makefile: 34
file content (418 lines) | stat: -rw-r--r-- 9,191 bytes parent folder | download | duplicates (4)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/env lua

--[[ lptytest.lua
 -
 - test aspects of lpty
 -
 - Gunnar Zötl <gz@tset.de>, 2010, 2011
 - Released under MIT/X11 license. See file LICENSE for details.
--]]

lpty = require "lpty"

p = lpty.new()

ntests = 0
nfailed = 0

-- announce which test we are performing and what it tests
function announce(str)
	str = str or ""
	ntests = ntests + 1
	print("Test " .. tostring(ntests) .. ": " .. str)
end

-- print that the test did not work out and optionally why
function fail(str)
	local msg = "  TEST FAILED"
	nfailed = nfailed + 1
	if str ~= nil then msg = msg .. ": " .. str end
	print(msg)
end

-- just prints wether the test went ok or not. An optional reason for failure may be passed,
-- which will then be printed.
function check(ok, msg)
	if ok==false then
		fail(msg)
	else
		print "  TEST OK"
	end
end

-- wait for a fraction of a second
function waitabit(n)
	n = n or 1
	t0 = os.clock()
	t1 = t0
	while t1 - t0 < 0.1 * n do
		t1 = os.clock()
	end
end

-- 1
announce("starting test client")
ok, val = pcall(lpty.startproc, p, "lua", "testclient.lua")
if ok then
	check(val)
else
	fail(tostring(val))
end

-- 2
announce("checking whether pty has process, should return true")
ok, val = pcall(lpty.hasproc, p)
if ok then
	check(val)
else
	fail(tostring(val))
end

-- 3
announce("checking whether we can read from the pty, should return false")
ok, val = pcall(lpty.readok, p)
if ok then
	check(val == false)
else
	fail(tostring(val))
end

-- 4
announce("reading from pty with a timeout of 1 second, should return nil")
ok, val = pcall(lpty.read, p, 1)
if ok then
	check(val == nil)
else
	fail(tostring(val))
end

-- 5
announce("checking whether we can write to the pty, should return true")
ok, val = pcall(lpty.sendok, p)
if ok then
	check(val)
else
	fail(tostring(val))
end

-- 6
announce("writing data 'abcba\\n' to the pty, should return length of data -> 6")
ok, val = pcall(lpty.send, p, "abcba\n")
if ok then
	check(val == 6)
else
	fail(tostring(val))
end

-- allow the client to react
waitabit()

-- 7
announce("checking whether we can read from the pty, should return true")
ok, val = pcall(lpty.readok, p)
if ok then
	check(val)
else
	fail(tostring(val))
end

-- 8
announce("reading from pty, should return 'abcba\\n+abcba+\\n'")
ok, val = pcall(lpty.read, p, 1)
if ok then
	val = string.gsub(val, "[\r\n]+", '.') -- normalize line endings
	check(val == "abcba.+abcba+.")
else
	fail(tostring(val))
end

-- 9
announce("terminating child process")
ok = pcall(lpty.endproc, p)
check(ok)

-- allow client to terminate
waitabit()

-- 10
announce("Checking whether pty has child process, should return false")
ok, val = pcall(lpty.hasproc, p)
if ok then
	check(val == false)
else
	fail(tostring(val))
end

-- 11
announce("checking whether we can read from pty, should return false")
ok, val = pcall(lpty.readok, p)
if ok then
	check(val == false)
else
	fail(tostring(val))
end

-- 12
announce("reading from pty with a timeout of 1 second, should return nil")
ok, val = pcall(lpty.read, p, 1)
if ok then
	check(val == nil)
else
	fail(tostring(val))
end

-- test timeout length. In order for this to work there may be no pending data in the pty to read.
function testto(to, tm)
	local t0 = os.time()
	local i
	for i=1,10 do
		p:read(to)
	end
	local t = os.time() - t0
	-- allow for some deviation
	return (tm - 1 < t and t < tm + 1)
end

-- 13
announce("testing timeout 0.5 second by running r:read(0.5) 10 times should take about 5 seconds")
check(testto(0.5, 5))

-- 14
announce("testing timeout 1.5 second by running r:read(1.5) 10 times should take about 15 seconds")
check(testto(1.5, 15))

-- creating pty with no local echo for remaining tests
pn = lpty.new { no_local_echo = true }

-- 15
announce("checking for data from no_local_echo pty, should return false")
ok, val = pcall(lpty.readok, pn)
if ok then
	check(val == false)
else
	fail(tostring(val))
end

-- 16
announce("sending data to no_local_echo pty then checking for data, should return false")
ok, val = pcall(lpty.send, pn, "abc\n")
while pn:readok() do pn:read() end
if not ok then
	fail(tostring(val))
else
	ok, val = pcall(lpty.readok, pn)
	if ok then
		check(val == false)
	else
		fail(tostring(val))
	end
end

-- 17
announce("starting test client for no_local_echo pty")
ok, val = pcall(lpty.startproc, pn, "lua", "testclient.lua")
if ok then
	check(val)
else
	fail(tostring(val))
end

-- 18
announce("reading from no_local_echo pty, should now return '+abc+\\n'")
ok, val = pcall(lpty.read, pn, 0.5)
if ok then
	val = string.gsub(val, "[\r\n]+", '.') -- normalize line endings
	check(val == "+abc+.")
else
	fail(tostring(val))
end

-- 19
announce("sending 'xxx\\n' to pty, reading back, should return '+xxx+\\n'")
ok, val = pcall(lpty.send, pn, "xxx\n")
if not ok then
	fail(tostring(val))
else
	ok, val = pcall(lpty.read, pn, 0.5)
	if ok then
		val = string.gsub(val, "[\r\n]+", '.') -- normalize line endings
		check(val == "+xxx+.")
	else
		fail(tostring(val))
	end
end

-- 20
announce("testing exit status for current child, should return (false, nil)")
ok, val, code = pcall(lpty.exitstatus, pn)
if not ok then
	fail(tostring(val))
else
	check((val == false) and (code == nil))
end

-- 21
announce("quitting child then testing exit status, should return ('exit', 0)")
ok, val = pcall(lpty.send, pn, "quit\n")
if not ok then
	fail(tostring(val))
else
	while pn:hasproc() do end
	ok, val, code = pcall(lpty.exitstatus, pn)
	if not ok then
		fail(tostring(val))
	else
		check((val == 'exit') and (code == 0))
	end
end

-- 22
announce("starting child with invalid executable, then testing exit status, should return ('exit', 1)")
ok, val = pcall(lpty.startproc, pn, "./firsebrumf")
if not ok then
	fail(tostring(val))
else
	while pn:hasproc() do end
	ok, val, code = pcall(lpty.exitstatus, pn)
	if not ok then
		fail(tostring(val))
	else
		check((val == 'exit') and (code == 1))
	end
end

-- 23
announce("starting child process, then killing it, then testing exit status, should return ('sig', *) with *>0")
ok, val = pcall(lpty.startproc, pn, "lua")
term = 0
if not ok then
	fail(tostring(val))
else
	while not pn:hasproc() do end
	ok, val = pcall(lpty.endproc, pn)
	if not ok then
		fail(tostring(val))
	else
		while pn:hasproc() do end
		ok, val, code = pcall(lpty.exitstatus, pn)
		if not ok then
			fail(tostring(val))
		else
			term = code
			check((val == 'sig') and (code > 0))
		end
	end
end

-- 24
announce("starting child process, then killing it with kill=true, then testing exit status, should return ('sig', *) with *>0 and also not equal to * from prev. test")
ok, val = pcall(lpty.startproc, pn, "lua")
if not ok then
	fail(tostring(val))
else
	while not pn:hasproc() do end
	ok, val = pcall(lpty.endproc, pn, true)
	if not ok then
		fail(tostring(val))
	else
		while pn:hasproc() do end
		ok, val, code = pcall(lpty.exitstatus, pn)
		if not ok then
			fail(tostring(val))
		else
			check((val == 'sig') and (code > 0) and (code ~= term))
		end
	end
end

-- cleanup
pn:flush()

-- 25
announce("reading environment from pty, should return a table with stuff in it")
ok, env = pcall(lpty.getenviron, pn)
envsiz = 0
if not ok then
	fail(tostring(val))
else
	ok = true
	for k, v in pairs(env) do
		envsiz = envsiz + 1
		ok = ok and (type(k) == 'string') and (type(v) == 'string')
	end
	check(ok)
end

-- 26
announce("calling /usr/bin/env with an empty environment, then reading output, should return nothing at all")
ok = pcall(lpty.setenviron, pn, {})
if not ok then
	fail(tostring(val))
else
	ok = pcall(lpty.startproc, pn, '/usr/bin/env')
	if not ok then
		fail(tostring(val))
	else
		while pn:hasproc() do end
		ok, val = pcall(lpty.read, pn, 1)
		if not ok then
			fail(tostring(val))
		else
			check(val == nil)
		end
	end
end

-- 27
announce("calling /usr/bin/env with {a=1} as its environment, then reading output, should return 'a=1\\n'")
ok = pcall(lpty.setenviron, pn, {a=1})
if not ok then
	fail(tostring(val))
else
	ok = pcall(lpty.startproc, pn, '/usr/bin/env')
	if not ok then
		fail(tostring(val))
	else
		while pn:hasproc() do end
		ok, val = pcall(lpty.read, pn, 1)
		if not ok then
			fail(tostring(val))
		else
			val = string.gsub(tostring(val), "[\r\n]+", '.') -- normalize line endings
			check(val == "a=1.")
		end
	end
end

-- 28
announce("resetting then reading environment from pty, should return a table with stuff in it, size as before we messed with it")
ok = pcall(lpty.setenviron, pn, nil)
ok, env = pcall(lpty.getenviron, pn)
mysiz = 0
if not ok then
	fail(tostring(val))
else
	ok = true
	for k, v in pairs(env) do
		mysiz = mysiz + 1
		ok = ok and (type(k) == 'string') and (type(v) == 'string')
	end
	check(ok and (mysiz == envsiz))
end

-- 29
announce("calling /usr/bin/env on standard environment, should return as many lines as there are entries in the environment (as counted before)")
ok = pcall(lpty.startproc, pn, '/usr/bin/env')
if not ok then
	fail(tostring(val))
else
	while pn:hasproc() do end
	val = ""
	while pn:readok() do val = val .. pn:read() end
	cnt = 0
	string.gsub(val, ".-\n", function () cnt = cnt + 1 end)
	check(cnt == envsiz)
end

-- all done
print("Tests " .. tostring(ntests) .. " failed " .. tostring(nfailed))