KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > security > SecuredBrokerTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.security;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.jbi.messaging.InOnly;
26 import javax.security.auth.Subject JavaDoc;
27 import javax.security.auth.callback.Callback JavaDoc;
28 import javax.security.auth.callback.CallbackHandler JavaDoc;
29 import javax.security.auth.callback.NameCallback JavaDoc;
30 import javax.security.auth.callback.PasswordCallback JavaDoc;
31 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
32 import javax.security.auth.login.LoginContext JavaDoc;
33
34 import junit.framework.TestCase;
35
36 import org.apache.servicemix.client.DefaultServiceMixClient;
37 import org.apache.servicemix.client.ServiceMixClient;
38 import org.apache.servicemix.jbi.container.JBIContainer;
39 import org.apache.servicemix.jbi.jaxp.StringSource;
40 import org.apache.servicemix.jbi.security.acl.AuthorizationMap;
41 import org.apache.servicemix.jbi.security.acl.impl.AuthorizationEntry;
42 import org.apache.servicemix.jbi.security.acl.impl.DefaultAuthorizationMap;
43 import org.apache.servicemix.tck.ReceiverComponent;
44
45 public class SecuredBrokerTest extends TestCase {
46
47     static {
48         String JavaDoc path = System.getProperty("java.security.auth.login.config");
49         if (path == null) {
50             URL JavaDoc resource = PropertiesLoginModuleTest.class.getResource("login.properties");
51             if (resource != null) {
52                 path = new File JavaDoc(resource.getFile()).getAbsolutePath();
53                 System.setProperty("java.security.auth.login.config", path);
54             }
55         }
56         System.out.println("Path to login config: " + path);
57     }
58
59     protected JBIContainer jbi;
60     protected ReceiverComponent receiver;
61     
62     protected void setUp() throws Exception JavaDoc {
63         jbi = new JBIContainer();
64         jbi.setUseMBeanServer(false);
65         jbi.setEmbedded(true);
66         List JavaDoc entries = new ArrayList JavaDoc();
67         entries.add(new AuthorizationEntry(ReceiverComponent.SERVICE, null, "programmers"));
68         AuthorizationMap map = new DefaultAuthorizationMap(entries);
69         SecuredBroker broker = new SecuredBroker(map);
70         jbi.setBroker(broker);
71         jbi.init();
72         
73         receiver = new ReceiverComponent();
74         jbi.activateComponent(receiver, "receiver");
75
76         jbi.start();
77     }
78     
79     protected void tearDown() throws Exception JavaDoc {
80         jbi.shutDown();
81     }
82     
83     protected Subject JavaDoc login(final String JavaDoc username, final String JavaDoc password) throws Exception JavaDoc {
84         LoginContext JavaDoc context = new LoginContext JavaDoc("servicemix-domain", new CallbackHandler JavaDoc() {
85             public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
86                 for (int i = 0; i < callbacks.length; i++) {
87                     if (callbacks[i] instanceof NameCallback JavaDoc) {
88                         ((NameCallback JavaDoc) callbacks[i]).setName(username);
89                     } else if (callbacks[i] instanceof PasswordCallback JavaDoc) {
90                         ((PasswordCallback JavaDoc) callbacks[i]).setPassword(password.toCharArray());
91                     } else {
92                         throw new UnsupportedCallbackException JavaDoc(callbacks[i]);
93                     }
94                 }
95             }
96         });
97         context.login();
98         return context.getSubject();
99     }
100     
101     public void testOk() throws Exception JavaDoc {
102         Subject JavaDoc subject = login("first", "secret");
103         ServiceMixClient client = new DefaultServiceMixClient(jbi);
104         InOnly me = client.createInOnlyExchange();
105         me.setService(ReceiverComponent.SERVICE);
106         me.getInMessage().setSecuritySubject(subject);
107         me.getInMessage().setContent(new StringSource("<hello>world</hello>"));
108         client.sendSync(me);
109         
110         receiver.getMessageList().assertMessagesReceived(1);
111     }
112     
113     public void testNOk() throws Exception JavaDoc {
114         Subject JavaDoc subject = login("second", "password");
115         ServiceMixClient client = new DefaultServiceMixClient(jbi);
116         InOnly me = client.createInOnlyExchange();
117         me.setService(ReceiverComponent.SERVICE);
118         me.getInMessage().setSecuritySubject(subject);
119         me.getInMessage().setContent(new StringSource("<hello>world</hello>"));
120         try {
121             client.sendSync(me);
122             fail("Should have thrown a SecurityException");
123         } catch (SecurityException JavaDoc e) {
124             // ok
125
}
126     }
127     
128 }
129
Popular Tags