wbinfo: create a more comprehensive test for sids2xids
[nivanova/samba-autobuild/.git] / source3 / script / tests / test_wbinfo_sids2xids_int.py
1 #!/usr/bin/env python
2
3 import sys,os,subprocess
4
5
6 if len(sys.argv) != 3:
7     print "Usage: test_wbinfo_sids2xids_int.py wbinfo net"
8     sys.exit(1)
9
10 wbinfo = sys.argv[1]
11 netcmd = sys.argv[2]
12
13 def flush_cache():
14     os.system(netcmd + " cache flush")
15
16 def fill_cache(inids, idtype='gid'):
17     for inid in inids:
18         if inid is None:
19             continue
20         subprocess.Popen([wbinfo, '--%s-to-sid=%s' % (idtype, inid)],
21                          stdout=subprocess.PIPE).communicate()
22
23 domain = subprocess.Popen([wbinfo, "--own-domain"],
24                           stdout=subprocess.PIPE).communicate()[0].strip()
25 domsid = subprocess.Popen([wbinfo, "-n", domain + "/"],
26                           stdout=subprocess.PIPE).communicate()[0]
27 domsid = domsid.split(' ')[0]
28
29 #print domain
30 #print domsid
31
32 sids=[ domsid + '-512', 'S-1-5-32-545', domsid + '-513' ]
33
34 flush_cache()
35
36 sids2xids = subprocess.Popen([wbinfo, '--sids-to-unix-ids=' +  ','.join(sids)],
37                              stdout=subprocess.PIPE).communicate()[0].strip()
38
39 gids=[]
40 uids=[]
41 idtypes = []
42
43 for line in sids2xids.split('\n'):
44     result = line.split(' ')[2:]
45     idtypes.append(result[0])
46
47     gid = None
48     uid = None
49     if result[0] == 'gid':
50         gid = result[1]
51     elif result[0] == 'uid':
52         uid = result[1]
53     elif result[0] == 'uid/gid':
54         gid = result[1]
55         uid = result[1]
56
57     if gid == '-1':
58         gid = ''
59     gids.append(gid)
60
61     if uid == '-1':
62         uid = ''
63     uids.append(uid)
64
65 # Check the list produced by the sids-to-xids call with the
66 # singular variant (sid-to-xid) for each sid in turn.
67 def check_singular(sids, ids, idtype='gid'):
68     i = 0
69     for sid in sids:
70         if ids[i] is None:
71             continue
72
73         outid = subprocess.Popen([wbinfo, '--sid-to-%s' % idtype, sid],
74                                  stdout=subprocess.PIPE).communicate()[0].strip()
75         if outid != ids[i]:
76             print "Expected %s, got %s\n" % (outid, ids[i])
77             flush_cache()
78             sys.exit(1)
79         i += 1
80
81 # Check the list produced by the sids-to-xids call with the
82 # multiple variant (sid-to-xid) for each sid in turn.
83 def check_multiple(sids, idtypes):
84     sids2xids = subprocess.Popen([wbinfo, '--sids-to-unix-ids=' +  ','.join(sids)],
85                                  stdout=subprocess.PIPE).communicate()[0].strip()
86     # print sids2xids
87     i = 0
88     for line in sids2xids.split('\n'):
89         result = line.split(' ')[2:]
90
91         if result[0] != idtypes[i]:
92             print "Expected %s, got %s\n" % (idtypes[i], result[0])
93             flush_cache()
94             sys.exit(1)
95         i += 1
96
97 # first round: with filled cache via sid-to-id
98 check_singular(sids, gids, 'gid')
99 check_singular(sids, uids, 'uid')
100
101 # second round: with empty cache
102 flush_cache()
103 check_singular(sids, gids, 'gid')
104 flush_cache()
105 check_singular(sids, uids, 'uid')
106
107 # third round: with filled cache via uid-to-sid
108 flush_cache()
109 fill_cache(uids, 'uid')
110 check_multiple(sids, idtypes)
111
112 # fourth round: with filled cache via gid-to-sid
113 flush_cache()
114 fill_cache(gids, 'gid')
115 check_multiple(sids, idtypes)
116
117 # flush the cache so any incorrect mappings don't break other tests
118 flush_cache()
119
120 sys.exit(0)