~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/marshall/encrypt/EncryptionStandaloneTest.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
  *
 
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.
 
11
  *
 
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.
 
16
  *
 
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.
 
21
  */
 
22
package org.jboss.test.remoting.marshall.encrypt;
 
23
 
 
24
import java.io.ByteArrayInputStream;
 
25
import java.io.ByteArrayOutputStream;
 
26
import java.io.IOException;
 
27
 
 
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;
 
37
 
 
38
import junit.framework.TestCase;
 
39
 
 
40
//$Id: EncryptionStandaloneTest.java 3560 2008-03-02 06:22:43Z ron.sigal@jboss.com $
 
41
 
 
42
/**
 
43
 *  Tests Remoting Encryption facilities
 
44
 *  @author <a href="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
 
45
 *  @since  Aug 16, 2006 
 
46
 *  @version $Revision: 3560 $
 
47
 */
 
48
public class EncryptionStandaloneTest extends TestCase
 
49
{
 
50
   private Marshaller marshaller;
 
51
   private UnMarshaller unmarshaller;
 
52
   
 
53
   private String[] standard = new String[]
 
54
        {null, "AES", "DES", "Blowfish", "DESede"};
 
55
   
 
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"};
 
63
   
 
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"}; 
 
71
 
 
72
   
 
73
   public void testSerializable() throws IOException, ClassNotFoundException
 
74
   {  
 
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]); 
 
81
   }
 
82
 
 
83
 
 
84
   public void testWrappedSerializable() throws IOException, ClassNotFoundException
 
85
   { 
 
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]);  
 
92
   }
 
93
 
 
94
   /**
 
95
    * Commented out usecases not supported
 
96
    * @throws IOException
 
97
    * @throws ClassNotFoundException
 
98
    */
 
99
   public void testHTTP() throws IOException, ClassNotFoundException
 
100
   { 
 
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]); */  
 
108
   }
 
109
   
 
110
 
 
111
   protected void runOneTest() throws IOException, ClassNotFoundException
 
112
   {
 
113
      String testData = "This is some test data";
 
114
      Object param = new String(testData);
 
115
 
 
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);
 
122
 
 
123
      System.out.println("Result: " + result);
 
124
      assertEquals(testData, result);
 
125
   }
 
126
   
 
127
   private void runAlgoTest(String algo) 
 
128
   throws IOException, ClassNotFoundException
 
129
   {
 
130
      EncryptingMarshaller em = new EncryptingMarshaller();
 
131
      EncryptingUnMarshaller um = new EncryptingUnMarshaller();
 
132
      if(algo != null)
 
133
      {
 
134
         em.setCipherAlgorithm(algo);
 
135
         um.setCipherAlgorithm(algo);
 
136
      }
 
137
      MarshalFactory.addMarshaller(EncryptingMarshaller.DATATYPE,em, um); 
 
138
 
 
139
      marshaller = MarshalFactory.getMarshaller(EncryptingMarshaller.DATATYPE);
 
140
      unmarshaller = MarshalFactory.getUnMarshaller(EncryptingMarshaller.DATATYPE);
 
141
      runOneTest(); 
 
142
   }
 
143
   
 
144
   private void runHttpTest(String algo) 
 
145
   throws IOException, ClassNotFoundException
 
146
   {
 
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);
 
152
      if(algo != null)
 
153
      {
 
154
         em.setCipherAlgorithm(algo);
 
155
         um.setCipherAlgorithm(algo);
 
156
      }
 
157
      MarshalFactory.addMarshaller(datatype,em, um); 
 
158
      marshaller = MarshalFactory.getMarshaller(datatype);
 
159
      unmarshaller = MarshalFactory.getUnMarshaller(datatype);
 
160
      runOneTest();
 
161
   } 
 
162
   
 
163
   private void runWrappedSerializableTest(String algo) 
 
164
   throws IOException, ClassNotFoundException
 
165
   { 
 
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));
 
173
      if(algo != null)
 
174
      {
 
175
         em.setCipherAlgorithm(algo);
 
176
         um.setCipherAlgorithm(algo);
 
177
      }
 
178
      MarshalFactory.addMarshaller(datatype,em, um); 
 
179
      
 
180
      marshaller = MarshalFactory.getMarshaller(datatype);
 
181
      unmarshaller = MarshalFactory.getUnMarshaller(datatype);
 
182
   } 
 
183
}