SecureRandomSpiTest.java revision cec4dd4b1d33f78997603d0f89c0d0e56e64dbcd
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
18package org.apache.harmony.security.tests.java.security;
19
20import dalvik.annotation.TestTargetClass;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetNew;
24
25import java.security.SecureRandomSpi;
26
27import junit.framework.TestCase;
28
29/**
30 * Tests for <code>SecureRandomSpi</code> class constructors
31 * and methods.
32 *
33 */
34@TestTargetClass(SecureRandomSpi.class)
35public class SecureRandomSpiTest extends TestCase {
36
37    /**
38     * Test for <code>SecureRandomSpi</code> constructor
39     * Assertion: constructs SecureRandomSpi
40     */
41    @TestTargets({
42        @TestTargetNew(
43            level = TestLevel.COMPLETE,
44            notes = "",
45            method = "SecureRandomSpi",
46            args = {}
47        ),
48        @TestTargetNew(
49            level = TestLevel.COMPLETE,
50            notes = "",
51            method = "engineGenerateSeed",
52            args = {int.class}
53        ),
54        @TestTargetNew(
55            level = TestLevel.COMPLETE,
56            notes = "",
57            method = "engineNextBytes",
58            args = {byte[].class}
59        ),
60        @TestTargetNew(
61            level = TestLevel.COMPLETE,
62            notes = "",
63            method = "engineSetSeed",
64            args = {byte[].class}
65        )
66    })
67    public void testSecureRandomSpi() {
68        try {
69            MySecureRandomSpi srs = new MySecureRandomSpi();
70            assertTrue(srs instanceof SecureRandomSpi);
71        } catch (Exception e) {
72            fail("Unexpected exception");
73        }
74
75        try {
76            MySecureRandomSpi srs = new MySecureRandomSpi();
77            srs.engineGenerateSeed(10);
78            srs.engineNextBytes(new byte[10]);
79            srs.engineSetSeed(new byte[3]);
80        } catch (Exception e) {
81            fail("Unexpected exception");
82        }
83    }
84
85    public class MySecureRandomSpi extends SecureRandomSpi {
86        protected void engineSetSeed(byte[] seed) {}
87        protected void engineNextBytes(byte[] bytes) {}
88        protected byte[] engineGenerateSeed(int numBytes) {
89            return null;
90        }
91    }
92}