1 package org.xmlBlaster.test.authentication;
2
3 import java.util.logging.Logger;
4 import org.xmlBlaster.util.Global;
5 import org.xmlBlaster.util.EmbeddedXmlBlaster;
6 import org.xmlBlaster.util.FileLocator;
7 import org.xmlBlaster.util.MsgUnit;
8 import org.xmlBlaster.util.XmlBlasterException;
9 import org.xmlBlaster.client.key.UpdateKey;
10 import org.xmlBlaster.client.qos.ConnectQos;
11 import org.xmlBlaster.client.qos.UpdateQos;
12 import org.xmlBlaster.client.I_Callback;
13 import org.xmlBlaster.client.I_XmlBlasterAccess;
14
15 import org.xmlBlaster.test.Util;
16
17 import junit.framework.*;
18
19 public class TestHtpasswdAuthorization extends TestCase implements I_Callback {
20 private static Logger log = Logger.getLogger(TestHtpasswdAuthorization.class
21 .getName());
22
23 private EmbeddedXmlBlaster serverThread = null;
24
25 private final String RIGHT_PASSWORD = "secret";
26
27 private String userhome;
28
29 private String passwdFileName;
30
31 private Global glob;
32
33 private I_XmlBlasterAccess con;
34
35 private int serverPort = 7604;
36
37 public final String ME = "TestAuthenticationHtPassWd";
38
39 public TestHtpasswdAuthorization(String name) {
40 super(name);
41 this.glob = new Global();
42
43 this.userhome = glob.getProperty().get("user.home", "") + java.io.File.separatorChar;
44 this.passwdFileName = this.userhome + "testAuthorize.htpasswd";
45
46 try {
47 // [some1] => [yZ6mkNGKZEgDc]
48 // [secret] => [yZ24stvIel1j6]
49 FileLocator.writeFile(this.passwdFileName,
50 "*:yZ6mkNGKZEgDc\n" +
51 "guest:yZ24stvIel1j6:connect,disconnect,publish(tennis;sailing;jogging),subscribe(surfing),unSubscribe(surfing),erase(tennis)\n" +
52 "snoopy:yZ24stvIel1j6:subscribe(\"exact:tennis\";\"domain:sport\";\"xpath:/xmlBlaster/key[starts-with(@oid,'sport.')]\")\n" +
53 "admin:yZ24stvIel1j6:!erase\n" +
54 "other:yZ24stvIel1j6:! subscribe,unSubscribe\n" +
55 "weird:yZ24stvIel1j6:connect,disconnect,subscribe,subscribe(),()\n" +
56 "strange:yZ24stvIel1j6:someStrangeMethod\n" +
57 "all:yZ24stvIel1j6::\n" +
58 "__sys__jdbc:yZ24stvIel1j6\n");
59 } catch (Exception ex) {
60 assertTrue("Could not create password file '" + this.passwdFileName
61 + "'. Tests won't work!", false);
62 }
63 }
64
65 protected void setUp() {
66 String[] ports = Util.getOtherServerPorts(serverPort);
67 String[] args = new String[4 + ports.length];
68 args[0] = "-Security.Server.Plugin.htpasswd.secretfile";
69 args[1] = this.passwdFileName;
70 args[2] = "-Security.Server.Plugin.htpasswd.allowPartialUsername";
71 args[3] = "false";
72 for (int i = 0; i < ports.length; i++) {
73 args[i + 4] = ports[i];
74 }
75 glob.init(args);
76 serverThread = EmbeddedXmlBlaster.startXmlBlaster(glob);
77 }
78
79 protected void tearDown() {
80 try {
81 Thread.sleep(1000);
82 } catch (Exception ex) {}
83 if (serverThread != null)
84 serverThread.stopServer(true);
85 glob.init(Util.getDefaultServerPorts());
86 Util.resetPorts(glob);
87 this.glob = null;
88 this.con = null;
89 Global.instance().shutdown();
90 }
91
92 public void testWildcardAuthenticationOK() {
93 log.info("testWildcardAuthentication()");
94 try {
95 con = glob.getXmlBlasterAccess();
96 ConnectQos qos = new ConnectQos(glob, "unknown", "some1");
97 con.connect(qos, this);
98 con.publish(new MsgUnit("<key oid='Hello'/>", "hi".getBytes(), "<qos/>"));
99 con.subscribe("<key oid='Hello'/>", "<qos/>");
100 con.unSubscribe("<key oid='Hello'/>", "<qos/>");
101 con.erase("<key oid='Hello'/>", "<qos/>");
102 con.disconnect(null);
103 } catch (XmlBlasterException ex) {
104 fail("Could not connect: " + ex.toString());
105 }
106 }
107
108 public void testWildcardAuthenticationFailed() {
109 log.info("testWildcardAuthentication()");
110 try {
111 con = glob.getXmlBlasterAccess();
112 ConnectQos qos = new ConnectQos(glob, "unknown2", "some1Wrong");
113 con.connect(qos, this);
114 fail("Should not connect");
115 } catch (XmlBlasterException ex) {
116 log.info("Success, expected an exception: " + ex.toString());
117 }
118 }
119
120 public void testMethodNameAuthorization() {
121 log.info("testMethodNameAuthorization()");
122 try {
123 con = glob.getXmlBlasterAccess();
124 ConnectQos qos = new ConnectQos(glob, "admin", RIGHT_PASSWORD);
125 con.connect(qos, this);
126 con.publish(new MsgUnit("<key oid='Hello'/>", "hi".getBytes(), "<qos/>"));
127 con.subscribe("<key oid='Hello'/>", "<qos/>");
128 con.unSubscribe("<key oid='Hello'/>", "<qos/>");
129 try {
130 con.erase("<key oid='Hello'/>", "<qos/>");
131 fail("Expected to get a authorization exception for erase() invocation");
132 }
133 catch (XmlBlasterException e) {
134 log.info("OK, expected this exception: " + e.getMessage());
135 }
136 con.disconnect(null);
137 } catch (XmlBlasterException ex) {
138 fail("Could not connect: " + ex.toString());
139 }
140 }
141
142 public void testMethodNameAuthorizationNegation() {
143 log.info("testMethodNameAuthorizationNegation()");
144 try {
145 con = glob.getXmlBlasterAccess();
146 ConnectQos qos = new ConnectQos(glob, "other", RIGHT_PASSWORD);
147 con.connect(qos, this);
148 con.publish(new MsgUnit("<key oid='Hello'/>", "hi".getBytes(), "<qos/>"));
149 try {
150 con.subscribe("<key oid='Hello'/>", "<qos/>");
151 fail("Expected to get a authorization exception for subscribe() invocation");
152 }
153 catch (XmlBlasterException e) {
154 log.info("OK, expected this exception: " + e.getMessage());
155 }
156 try {
157 con.unSubscribe("<key oid='Hello'/>", "<qos/>");
158 fail("Expected to get a authorization exception for unSubscribe() invocation");
159 }
160 catch (XmlBlasterException e) {
161 log.info("OK, expected this exception: " + e.getMessage());
162 }
163 con.erase("<key oid='Hello'/>", "<qos/>");
164 con.disconnect(null);
165 } catch (XmlBlasterException ex) {
166 fail("Could not connect: " + ex.toString());
167 }
168 }
169
170 public void testTopicAuthorization() {
171 log.info("testTopicAuthorization()");
172 try {
173 con = glob.getXmlBlasterAccess();
174 ConnectQos qos = new ConnectQos(glob, "guest", RIGHT_PASSWORD);
175 con.connect(qos, this);
176
177 con.publish(new MsgUnit("<key oid='tennis'/>", "hi".getBytes(), "<qos/>"));
178 con.publish(new MsgUnit("<key oid='sailing'/>", "hi".getBytes(), "<qos/>"));
179 try {
180 con.publish(new MsgUnit("<key oid='Hello'/>", "hi".getBytes(), "<qos/>"));
181 fail("Expected to get a authorization exception for illgeal topic publish() invocation");
182 }
183 catch (XmlBlasterException e) {
184 log.info("OK, expected this exception: " + e.getMessage());
185 }
186
187 con.subscribe("<key oid='surfing'/>", "<qos/>");
188 try {
189 con.subscribe("<key oid='Hello'/>", "<qos/>");
190 fail("Expected to get a authorization exception for subscribe() invocation");
191 }
192 catch (XmlBlasterException e) {
193 log.info("OK, expected this exception: " + e.getMessage());
194 }
195
196 con.unSubscribe("<key oid='surfing'/>", "<qos/>");
197 try {
198 con.unSubscribe("<key oid='Hello'/>", "<qos/>");
199 fail("Expected to get a authorization exception for unSubscribe() invocation");
200 }
201 catch (XmlBlasterException e) {
202 log.info("OK, expected this exception: " + e.getMessage());
203 }
204
205 con.erase("<key oid='tennis'/>", "<qos/>");
206 try {
207 con.erase("<key oid='Hello'/>", "<qos/>");
208 fail("Expected to get a authorization exception for erase() invocation");
209 }
210 catch (XmlBlasterException e) {
211 log.info("OK, expected this exception: " + e.getMessage());
212 }
213 con.disconnect(null);
214 } catch (XmlBlasterException ex) {
215 fail("Could not connect: " + ex.toString());
216 }
217 }
218
219 public void testXPathSubscribeAuthorization() {
220 log.info("testXPathSubscribeAuthorization()");
221 try {
222 con = glob.getXmlBlasterAccess();
223 ConnectQos qos = new ConnectQos(glob, "snoopy", RIGHT_PASSWORD);
224 con.connect(qos, this);
225
226 con.subscribe("<key oid='' queryType='XPATH'>/xmlBlaster/key[starts-with(@oid,'sport.')]</key>", "<qos/>");
227 try {
228 con.subscribe("<key oid='' queryType='XPATH'>/xmlBlaster/key[starts-with(@oid,'sportX.')]</key>",
229 "<qos/>");
230 fail("Expected to get a authorization exception for subscribe() invocation");
231 }
232 catch (XmlBlasterException e) {
233 log.info("OK, expected this exception: " + e.getMessage());
234 }
235
236 con.disconnect(null);
237 } catch (XmlBlasterException ex) {
238 fail("Could not connect: " + ex.toString());
239 }
240 }
241
242 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) throws XmlBlasterException {
243 log.info("Receiving callback message: " + updateKey.getOid());
244 return "";
245 }
246 }
syntax highlighted by Code2HTML, v. 0.9.1