Imported Upstream version 1.15.1
[platform/upstream/krb5.git] / src / tests / t_hostrealm.py
1 #!/usr/bin/python
2 from k5test import *
3
4 plugin = os.path.join(buildtop, "plugins", "hostrealm", "test",
5                       "hostrealm_test.so")
6
7 # Disable the "dns" module (we can't easily test TXT lookups) and
8 # arrange the remaining modules in an order which makes sense for most
9 # tests.
10 conf = {'plugins': {'hostrealm': {'module': ['test1:' + plugin,
11                                              'test2:' + plugin],
12                                   'enable_only': ['test2', 'profile',
13                                                   'domain', 'test1']}},
14         'domain_realm': {'.x': 'DOTMATCH', 'x': 'MATCH', '.1': 'NUMMATCH'}}
15 realm = K5Realm(krb5_conf=conf, create_kdb=False)
16
17 def test(realm, args, expected_realms, msg, env=None):
18     out = realm.run(['./hrealm'] + args, env=env)
19     if out.split('\n') != expected_realms + ['']:
20         fail(msg)
21
22 def test_error(realm, args, expected_error, msg, env=None):
23     out = realm.run(['./hrealm'] + args, env=env, expected_code=1)
24     if expected_error not in out:
25         fail(msg)
26
27 def testh(realm, host, expected_realms, msg, env=None):
28     test(realm, ['-h', host], expected_realms, msg, env=env)
29 def testf(realm, host, expected_realms, msg, env=None):
30     test(realm, ['-f', host], expected_realms, msg, env=env)
31 def testd(realm, expected_realm, msg, env=None):
32     test(realm, ['-d'], [expected_realm], msg, env=env)
33 def testh_error(realm, host, expected_error, msg, env=None):
34     test_error(realm, ['-h', host], expected_error, msg, env=env)
35 def testf_error(realm, host, expected_error, msg, env=None):
36     test_error(realm, ['-f', host], expected_error, msg, env=env)
37 def testd_error(realm, expected_error, msg, env=None):
38     test_error(realm, ['-d'], expected_error, msg, env=env)
39
40 ###
41 ### krb5_get_host_realm tests
42 ###
43
44 # The test2 module returns a fatal error on hosts beginning with 'z',
45 # and an answer on hosts begining with 'a'.
46 testh_error(realm, 'zoo', 'service not available', 'host_realm test2 z')
47 testh(realm, 'abacus', ['a'], 'host_realm test2 a')
48
49 # The profile module gives answers for hostnames equal to or ending in
50 # 'X', due to [domain_realms].  There is also an entry for hostnames
51 # ending in '1', but hostnames which appear to be IP or IPv6 addresses
52 # should instead fall through to test1.
53 testh(realm, 'x', ['MATCH'], 'host_realm profile x')
54 testh(realm, '.x', ['DOTMATCH'], 'host_realm profile .x')
55 testh(realm, 'b.x', ['DOTMATCH'], 'host_realm profile b.x')
56 testh(realm, '.b.c.x', ['DOTMATCH'], 'host_realm profile .b.c.x')
57 testh(realm, 'b.1', ['NUMMATCH'], 'host_realm profile b.1')
58 testh(realm, '4.3.2.1', ['4', '3', '2', '1'], 'host_realm profile 4.3.2.1')
59 testh(realm, 'b:c.x', ['b:c', 'x'], 'host_realm profile b:c.x')
60 # hostname cleaning should convert "X." to "x" before matching.
61 testh(realm, 'X.', ['MATCH'], 'host_realm profile X.')
62
63 # The test1 module returns a list of the hostname components.
64 testh(realm, 'b.c.d', ['b', 'c', 'd'], 'host_realm test1')
65
66 # If no module returns a result, we should get the referral realm.
67 testh(realm, '', [''], 'host_realm referral realm')
68
69 ###
70 ### krb5_get_fallback_host_realm tests
71 ###
72
73 # Return a special environment with realm_try_domains set to n.
74 def try_env(realm, testname, n):
75     conf = {'libdefaults': {'realm_try_domains': str(n)}}
76     return realm.special_env(testname, False, krb5_conf=conf)
77
78 # The domain module will answer with the uppercased parent domain,
79 # with no special configuration.
80 testf(realm, 'a.b.c', ['B.C'], 'fallback_realm domain a.b.c')
81
82 # With realm_try_domains = 0, the hostname itself will be looked up as
83 # a realm and returned if found.
84 try0 = try_env(realm, 'try0', 0)
85 testf(realm, 'krbtest.com', ['KRBTEST.COM'], 'fallback_realm try0', env=try0)
86 testf(realm, 'a.b.krbtest.com', ['B.KRBTEST.COM'],
87       'fallback_realm try0 grandparent', env=try0)
88 testf(realm, 'a.b.c', ['B.C'], 'fallback_realm try0 nomatch', env=try0)
89
90 # With realm_try_domains = 2, the parent and grandparent will be
91 # checked as well, but it stops there.
92 try2 = try_env(realm, 'try2', 2)
93 testf(realm, 'krbtest.com', ['KRBTEST.COM'], 'fallback_realm try2', env=try2)
94 testf(realm, 'a.b.krbtest.com', ['KRBTEST.COM'],
95       'fallback_realm try2 grandparent', env=try2)
96 testf(realm, 'a.b.c.krbtest.com', ['B.C.KRBTEST.COM'],
97       'fallback_realm try2 great-grandparent', env=try2)
98
99 # The test1 module answers with a list of components.  Use an IPv4
100 # address to bypass the domain module.
101 testf(realm, '1.2.3.4', ['1', '2', '3', '4'], 'fallback_realm test1')
102
103 # If no module answers, the default realm is returned.  The test2
104 # module returns an error when we try to look that up.
105 testf_error(realm, '', 'service not available', 'fallback_realm default')
106
107 ###
108 ### krb5_get_default_realm tests
109 ###
110
111 # The test2 module returns an error.
112 testd_error(realm, 'service not available', 'default_realm test2')
113
114 # The profile module returns the default realm from the profile.
115 # Disable test2 to expose this behavior.
116 disable_conf = {'plugins': {'hostrealm': {'disable': 'test2'}}}
117 notest2 = realm.special_env('notest2', False, krb5_conf=disable_conf)
118 testd(realm, 'KRBTEST.COM', 'default_realm profile', env=notest2)
119
120 # The test1 module returns a list of two realms, of which we can only
121 # see the first.  Remove the profile default_realm setting to expose
122 # this behavior.
123 remove_default = {'libdefaults': {'default_realm': None}}
124 nodefault_conf = dict(disable_conf.items() + remove_default.items())
125 nodefault = realm.special_env('nodefault', False, krb5_conf=nodefault_conf)
126 testd(realm, 'one', 'default_realm test1', env=nodefault)
127
128 success('hostrealm interface tests')