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