View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.util;
20  
21  import java.io.IOException;
22  import java.io.StringReader;
23  import java.io.BufferedReader;
24  
25  import junit.framework.Test;
26  import junit.framework.TestSuite;
27  
28  import org.apache.myfaces.trinidad.util.Base64InputStream;
29  import org.apache.myfaces.trinidadbuild.test.FacesTestCase;
30  
31  /**
32   * Unit tests for Base64InputStream.
33   * 
34   */
35  
36  public class Base64InputStreamTest extends FacesTestCase 
37  {   
38    
39    /**
40     * Creates a new Base64InputStreamTest.
41     * 
42     * @param testName  the unit test name
43     */  
44    public Base64InputStreamTest(String testName)
45      {
46        super(testName);
47    }
48    
49    @Override
50    protected void setUp() throws Exception
51    {
52      super.setUp();
53    }
54    
55    @Override
56    protected void tearDown() throws Exception
57    {
58      super.tearDown();
59    }
60    
61    public static Test suite()
62    {
63      return new TestSuite(Base64InputStreamTest.class);
64    }
65    
66    /**
67     * Tests decoding of stream that contains no trailing padding characters.
68     */
69    public void testNoPaddingChar() throws IOException
70    {
71      String  str = "abcdefghijklmnopqrstuvwxBase64 Encoding is a popular way to convert the 8bit and the binary to the 7bit for network trans using Socket, and a security method to handle text or file, often used in Authentical Login and Mail Attachment, also stored in text file or database. Most SMTP server will handle the login UserName and Password in this way. 1";
72        String str_encoded = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4QmFzZTY0IEVuY29kaW5nIGlzIGEgcG9wdWxhciB3YXkgdG8gY29udmVydCB0aGUgOGJpdCBhbmQgdGhlIGJpbmFyeSB0byB0aGUgN2JpdCBmb3IgbmV0d29yayB0cmFucyB1c2luZyBTb2NrZXQsIGFuZCBhIHNlY3VyaXR5IG1ldGhvZCB0byBoYW5kbGUgdGV4dCBvciBmaWxlLCBvZnRlbiB1c2VkIGluIEF1dGhlbnRpY2FsIExvZ2luIGFuZCBNYWlsIEF0dGFjaG1lbnQsIGFsc28gc3RvcmVkIGluIHRleHQgZmlsZSBvciBkYXRhYmFzZS4gTW9zdCBTTVRQIHNlcnZlciB3aWxsIGhhbmRsZSB0aGUgbG9naW4gVXNlck5hbWUgYW5kIFBhc3N3b3JkIGluIHRoaXMgd2F5LiAx";
73    
74        _testRead(str, str_encoded);  
75        
76    }
77     
78    /**
79     * Tests decoding of stream that contains exactly one trailing padding character.
80     */
81    public void testOnePaddingChar() throws IOException
82    {
83      String str = "Base64 Encoding is a popular way to convert the 8bit and the binary to the 7bit for network trans using Socket, and a security method to handle text or file, often used in Authentical Login and Mail Attachment, also stored in text file or database. Most SMTP server will handle the login UserName and Password in this way.9";
84        String str_encoded = "QmFzZTY0IEVuY29kaW5nIGlzIGEgcG9wdWxhciB3YXkgdG8gY29udmVydCB0aGUgOGJpdCBhbmQgdGhlIGJpbmFyeSB0byB0aGUgN2JpdCBmb3IgbmV0d29yayB0cmFucyB1c2luZyBTb2NrZXQsIGFuZCBhIHNlY3VyaXR5IG1ldGhvZCB0byBoYW5kbGUgdGV4dCBvciBmaWxlLCBvZnRlbiB1c2VkIGluIEF1dGhlbnRpY2FsIExvZ2luIGFuZCBNYWlsIEF0dGFjaG1lbnQsIGFsc28gc3RvcmVkIGluIHRleHQgZmlsZSBvciBkYXRhYmFzZS4gTW9zdCBTTVRQIHNlcnZlciB3aWxsIGhhbmRsZSB0aGUgbG9naW4gVXNlck5hbWUgYW5kIFBhc3N3b3JkIGluIHRoaXMgd2F5Ljk="; 
85     
86      _testRead(str, str_encoded);   
87        
88    }
89     
90     /**
91     * Tests decoding of stream that contains exactly two trailing padding characters.
92     */    
93      public void testTwoPaddingChars() throws IOException
94    {
95        String str = "Base64 Encoding is a popular way to convert the 8bit and the binary to the 7bit for network trans using Socket, and a security method to handle text or file, often used in Authentical Login and Mail Attachment, also stored in text file or database. Most SMTP server will handle the login UserName and Password in this way.";
96        String str_encoded = "QmFzZTY0IEVuY29kaW5nIGlzIGEgcG9wdWxhciB3YXkgdG8gY29udmVydCB0aGUgOGJpdCBhbmQgdGhlIGJpbmFyeSB0byB0aGUgN2JpdCBmb3IgbmV0d29yayB0cmFucyB1c2luZyBTb2NrZXQsIGFuZCBhIHNlY3VyaXR5IG1ldGhvZCB0byBoYW5kbGUgdGV4dCBvciBmaWxlLCBvZnRlbiB1c2VkIGluIEF1dGhlbnRpY2FsIExvZ2luIGFuZCBNYWlsIEF0dGFjaG1lbnQsIGFsc28gc3RvcmVkIGluIHRleHQgZmlsZSBvciBkYXRhYmFzZS4gTW9zdCBTTVRQIHNlcnZlciB3aWxsIGhhbmRsZSB0aGUgbG9naW4gVXNlck5hbWUgYW5kIFBhc3N3b3JkIGluIHRoaXMgd2F5Lg==";
97      
98      _testRead(str, str_encoded);   
99        
100   }  
101    
102    
103   /**
104    *
105    *  Uses Base64InputStream to reads from the encoded string and 
106    *  compares output with the expected decoded string.
107    *
108    *  @param  str     the decoded string
109    *  @param  str_encoded the base64 encoded string
110    * 
111    **/   
112    
113   private void _testRead(String str, String str_encoded) throws IOException
114   {
115    
116       // construct the Base64InputStream from the encoded string
117       StringReader strreader = new StringReader(str_encoded);
118       BufferedReader buffreader = new BufferedReader(strreader); 
119       Base64InputStream b64_in = new Base64InputStream(buffreader);
120  
121       // read decoded chars from the Base64InputStream and form decoded string
122       String result = "";
123       int c;
124       while ( (c = b64_in.read()) != -1 ) 
125       {
126          result = result + (char)c;
127       }
128        
129     // compare resulting decoded string with the expected decoded string 
130     assertEquals(result, str);  
131   }
132   
133   
134   
135 } // end Base64InputStreamTest class