2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
22
package org.jboss.test.remoting.marshall.encrypt;
24
import java.io.ByteArrayInputStream;
25
import java.io.ByteArrayOutputStream;
26
import java.io.IOException;
28
import org.jboss.remoting.marshal.MarshalFactory;
29
import org.jboss.remoting.marshal.Marshaller;
30
import org.jboss.remoting.marshal.UnMarshaller;
31
import org.jboss.remoting.marshal.encryption.EncryptingMarshaller;
32
import org.jboss.remoting.marshal.encryption.EncryptingUnMarshaller;
33
import org.jboss.remoting.marshal.http.HTTPMarshaller;
34
import org.jboss.remoting.marshal.http.HTTPUnMarshaller;
35
import org.jboss.remoting.marshal.serializable.SerializableMarshaller;
36
import org.jboss.remoting.marshal.serializable.SerializableUnMarshaller;
38
import junit.framework.TestCase;
40
//$Id: EncryptionStandaloneTest.java 3560 2008-03-02 06:22:43Z ron.sigal@jboss.com $
43
* Tests Remoting Encryption facilities
44
* @author <a href="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
46
* @version $Revision: 3560 $
48
public class EncryptionStandaloneTest extends TestCase
50
private Marshaller marshaller;
51
private UnMarshaller unmarshaller;
53
private String[] standard = new String[]
54
{null, "AES", "DES", "Blowfish", "DESede"};
56
private String[] padded = new String[]
57
{"AES/CBC/PKCS5Padding", "AES/PCBC/PKCS5Padding","AES/PCBC/PKCS5Padding",
58
"AES/CFB/PKCS5Padding", "AES/OFB/PKCS5Padding",
59
"DES/CBC/PKCS5Padding", "DES/PCBC/PKCS5Padding","DES/PCBC/PKCS5Padding",
60
"DES/CFB/PKCS5Padding", "DES/OFB/PKCS5Padding",
61
"DESede/CBC/PKCS5Padding", "DESede/PCBC/PKCS5Padding","DESede/PCBC/PKCS5Padding",
62
"DESede/CFB/PKCS5Padding", "DESede/OFB/PKCS5Padding"};
64
private String[] unpadded = new String[]
65
{"AES/CBC/NoPadding", "AES/PCBC/NoPadding","AES/PCBC/NoPadding",
66
"AES/CFB/NoPadding", "AES/OFB/NoPadding",
67
"DES/CBC/NoPadding", "DES/PCBC/NoPadding","DES/PCBC/NoPadding",
68
"DES/CFB/NoPadding", "DES/OFB/NoPadding",
69
"DESede/CBC/NoPadding", "DESede/PCBC/NoPadding","DESede/PCBC/NoPadding",
70
"DESede/CFB/NoPadding", "DESede/OFB/NoPadding"};
73
public void testSerializable() throws IOException, ClassNotFoundException
75
for(int i = 0 ; i < standard.length; i++)
76
runAlgoTest(standard[i]);
77
for(int i = 0 ; i < padded.length; i++)
78
runAlgoTest(padded[i]);
79
for(int i = 0 ; i < unpadded.length; i++)
80
runAlgoTest(unpadded[i]);
84
public void testWrappedSerializable() throws IOException, ClassNotFoundException
86
for(int i = 0 ; i < standard.length; i++)
87
runWrappedSerializableTest(standard[i]);
88
for(int i = 0 ; i < padded.length; i++)
89
runWrappedSerializableTest(padded[i]);
90
for(int i = 0 ; i < unpadded.length; i++)
91
runWrappedSerializableTest(unpadded[i]);
95
* Commented out usecases not supported
97
* @throws ClassNotFoundException
99
public void testHTTP() throws IOException, ClassNotFoundException
101
for(int i = 0 ; i < standard.length; i++)
102
runHttpTest(standard[i]);
103
for(int i = 0 ; i < padded.length; i++)
104
runHttpTest(padded[i]);
105
//NoPadding is not correctly supported by HttpUnMarshaller
106
/*for(int i = 0 ; i < unpadded.length; i++)
107
runHttpTest(unpadded[i]); */
111
protected void runOneTest() throws IOException, ClassNotFoundException
113
String testData = "This is some test data";
114
Object param = new String(testData);
116
ByteArrayOutputStream output = new ByteArrayOutputStream();
117
marshaller.write(param, output);
118
byte[] byteArray = new byte[output.size()];
119
byteArray = output.toByteArray();
120
ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
121
Object result = unmarshaller.read(input, null);
123
System.out.println("Result: " + result);
124
assertEquals(testData, result);
127
private void runAlgoTest(String algo)
128
throws IOException, ClassNotFoundException
130
EncryptingMarshaller em = new EncryptingMarshaller();
131
EncryptingUnMarshaller um = new EncryptingUnMarshaller();
134
em.setCipherAlgorithm(algo);
135
um.setCipherAlgorithm(algo);
137
MarshalFactory.addMarshaller(EncryptingMarshaller.DATATYPE,em, um);
139
marshaller = MarshalFactory.getMarshaller(EncryptingMarshaller.DATATYPE);
140
unmarshaller = MarshalFactory.getUnMarshaller(EncryptingMarshaller.DATATYPE);
144
private void runHttpTest(String algo)
145
throws IOException, ClassNotFoundException
147
String datatype = "encryptedHTTP";
148
Marshaller m = MarshalFactory.getMarshaller(HTTPMarshaller.DATATYPE);
149
UnMarshaller u = MarshalFactory.getUnMarshaller(HTTPUnMarshaller.DATATYPE);
150
EncryptingMarshaller em = new EncryptingMarshaller(m);
151
EncryptingUnMarshaller um = new EncryptingUnMarshaller(u);
154
em.setCipherAlgorithm(algo);
155
um.setCipherAlgorithm(algo);
157
MarshalFactory.addMarshaller(datatype,em, um);
158
marshaller = MarshalFactory.getMarshaller(datatype);
159
unmarshaller = MarshalFactory.getUnMarshaller(datatype);
163
private void runWrappedSerializableTest(String algo)
164
throws IOException, ClassNotFoundException
166
String datatype = "encryptedSerializable";
167
String sd = SerializableMarshaller.DATATYPE;
168
String sud = SerializableUnMarshaller.DATATYPE;
169
EncryptingMarshaller em =
170
new EncryptingMarshaller(MarshalFactory.getMarshaller(sd));
171
EncryptingUnMarshaller um =
172
new EncryptingUnMarshaller(MarshalFactory.getUnMarshaller(sud));
175
em.setCipherAlgorithm(algo);
176
um.setCipherAlgorithm(algo);
178
MarshalFactory.addMarshaller(datatype,em, um);
180
marshaller = MarshalFactory.getMarshaller(datatype);
181
unmarshaller = MarshalFactory.getUnMarshaller(datatype);