94 |
07 Mar 05 |
nicklas |
1 |
/* |
94 |
07 Mar 05 |
nicklas |
$Id$ |
94 |
07 Mar 05 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2005 Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg |
94 |
07 Mar 05 |
nicklas |
6 |
|
2304 |
22 May 06 |
jari |
This file is part of BASE - BioArray Software Environment. |
2304 |
22 May 06 |
jari |
Available at http://base.thep.lu.se/ |
94 |
07 Mar 05 |
nicklas |
9 |
|
94 |
07 Mar 05 |
nicklas |
BASE is free software; you can redistribute it and/or |
94 |
07 Mar 05 |
nicklas |
modify it under the terms of the GNU General Public License |
4480 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
94 |
07 Mar 05 |
nicklas |
of the License, or (at your option) any later version. |
94 |
07 Mar 05 |
nicklas |
14 |
|
94 |
07 Mar 05 |
nicklas |
BASE is distributed in the hope that it will be useful, |
94 |
07 Mar 05 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
94 |
07 Mar 05 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
94 |
07 Mar 05 |
nicklas |
GNU General Public License for more details. |
94 |
07 Mar 05 |
nicklas |
19 |
|
94 |
07 Mar 05 |
nicklas |
You should have received a copy of the GNU General Public License |
4514 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
94 |
07 Mar 05 |
nicklas |
22 |
*/ |
94 |
07 Mar 05 |
nicklas |
23 |
import net.sf.basedb.core.*; |
94 |
07 Mar 05 |
nicklas |
24 |
|
94 |
07 Mar 05 |
nicklas |
25 |
public class TestSessionControl |
94 |
07 Mar 05 |
nicklas |
26 |
{ |
94 |
07 Mar 05 |
nicklas |
27 |
|
94 |
07 Mar 05 |
nicklas |
28 |
static boolean ok = true; |
94 |
07 Mar 05 |
nicklas |
29 |
public static void main(String[] args) |
94 |
07 Mar 05 |
nicklas |
30 |
{ |
94 |
07 Mar 05 |
nicklas |
31 |
TestUtil.checkArgs(args); |
94 |
07 Mar 05 |
nicklas |
32 |
TestUtil.begin(); |
94 |
07 Mar 05 |
nicklas |
33 |
ok = test_all(); |
94 |
07 Mar 05 |
nicklas |
34 |
TestUtil.stop(); |
94 |
07 Mar 05 |
nicklas |
35 |
} |
94 |
07 Mar 05 |
nicklas |
36 |
|
94 |
07 Mar 05 |
nicklas |
37 |
static boolean test_all() |
94 |
07 Mar 05 |
nicklas |
38 |
{ |
94 |
07 Mar 05 |
nicklas |
39 |
write("++Testing SessionControl"); |
94 |
07 Mar 05 |
nicklas |
40 |
test_get_session_control(TestUtil.getLocalIp(), false); |
94 |
07 Mar 05 |
nicklas |
41 |
test_get_session_control("unknown.ip.address", true); |
222 |
23 Mar 05 |
nicklas |
42 |
test_user_default_setting("test.default", "This is the users default setting"); |
222 |
23 Mar 05 |
nicklas |
43 |
test_user_client_setting("test.client", "This is the users client setting"); |
266 |
29 Mar 05 |
nicklas |
44 |
int userId = TestUser.test_create(true); |
266 |
29 Mar 05 |
nicklas |
45 |
test_impersonate(userId); |
266 |
29 Mar 05 |
nicklas |
46 |
TestUser.test_delete(userId); |
94 |
07 Mar 05 |
nicklas |
47 |
write("++Testing SessionControl "+(ok ? "OK" : "Failed")+"\n"); |
94 |
07 Mar 05 |
nicklas |
48 |
return ok; |
94 |
07 Mar 05 |
nicklas |
49 |
} |
94 |
07 Mar 05 |
nicklas |
50 |
|
94 |
07 Mar 05 |
nicklas |
51 |
static void test_get_session_control(String remoteId, boolean shouldFail) |
94 |
07 Mar 05 |
nicklas |
52 |
{ |
94 |
07 Mar 05 |
nicklas |
53 |
SessionControl sc = TestUtil.getSessionControl(); |
94 |
07 Mar 05 |
nicklas |
54 |
try |
94 |
07 Mar 05 |
nicklas |
55 |
{ |
7159 |
26 May 16 |
nicklas |
56 |
Application.getSessionControl(sc.getId(), sc.getExternalClientId(), remoteId, false); |
94 |
07 Mar 05 |
nicklas |
57 |
if (shouldFail) |
94 |
07 Mar 05 |
nicklas |
58 |
{ |
94 |
07 Mar 05 |
nicklas |
59 |
write("--Get SessionControl FAILED (Expected exception)"); |
94 |
07 Mar 05 |
nicklas |
60 |
ok = false; |
94 |
07 Mar 05 |
nicklas |
61 |
} |
94 |
07 Mar 05 |
nicklas |
62 |
else |
94 |
07 Mar 05 |
nicklas |
63 |
{ |
94 |
07 Mar 05 |
nicklas |
64 |
write("--Get SessionControl OK"); |
94 |
07 Mar 05 |
nicklas |
65 |
} |
94 |
07 Mar 05 |
nicklas |
66 |
} |
358 |
04 Apr 05 |
nicklas |
67 |
catch (Throwable ex) |
94 |
07 Mar 05 |
nicklas |
68 |
{ |
94 |
07 Mar 05 |
nicklas |
69 |
if (shouldFail) |
94 |
07 Mar 05 |
nicklas |
70 |
{ |
94 |
07 Mar 05 |
nicklas |
71 |
write("--Get SessionControl OK (Expected exception: "+ex.getMessage()); |
94 |
07 Mar 05 |
nicklas |
72 |
} |
94 |
07 Mar 05 |
nicklas |
73 |
else |
94 |
07 Mar 05 |
nicklas |
74 |
{ |
94 |
07 Mar 05 |
nicklas |
75 |
write("--Get SessionControl FAILED"); |
94 |
07 Mar 05 |
nicklas |
76 |
ex.printStackTrace(); |
94 |
07 Mar 05 |
nicklas |
77 |
ok = false; |
94 |
07 Mar 05 |
nicklas |
78 |
} |
94 |
07 Mar 05 |
nicklas |
79 |
} |
94 |
07 Mar 05 |
nicklas |
80 |
} |
222 |
23 Mar 05 |
nicklas |
81 |
|
222 |
23 Mar 05 |
nicklas |
82 |
static void test_user_default_setting(String name, String value) |
222 |
23 Mar 05 |
nicklas |
83 |
{ |
222 |
23 Mar 05 |
nicklas |
84 |
SessionControl sc = TestUtil.getSessionControl(); |
222 |
23 Mar 05 |
nicklas |
85 |
try |
222 |
23 Mar 05 |
nicklas |
86 |
{ |
222 |
23 Mar 05 |
nicklas |
87 |
String oldValue = sc.getUserDefaultSetting(name); |
222 |
23 Mar 05 |
nicklas |
88 |
if (!TestUtil.getSilent()) write("Setting "+name+"="+oldValue); |
222 |
23 Mar 05 |
nicklas |
89 |
sc.setUserDefaultSetting(name, value); |
222 |
23 Mar 05 |
nicklas |
90 |
if (!TestUtil.getSilent()) write("Setting "+name+"="+sc.getUserDefaultSetting(name)); |
222 |
23 Mar 05 |
nicklas |
91 |
TestUtil.logout(); |
222 |
23 Mar 05 |
nicklas |
92 |
TestUtil.login(); |
222 |
23 Mar 05 |
nicklas |
93 |
String newValue = sc.getUserDefaultSetting(name); |
222 |
23 Mar 05 |
nicklas |
94 |
if (!TestUtil.getSilent()) write("Setting "+name+"="+newValue); |
222 |
23 Mar 05 |
nicklas |
95 |
if (!value.equals(newValue)) throw new BaseException("New value != value"); |
222 |
23 Mar 05 |
nicklas |
96 |
sc.setUserDefaultSetting(name, oldValue); |
222 |
23 Mar 05 |
nicklas |
97 |
write("--Test user default setting OK"); |
222 |
23 Mar 05 |
nicklas |
98 |
} |
358 |
04 Apr 05 |
nicklas |
99 |
catch (Throwable ex) |
222 |
23 Mar 05 |
nicklas |
100 |
{ |
222 |
23 Mar 05 |
nicklas |
101 |
write("--Test user default setting FAILED"); |
222 |
23 Mar 05 |
nicklas |
102 |
ex.printStackTrace(); |
222 |
23 Mar 05 |
nicklas |
103 |
ok = false; |
222 |
23 Mar 05 |
nicklas |
104 |
} |
222 |
23 Mar 05 |
nicklas |
105 |
} |
222 |
23 Mar 05 |
nicklas |
106 |
|
222 |
23 Mar 05 |
nicklas |
107 |
static void test_user_client_setting(String name, String value) |
222 |
23 Mar 05 |
nicklas |
108 |
{ |
222 |
23 Mar 05 |
nicklas |
109 |
SessionControl sc = TestUtil.getSessionControl(); |
222 |
23 Mar 05 |
nicklas |
110 |
try |
222 |
23 Mar 05 |
nicklas |
111 |
{ |
222 |
23 Mar 05 |
nicklas |
112 |
String oldValue = sc.getUserClientSetting(name); |
222 |
23 Mar 05 |
nicklas |
113 |
if (!TestUtil.getSilent()) write("Setting "+name+"="+oldValue); |
222 |
23 Mar 05 |
nicklas |
114 |
sc.setUserClientSetting(name, value); |
222 |
23 Mar 05 |
nicklas |
115 |
if (!TestUtil.getSilent()) write("Setting "+name+"="+sc.getUserClientSetting(name)); |
222 |
23 Mar 05 |
nicklas |
116 |
TestUtil.logout(); |
222 |
23 Mar 05 |
nicklas |
117 |
TestUtil.login(); |
222 |
23 Mar 05 |
nicklas |
118 |
String newValue = sc.getUserClientSetting(name); |
222 |
23 Mar 05 |
nicklas |
119 |
if (!TestUtil.getSilent()) write("Setting "+name+"="+newValue); |
222 |
23 Mar 05 |
nicklas |
120 |
if (!value.equals(newValue)) throw new BaseException("New value != value"); |
222 |
23 Mar 05 |
nicklas |
121 |
sc.setUserClientSetting(name, oldValue); |
222 |
23 Mar 05 |
nicklas |
122 |
write("--Test user client setting OK"); |
222 |
23 Mar 05 |
nicklas |
123 |
} |
358 |
04 Apr 05 |
nicklas |
124 |
catch (Throwable ex) |
222 |
23 Mar 05 |
nicklas |
125 |
{ |
222 |
23 Mar 05 |
nicklas |
126 |
write("--Test user client setting FAILED"); |
222 |
23 Mar 05 |
nicklas |
127 |
ex.printStackTrace(); |
222 |
23 Mar 05 |
nicklas |
128 |
ok = false; |
222 |
23 Mar 05 |
nicklas |
129 |
} |
222 |
23 Mar 05 |
nicklas |
130 |
} |
94 |
07 Mar 05 |
nicklas |
131 |
|
266 |
29 Mar 05 |
nicklas |
132 |
static void test_impersonate(int userId) |
266 |
29 Mar 05 |
nicklas |
133 |
{ |
266 |
29 Mar 05 |
nicklas |
134 |
if (userId == 0) return; |
266 |
29 Mar 05 |
nicklas |
135 |
SessionControl sc = TestUtil.getSessionControl(); |
266 |
29 Mar 05 |
nicklas |
136 |
try |
266 |
29 Mar 05 |
nicklas |
137 |
{ |
1631 |
18 Nov 05 |
nicklas |
138 |
SessionControl impersonated = sc.impersonateLogin(userId, "Testing impersonation"); |
1631 |
18 Nov 05 |
nicklas |
139 |
if (!TestUtil.getSilent()) write("We are now impersonated. Logged in as: " |
1631 |
18 Nov 05 |
nicklas |
140 |
+ impersonated.getLoggedInUserId() + "; impersonated = " + impersonated.isImpersonated()); |
1631 |
18 Nov 05 |
nicklas |
141 |
impersonated.logout(); |
266 |
29 Mar 05 |
nicklas |
142 |
write("--Impersonate OK"); |
266 |
29 Mar 05 |
nicklas |
143 |
} |
358 |
04 Apr 05 |
nicklas |
144 |
catch (Throwable ex) |
266 |
29 Mar 05 |
nicklas |
145 |
{ |
266 |
29 Mar 05 |
nicklas |
146 |
write("--Impersonate FAILED"); |
266 |
29 Mar 05 |
nicklas |
147 |
ex.printStackTrace(); |
266 |
29 Mar 05 |
nicklas |
148 |
ok = false; |
266 |
29 Mar 05 |
nicklas |
149 |
} |
266 |
29 Mar 05 |
nicklas |
150 |
} |
94 |
07 Mar 05 |
nicklas |
151 |
|
94 |
07 Mar 05 |
nicklas |
152 |
static void write(String message) |
94 |
07 Mar 05 |
nicklas |
153 |
{ |
94 |
07 Mar 05 |
nicklas |
154 |
System.out.println(message); |
94 |
07 Mar 05 |
nicklas |
155 |
} |
94 |
07 Mar 05 |
nicklas |
156 |
|
94 |
07 Mar 05 |
nicklas |
157 |
} |
94 |
07 Mar 05 |
nicklas |
158 |
|