|
1 /* |
|
2 * Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 6844193 |
|
27 * @run main/timeout=300 MaxRetries |
|
28 * @summary support max_retries in krb5.conf |
|
29 */ |
|
30 |
|
31 import java.io.*; |
|
32 import java.security.Security; |
|
33 |
|
34 public class MaxRetries { |
|
35 public static void main(String[] args) |
|
36 throws Exception { |
|
37 |
|
38 System.setProperty("sun.security.krb5.debug", "true"); |
|
39 new OneKDC(null).writeJAASConf(); |
|
40 System.setProperty("java.security.krb5.conf", "alternative-krb5.conf"); |
|
41 |
|
42 // For tryLast |
|
43 Security.setProperty("krb5.kdc.bad.policy", "trylast"); |
|
44 rewriteMaxRetries(4); |
|
45 test1(4000, 6); // 1 1 1 1 2 2 |
|
46 test1(4000, 2); // 2 2 |
|
47 |
|
48 rewriteMaxRetries(1); |
|
49 test1(1000, 3); // 1 2 2 |
|
50 test1(1000, 2); // 2 2 |
|
51 |
|
52 rewriteMaxRetries(-1); |
|
53 test1(5000, 4); // 1 1 2 2 |
|
54 test1(5000, 2); // 2 2 |
|
55 |
|
56 // For tryLess |
|
57 Security.setProperty("krb5.kdc.bad.policy", "tryless"); |
|
58 rewriteMaxRetries(4); |
|
59 test1(4000, 7); // 1 1 1 1 2 1 2 |
|
60 test1(4000, 4); // 1 2 1 2 |
|
61 |
|
62 rewriteMaxRetries(1); |
|
63 test1(1000, 4); // 1 2 1 2 |
|
64 test1(1000, 4); // 1 2 1 2 |
|
65 |
|
66 rewriteMaxRetries(-1); |
|
67 test1(5000, 5); // 1 1 2 1 2 |
|
68 test1(5000, 4); // 1 2 1 2 |
|
69 |
|
70 rewriteUdpPrefLimit(-1, -1); // default, no limit |
|
71 test2("UDP"); |
|
72 |
|
73 rewriteUdpPrefLimit(10, -1); // global rules |
|
74 test2("TCP"); |
|
75 |
|
76 rewriteUdpPrefLimit(10, 10000); // realm rules |
|
77 test2("UDP"); |
|
78 |
|
79 rewriteUdpPrefLimit(10000, 10); // realm rules |
|
80 test2("TCP"); |
|
81 } |
|
82 |
|
83 /** |
|
84 * One round of test for max_retries and timeout. |
|
85 * @param timeout the expected timeout |
|
86 * @param count the expected total try |
|
87 */ |
|
88 private static void test1(int timeout, int count) throws Exception { |
|
89 String timeoutTag = "timeout=" + timeout; |
|
90 ByteArrayOutputStream bo = new ByteArrayOutputStream(); |
|
91 PrintStream oldout = System.out; |
|
92 System.setOut(new PrintStream(bo)); |
|
93 Context c = Context.fromJAAS("client"); |
|
94 System.setOut(oldout); |
|
95 |
|
96 String[] lines = new String(bo.toByteArray()).split("\n"); |
|
97 System.out.println("----------------- TEST (" + timeout + "," + |
|
98 count + ") -----------------"); |
|
99 for (String line: lines) { |
|
100 if (line.startsWith(">>> KDCCommunication")) { |
|
101 System.out.println(line); |
|
102 if (line.indexOf(timeoutTag) < 0) { |
|
103 throw new Exception("Wrong timeout value"); |
|
104 } |
|
105 count--; |
|
106 } |
|
107 } |
|
108 if (count != 0) { |
|
109 throw new Exception("Retry count is " + count + " less"); |
|
110 } |
|
111 } |
|
112 |
|
113 /** |
|
114 * One round of test for udp_preference_limit. |
|
115 * @param proto the expected protocol used |
|
116 */ |
|
117 private static void test2(String proto) throws Exception { |
|
118 ByteArrayOutputStream bo = new ByteArrayOutputStream(); |
|
119 PrintStream oldout = System.out; |
|
120 System.setOut(new PrintStream(bo)); |
|
121 Context c = Context.fromJAAS("client"); |
|
122 System.setOut(oldout); |
|
123 |
|
124 int count = 2; |
|
125 String[] lines = new String(bo.toByteArray()).split("\n"); |
|
126 System.out.println("----------------- TEST -----------------"); |
|
127 for (String line: lines) { |
|
128 if (line.startsWith(">>> KDCCommunication")) { |
|
129 System.out.println(line); |
|
130 count--; |
|
131 if (line.indexOf(proto) < 0) { |
|
132 throw new Exception("Wrong timeout value"); |
|
133 } |
|
134 } |
|
135 } |
|
136 if (count != 0) { |
|
137 throw new Exception("Retry count is " + count + " less"); |
|
138 } |
|
139 } |
|
140 |
|
141 /** |
|
142 * Set udp_preference_limit for global and realm |
|
143 */ |
|
144 private static void rewriteUdpPrefLimit(int global, int realm) |
|
145 throws Exception { |
|
146 BufferedReader fr = new BufferedReader(new FileReader(OneKDC.KRB5_CONF)); |
|
147 FileWriter fw = new FileWriter("alternative-krb5.conf"); |
|
148 while (true) { |
|
149 String s = fr.readLine(); |
|
150 if (s == null) { |
|
151 break; |
|
152 } |
|
153 if (s.startsWith("[realms]")) { |
|
154 // Reconfig global setting |
|
155 if (global != -1) { |
|
156 fw.write("udp_preference_limit = " + global + "\n"); |
|
157 } |
|
158 } else if (s.trim().startsWith("kdc = ")) { |
|
159 if (realm != -1) { |
|
160 // Reconfig for realm |
|
161 fw.write(" udp_preference_limit = " + realm + "\n"); |
|
162 } |
|
163 } |
|
164 fw.write(s + "\n"); |
|
165 } |
|
166 fr.close(); |
|
167 fw.close(); |
|
168 sun.security.krb5.Config.refresh(); |
|
169 } |
|
170 |
|
171 /** |
|
172 * Set max_retries and timeout value for realm. The global value is always |
|
173 * 2 and 5000. |
|
174 * @param value max_retries and timeout/1000 for a realm, -1 means none. |
|
175 */ |
|
176 private static void rewriteMaxRetries(int value) throws Exception { |
|
177 BufferedReader fr = new BufferedReader(new FileReader(OneKDC.KRB5_CONF)); |
|
178 FileWriter fw = new FileWriter("alternative-krb5.conf"); |
|
179 while (true) { |
|
180 String s = fr.readLine(); |
|
181 if (s == null) { |
|
182 break; |
|
183 } |
|
184 if (s.startsWith("[realms]")) { |
|
185 // Reconfig global setting |
|
186 fw.write("max_retries = 2\n"); |
|
187 fw.write("kdc_timeout = 5000\n"); |
|
188 } else if (s.trim().startsWith("kdc = ")) { |
|
189 if (value != -1) { |
|
190 // Reconfig for realm |
|
191 fw.write(" max_retries = " + value + "\n"); |
|
192 fw.write(" kdc_timeout = " + (value*1000) + "\n"); |
|
193 } |
|
194 // Add a bad KDC as the first candidate |
|
195 fw.write(" kdc = localhost:33333\n"); |
|
196 } |
|
197 fw.write(s + "\n"); |
|
198 } |
|
199 fr.close(); |
|
200 fw.close(); |
|
201 sun.security.krb5.Config.refresh(); |
|
202 } |
|
203 } |