1from k5test import *
2from filecmp import cmp
3
4def dump_compare(realm, opt, srcfile):
5    mark('dump comparison against %s' % os.path.basename(srcfile))
6    realm.run([kdb5_util, 'dump'] + opt + [dumpfile])
7    if not cmp(srcfile, dumpfile, False):
8        fail('Dump output does not match %s' % srcfile)
9
10
11def load_dump_check_compare(realm, opt, srcfile):
12    mark('load check from %s' % os.path.basename(srcfile))
13    realm.run([kdb5_util, 'destroy', '-f'])
14    realm.run([kdb5_util, 'load'] + opt + [srcfile])
15    realm.run([kadminl, 'getprincs'], expected_msg='user@')
16    realm.run([kadminl, 'getprinc', 'nokeys'],
17              expected_msg='Number of keys: 0')
18    realm.run([kadminl, 'getpols'], expected_msg='testpol')
19    dump_compare(realm, opt, srcfile)
20
21
22for realm in multidb_realms(start_kdc=False):
23
24    # Make sure we can dump and load an ordinary database, and that
25    # principals and policies survive a dump/load cycle.
26
27    realm.run([kadminl, 'addpol', 'fred'])
28
29    # Create a dump file.
30    dumpfile = os.path.join(realm.testdir, 'dump')
31    realm.run([kdb5_util, 'dump', dumpfile])
32
33    # Write additional policy records to the dump.  Use the 1.8 format for
34    # one of them, to test retroactive compatibility (for issue #8213).
35    f = open('testdir/dump', 'a')
36    f.write('policy\tcompat\t0\t0\t3\t4\t5\t0\t0\t0\t0\n')
37    f.write('policy\tbarney\t0\t0\t1\t1\t1\t0\t0\t0\t0\t0\t0\t0\t-\t1\t2\t28\t'
38            'fd100f5064625f6372656174696f6e404b5242544553542e434f4d00\n')
39    f.close()
40
41    # Destroy and load the database; check that the policies exist.
42    # Spot-check principal and policy fields.
43    mark('reload after dump')
44    realm.run([kdb5_util, 'destroy', '-f'])
45    realm.run([kdb5_util, 'load', dumpfile])
46    out = realm.run([kadminl, 'getprincs'])
47    if realm.user_princ not in out or realm.host_princ not in out:
48        fail('Missing principal after load')
49    out = realm.run([kadminl, 'getprinc', realm.user_princ])
50    if 'Expiration date: [never]' not in out or 'MKey: vno 1' not in out:
51        fail('Principal has wrong value after load')
52    out = realm.run([kadminl, 'getpols'])
53    if 'fred\n' not in out or 'barney\n' not in out:
54        fail('Missing policy after load')
55    realm.run([kadminl, 'getpol', 'compat'],
56              expected_msg='Number of old keys kept: 5')
57    realm.run([kadminl, 'getpol', 'barney'],
58              expected_msg='Number of old keys kept: 1')
59
60    # Dump/load again, and make sure everything is still there.
61    mark('second reload')
62    realm.run([kdb5_util, 'dump', dumpfile])
63    realm.run([kdb5_util, 'load', dumpfile])
64    out = realm.run([kadminl, 'getprincs'])
65    if realm.user_princ not in out or realm.host_princ not in out:
66        fail('Missing principal after load')
67    out = realm.run([kadminl, 'getpols'])
68    if 'compat\n' not in out or 'fred\n' not in out or 'barney\n' not in out:
69        fail('Missing policy after second load')
70
71    srcdumpdir = os.path.join(srctop, 'tests', 'dumpfiles')
72    srcdump = os.path.join(srcdumpdir, 'dump')
73    srcdump_r18 = os.path.join(srcdumpdir, 'dump.r18')
74    srcdump_r13 = os.path.join(srcdumpdir, 'dump.r13')
75    srcdump_b7 = os.path.join(srcdumpdir, 'dump.b7')
76
77    # Load a dump file from the source directory.
78    realm.run([kdb5_util, 'destroy', '-f'])
79    realm.run([kdb5_util, 'load', srcdump])
80    realm.run([kdb5_util, 'stash', '-P', 'master'])
81
82    # Dump the resulting DB in each non-iprop format and compare with
83    # expected outputs.
84    dump_compare(realm, [], srcdump)
85    dump_compare(realm, ['-r18'], srcdump_r18)
86    dump_compare(realm, ['-r13'], srcdump_r13)
87    dump_compare(realm, ['-b7'], srcdump_b7)
88
89    # Load each format of dump, check it, re-dump it, and compare.
90    load_dump_check_compare(realm, ['-r18'], srcdump_r18)
91    load_dump_check_compare(realm, ['-r13'], srcdump_r13)
92    load_dump_check_compare(realm, ['-b7'], srcdump_b7)
93
94success('Dump/load tests')
95