1#!/usr/bin/env python2.7 -B
2
3import ps
4
5
6def test_pids():
7  text = """USER      PID   PPID  VSIZE  RSS   WCHAN              PC  NAME
8root      1     0     10632  776   SyS_epoll_ 0000000000 S /init
9root      2     0     0      0       kthreadd 0000000000 S kthreadd
10u0_a22    7308  633   1808572 79760 SyS_epoll_ 0000000000 S com.google.android.dialer
11u0_a19    7370  633   1841228 37828 SyS_epoll_ 0000000000 S com.google.android.gms.feedback
12u0_a136   7846  634   1320656 119964 SyS_epoll_ 0000000000 S com.sonos.acr
13"""
14
15  actual = ps.ParsePs(text)
16
17  expected = [
18      ('root', '1', '0', '/init'),
19      ('root', '2', '0', 'kthreadd'),
20      ('u0_a22', '7308', '633', 'com.google.android.dialer'),
21      ('u0_a19', '7370', '633', 'com.google.android.gms.feedback'),
22      ('u0_a136', '7846', '634', 'com.sonos.acr')
23    ]
24
25  if actual != expected:
26    print "Expected:"
27    print expected
28    print
29    print "Actual:"
30    print actual
31    raise Exception("test failed")
32
33
34def test_uids():
35  text = """vers,1
36vrfy,com.android.vending,10035
37ifv,com.google.android.gms,10019
38lib,com.vzw.apnlib,jar,/system/app/VZWAPNLib/VZWAPNLib.apk
39lib,com.google.android.media.effects,jar,/system/framework/com.google.android.media.effects.jar
40pkg,com.amazon.mShop.android.shopping,10118,116434610,1486361139496,1491403362196,com.android.vending
41pkg-splt,base,0
42pkg-usr,0,IbsusL,0,com.android.vending
43pkg,com.getgoodcode.bart,10129,21,1486361637815,1486361637815,com.android.vending
44pkg-splt,base,0
45pkg-usr,0,IbsuSl,0,?
46pkg,com.flightaware.android.liveFlightTracker,10115,138,1486361042695,1486361042695,com.android.vending
47pkg-splt,base,0
48pkg-usr,0,IbsuSl,0,?
49pkg,com.android.cts.priv.ctsshim,10010,24,1230796800000,1230796800000,?
50pkg-splt,base,0
51pkg-usr,0,IbsusL,0,?
52"""
53  actual = ps.ParseUids(text)
54
55  expected = [
56    ('10118', 'com.amazon.mShop.android.shopping'),
57    ('10129', 'com.getgoodcode.bart'),
58    ('10115', 'com.flightaware.android.liveFlightTracker'),
59    ('10010', 'com.android.cts.priv.ctsshim')
60  ]
61
62  if actual != expected:
63    print "Expected:"
64    print expected
65    print
66    print "Actual:"
67    print actual
68    raise Exception("test failed")
69
70
71def test_update():
72  """Requires an attached device."""
73  processes = ps.ProcessSet()
74  processes.Update()
75  processes.Update()
76  processes.Print()
77  process = processes.FindPid("0", "0")
78  print "process:", process
79  print "uid:", process.uid.uid
80  print "username:", process.uid.name
81  print "pid:", process.pid
82  print "ppid:", process.ppid
83  print "name:", process.name
84  print "displayName:", process.DisplayName()
85
86
87def main():
88  #test_uids()
89  #test_pids()
90  test_update()
91
92
93if __name__ == "__main__":
94    main()
95
96
97# vim: set ts=2 sw=2 sts=2 tw=100 nocindent autoindent smartindent expandtab:
98