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