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