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 Vera Y. Petrashkova
20*/
21
22package org.apache.harmony.crypto.tests.javax.crypto;
23
24import java.security.InvalidAlgorithmParameterException;
25import java.security.InvalidKeyException;
26import java.security.Key;
27import java.security.NoSuchAlgorithmException;
28import java.security.NoSuchProviderException;
29import java.security.Provider;
30import java.security.Security;
31import java.security.spec.AlgorithmParameterSpec;
32
33import javax.crypto.Mac;
34import javax.crypto.spec.SecretKeySpec;
35
36import org.apache.harmony.security.tests.support.SpiEngUtils;
37import junit.framework.TestCase;
38
39
40/**
41 * Tests for Mac class constructors and methods
42 *
43 */
44
45public class Mac_ImplTest extends TestCase {
46
47    private static final String srvMac = "Mac";
48
49    private static final String defaultAlg = "MyMacProv";
50
51    private static final String MacProviderClass = "org.apache.harmony.crypto.tests.support.MyMacSpi";
52
53    private static final String[] invalidValues = SpiEngUtils.invalidValues;
54
55    private static final String[] validValues;
56
57    static {
58        validValues = new String[5];
59        validValues[0] = defaultAlg;
60        validValues[1] = defaultAlg.toUpperCase();
61        validValues[2] = defaultAlg.toLowerCase();
62        validValues[3] = "myMACprov";
63        validValues[4] = "MyMaCpRoV";
64    }
65
66    Provider mProv;
67
68    protected void setUp() throws Exception {
69        super.setUp();
70        mProv = (new SpiEngUtils()).new MyProvider("MyMacProvider", "Testing provider",
71                srvMac.concat(".").concat(defaultAlg),
72                MacProviderClass);
73        Security.insertProviderAt(mProv, 2);
74    }
75
76    /*
77     * @see TestCase#tearDown()
78     */
79    protected void tearDown() throws Exception {
80        super.tearDown();
81        Security.removeProvider(mProv.getName());
82    }
83
84    protected void checkResult(Mac mac) throws InvalidKeyException,
85            InvalidAlgorithmParameterException {
86        assertEquals("Incorrect MacLength", mac.getMacLength(), 0);
87        byte [] b = {(byte)0, (byte)0, (byte)0, (byte)0, (byte)0};
88        SecretKeySpec scs = new SecretKeySpec(b, "SHA1");
89        AlgParSpec parms = new AlgParSpec();
90        tmpKey tKey = new tmpKey();
91        mac.init(scs);
92        byte[] bb = mac.doFinal();
93        assertEquals(bb.length, 0);
94        mac.reset();
95        bb = mac.doFinal();
96        assertEquals(bb.length, 1);
97        try {
98            mac.init(null);
99            fail("InvalidKeyException should be thrown");
100        } catch (InvalidKeyException e) {
101        }
102        try {
103            mac.init(null, null);
104            fail("InvalidKeyException should be thrown");
105        } catch (InvalidKeyException e) {
106        }
107        mac.init(scs, null);
108        mac.init(scs, parms);
109        try {
110            mac.init(tKey, null);
111            fail("InvalidAlgorithmParameterException or IllegalArgumentException "
112                    + "should be thrown for incorrect parameter");
113        } catch (IllegalArgumentException e) {
114        } catch (InvalidAlgorithmParameterException e) {
115        }
116        try {
117            mac.clone();
118            fail("No expected CloneNotSupportedException");
119        } catch (CloneNotSupportedException e) {
120        }
121    }
122
123    /**
124     * Test for <code>getInstance(String algorithm)</code> method
125     * Assertions:
126     * throws NullPointerException when algorithm is null;
127     * throws NoSuchAlgorithmException when algorithm is not correct;
128     * returns Mac object
129     */
130    public void testGetInstance01() throws NoSuchAlgorithmException,
131            InvalidKeyException,
132            InvalidAlgorithmParameterException {
133        try {
134            Mac.getInstance(null);
135            fail("NullPointerException or NoSuchAlgorithmException should be thrown when algorithm is null");
136        } catch (NullPointerException e) {
137        } catch (NoSuchAlgorithmException e) {
138        }
139        for (int i = 0; i < invalidValues.length; i++) {
140            try {
141                Mac.getInstance(invalidValues[i]);
142                fail("NoSuchAlgorithmException must be thrown (algorithm: "
143                        .concat(invalidValues[i]).concat(")"));
144            } catch (NoSuchAlgorithmException e) {
145            }
146        }
147        Mac keyAgr;
148        for (int i = 0; i < validValues.length; i++) {
149            keyAgr = Mac.getInstance(validValues[i]);
150            assertEquals("Incorrect algorithm", keyAgr.getAlgorithm(),
151                    validValues[i]);
152            assertEquals("Incorrect provider", keyAgr.getProvider(), mProv);
153            checkResult(keyAgr);
154        }
155    }
156    /**
157     * Test for <code>getInstance(String algorithm, String provider)</code>
158     * method
159     * Assertions:
160     * throws NullPointerException when algorithm is null;
161     * throws NoSuchAlgorithmException when algorithm is not correct;
162     * throws IllegalArgumentException when provider is null;
163     * throws NoSuchProviderException when provider is available;
164     * returns Mac object
165     */
166    public void testGetInstance02() throws NoSuchAlgorithmException,
167            NoSuchProviderException, IllegalArgumentException,
168            InvalidKeyException,
169            InvalidAlgorithmParameterException {
170        try {
171            Mac.getInstance(null, mProv.getName());
172            fail("NullPointerException or NoSuchAlgorithmException should be thrown when algorithm is null");
173        } catch (NullPointerException e) {
174        } catch (NoSuchAlgorithmException e) {
175        }
176        for (int i = 0; i < invalidValues.length; i++) {
177            try {
178                Mac.getInstance(invalidValues[i], mProv
179                        .getName());
180                fail("NoSuchAlgorithmException must be thrown (algorithm: "
181                        .concat(invalidValues[i]).concat(")"));
182            } catch (NoSuchAlgorithmException e) {
183            }
184        }
185        String prov = null;
186        for (int i = 0; i < validValues.length; i++) {
187            try {
188                Mac.getInstance(validValues[i], prov);
189                fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
190                        .concat(invalidValues[i]).concat(")"));
191            } catch (IllegalArgumentException e) {
192            }
193        }
194        for (int i = 0; i < validValues.length; i++) {
195            for (int j = 1; j < invalidValues.length; j++) {
196                try {
197                    Mac.getInstance(validValues[i],
198                            invalidValues[j]);
199                    fail("NoSuchProviderException must be thrown (algorithm: "
200                            .concat(invalidValues[i]).concat(" provider: ")
201                            .concat(invalidValues[j]).concat(")"));
202                } catch (NoSuchProviderException e) {
203                }
204            }
205        }
206        Mac keyAgr;
207        for (int i = 0; i < validValues.length; i++) {
208            keyAgr = Mac.getInstance(validValues[i], mProv
209                    .getName());
210            assertEquals("Incorrect algorithm", keyAgr.getAlgorithm(),
211                    validValues[i]);
212            assertEquals("Incorrect provider", keyAgr.getProvider().getName(),
213                    mProv.getName());
214            checkResult(keyAgr);
215        }
216    }
217
218    /**
219     * Test for <code>getInstance(String algorithm, Provider provider)</code>
220     * method
221     * Assertions:
222     * throws NullPointerException when algorithm is null;
223     * throws NoSuchAlgorithmException when algorithm is not correct;
224     * throws IllegalArgumentException when provider is null;
225     * returns Mac object
226     */
227    public void testGetInstance03() throws NoSuchAlgorithmException,
228            IllegalArgumentException,
229            InvalidKeyException,
230            InvalidAlgorithmParameterException {
231        try {
232            Mac.getInstance(null, mProv);
233            fail("NullPointerException or NoSuchAlgorithmException should be thrown when algorithm is null");
234        } catch (NullPointerException e) {
235        } catch (NoSuchAlgorithmException e) {
236        }
237        for (int i = 0; i < invalidValues.length; i++) {
238            try {
239                Mac.getInstance(invalidValues[i], mProv);
240                fail("NoSuchAlgorithmException must be thrown (algorithm: "
241                        .concat(invalidValues[i]).concat(")"));
242            } catch (NoSuchAlgorithmException e) {
243            }
244        }
245        Provider prov = null;
246        for (int i = 0; i < validValues.length; i++) {
247            try {
248                Mac.getInstance(validValues[i], prov);
249                fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
250                        .concat(invalidValues[i]).concat(")"));
251            } catch (IllegalArgumentException e) {
252            }
253        }
254        Mac keyAgr;
255        for (int i = 0; i < validValues.length; i++) {
256            keyAgr = Mac.getInstance(validValues[i], mProv);
257            assertEquals("Incorrect algorithm", keyAgr.getAlgorithm(),
258                    validValues[i]);
259            assertEquals("Incorrect provider", keyAgr.getProvider(), mProv);
260            checkResult(keyAgr);
261       }
262    }
263    public static class AlgParSpec implements AlgorithmParameterSpec {
264
265    }
266    public static class tmpKey implements Key {
267        public tmpKey() {
268
269        }
270        public String getAlgorithm() {
271            return "Test";
272        }
273        public String getFormat() {
274            return "Format";
275        }
276        public byte[] getEncoded() {
277            return null;
278        }
279    }
280}
281