Home | History | Annotate | Download | only in spec
      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 
     18 /**
     19  * @author Alexander Y. Kleymenov
     20  * @version $Revision$
     21  */
     22 
     23 package org.apache.harmony.crypto.tests.javax.crypto.spec;
     24 
     25 import java.util.Arrays;
     26 import javax.crypto.spec.PSource;
     27 
     28 import junit.framework.Test;
     29 import junit.framework.TestCase;
     30 import junit.framework.TestSuite;
     31 
     32 /**
     33  */
     34 public class PSourceTest extends TestCase {
     35 
     36     /**
     37      * PSpecified(byte[] p) method testing. Tests that NullPointerException
     38      * is thrown in the case of null p array. Also it checks the value of
     39      * DEFAULT field, and that input p array is copied to protect against
     40      * subsequent modification.
     41      */
     42     public void testPSpecified() {
     43         try {
     44             new PSource.PSpecified(null);
     45             fail("NullPointerException should be thrown in the case of "
     46                     + "null p array.");
     47         } catch (NullPointerException e) {
     48         }
     49 
     50         assertEquals("The PSource.PSpecified DEFAULT value should be byte[0]",
     51                 0, PSource.PSpecified.DEFAULT.getValue().length);
     52 
     53         byte[] p = new byte[] {1, 2, 3, 4, 5};
     54         PSource.PSpecified ps = new PSource.PSpecified(p);
     55         p[0]++;
     56         assertFalse("The change of p specified in the constructor "
     57                 + "should not cause the change of internal array.", p[0] == ps
     58                 .getValue()[0]);
     59     }
     60 
     61     /**
     62      * getValue() method testing. Tests that returned array is equal to the
     63      * array specified in the constructor. Checks that modification
     64      * of returned array does not affect the internal array.
     65      */
     66     public void testGetValue() {
     67         byte[] p = new byte[] {1, 2, 3, 4, 5};
     68 
     69         PSource.PSpecified ps = new PSource.PSpecified(p);
     70         byte[] result = ps.getValue();
     71         if (!Arrays.equals(p, result)) {
     72             fail("The returned array does not equal to the specified "
     73                     + "in the constructor.");
     74         }
     75         result[0]++;
     76         assertFalse("The change of returned by getValue() array "
     77                 + "should not cause the change of internal array.",
     78                 result[0] == ps.getValue()[0]);
     79     }
     80 
     81 
     82     /**
     83      * PSource(String pSrcName) method testing. Tests that returned value is
     84      * equal to the value specified in the constructor.
     85      */
     86     public void testPSource() {
     87         try {
     88             new PSource(null) {};
     89             fail("NullPointerException should be thrown in the case of "
     90                     + "null pSrcName.");
     91         } catch (NullPointerException e) {
     92         }
     93     }
     94 
     95     /**
     96      * getAlgorithm() method testing. Tests that returned value is
     97      * equal to the value specified in the constructor.
     98      */
     99     public void testGetAlgorithm() {
    100         String pSrcName = "pSrcName";
    101         PSource ps = new PSource(pSrcName) {};
    102         assertTrue("The returned value is not equal to the value specified "
    103                 + "in constructor", pSrcName.equals(ps.getAlgorithm()));
    104     }
    105 
    106     public static Test suite() {
    107         return new TestSuite(PSourceTest.class);
    108     }
    109 }
    110