]> git.cameronkatri.com Git - apple_cmds.git/blob - system_cmds/zprint.tproj/test_zprint.lua
Merge branch 'apple'
[apple_cmds.git] / system_cmds / zprint.tproj / test_zprint.lua
1 #!/usr/local/bin/recon
2
3 local darwin = require 'darwin'
4 local proc = require 'proc'
5 local zprint = require 'zprint'
6
7 if darwin.geteuid() ~= 0 then
8 io.stderr:write(arg[0], ': must be run as root (under sudo)\n')
9 os.exit(1)
10 end
11
12 local function test_output(zpout)
13 -- These should be present in the output of zprint.
14 local expectations = {
15 {
16 region = 'zones',
17 name = 'vm.pages',
18 }, {
19 region = 'tags',
20 name = 'VM_KERN_MEMORY_DIAG',
21 }, {
22 region = 'maps',
23 name = 'VM_KERN_COUNT_WIRED_STATIC_KERNELCACHE',
24 }, {
25 region = 'zone_views',
26 -- This ties the kernel's hands when it comes to naming.
27 name = 'data.kalloc.16[raw]',
28 }
29 }
30
31 local found_all = true
32 for i = 1, #expectations do
33 local region = expectations[i].region
34 local name = expectations[i].name
35 local iter = zprint[region]
36 if not iter then
37 io.stderr:write('zprint library has no iterator for ', region, '\n')
38 os.exit(4)
39 end
40
41 local found = false
42 for elt in zprint[region](zpout) do
43 if elt.name == name then
44 found = true
45 break
46 end
47 end
48 if found then
49 io.stdout:write('PASS: found ', name, ' in ', region, '\n')
50 else
51 io.stdout:write('FAIL: could not find ', name, ' in ', region, '\n')
52 found_all = false
53 end
54 end
55 return found_all
56 end
57
58 local function run_zprint(args)
59 if not args then
60 args = {}
61 end
62
63 table.insert(args, 1, 'zprint')
64 local zpout, err, status, code = proc.run(args)
65 if not zpout then
66 io.stderr:write(arg[0], ': failed to run zprint: ', err, '\n')
67 os.exit(2)
68 end
69
70 if code ~= 0 then
71 io.stderr:write(arg[0], ': zprint ', status, 'ed with code ', tostring(code),
72 ', stderr = ', err, '\n')
73 os.exit(3)
74 end
75 return zpout
76 end
77
78 local function run_and_test(...)
79 local zpout = run_zprint(table.pack(...))
80 local passed = test_output(zpout)
81 if not passed then
82 os.exit(5)
83 end
84 end
85
86 print("TEST: zprint output")
87 run_and_test()
88 print("\nTEST: zprint -t output")
89 run_and_test("-t")