6c2b552f62c0b94671230f437ff2f83b684d367e
[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
32 domain = subprocess.Popen([wbinfo, "--own-domain"],
33                           stdout=subprocess.PIPE).communicate()[0].strip()
34 domsid = subprocess.Popen([wbinfo, "-n", domain + "/"],
35                           stdout=subprocess.PIPE).communicate()[0]
36 domsid = domsid.split(' ')[0]
37
38 # print domain
39 # print domsid
40
41 sids = [domsid + '-512', 'S-1-5-32-545', domsid + '-513', 'S-1-1-0', 'S-1-3-1', 'S-1-5-1']
42
43 flush_cache(sids=sids)
44
45 sids2xids = subprocess.Popen([wbinfo, '--sids-to-unix-ids=' + ','.join(sids)],
46                              stdout=subprocess.PIPE).communicate()[0].strip()
47
48 gids = []
49 uids = []
50 idtypes = []
51
52 for line in sids2xids.split('\n'):
53     result = line.split(' ')[2:]
54     idtypes.append(result[0])
55
56     gid = None
57     uid = None
58     if result[0] == 'gid':
59         gid = result[1]
60     elif result[0] == 'uid':
61         uid = result[1]
62     elif result[0] == 'uid/gid':
63         gid = result[1]
64         uid = result[1]
65
66     if gid == '-1':
67         gid = ''
68     gids.append(gid)
69
70     if uid == '-1':
71         uid = ''
72     uids.append(uid)
73
74 # Check the list produced by the sids-to-xids call with the
75 # singular variant (sid-to-xid) for each sid in turn.
76
77
78 def check_singular(sids, ids, idtype='gid'):
79     i = 0
80     for sid in sids:
81         if ids[i] is None:
82             continue
83
84         outid = subprocess.Popen([wbinfo, '--sid-to-%s' % idtype, sid],
85                                  stdout=subprocess.PIPE).communicate()[0].strip()
86         if outid != ids[i]:
87             print("Expected %s, got %s\n" % (outid, ids[i]))
88             flush_cache(sids=sids, uids=uids, gids=gids)
89             sys.exit(1)
90         i += 1
91
92 # Check the list produced by the sids-to-xids call with the
93 # multiple variant (sid-to-xid) for each sid in turn.
94
95
96 def check_multiple(sids, idtypes):
97     sids2xids = subprocess.Popen([wbinfo, '--sids-to-unix-ids=' + ','.join(sids)],
98                                  stdout=subprocess.PIPE).communicate()[0].strip()
99     # print sids2xids
100     i = 0
101     for line in sids2xids.split('\n'):
102         result = line.split(' ')[2:]
103
104         if result[0] != idtypes[i]:
105             print("Expected %s, got %s\n" % (idtypes[i], result[0]))
106             flush_cache(sids=sids, uids=uids, gids=gids)
107             sys.exit(1)
108         i += 1
109
110
111 # first round: with filled cache via sid-to-id
112 check_singular(sids, gids, 'gid')
113 check_singular(sids, uids, 'uid')
114
115 # second round: with empty cache
116 flush_cache(sids=sids, gids=gids)
117 check_singular(sids, gids, 'gid')
118 flush_cache(sids=sids, uids=uids)
119 check_singular(sids, uids, 'uid')
120
121 # third round: with filled cache via uid-to-sid
122 flush_cache(sids=uids, uids=uids)
123 fill_cache(uids, 'uid')
124 check_multiple(sids, idtypes)
125
126 # fourth round: with filled cache via gid-to-sid
127 flush_cache(sids=sids, gids=gids)
128 fill_cache(gids, 'gid')
129 check_multiple(sids, idtypes)
130
131 # flush the cache so any incorrect mappings don't break other tests
132 flush_cache(sids=sids, uids=uids, gids=gids)
133
134 sys.exit(0)