xref: /titanic_44/usr/src/cmd/krb5/kadmin/gui/native/Test.java (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright (c) 1999-2000 by Sun Microsystems, Inc.
26  * All rights reserved.
27  */
28 
29 import java.awt.*;
30 
31 import Principal;
32 import Policy;
33 import Defaults;
34 
35 public class Test {
36 
37 	/**
38 	 * main method for easier debugging
39 	 */
40 	public static void main(String[] args) {
41 	    System.out.println("\nThese are unit tests for the JNI code.");
42 	    System.out.println("They assume the existence of a jnitest/admin");
43 	    System.out.println("user principal, with the password 'test123',");
44 	    System.out.println("and the existance of a 'default' policy.\n");
45 
46 	    Kadmin k = new Kadmin();
47 	    String [] p;
48 	    Defaults d = new Defaults("$HOME/.gkadmin", Color.white);
49 
50 	    System.out.println("==> Set up a session for jnitest/admin.\n");
51 	    boolean b = false;
52 
53 	    System.out.println("*** First, one with a bogus port number.");
54 	    try {
55 		b = k.sessionInit("jnitest/admin", "test123",
56 		    "SUNSOFT.ENG.SUN.COM", "ulong.eng.sun.com", 333);
57 		b = false;
58 		k.sessionExit();
59 		System.out.println("Unexpected success!\n");
60 	    } catch (Exception e) {
61 		b = false;
62 		System.out.println("Expected failure "+e.getMessage()+"\n");
63 	    }
64 
65 	    System.out.println("*** Next, one with the correct port number.");
66 	    try {
67 		b = k.sessionInit("jnitest/admin", "test123",
68 		    "SUNSOFT.ENG.SUN.COM", "ulong.eng.sun.com", 749);
69 		b = false;
70 		k.sessionExit();
71 		System.out.println("sessionInit was successful.\n");
72 	    } catch (Exception e) {
73 		b = false;
74 		System.out.println("Unexpected exception!"+e.getMessage()+"\n");
75 	    }
76 
77 	    System.out.println("*** Finally, try one with a zero port number.");
78 	    try {
79 		b = k.sessionInit("jnitest/admin", "test123",
80 		    "SUNSOFT.ENG.SUN.COM", "ulong.eng.sun.com", 0);
81 	    } catch (Exception e) {
82 		b = false;
83 		System.out.println("Unexpected exception!"+e.getMessage()+"\n");
84 	    }
85 	    if (!b) {
86 		System.out.println("sessionInit failed\n");
87 		return;
88 	    }
89 	    System.out.println("sessionInit was successful!\n");
90 
91 
92 	    System.out.println("==> Get the lists\n");
93 	    System.out.println("*** Principal list");
94 	    try {
95 		p = k.getPrincipalList();
96 		System.out.println("Called getPrincipalList()");
97 	    } catch (Exception e) {
98 		p = null;
99 		System.out.println("getPrincipalList exception"+e.getMessage());
100 	    }
101 	    for (int i = 0; p != null && i < p.length; i++)
102 		System.out.println(p[i]);
103 	    System.out.println(new Integer(p.length).toString()+" entries.");
104 	    System.out.println("Principal list done\n");
105 
106 
107 	    System.out.println("*** Policy list");
108 	    try {
109 		p = k.getPolicyList();
110 		System.out.println("Called getPolicyList()");
111 	    } catch (Exception e) {
112 		p = null;
113 		System.out.println("getPolicyList exception "+e.getMessage());
114 	    }
115 	    for (int i = 0; p != null && i < p.length; i++)
116 		System.out.println(p[i]);
117 	    System.out.println(new Integer(p.length).toString()+" entries.");
118 	    System.out.println("Policy list done\n");
119 
120 
121 	    System.out.println("==> Get privileges\n");
122 	    System.out.println("Calling getPrivs()");
123 	    int privs = 0;
124 	    try {
125 		privs = k.getPrivs();
126 		System.out.println("Privs are "
127 		    +(new Integer(privs)).toHexString(privs)+"\n");
128 	    } catch (Exception e) {
129 		System.out.println("getPrivs exception "+e.getMessage()+"\n");
130 	    }
131 
132 
133 	    System.out.println("==> Database tests\n");
134 	    System.out.println("*** load a principal");
135 	    System.out.println("Calling Principal(k)");
136 	    Principal pr = new Principal(k, d);
137 	    System.out.println("Calling loadPrincipal(jnitest/admin)");
138 	    try {
139 		b = k.loadPrincipal("jnitest/admin", pr);
140 	    } catch (Exception e) {
141 		b = false;
142 		System.out.println("loadPrincipal exception "+e.getMessage());
143 	    }
144 	    if (!b)
145 		System.out.println("loadPrincipal failed\n");
146 	    else {
147 		System.out.println("loadPrincipal succeeded, details:");
148 		System.out.println(pr.toString());
149 	    }
150 
151 
152 	    System.out.println("*** load a policy");
153 	    Policy po = new Policy(k);
154 	    try {
155 		b = k.loadPolicy("default", po);
156 	    } catch (Exception e) {
157 		b = false;
158 		System.out.println("loadPolicy exception "+e.getMessage());
159 	    }
160 	    if (!b)
161 		System.out.println("loadPolicy failed\n");
162 	    else {
163 		System.out.println("loadPolicy succeeded");
164 		System.out.println("RefCount for "+po.PolicyName+" is "
165 		    +po.RefCount.toString());
166 		System.out.println("PwMinLife for "+po.PolicyName+" is "
167 		    +po.PwMinLife.toString()+"\n");
168 	    }
169 
170 
171 	    System.out.println("*** load and store a policy");
172 	    try {
173 		b = k.loadPolicy("default", po);
174 	    } catch (Exception e) {
175 		b = false;
176 		System.out.println("loadPolicy exception "+e.getMessage());
177 	    }
178 	    if (b) {
179 		po.setPolMinlife("555");
180 		try {
181 		    b = k.savePolicy(po);
182 		} catch (Exception e) {
183 		    b = false;
184 		    System.out.println("savePolicy exception "+e.getMessage());
185 		}
186 		if (!b)
187 		    System.out.println("savePolicy failed\n");
188 		else
189 		    System.out.println("savePolicy succeeded\n");
190 	    }
191 
192 
193 	    System.out.println("*** create a policy");
194 	    po = new Policy("aliens");
195 	    try {
196 		b = k.createPolicy(po);
197 	    } catch (Exception e) {
198 		b = false;
199 		System.out.println("createPolicy exception "+e.getMessage());
200 	    }
201 	    if (!b)
202 		System.out.println("createPolicy failed\n");
203 	    else
204 		System.out.println("createPolicy succeeded\n");
205 
206 	    System.out.println("*** verify creation");
207 	    try {
208 		p = k.getPolicyList();
209 		System.out.println("Called getPolicyList()");
210 	    } catch (Exception e) {
211 		p = null;
212 		System.out.println("getPolicyList exception "+e.getMessage());
213 	    }
214 	    for (int i = 0; p != null && i < p.length; i++)
215 		if (p[i].equals("aliens"))
216 		    System.out.println("Found 'aliens' as expected");
217 	    System.out.println(new Integer(p.length).toString()+" entries.");
218 	    System.out.println("Policy list done\n");
219 
220 
221 	    System.out.println("*** delete a policy");
222 	    try {
223 		b = k.deletePolicy("aliens");
224 	    } catch (Exception e) {
225 		b = false;
226 		System.out.println("deletePolicy exception "+e.getMessage());
227 	    }
228 	    if (!b)
229 		System.out.println("deletePolicy failed\n");
230 	    else
231 		System.out.println("deletePolicy succeeded\n");
232 
233 	    System.out.println("*** verify deletion");
234 	    try {
235 		p = k.getPolicyList();
236 		System.out.println("Called getPolicyList()");
237 	    } catch (Exception e) {
238 		p = null;
239 		System.out.println("getPolicyList exception "+e.getMessage());
240 	    }
241 	    for (int i = 0; p != null && i < p.length; i++)
242 		if (p[i].equals("aliens"))
243 		    System.out.println("Found 'aliens' - oops!");
244 	    System.out.println(new Integer(p.length).toString()+" entries.");
245 	    System.out.println("Policy list done\n");
246 
247 
248 	    System.out.println("*** load and store a principal");
249 	    try {
250 		b = k.loadPrincipal("jnitest/admin", pr);
251 	    } catch (Exception e) {
252 		b = false;
253 		System.out.println("loadPrincipal exception "+e);
254 	    }
255 	    if (b) {
256 		System.out.println("Loaded "+pr.toString());
257 		pr.setPolicy("default");
258 		System.out.println("Expiry "+pr.PrExpireTime.toString());
259 		System.out.println("PwExpiry "+pr.PwExpireTime.toString());
260 		try {
261 		    b = k.savePrincipal(pr);
262 		} catch (Exception e) {
263 		    b = false;
264 		    System.out.println("savePrincipal exception "+e);
265 		}
266 		if (!b)
267 		    System.out.println("savePrincipal failed\n");
268 		else
269 		    System.out.println("savePrincipal succeeded\n");
270 	    }
271 
272 
273 	    System.out.println("*** ensure a principal is absent");
274 	    try {
275 		b = k.deletePrincipal("harriet");
276 	        System.out.println("deleted principal\n");
277 	    } catch (Exception e) {
278 	        System.out.println("Expected exception"+e+"\n");
279 	    }
280 
281 
282 	    System.out.println("*** create a principal");
283 	    pr = new Principal("harriet");
284 	    pr.setPassword("test123");
285 	    System.out.println("Built "+pr.toString());
286 	    try {
287 		b = k.createPrincipal(pr);
288 	    } catch (Exception e) {
289 		b = false;
290 		System.out.println("createPrincipal exception "+e);
291 	    }
292 	    if (!b)
293 		System.out.println("createPrincipal failed");
294 	    else
295 		System.out.println("createPrincipal succeeded");
296 
297 	    System.out.println("*** verify creation");
298 	    try {
299 		p = k.getPrincipalList();
300 	    } catch (Exception e) {
301 		p = null;
302 		System.out.println("getPrincipalList exception "+e);
303 	    }
304 	    System.out.println("Called getPrincipalList()");
305 	    for (int i = 0; p != null && i < p.length; i++)
306 		if (p[i].equals("harriet"))
307 		    System.out.println("Found 'harriet' as expected");
308 	    System.out.println(new Integer(p.length).toString()+" entries.");
309 	    System.out.println("Principal list done\n");
310 
311 
312 	    System.out.println("*** set comments");
313 	    pr = new Principal(k, d);
314 	    try {
315 		b = k.loadPrincipal("harriet", pr);
316 	    } catch (Exception e) {
317 		b = false;
318 		System.out.println("loadPrincipal exception "+e);
319 	    }
320 	    if (b) {
321 		pr.setComments("Who knows this user?");
322 		try {
323 		    b = k.savePrincipal(pr);
324 		} catch (Exception e) {
325 		    b = false;
326 		    System.out.println("savePrincipal exception "+e);
327 		}
328 		if (b) {
329 		    try {
330 			b = k.loadPrincipal("harriet", pr);
331 		    } catch (Exception e) {
332 			b = false;
333 			System.out.println("loadPrincipal exception "+e);
334 		    }
335 		    System.out.println("Loaded "+pr.toString());
336 		    System.out.println("Comments are "+pr.Comments+"\n");
337 		} else
338 		    System.out.println("savePrincipal failed");
339 	    }
340 
341 
342 	    System.out.println("*** set password");
343 	    pr = new Principal(k, d);
344 	    try {
345 		b = k.loadPrincipal("harriet", pr);
346 	    } catch (Exception e) {
347 		b = false;
348 		System.out.println("loadPrincipal exception "+e);
349 	    }
350 	    if (b) {
351 		pr.setPassword("test234");
352 		try {
353 		    b = k.savePrincipal(pr);
354 		} catch (Exception e) {
355 		    b = false;
356 		    System.out.println("savePrincipal exception "+e);
357 		}
358 		if (!b)
359 		    System.out.println("savePrincipal failed\n");
360 		else
361 		    System.out.println("savePrincipal succeeded\n");
362 	    }
363 
364 
365 	    System.out.println("*** delete principal");
366 	    try {
367 		b = k.deletePrincipal("harriet");
368 	    } catch (Exception e) {
369 		b = false;
370 		System.out.println("deletePrincipal exception "+e);
371 	    }
372 	    if (!b)
373 		System.out.println("deletePrincipal failed\n");
374 	    else
375 		System.out.println("deletePrincipal succeeded\n");
376 
377 	    System.out.println("*** verify deletion");
378 	    try {
379 		p = k.getPrincipalList();
380 		System.out.println("Called getPrincipalList()");
381 	    } catch (Exception e) {
382 		p = null;
383 		System.out.println("getPrincipalList exception "+e);
384 	    }
385 	    for (int i = 0; p != null && i < p.length; i++)
386 		if (p[i].equals("harriet"))
387 		    System.out.println("Found 'harriet' - oops!");
388 	    System.out.println(new Integer(p.length).toString()+" entries.");
389 	    System.out.println("Principal list done\n");
390 
391 
392 	    System.out.println("*** create a principal with comments");
393 	    pr = new Principal("harriet");
394 	    pr.setPassword("test123");
395 	    pr.setComments("Room 1229");
396 	    System.out.println("Built "+pr.toString());
397 	    try {
398 		b = k.createPrincipal(pr);
399 	    } catch (Exception e) {
400 		b = false;
401 		System.out.println("createPrincipal exception "+e);
402 	    }
403 	    if (!b)
404 		System.out.println("createPrincipal failed\n");
405 	    else
406 		System.out.println("createPrincipal succeeded\n");
407 
408 	    System.out.println("*** verify comments");
409 	    try {
410 		b = k.loadPrincipal("harriet", pr);
411 	    } catch (Exception e) {
412 		b = false;
413 		System.out.println("loadPrincipal exception "+e);
414 	    }
415 	    if (b) {
416 		System.out.println("Loaded "+pr.toString());
417 		System.out.println("Comments "+pr.Comments+"\n");
418 		try {
419 		    b = k.deletePrincipal("harriet");
420 		} catch (Exception e) {
421 		    b = false;
422 		    System.out.println("deletePrincipal exception "+e);
423 		}
424 	    }
425 
426 	    System.out.println("All tests completed.\n");
427 	    k.sessionExit();
428 	}
429 }
430