MacTest.java revision 229e34b182b98e1dba15d3dc6341954986ae2b7a
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* @version $Revision$
21*/
22
23package org.apache.harmony.crypto.tests.javax.crypto;
24
25import java.math.BigInteger;
26import java.nio.ByteBuffer;
27import java.security.InvalidAlgorithmParameterException;
28import java.security.InvalidKeyException;
29import java.security.NoSuchAlgorithmException;
30import java.security.NoSuchProviderException;
31import java.security.Provider;
32import java.security.spec.DSAParameterSpec;
33import java.security.spec.PSSParameterSpec;
34
35import javax.crypto.Mac;
36import javax.crypto.MacSpi;
37import javax.crypto.ShortBufferException;
38import javax.crypto.spec.DHGenParameterSpec;
39
40import javax.crypto.spec.SecretKeySpec;
41
42import org.apache.harmony.crypto.tests.support.MyMacSpi;
43import org.apache.harmony.security.tests.support.SpiEngUtils;
44
45import junit.framework.TestCase;
46
47import junit.framework.Test;
48import junit.framework.TestSuite;
49
50/**
51 * Tests for Mac class constructors and methods
52 *
53 */
54public class MacTest extends TestCase {
55
56    public static final String srvMac = "Mac";
57
58    private static String defaultAlgorithm = null;
59
60    private static String defaultProviderName = null;
61
62    private static Provider defaultProvider = null;
63
64    private static boolean DEFSupported = false;
65
66    private static final String NotSupportedMsg = "There is no suitable provider for Mac";
67
68    private static final String[] invalidValues = SpiEngUtils.invalidValues;
69
70    private static String[] validValues = new String[3];
71
72    public static final String validAlgorithmsMac [] =
73        {"HmacSHA1", "HmacMD5", "HmacSHA256", "HmacSHA384", "HmacSHA512"};
74
75
76    static {
77        for (int i = 0; i < validAlgorithmsMac.length; i++) {
78            defaultProvider = SpiEngUtils.isSupport(validAlgorithmsMac[i],
79                    srvMac);
80            DEFSupported = (defaultProvider != null);
81            if (DEFSupported) {
82                defaultAlgorithm = validAlgorithmsMac[i];
83                defaultProviderName = defaultProvider.getName();
84                validValues[0] = defaultAlgorithm;
85                validValues[1] = defaultAlgorithm.toUpperCase();
86                validValues[2] = defaultAlgorithm.toLowerCase();
87                break;
88            }
89        }
90    }
91
92    private Mac [] createMacs() {
93        if (!DEFSupported) {
94            fail(NotSupportedMsg);
95            return null;
96        }
97        try {
98            Mac m [] = new Mac[3];
99            m[0] = Mac.getInstance(defaultAlgorithm);
100            m[1] = Mac.getInstance(defaultAlgorithm, defaultProvider);
101            m[2] = Mac.getInstance(defaultAlgorithm, defaultProviderName);
102            return m;
103        } catch (Exception e) {
104            return null;
105        }
106    }
107
108    /**
109     * Test for <code>getInstance(String algorithm)</code> method
110     * Assertion:
111     * throws NullPointerException when algorithm is null
112     * throws NoSuchAlgorithmException when algorithm is not available
113     */
114    public void testMac01() {
115        try {
116            Mac.getInstance(null);
117            fail("NullPointerException or NoSuchAlgorithmException should be thrown when algorithm is null");
118        } catch (NullPointerException e) {
119        } catch (NoSuchAlgorithmException e) {
120        }
121        for (int i = 0; i < invalidValues.length; i++) {
122            try {
123                Mac.getInstance(invalidValues[i]);
124                fail("NoSuchAlgorithmException must be thrown when algorithm is not available: "
125                        .concat(invalidValues[i]));
126            } catch (NoSuchAlgorithmException e) {
127            }
128        }
129    }
130
131    /**
132     * Test for <code>getInstance(String algorithm)</code> method
133     * Assertion: returns Mac object
134     */
135    public void testMac02() throws NoSuchAlgorithmException {
136        if (!DEFSupported) {
137            fail(NotSupportedMsg);
138            return;
139        }
140        Mac mac;
141        for (int i = 0; i < validValues.length; i++) {
142            mac = Mac.getInstance(validValues[i]);
143            assertEquals("Incorrect algorithm", mac.getAlgorithm(), validValues[i]);
144        }
145    }
146    /**
147     * Test for <code>getInstance(String algorithm, String provider)</code> method
148     * Assertion:
149     * throws IllegalArgumentException when provider is null or empty
150     * throws NoSuchProviderException when provider is not available
151     */
152    public void testMac03() throws NoSuchAlgorithmException, NoSuchProviderException {
153        if (!DEFSupported) {
154            fail(NotSupportedMsg);
155            return;
156        }
157        String provider = null;
158        for (int i = 0; i < validValues.length; i++) {
159            try {
160                Mac.getInstance(validValues[i], provider);
161                fail("IllegalArgumentException must be thrown when provider is null");
162            } catch (IllegalArgumentException e) {
163            }
164            try {
165                Mac.getInstance(validValues[i], "");
166                fail("IllegalArgumentException must be thrown when provider is empty");
167            } catch (IllegalArgumentException e) {
168            }
169            for (int j = 1; j < invalidValues.length; j++) {
170                try {
171                    Mac.getInstance(validValues[i], invalidValues[j]);
172                    fail("NoSuchProviderException must be thrown (algorithm: "
173                            .concat(validValues[i]).concat(" provider: ")
174                            .concat(invalidValues[j]).concat(")"));
175                } catch (NoSuchProviderException e) {
176                }
177            }
178        }
179    }
180
181    /**
182     * Test for <code>getInstance(String algorithm, String provider)</code> method
183     * Assertion:
184     * throws NullPointerException when algorithm is null
185     * throws NoSuchAlgorithmException when algorithm is not available
186     */
187    public void testMac04() throws NoSuchAlgorithmException,
188            IllegalArgumentException, NoSuchProviderException {
189        if (!DEFSupported) {
190            fail(NotSupportedMsg);
191            return;
192        }
193        try {
194            Mac.getInstance(null, defaultProviderName);
195            fail("NullPointerException or NoSuchAlgorithmException should be thrown when algorithm is null");
196        } catch (NullPointerException e) {
197        } catch (NoSuchAlgorithmException e) {
198        }
199        for (int i = 0; i < invalidValues.length; i++) {
200            try {
201                Mac.getInstance(invalidValues[i], defaultProviderName);
202                fail("NoSuchAlgorithmException must be throws when algorithm is not available: "
203                        .concat(invalidValues[i]));
204            } catch( NoSuchAlgorithmException e) {
205            }
206        }
207    }
208    /**
209     * Test for <code>getInstance(String algorithm, String provider)</code> method
210     * Assertion: returns Mac object
211     */
212    public void testMac05() throws NoSuchAlgorithmException, NoSuchProviderException,
213            IllegalArgumentException {
214        if (!DEFSupported) {
215            fail(NotSupportedMsg);
216            return;
217        }
218        Mac mac;
219        for (int i = 0; i < validValues.length; i++) {
220            mac = Mac.getInstance(validValues[i], defaultProviderName);
221            assertEquals("Incorrect algorithm", mac.getAlgorithm(), validValues[i]);
222            assertEquals("Incorrect provider", mac.getProvider().getName(),
223                    defaultProviderName);
224        }
225    }
226
227    /**
228     * Test for <code>getInstance(String algorithm, Provider provider)</code> method
229     * Assertion: throws IllegalArgumentException when provider is null
230     */
231    public void testMac06() throws NoSuchAlgorithmException, NoSuchProviderException {
232        if (!DEFSupported) {
233            fail(NotSupportedMsg);
234            return;
235        }
236        Provider provider = null;
237        for (int i = 0; i < validValues.length; i++) {
238            try {
239                Mac.getInstance(validValues[i], provider);
240                fail("IllegalArgumentException must be thrown when provider is null");
241            } catch (IllegalArgumentException e) {
242            }
243        }
244    }
245    /**
246     * Test for <code>getInstance(String algorithm, Provider provider)</code> method
247     * Assertion:
248     * throws NullPointerException when algorithm is null
249     * throws NoSuchAlgorithmException when algorithm is not available
250     */
251    public void testMac07() throws NoSuchAlgorithmException,
252            NoSuchProviderException, IllegalArgumentException {
253        if (!DEFSupported) {
254            fail(NotSupportedMsg);
255            return;
256        }
257        try {
258            Mac.getInstance(null, defaultProvider);
259            fail("NullPointerException or NoSuchAlgorithmException should be thrown when algorithm is null");
260        } catch (NullPointerException e) {
261        } catch (NoSuchAlgorithmException e) {
262        }
263        for (int i = 0; i < invalidValues.length; i++) {
264            try {
265                Mac.getInstance(invalidValues[i], defaultProvider);
266                fail("NoSuchAlgorithmException must be thrown when algorithm is not available: "
267                        .concat(invalidValues[i]));
268            } catch (NoSuchAlgorithmException e) {
269            }
270        }
271    }
272
273    /**
274     * Test for <code>getInstance(String algorithm, Provider provider)</code> method
275     * Assertion: returns Mac object
276     */
277    public void testMac08() throws NoSuchAlgorithmException, NoSuchProviderException,
278            IllegalArgumentException {
279        if (!DEFSupported) {
280            fail(NotSupportedMsg);
281            return;
282        }
283        Mac mac;
284        for (int i = 0; i < validValues.length; i++) {
285            mac = Mac.getInstance(validValues[i], defaultProvider);
286            assertEquals("Incorrect algorithm", mac.getAlgorithm(), validValues[i]);
287            assertEquals("Incorrect provider", mac.getProvider(), defaultProvider);
288        }
289    }
290    /**
291     * Test for <code>update</code> and <code>doFinal</code> methods
292     * Assertion: throws IllegalStateException when Mac is not initialized
293     * @throws Exception
294     */
295    public void testMac09() throws Exception {
296        if (!DEFSupported) {
297            fail(NotSupportedMsg);
298            return;
299        }
300        Mac [] macs = createMacs();
301        assertNotNull("Mac objects were not created", macs);
302        byte [] buf = new byte[10];
303        ByteBuffer bBuf = ByteBuffer.wrap(buf, 0, 10);
304        byte [] bb = {(byte)1, (byte)2, (byte)3, (byte)4, (byte)5};
305        SecretKeySpec sks = new SecretKeySpec(bb, "SHA1");
306        for (int i = 0; i < macs.length; i++) {
307            try {
308                macs[i].update((byte)0);
309                fail("IllegalStateException must be thrown");
310            } catch (IllegalStateException e) {
311            }
312            try {
313                macs[i].update(buf);
314                fail("IllegalStateException must be thrown");
315            } catch (IllegalStateException e) {
316            }
317            try {
318                macs[i].update(buf, 0, 3);
319                fail("IllegalStateException must be thrown");
320            } catch (IllegalStateException e) {
321            }
322            try {
323                macs[i].update(bBuf);
324                fail("IllegalStateException must be thrown");
325            } catch (IllegalStateException e) {
326            }
327            try {
328                macs[i].doFinal();
329                fail("IllegalStateException must be thrown");
330            } catch (IllegalStateException e) {
331            }
332            try {
333                macs[i].doFinal(new byte[10]);
334                fail("IllegalStateException must be thrown");
335            } catch (IllegalStateException e) {
336            }
337            try {
338                macs[i].doFinal(new byte[10], 0);
339                fail("IllegalStateException must be thrown");
340            } catch (IllegalStateException e) {
341            }
342
343            macs[i].init(sks);
344            try {
345                macs[i].doFinal(new byte[1], 0);
346                fail("ShortBufferException expected");
347            } catch (ShortBufferException e) {
348                //expected
349            }
350        }
351    }
352    /**
353     * Test for <code>doFinal(byte[] output, int outOffset)</code> method
354     * Assertion:
355     * throws ShotBufferException when outOffset  is negative or
356     * outOffset >= output.length  or when given buffer is small
357     */
358    public void testMac10() throws NoSuchAlgorithmException,
359            NoSuchProviderException, IllegalArgumentException,
360            IllegalStateException, InvalidKeyException {
361        if (!DEFSupported) {
362            fail(NotSupportedMsg);
363            return;
364        }
365        Mac[] macs = createMacs();
366        assertNotNull("Mac objects were not created", macs);
367        byte[] b = { (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
368        byte[] byteA = new byte[b.length];
369        SecretKeySpec sks = new SecretKeySpec(b, "SHA1");
370        for (int i = 0; i < macs.length; i++) {
371            macs[i].init(sks);
372            try {
373                macs[i].doFinal(null, 10);
374                fail("ShortBufferException must be thrown");
375            } catch (ShortBufferException e) {
376            }
377            try {
378                macs[i].doFinal(byteA, -4);
379                fail("ShortBufferException must be thrown");
380            } catch (ShortBufferException e) {
381            }
382            try {
383                macs[i].doFinal(byteA, 10);
384                fail("ShortBufferException must be thrown");
385            } catch (ShortBufferException e) {
386            }
387            try {
388                macs[i].doFinal(new byte[1], 0);
389                fail("ShortBufferException must be thrown");
390            } catch (ShortBufferException e) {
391            }
392            byte[] res = macs[i].doFinal();
393            try {
394                macs[i].doFinal(new byte[res.length - 1], 0);
395                fail("ShortBufferException must be thrown");
396            } catch (ShortBufferException e) {
397            }
398        }
399    }
400
401    /**
402     * Test for <code>doFinal(byte[] output, int outOffset)</code> and
403     * <code>doFinal()</code> methods Assertion: Mac result is stored in
404     * output buffer
405     */
406    public void testMac11() throws NoSuchAlgorithmException, NoSuchProviderException,
407            IllegalArgumentException, IllegalStateException,
408            InvalidKeyException, ShortBufferException {
409        if (!DEFSupported) {
410            fail(NotSupportedMsg);
411            return;
412        }
413        Mac [] macs = createMacs();
414        assertNotNull("Mac objects were not created", macs);
415        byte [] b = {(byte)0, (byte)0, (byte)0, (byte)0, (byte)0};
416        SecretKeySpec scs = new SecretKeySpec(b, "SHA1");
417        for (int i = 0; i < macs.length; i++) {
418            macs[i].init(scs);
419            byte [] res1 = macs[i].doFinal();
420            byte [] res2 = new byte[res1.length + 10];
421            macs[i].doFinal(res2, 0);
422            for (int j = 0; j < res1.length; j++) {
423                assertEquals("Not equals byte number: "
424                        .concat(Integer.toString(j)), res1[j], res2[j]);
425            }
426        }
427    }
428    /**
429     * Test for <code>doFinal(byte[] input)</code> method
430     * Assertion: update Mac and returns result
431     */
432    public void testMac12() throws NoSuchAlgorithmException, NoSuchProviderException,
433            IllegalArgumentException, IllegalStateException,
434            InvalidKeyException  {
435        if (!DEFSupported) {
436            fail(NotSupportedMsg);
437            return;
438        }
439        Mac [] macs = createMacs();
440        assertNotNull("Mac objects were not created", macs);
441        byte [] b = {(byte)0, (byte)0, (byte)0, (byte)0, (byte)0};
442        byte [] upd = {(byte)5, (byte)4, (byte)3, (byte)2, (byte)1, (byte)0};
443        SecretKeySpec scs = new SecretKeySpec(b, "SHA1");
444        for (int i = 0; i < macs.length; i++) {
445            macs[i].init(scs);
446            byte [] res1 = macs[i].doFinal();
447            byte [] res2 = macs[i].doFinal();
448            assertEquals("Results are not the same", res1.length, res2.length);
449            for(int t = 0; t < res1.length; t++) {
450                assertEquals("Results are not the same", res1[t], res2[t]);
451            }
452            res2 = macs[i].doFinal(upd);
453            macs[i].update(upd);
454            res1 = macs[i].doFinal();
455            assertEquals("Results are not the same", res1.length, res2.length);
456            for(int t = 0; t < res1.length; t++) {
457                assertEquals("Results are not the same", res1[t], res2[t]);
458            }
459        }
460    }
461
462    /**
463     * Test for <code>update(byte[] input, int outset, int len)</code> method
464     * Assertion: throws IllegalArgumentException when offset or len is negative,
465     * offset + len >= input.length
466     */
467    public void testMac13() throws NoSuchAlgorithmException,
468            NoSuchProviderException, IllegalArgumentException, IllegalStateException,
469            InvalidKeyException {
470        if (!DEFSupported) {
471            fail(NotSupportedMsg);
472            return;
473        }
474        Mac [] macs = createMacs();
475        assertNotNull("Mac objects were not created", macs);
476        byte [] b = {(byte)0, (byte)0, (byte)0, (byte)0, (byte)0};
477        SecretKeySpec scs = new SecretKeySpec(b, "SHA1");
478        for (int i = 0; i < macs.length; i++) {
479            macs[i].init(scs);
480            try {
481                macs[i].update(b, -10, b.length);
482                fail("IllegalArgumentException must be thrown");
483            } catch (IllegalArgumentException e) {
484            }
485            try {
486                macs[i].update(b, 0, -10);
487                fail("IllegalArgumentException must be thrown");
488            } catch (IllegalArgumentException e) {
489            }
490            try {
491                macs[i].update(b, 0, b.length + 1);
492                fail("IllegalArgumentException must be thrown");
493            } catch (IllegalArgumentException e) {
494            }
495            try {
496                macs[i].update(b, b.length - 1, 2);
497                fail("IllegalArgumentException must be thrown");
498            } catch (IllegalArgumentException e) {
499            }
500        }
501    }
502    /**
503     * Test for <code>update(byte[] input, int outset, int len)</code> and
504     * <code>update(byte[] input</code>
505     * methods
506     * Assertion: updates Mac
507     */
508    public void testMac14() throws NoSuchAlgorithmException,
509            NoSuchProviderException, IllegalArgumentException, IllegalStateException,
510            InvalidKeyException {
511        if (!DEFSupported) {
512            fail(NotSupportedMsg);
513            return;
514        }
515        Mac [] macs = createMacs();
516        assertNotNull("Mac objects were not created", macs);
517        byte [] b = {(byte)0, (byte)0, (byte)0, (byte)0, (byte)0};
518        byte [] upd1 = {(byte)0, (byte)1, (byte)5, (byte)4, (byte)3, (byte)2};
519        byte [] upd2 = {(byte)5, (byte)4, (byte)3, (byte)2};
520        byte [] res1;
521        byte [] res2;
522        SecretKeySpec scs = new SecretKeySpec(b, "SHA1");
523        for (int i = 0; i < macs.length; i++) {
524            macs[i].init(scs);
525            macs[i].update(upd1, 2, 4);
526            res1 = macs[i].doFinal();
527            macs[i].init(scs);
528            macs[i].update(upd2);
529            res2 = macs[i].doFinal();
530            assertEquals("Results are not the same", res1.length, res2.length);
531            for(int t = 0; t < res1.length; t++) {
532                assertEquals("Results are not the same", res1[t], res2[t]);
533            }
534            macs[i].init(scs);
535            macs[i].update((byte)5);
536            res1 = macs[i].doFinal();
537            macs[i].init(scs);
538            macs[i].update(upd1,2,1);
539            res2 = macs[i].doFinal();
540            assertEquals("Results are not the same", res1.length, res2.length);
541            for(int t = 0; t < res1.length; t++) {
542                assertEquals("Results are not the same", res1[t], res2[t]);
543            }
544        }
545    }
546    /**
547     * Test for <code>clone()</code> method
548     * Assertion: returns Mac object or throws CloneNotSupportedException
549     */
550    public void testMacClone() throws NoSuchAlgorithmException, CloneNotSupportedException {
551        if (!DEFSupported) {
552            fail(NotSupportedMsg);
553            return;
554        }
555        Mac [] macs = createMacs();
556        assertNotNull("Mac objects were not created", macs);
557        for (int i = 0; i < macs.length; i++) {
558            try {
559                Mac mac1 = (Mac) macs[i].clone();
560                assertEquals(mac1.getAlgorithm(), macs[i].getAlgorithm());
561                assertEquals(mac1.getProvider(), macs[i].getProvider());
562                assertFalse(macs[i].equals(mac1));
563            } catch (CloneNotSupportedException e) {
564            }
565        }
566    }
567    /**
568     * Test for
569     * <code>init(Key key, AlgorithmParameterSpec params)</code>
570     * <code>init(Key key)</code>
571     * methods
572     * Assertion: throws InvalidKeyException and InvalidAlgorithmParameterException
573     * when parameters are not appropriate
574     */
575    public void testInit() throws NoSuchAlgorithmException, NoSuchProviderException,
576            IllegalArgumentException, IllegalStateException, InvalidAlgorithmParameterException,
577            InvalidKeyException {
578        if (!DEFSupported) {
579            fail(NotSupportedMsg);
580            return;
581        }
582        Mac [] macs = createMacs();
583        assertNotNull("Mac objects were not created", macs);
584        byte [] b = {(byte)1, (byte)2, (byte)3, (byte)4, (byte)5};
585        SecretKeySpec sks = new SecretKeySpec(b, "SHA1");
586        DHGenParameterSpec algPS = new DHGenParameterSpec(1, 2);
587        PSSParameterSpec algPSS = new PSSParameterSpec(20);
588        SecretKeySpec sks1 = new SecretKeySpec(b, "RSA");
589
590        for (int i = 0; i < macs.length; i++) {
591            macs[i].init(sks);
592            try {
593                macs[i].init(sks1, algPSS);
594                fail("init(..) accepts incorrect AlgorithmParameterSpec parameter");
595            } catch (InvalidAlgorithmParameterException e) {
596            }
597            try {
598                macs[i].init(sks, algPS);
599                fail("init(..) accepts incorrect AlgorithmParameterSpec parameter");
600            } catch (InvalidAlgorithmParameterException e) {
601            }
602
603            try {
604                macs[i].init(null, null);
605                fail("InvalidKeyException must be thrown");
606            } catch (InvalidKeyException e) {
607            }
608
609            try {
610                macs[i].init(null);
611                fail("InvalidKeyException must be thrown");
612            } catch (InvalidKeyException e) {
613            }
614//            macs[i].init(sks, null);
615        }
616    }
617
618    /**
619     * Test for <code>update(ByteBuffer input)</code>
620     * <code>update(byte[] input, int offset, int len)</code>
621     * methods
622     * Assertion: processes Mac; if input is null then do nothing
623     */
624    public void testUpdateByteBuffer01() throws NoSuchAlgorithmException, NoSuchProviderException,
625            IllegalArgumentException, IllegalStateException, InvalidAlgorithmParameterException,
626            InvalidKeyException {
627        if (!DEFSupported) {
628            fail(NotSupportedMsg);
629            return;
630        }
631        Mac [] macs = createMacs();
632        assertNotNull("Mac objects were not created", macs);
633        byte [] bb = {(byte)1, (byte)2, (byte)3, (byte)4, (byte)5};
634        SecretKeySpec sks = new SecretKeySpec(bb, "SHA1");
635        ByteBuffer byteNull = null;
636        ByteBuffer byteBuff = ByteBuffer.allocate(0);
637        byte [] bb1;
638        byte [] bb2;
639        for (int i = 0; i < macs.length; i++) {
640            macs[i].init(sks);
641            bb1 = macs[i].doFinal();
642            try {
643                macs[i].update(byteNull);
644                fail("IllegalArgumentException must be thrown because buffer is null");
645            } catch (IllegalArgumentException e) {
646            }
647            macs[i].update(byteBuff);
648            bb2 = macs[i].doFinal();
649            for (int t = 0; t < bb1.length; t++) {
650                assertEquals("Incorrect doFinal result", bb1[t], bb2[t]);
651            }
652            macs[i].init(sks);
653            bb1 = macs[i].doFinal();
654            macs[i].update(null, 0, 0);
655            bb2 = macs[i].doFinal();
656            for (int t = 0; t < bb1.length; t++) {
657                assertEquals("Incorrect doFinal result", bb1[t], bb2[t]);
658            }
659        }
660    }
661    /**
662     * Test for <code>update(ByteBuffer input)</code>
663     * <code>update(byte[] input, int offset, int len)</code>
664     * methods
665     * Assertion: processes Mac
666     */
667    public void testUpdateByteBuffer02() throws NoSuchAlgorithmException, NoSuchProviderException,
668            IllegalArgumentException, IllegalStateException, InvalidAlgorithmParameterException,
669            InvalidKeyException {
670        if (!DEFSupported) {
671            fail(NotSupportedMsg);
672            return;
673        }
674        Mac [] macs = createMacs();
675        assertNotNull("Mac objects were not created", macs);
676        byte [] bb = {(byte)1, (byte)2, (byte)3, (byte)4, (byte)5};
677        SecretKeySpec sks = new SecretKeySpec(bb, "SHA1");
678        byte [] bbuf = {(byte)5, (byte)4, (byte)3, (byte)2, (byte)1};
679        ByteBuffer byteBuf;
680        byte [] bb1;
681        byte [] bb2;
682        for (int i = 0; i < macs.length; i++) {
683            byteBuf = ByteBuffer.allocate(5);
684            byteBuf.put(bbuf);
685            byteBuf.position(2);
686            macs[i].init(sks);
687            macs[i].update(byteBuf);
688            bb1 = macs[i].doFinal();
689
690            macs[i].init(sks);
691            macs[i].update(bbuf, 2, 3);
692            bb2 = macs[i].doFinal();
693            for (int t = 0; t < bb1.length; t++) {
694                assertEquals("Incorrect doFinal result", bb1[t], bb2[t]);
695            }
696        }
697    }
698    /**
699     * Test for <code>clone()</code> method
700     * Assertion: clone if provider is clo
701     */
702    public void testClone()  {
703        if (!DEFSupported) {
704            fail(NotSupportedMsg);
705            return;
706        }
707        Mac [] macs = createMacs();
708        assertNotNull("Mac objects were not created", macs);
709        Mac res;
710        for (int i = 0; i < macs.length; i++) {
711            try {
712                res = (Mac)macs[i].clone();
713                assertTrue("Object should not be equals", !macs[i].equals(res));
714                assertEquals("Incorrect class", macs[i].getClass(), res.getClass());
715            } catch (CloneNotSupportedException e) {
716            }
717        }
718    }
719    /**
720     * Test for <code>getMacLength()</code> method
721     * Assertion: return Mac length
722     */
723    public void testGetMacLength() {
724        if (!DEFSupported) {
725            fail(NotSupportedMsg);
726            return;
727        }
728        Mac [] macs = createMacs();
729        assertNotNull("Mac objects were not created", macs);
730        for (int i = 0; i < macs.length; i++) {
731            assertTrue("Length should be positive", (macs[i].getMacLength() >= 0));
732        }
733    }
734
735    /**
736     * Test for <code>reset()</code> method
737     * Assertion: return Mac length
738     */
739    public void testReset() throws InvalidKeyException {
740        if (!DEFSupported) {
741            fail(NotSupportedMsg);
742            return;
743        }
744        Mac [] macs = createMacs();
745        assertNotNull("Mac objects were not created", macs);
746        byte [] bb = {(byte)1, (byte)2, (byte)3, (byte)4, (byte)5};
747        SecretKeySpec sks = new SecretKeySpec(bb, "SHA1");
748        byte [] bbuf = {(byte)5, (byte)4, (byte)3, (byte)2, (byte)1};
749        byte [] bb1;
750        byte [] bb2;
751        for (int i = 0; i < macs.length; i++) {
752            macs[i].init(sks);
753            bb1 = macs[i].doFinal();
754            macs[i].reset();
755            bb2 = macs[i].doFinal();
756            assertEquals("incorrect result",bb1.length, bb2.length);
757            for (int t = 0; t < bb1.length; t++) {
758               assertEquals("Incorrect doFinal result", bb1[t], bb2[t]);
759            }
760            macs[i].reset();
761            macs[i].update(bbuf);
762            bb1 = macs[i].doFinal();
763            macs[i].reset();
764            macs[i].update(bbuf, 0, bbuf.length);
765            bb2 = macs[i].doFinal();
766            assertEquals("incorrect result",bb1.length, bb2.length);
767            for (int t = 0; t < bb1.length; t++) {
768               assertEquals("Incorrect doFinal result", bb1[t], bb2[t]);
769            }
770        }
771    }
772    /**
773     * Test for <code>Mac</code> constructor
774     * Assertion: returns Mac object
775     */
776    public void testMacConstructor() throws NoSuchAlgorithmException,
777            InvalidKeyException, InvalidAlgorithmParameterException {
778        if (!DEFSupported) {
779            fail(NotSupportedMsg);
780            return;
781        }
782        MacSpi spi = new MyMacSpi();
783        Mac mac = new myMac(spi, defaultProvider, defaultAlgorithm);
784        assertEquals("Incorrect algorithm", mac.getAlgorithm(),
785                defaultAlgorithm);
786        assertEquals("Incorrect provider", mac.getProvider(), defaultProvider);
787        try {
788            mac.init(null, null);
789            fail("Exception should be thrown because init(..) uses incorrect parameters");
790        } catch (Exception e) {
791        }
792        assertEquals("Invalid mac length", mac.getMacLength(), 0);
793
794        mac = new myMac(null, null, null);
795        assertNull("Algorithm must be null", mac.getAlgorithm());
796        assertNull("Provider must be null", mac.getProvider());
797        try {
798            mac.init(null, null);
799            fail("Exception should be thrown because init(..) uses incorrect parameters");
800        } catch (Exception e) {
801        }
802        try {
803            mac.getMacLength();
804            fail("NullPointerException must be thrown");
805        } catch (NullPointerException e) {
806        }
807    }
808
809    public void test_getAlgorithm() throws NoSuchAlgorithmException {
810        Mac mac;
811        for (int i = 0; i < validValues.length; i++) {
812            mac = Mac.getInstance(validValues[i]);
813            assertEquals("Incorrect algorithm", mac.getAlgorithm(), validValues[i]);
814        }
815
816        mac = new Mock_Mac(null, null, null);
817        assertNull(mac.getAlgorithm());
818    }
819
820    public void test_getProvider() throws NoSuchAlgorithmException {
821        Mac mac;
822        for (int i = 0; i < validValues.length; i++) {
823            mac = Mac.getInstance(validValues[i]);
824            assertNotNull(mac.getProvider());
825        }
826
827        mac = new Mock_Mac(null, null, null);
828        assertNull(mac.getProvider());
829    }
830
831    class Mock_Mac extends Mac {
832        protected Mock_Mac(MacSpi arg0, Provider arg1, String arg2) {
833            super(arg0, arg1, arg2);
834        }
835    }
836
837    public static Test suite() {
838        return new TestSuite(MacTest.class);
839    }
840}
841/**
842 * Additional class for Mac constructor verification
843 */
844class myMac extends Mac {
845
846    public myMac(MacSpi macSpi, Provider provider,
847            String algorithm) {
848        super(macSpi, provider, algorithm);
849    }
850}
851