-
Notifications
You must be signed in to change notification settings - Fork 110
/
secformClient.java
2304 lines (1967 loc) · 87.2 KB
/
secformClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2007, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.ts.tests.common.jspservletsec;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import com.sun.ts.lib.harness.EETest;
import com.sun.ts.lib.porting.TSURL;
import com.sun.ts.lib.util.TestUtil;
import com.sun.ts.lib.util.WebUtil;
import com.sun.ts.lib.util.WebUtil.Response;
public class secformClient extends EETest {
// Configurable constants:
private String protocol = "http";
private String hostname = null;
private int portnum = 0;
protected String pageBase = null;
protected String pageSec = null;
protected String pageGuest = null;
protected String pageRoleReverse = null;
protected String pageUnprotected = null;
private String pageProgAuthen = null;
private String pageProgLogin = null;
private String pageProgLogout = null;
private String pageOne = null;
private String pageTwo = null;
private String pageSample = null;
private String pageallRoles = null;
// common for JSP and Servlet
private String pageLogin = "/login.jsp";
private String pageError = "/error.jsp";
private String pageSecurityCheck = "/j_security_check";
private String pageJspBase = "/jsp_sec_secform_web";
private String pageJspSec = pageJspBase + "/jspSec.jsp";
private String pageJspUnprotected = pageJspBase + "/unprotected.jsp";
private String pageJspGuest = pageJspBase + "/guestPage.jsp";
private String pageJspRoleReverse = pageJspBase + "/rolereverse.jsp";
private String pageJspOne = pageJspBase + "/One.jsp";
private String pageJspTwo = pageJspBase + "/Two.jsp";
private String pageJspSample = pageJspBase + "/Sample.jsp";
private String pageJspallRoles = pageJspBase + "/allRoles.jsp";
private String pageServletBase = "/servlet_sec_secform_web";
private String pageServletSec = pageServletBase + "/ServletSecTest";
private String pageServletUnprotected = pageServletBase + "/UnProtectedTest";
private String pageServletProgLogin = pageServletBase
+ "/ServletProgrammaticLogin";
private String pageServletProgLogout = pageServletBase
+ "/ServletProgrammaticLogout";
private String pageServletProgAuthen = pageServletBase
+ "/ServletProgrammaticAuthen";
private String pageServletGuest = pageServletBase + "/GuestPageTest";
private String pageServletRoleReverse = pageServletBase + "/RoleReverseTest";
private String pageServletOne = pageServletBase + "/OneTest";
private String pageServletTwo = pageServletBase + "/TwoTest";
private String pageServletSample = pageServletBase + "/SampleTest";
private String pageServletallRoles = pageServletBase + "/allRolesTest";
private String searchFor = "The user principal is: "; // (+username)
private String searchForGetRemoteUser = "getRemoteUser(): "; // (+username)
private String username = "";
private String password = "";
private String unauthUsername = "";
private String unauthPassword = "";
private String tshome = "";
// Constants:
private final String WebHostProp = "webServerHost";
private final String WebPortProp = "webServerPort";
private final String UserNameProp = "user";
private final String PasswordProp = "password";
private final String unauthUserNameProp = "authuser";
private final String unauthPasswordProp = "authpassword";
private final String tsHomeProp = "ts_home";
private String testDir = System.getProperty("user.dir");
// Shared test variables:
private Properties props = null;
private String request = null;
private WebUtil.Response response = null;
private WebUtil.Response loginPageRequestResponse = null;
private WebUtil.Response errorPageRequestResponse = null;
private Hashtable cookies = null;
private TSURL tsurl = new TSURL();
/*
* @class.setup_props: webServerHost; webServerPort; user; password; authuser;
* authpassword; ts_home;
*/
public void setup(String[] args, Properties p) throws Fault {
props = p;
try {
hostname = p.getProperty(WebHostProp);
portnum = Integer.parseInt(p.getProperty(WebPortProp));
username = p.getProperty(UserNameProp);
password = p.getProperty(PasswordProp);
unauthUsername = p.getProperty(unauthUserNameProp);
unauthPassword = p.getProperty(unauthPasswordProp);
tshome = p.getProperty(tsHomeProp);
TestUtil.logMsg("username: " + username);
TestUtil.logMsg("password: " + password);
if (args[0].equals("jsp")) {
pageBase = pageJspBase;
pageSec = pageJspSec;
pageGuest = pageJspGuest;
pageUnprotected = pageJspUnprotected;
pageRoleReverse = pageJspRoleReverse;
pageOne = pageJspOne;
pageTwo = pageJspTwo;
pageSample = pageJspSample;
pageallRoles = pageJspallRoles;
// prefix pageJspBase to pageLogin, pageError ,pageSecurityCheck
pageLogin = pageJspBase + pageLogin;
pageError = pageJspBase + pageError;
pageSecurityCheck = pageJspBase + pageSecurityCheck;
} else {
pageBase = pageServletBase;
pageSec = pageServletSec;
pageGuest = pageServletGuest;
pageUnprotected = pageServletUnprotected;
pageRoleReverse = pageServletRoleReverse;
pageOne = pageServletOne;
pageTwo = pageServletTwo;
pageSample = pageServletSample;
pageallRoles = pageServletallRoles;
pageProgLogin = pageServletProgLogin;
pageProgLogout = pageServletProgLogout;
pageProgAuthen = pageServletProgAuthen;
// prefix pageServletBase to pageLogin, pageError, pageSecurityCheck
pageLogin = pageServletBase + pageLogin;
pageError = pageServletBase + pageError;
pageSecurityCheck = pageServletBase + pageSecurityCheck;
}
} catch (Exception e) {
logErr("Error: got exception: ", e);
}
}
/*
* testName: test1
*
* @assertion: Test FORM-based authentication, specified in the Java Servlet
* Specification v2.2, Sec 11.5.3. Also tests an assertion in section 11.3.
*
* 1. If user has not been authenticated and user attempts to access a
* protected web resource, the correct login form is returned. 2. If user has
* not been authenticated and user attempts to access a protected web
* resource, and user enters a valid username and password, the original web
* resource is returned and user is authenticated. A call to getRemoteUser()
* must return the username. 3. If user has been authenticated and user is
* authorized to access a protected web resource, user gets web resource
* without the need to re-authenticate. A call to getRemoteUser() still
* returns the username.
*
* @test_Strategy: 1. Send request to access jspSec.jsp 2. Receive login
* page(make sure it the expected login page) 3. Send form response with
* username and password 4. Receive jspSec.jsp (ensure principal is correct,
* and ensure getRemoteUser() returns the username, and ensure isUserInRole()
* is working properly) 5. Re-request jspSec.jsp 6. Ensure principal is still
* correct and getRemoteUser() still returns the correct username. Also ensure
* isUserInRole() is still working properly.
*/
public void test1() throws Fault {
try {
// The first part of this test is used in test2 and test3 as
// well, so the code has been moved to a helper method.
requestAndGetLoginPage(pageSec, 1);
// Send response to login form with session id cookie:
request = pageSecurityCheck;
TestUtil.logMsg(
"Sending request \"" + request + "\" with login information.");
Properties postData = new Properties();
postData.setProperty("j_username", username);
postData.setProperty("j_password", password);
response = WebUtil.sendRequest("POST", InetAddress.getByName(hostname),
portnum, tsurl.getRequest(request), postData, cookies);
TestUtil.logMsg("response.statusToken:" + response.statusToken);
TestUtil.logMsg("response.content:" + response.content);
// Check that the page was found (no error).
if (response.isError()) {
TestUtil.logErr("Could not find " + request);
throw new Fault("test1 failed.");
}
// Call followRedirect() to make sure we receive the required page
response = followRedirect(response, 1);
// Print response content
TestUtil.logMsg("received response content 1: " + response.content);
// Check to make sure we are authenticated by checking the page
// content. The jsp should output "The user principal is: j2ee"
String searchString = searchFor + username;
if (response.content.indexOf(searchString) == -1) {
TestUtil.logErr("User Principal incorrect. Page received:");
TestUtil.logErr(response.content);
TestUtil.logErr("(Should say: \"" + searchString + "\")");
throw new Fault("test1 failed.");
}
TestUtil.logMsg("User Principal correct.");
// Check to make sure getRemoteUser returns the user name.
searchString = searchForGetRemoteUser + username;
if (response.content.indexOf(searchString) == -1) {
TestUtil.logErr("getRemoteUser() did not return " + username + ":");
TestUtil.logErr(response.content);
TestUtil.logErr("(Should say: \"" + searchString + "\")");
throw new Fault("test1 failed.");
}
TestUtil.logMsg("getRemoteUser() correct.");
// Check to make sure isUserInRole is working properly:
Hashtable roleCheck = new Hashtable();
roleCheck.put("ADM", new Boolean(true));
roleCheck.put("MGR", new Boolean(false));
roleCheck.put("VP", new Boolean(false));
roleCheck.put("EMP", new Boolean(true));
// roleCheck.put( "Administrator", new Boolean( false ) );
if (!checkRoles(response.content, roleCheck)) {
TestUtil.logErr("isUserInRole() does not work correctly.");
TestUtil.logErr("Page Received:");
TestUtil.logErr(response.content);
throw new Fault("test1 failed.");
}
TestUtil.logMsg("isUserInRole() correct.");
// Now that we are authenticated, try accessing the resource again
// to ensure we need not go through the login page again.
request = pageSec;
TestUtil.logMsg("Cookies =" + cookies.toString());
TestUtil.logMsg("Re-sending request \"" + request + "\"");
response = WebUtil.sendRequest("GET", InetAddress.getByName(hostname),
portnum, tsurl.getRequest(request), null, cookies);
// Check that the page was found (no error).
if (response.isError()) {
TestUtil.logErr("Could not find " + pageSec);
throw new Fault("test1 failed.");
}
// Check to make sure we are still authenticated.
if (response.content.indexOf(searchString) == -1) {
TestUtil.logErr("User Principal incorrect. Page received:");
TestUtil.logErr(response.content);
TestUtil.logErr("(Should say: \"" + searchString + "\")");
throw new Fault("test1 failed.");
}
TestUtil.logMsg("User Principal still correct.");
// Check to make sure getRemoteUser still returns the user name.
searchString = searchForGetRemoteUser + username;
if (response.content.indexOf(searchString) == -1) {
TestUtil.logErr("getRemoteUser() did not return " + username
+ " after lazy authentication:");
TestUtil.logErr(response.content);
TestUtil.logErr("(Should say: \"" + searchString + "\")");
throw new Fault("test1 failed.");
}
TestUtil.logMsg("getRemoteUser() still correct.");
// Check to make sure isUserInRole is still working properly:
if (!checkRoles(response.content, roleCheck)) {
TestUtil.logErr("isUserInRole() does not work correctly.");
TestUtil.logErr("Page Received:");
TestUtil.logErr(response.content);
throw new Fault("test1 failed.");
}
TestUtil.logMsg("isUserInRole() still correct.");
} catch (Exception e) {
TestUtil.logErr("Caught exception: " + e.getMessage());
e.printStackTrace();
throw new Fault("test1 failed: ", e);
}
}
/*
* testName: test2
*
* @assertion: Test FORM-based authentication, specified in the Java Servlet
* Specification v2.2, Sec 11.5.3.
*
* If user has not been authenticated and user attempts to access a protected
* web resource, and user enters incorrect username and password, the error
* page is returned.
*
* @test_Strategy: 1. Send request to access jspSec.jsp 2. Receive login page
* 3. Send form response with username and incorrect password 4. Receive error
* page (make sure it is the expected error page)
*/
public void test2() throws Fault {
try {
// The first part of this test is used in test1 and test3 as
// well, so the code has been moved to a helper method.
requestAndGetLoginPage(pageSec, 2);
// Send response to login form with session id cookie and username
// and incorrect password:
request = pageSecurityCheck;
TestUtil.logMsg("Sending request \"" + request
+ "\" with incorrect login information.");
Properties postData = new Properties();
postData.setProperty("j_username", username);
postData.setProperty("j_password", "incorrect" + password);
response = WebUtil.sendRequest("POST", InetAddress.getByName(hostname),
portnum, tsurl.getRequest(request), postData, cookies);
TestUtil.logMsg("response.statusToken:" + response.statusToken);
// Call followRedirect() to make sure we receive the required page
response = followRedirect(response, 2);
// Check to make sure the user principal is null:
String searchString = searchFor + "null";
if (response.content.indexOf(searchString) == -1) {
TestUtil.logErr("User principal is not null in error page:");
TestUtil.logErr(response.content);
throw new Fault("test2 failed.");
}
TestUtil.logMsg("User Principal is null as expected.");
// Request error page
request = pageError;
TestUtil.logMsg("Sending request \"" + request + "\"");
errorPageRequestResponse = WebUtil.sendRequest("GET",
InetAddress.getByName(hostname), portnum, tsurl.getRequest(request),
null, cookies);
// Check that the page was found (no error).
if (errorPageRequestResponse.isError()) {
TestUtil.logErr("Could not find " + request);
throw new Fault("test2 failed.");
}
// Compare the received error page with the expected error page
// i.e Check whether
// response.content ==errorPageRequestResponse.content
if (response.content.equals(errorPageRequestResponse.content)) {
TestUtil.logMsg("Received the expected error page");
} else {
TestUtil.logMsg("Received incorrect error page");
throw new Fault("test2 failed.");
}
} catch (Exception e) {
TestUtil.logErr("Caught exception: " + e.getMessage());
e.printStackTrace();
throw new Fault("test2 failed: ", e);
}
}
/*
* testName: test3
*
* @assertion: Test FORM-based authentication, specified in the Java Servlet
* Specification v2.2, Sec 11.5.3.
*
* If user has not been authenticated and user attempts to access a protected
* web resource, and user enters correct username and password of a user that
* is authorized to access the resource, the resource is returned (similar to
* test1, but uses user javajoe instead of j2ee). This test establishes that
* the javajoe user is set up properly.
*
* @test_Strategy: 1. Send request to access guestPage.jsp 2. Receive login
* page 3. Send form response with username(javajoe) and password 4. Receive
* resource (check user principal)
*
*/
public void test3() throws Fault {
try {
// The first part of this test is used in test2 and test3 as
// well, so the code has been moved to a helper method.
requestAndGetLoginPage(pageGuest, 3);
// Send response to login form with session id cookie:
request = pageSecurityCheck;
TestUtil.logMsg("Sending request \"" + request
+ "\" with login information (as " + unauthUsername + ").");
Properties postData = new Properties();
postData.setProperty("j_username", unauthUsername);
postData.setProperty("j_password", unauthPassword);
response = WebUtil.sendRequest("POST", InetAddress.getByName(hostname),
portnum, tsurl.getRequest(request), postData, cookies);
// Check that the page was found (no error).
if (response.isError()) {
TestUtil.logErr("Could not find " + request);
throw new Fault("test3 failed.");
}
// Call followRedirect() to make sure we receive the required page
response = followRedirect(response, 3);
// Check to make sure we are authenticated by checking the page
// content. The jsp should output "The user principal is: javajoe"
String searchString = searchFor + unauthUsername;
if (response.content.indexOf(searchString) == -1) {
TestUtil.logErr("User Principal incorrect. Page received:");
TestUtil.logErr(response.content);
TestUtil.logErr("(Should say: \"" + searchString + "\")");
throw new Fault("test3 failed.");
}
TestUtil.logMsg("User Principal correct.");
} catch (Exception e) {
TestUtil.logErr("Caught exception: " + e.getMessage());
e.printStackTrace();
throw new Fault("test3 failed: ", e);
}
}
/*
* testName: test4
*
* @assertion: Test FORM-based authentication, specified in the Java Servlet
* Specification v2.2, Sec 11.5.3.
*
* If user has not been authenticated and user attempts to access a protected
* web resource, and user enters correct username and password of a user that
* is not authorized to access the resource, the resource is not returned. The
* authenticated user is not denied access to an unprotected page.
*
* @test_Strategy: 1. Send request to access jspSec.jsp 2. Receive login page
* 3. Send form response with username and password 4. Receive an error
* (expected unauthorized error) 5. Send request to access unprotected.jsp 6.
* Receive unprotected.jsp.
*/
public void test4() throws Fault {
try {
// The first part of this test is used in test1 and test2 as
// well, so the code has been moved to a helper method.
requestAndGetLoginPage(pageSec, 4);
// Send response to login form with session id cookie and username
// and password:
request = pageSecurityCheck;
TestUtil.logMsg("Sending request \"" + request
+ "\" with correct login information (" + unauthUsername + ")"
+ ", but incorrect authorization for this resource.");
Properties postData = new Properties();
postData.setProperty("j_username", unauthUsername);
postData.setProperty("j_password", unauthPassword);
response = WebUtil.sendRequest("POST", InetAddress.getByName(hostname),
portnum, tsurl.getRequest(request), postData, cookies);
TestUtil.logMsg("response.content = " + response.content);
if (response.statusToken.equals("302")) {
// We should receive a redirection page
if (response.location == null) {
TestUtil.logErr("No redirection to login page received.");
throw new Fault("test4 failed.");
}
// Extract location from redirection and format new request:
request = WebUtil.getRequestFromURL(response.location);
TestUtil.logMsg("Redirect to: " + response.location);
// update cookies if the webserver choose to send cookies
addNewCookies(cookies, response.cookies);
// Request redirected page (login page):
TestUtil.logMsg("Sending request \"" + request + "\"");
response = WebUtil.sendRequest("GET", InetAddress.getByName(hostname),
portnum, request, null, cookies);
}
// Receive "403" or "404" error code for unauthorized access (forbidden).
if ((response.statusToken.equals("403"))
|| (response.statusToken.equals("404"))) {
TestUtil.logMsg("Status Token " + response.statusToken);
TestUtil.logMsg("Received expected unauthorized access error");
} else {
TestUtil.logErr(
"Did not receive error for unauthorized access: " + request);
TestUtil.logMsg("Status Token " + response.statusToken);
TestUtil.logErr("Page content:");
TestUtil.logErr(response.content);
throw new Fault("test4 failed.");
}
// Request unprotected page (unprotected.jsp page):
request = pageUnprotected;
TestUtil.logMsg("Sending request \"" + request + "\"");
response = WebUtil.sendRequest("GET", InetAddress.getByName(hostname),
portnum, tsurl.getRequest(request), null, null);
// Check that we did not receive an error and that we did not
// receive a redirection:
if (response.isError()) {
TestUtil.logErr("Error retrieving " + request);
throw new Fault("test4 failed.");
}
// Check that the page returned is the correct one. The principal
// is not checked.
String searchString = searchFor;
if (response.content.indexOf(searchString) == -1) {
TestUtil.logErr("Incorrect page received:");
TestUtil.logErr(response.content);
TestUtil.logErr("(Should contain: \"" + searchString + "\")");
throw new Fault("test4 failed.");
}
TestUtil.logMsg("Access to unprotected page granted.");
} catch (Exception e) {
TestUtil.logErr("Caught exception: " + e.getMessage());
e.printStackTrace();
throw new Fault("test4 failed: ", e);
}
}
/*
* testName: test5
*
* @assertion: Test FORM-based authentication, specified in the Java Servlet
* Specification v2.2, Sec 11.5.3. Also tests assertions from section 11.3.
*
* If user has not been authenticated and user attempts to access an
* unprotected web resource, the resource is returned, and the user is not
* forced to authenticate. Also, isUserInRole() must return false for any
* valid or invalid role reference. A call to getRemoteUser() must return
* null.
*
* @test_Strategy: 1. Send request to access unprotected.jsp 2. Receive
* unprotected.jsp 3. Search the returned page for "!true!", which would
* indicate that at least one call to isUserInRole attempted by
* unprotected.jsp returned true. 4. Check that the call to getRemoteUser()
* returned null.
*/
public void test5() throws Fault {
try {
// Request restricted jsp page.
String request = pageUnprotected;
TestUtil.logMsg("Sending request \"" + request + "\"");
response = WebUtil.sendRequest("GET", InetAddress.getByName(hostname),
portnum, tsurl.getRequest(request), null, null);
// Check that the page was found (no error).
if (response.isError()) {
TestUtil.logErr("Could not find " + pageUnprotected);
throw new Fault("test5 failed.");
}
// Check that we did not receive an error and that we did not
// receive a redirection:
if (response.isError()) {
TestUtil.logErr("Error retrieving " + request);
throw new Fault("test5 failed.");
}
// Check that the page returned is the correct one. The principal
// is not checked.
String searchString = searchFor;
if (response.content.indexOf(searchString) == -1) {
TestUtil.logErr("Incorrect page received:");
TestUtil.logErr(response.content);
TestUtil.logErr("(Should contain: \"" + searchString + "\")");
throw new Fault("test5 failed.");
}
TestUtil.logMsg("Access to unprotected page granted.");
// Check to see if any of the calls to isUserInRole returned true:
TestUtil.logMsg("Checking isUserInRole...");
searchString = "!true!";
if (response.content.indexOf(searchString) != -1) {
TestUtil.logErr("At least one call to isUserInRole returned true.");
TestUtil.logErr("Page received:");
TestUtil.logErr(response.content);
throw new Fault("test5 failed.");
}
TestUtil.logMsg("isUserInRole test passed.");
// Check to make sure the call to getRemoteUser() returned null.
TestUtil.logMsg("Checking getRemoteUser()...");
searchString = searchForGetRemoteUser + "null";
if (response.content.indexOf(searchString) == -1) {
TestUtil.logErr("A call to getRemoteUser() did not return null.");
TestUtil.logErr("Page received:");
TestUtil.logErr(response.content);
throw new Fault("test5 failed.");
}
TestUtil.logMsg("getRemoteUser() test passed.");
} catch (Exception e) {
TestUtil.logErr("Caught exception: " + e.getMessage());
e.printStackTrace();
throw new Fault("test5 failed: ", e);
}
}
/*
* testName: test6
*
* @assertion: Test FORM-based authentication, specified in the Java Servlet
* Specification v2.2, Sec 11.5.3. Also tests assertions from section 11.3.
*
* Given two servlets in the same application, each of which calls
* isUserInRole(X), and where X is linked to different roles in the scope of
* each of the servlets (i.e. R1 for servlet 1 and R2 for servlet 2), then a
* user whose identity is mapped to R1 but not R2, shall get a true return
* value from isUserInRole( X ) in servlet 1, and a false return value from
* servlet 2 (a user whose identity is mapped to R2 but not R1 should get the
* inverse set of return values).
*
* @test_Strategy: Since test1 already verifies the functionality for
* isUserInRole returning true, this test needs only verify that it should
* return false for the other jsp. For this test, MGR and ADM are swapped, so
* isUserInRole() should return opposite values from test1.
*
* 1. Send request to access rolereverse.jsp 2. Receive login page 3. Send
* form response with username and password 4. Receive resource (check
* isUserInRole for all known roles)
*/
public void test6() throws Fault {
try {
// The first part of this test is used in test2 and test3 as
// well, so the code has been moved to a helper method.
requestAndGetLoginPage(pageRoleReverse, 6);
// Send response to login form with session id cookie:
request = pageSecurityCheck;
TestUtil.logMsg("Sending request \"" + request
+ "\" with login information (as " + username + ").");
Properties postData = new Properties();
postData.setProperty("j_username", username);
postData.setProperty("j_password", password);
response = WebUtil.sendRequest("POST", InetAddress.getByName(hostname),
portnum, tsurl.getRequest(request), postData, cookies);
// Check that the page was found (no error).
if (response.isError()) {
TestUtil.logErr("Could not find " + request);
throw new Fault("test6 failed.");
}
// Call followRedirect() to make sure we receive the required page
response = followRedirect(response, 6);
// Check to make sure we are authenticated by checking the page
// content. The jsp should output "The user principal is: j2ee"
String searchString = searchFor + username;
if (response.content.indexOf(searchString) == -1) {
TestUtil.logErr("User Principal incorrect. Page received:");
TestUtil.logErr(response.content);
TestUtil.logErr("(Should say: \"" + searchString + "\")");
throw new Fault("test6 failed.");
}
TestUtil.logMsg("User Principal correct.");
// Check to make sure isUserInRole is working properly:
Hashtable roleCheck = new Hashtable();
roleCheck.put("ADM", new Boolean(false));
roleCheck.put("MGR", new Boolean(true));
roleCheck.put("VP", new Boolean(false));
roleCheck.put("EMP", new Boolean(true));
// roleCheck.put( "Manager", new Boolean( false ) );
if (!checkRoles(response.content, roleCheck)) {
TestUtil.logErr("isUserInRole() does not work correctly.");
TestUtil.logErr("Page Received:");
TestUtil.logErr(response.content);
throw new Fault("test6 failed.");
}
TestUtil.logMsg("isUserInRole() correct.");
} catch (Exception e) {
TestUtil.logErr("Caught exception: " + e.getMessage());
e.printStackTrace();
throw new Fault("test6 failed: ", e);
}
}
/*
* testName: test7
*
* @assertion: Servlet Specification v2.3, sec 9.4 A special directory exists
* within the application hierarchy named WEB-INF. This directory contains all
* things related to the application that aren't in the document root of the
* application. It is important to note that the WEB-INF node is not part of
* the public document tree of the application. No file contained in the
* WEB-INF directory may be served directly to a client.
*
* @test_Strategy: 1) send a http request to WEB-INF directory 2) expect 404
* or 403 3) repeat step 1 and 2 for the following a) web-inf (for case
* insensitive platforms) b) WEB-INF/web.xml c) web-inf/web.xml 4) based on
* the http return code report test status
*/
public void test7() {
List<String> statusCodes;
try {
// Try accessing WEB-INF
request = pageBase + "/WEB-INF/";
statusCodes = new ArrayList<String>();
statusCodes.add("404");
statusCodes.add("403");
this.testStatusCodes(request, statusCodes, "test7");
// Try accessing /web-inf (for case insensitive platforms)
request = pageBase + "/web-inf/";
statusCodes = new ArrayList<String>();
statusCodes.add("404");
statusCodes.add("403");
this.testStatusCodes(request, statusCodes, "test7");
// Try accessing WEB-INF/web.xml
request = pageBase + "/WEB-INF/web.xml";
statusCodes = new ArrayList<String>();
statusCodes.add("404");
this.testStatusCodes(request, statusCodes, "test7");
// Try accessing web-inf/web.xml (for case insensitive platforms)
request = pageBase + "/web-inf/web.xml";
statusCodes = new ArrayList<String>();
statusCodes.add("404");
this.testStatusCodes(request, statusCodes, "test7");
// Try accessing WEB-INF/web.xml
request = pageBase + "/WEB-INF/web.xml";
statusCodes = new ArrayList<String>();
statusCodes.add("404");
this.testStatusCodes(request, statusCodes, "test7");
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* testName: test8
*
* @assertion: Servlet Specification v2.3, sec 9.5 Web applications can be
* packaged and signed, using the standard Java Archive tools, into a Web
* ARchive format (war) file. When packaged into such a form, a META-INF
* directory will be present which contains information useful to the Java
* Archive tools. If this directory is present, the servlet container must not
* allow it be served as content to a web client's request.
*
* @test_Strategy: 1) send a http request to META-INF directory 2) expect 404
* or a 403 3) repeat step 1 and 2 for the following a) meta-inf (for case
* insensitive platforms) b) META-INF/MANIFEST.MF c) meta-inf/manifest.mf 4)
* based on the http return code, report test status
*/
public void test8() throws Fault {
try {
// Try accessing META-INF
request = pageBase + "/META-INF/";
TestUtil.logMsg("Sending request \"" + request + "\"");
response = WebUtil.sendRequest("GET", InetAddress.getByName(hostname),
portnum, tsurl.getRequest(request), null, null);
// Call followRedirect() to make sure we receive the required page
response = followRedirect(response, 8);
// Receive "404" or "403" error code.
if (response.statusToken.equals("404")
|| response.statusToken.equals("403")) {
TestUtil.logMsg("Status Token " + response.statusToken);
TestUtil.logMsg("Received expected error code");
} else {
TestUtil.logErr("Did not receive expected error code" + request);
TestUtil.logMsg("Status Token " + response.statusToken);
TestUtil.logErr("Page content:");
TestUtil.logErr(response.content);
throw new Fault("test8 failed.");
}
// Try accessing /meta-inf (for case insensitive platforms)
request = pageBase + "/meta-inf/";
TestUtil.logMsg("Sending request \"" + request + "\"");
response = WebUtil.sendRequest("GET", InetAddress.getByName(hostname),
portnum, tsurl.getRequest(request), null, null);
// Call followRedirect() to make sure we receive the required page
response = followRedirect(response, 8);
// Receive "404" or "403" error code.
if (response.statusToken.equals("404")
|| response.statusToken.equals("403")) {
TestUtil.logMsg("Status Token " + response.statusToken);
TestUtil.logMsg("Received expected error code");
} else {
TestUtil.logErr("Did not receive expected error code" + request);
TestUtil.logMsg("Status Token " + response.statusToken);
TestUtil.logErr("Page content:");
TestUtil.logErr(response.content);
throw new Fault("test8 failed.");
}
// Try accessing META-INF/MANIFEST.MF
request = pageBase + "/META-INF/MANIFEST.MF";
TestUtil.logMsg("Sending request \"" + request + "\"");
response = WebUtil.sendRequest("GET", InetAddress.getByName(hostname),
portnum, tsurl.getRequest(request), null, null);
// Call followRedirect() to make sure we receive the required page
response = followRedirect(response, 8);
// Receive "404" or "403" error code.
if (response.statusToken.equals("404")
|| response.statusToken.equals("403")) {
TestUtil.logMsg("Status Token " + response.statusToken);
TestUtil.logMsg("Received expected error code");
} else {
TestUtil.logErr("Did not receive expected error code" + request);
TestUtil.logMsg("Status Token " + response.statusToken);
TestUtil.logErr("Page content:");
TestUtil.logErr(response.content);
throw new Fault("test8 failed.");
}
// Try accessing meta-inf/manifest.mf(for case insensitive platforms)
request = pageBase + "/meta-inf/manifest.mf";
TestUtil.logMsg("Sending request \"" + request + "\"");
response = WebUtil.sendRequest("GET", InetAddress.getByName(hostname),
portnum, tsurl.getRequest(request), null, null);
// Call followRedirect() to make sure we receive the required page
response = followRedirect(response, 8);
// Receive "404" or "403" error code.
if (response.statusToken.equals("404")
|| response.statusToken.equals("403")) {
TestUtil.logMsg("Status Token " + response.statusToken);
TestUtil.logMsg("Received expected error code");
} else {
TestUtil.logErr("Did not receive expected error code" + request);
TestUtil.logMsg("Status Token " + response.statusToken);
TestUtil.logErr("Page content:");
TestUtil.logErr(response.content);
throw new Fault("test8 failed.");
}
} catch (Exception e) {
TestUtil.logErr("Caught exception: " + e.getMessage());
e.printStackTrace();
throw new Fault("test8 failed: ", e);
}
}
/*
* testName: test9
*
* @assertion: URLMapping from Servlet Specification v2.3, sec 11.2
*
* 1) A string beginning with a / character and ending with a /* postfix is
* used as a path mapping. 2) A string beginning with a *. prefix is used as
* an extension mapping. 3) All other strings are used as exact matches only
* 4) A string containing only the / character indicates that servlet
* specified by the mapping becomes the "default" servlet of the application.
* In this case the servlet path is the request URI minus the context path and
* the path info is null.
*
*
* @test_Strategy: 1) Deploy a two webcomponents One.jsp and Two.jsp
* exercising various mapping rules 2) Make a http request with a URL(based on
* the above mapping rules) 3) Make a http request with a absolute match URL.
* 4) compare the results obtained through step 2 and 3 and declare test
* result
*
*/
public void test9() throws Fault {
try {
String testURL = null;
String exactMatchURL = null;
// This tests exercises the URL mapping rules
// See compareURLContents() method description for more info
// Note: pageOne can be a JSP or Servlet
// 1) for JSP
// pageOne = pageBase + "/One.jsp";
// 2) for servlet
// pageOne = pageBase + "/OneTest";
// Try accessing pageOne using "/One/index.html"
testURL = pageBase + "/One/index.html";
exactMatchURL = pageOne;
compareURLContents(testURL, 9, exactMatchURL);
// Try accessing pageOne using "/One/*"
testURL = pageBase + "/One/*";
exactMatchURL = pageOne;
compareURLContents(testURL, 9, exactMatchURL);
// Note: pageTwo can be a JSP or Servlet
// 1) for JSP