1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.xnet.provider.jsse;
18
19import java.security.MessageDigest;
20import java.util.ArrayList;
21import java.util.Arrays;
22import java.util.List;
23import javax.crypto.Cipher;
24import javax.crypto.Mac;
25import junit.framework.TestCase;
26import libcore.java.security.StandardNames;
27import org.apache.harmony.xnet.provider.jsse.CipherSuite;
28
29public class CipherSuiteTest extends TestCase {
30    public void test_getByName() throws Exception {
31        for (String name : StandardNames.CIPHER_SUITES) {
32            if (name.equals(StandardNames.CIPHER_SUITE_SECURE_RENEGOTIATION)) {
33                assertNull(CipherSuite.getByName(name));
34            } else {
35                test_CipherSuite(name);
36            }
37        }
38
39        assertNull(CipherSuite.getByName("bogus"));
40        try {
41            CipherSuite.getByName(null);
42            fail();
43        } catch (NullPointerException expected) {
44        }
45    }
46
47    private void test_CipherSuite(String name) throws Exception {
48        CipherSuite cs = CipherSuite.getByName(name);
49        assertNotNull(name, cs);
50        assertEquals(name, cs.getName());
51        test_CipherSuite(cs);
52    }
53
54    private void test_CipherSuite(CipherSuite cs) throws Exception {
55        assertNotNull(cs);
56
57        String name = cs.getName();
58        assertNotNull(name);
59        assertSame(name, cs, CipherSuite.getByName(name));
60        assertTrue(name, StandardNames.CIPHER_SUITES.contains(name));
61        assertTrue(name, name.startsWith("SSL_") || name.startsWith("TLS_"));
62
63        assertEquals(cs.isAnonymous(), name.contains("_anon_"));
64
65        byte[] bytes = cs.toBytes();
66        assertNotNull(name, bytes);
67        assertEquals(name, 2, bytes.length);
68        assertTrue(name + bytes[0], bytes[0] == (byte) 0x00 || bytes[0] == (byte) 0xc0);
69        assertSame(name, cs, CipherSuite.getByCode(bytes[0], bytes[1]));
70        assertSame(name, cs, CipherSuite.getByCode((byte) 0, bytes[0], bytes[1]));
71
72        assertTrue(name, cs.toString().contains(name));
73
74        String bulkEncryptionAlgorithm = cs.getBulkEncryptionAlgorithm();
75        int blockSize = cs.getBlockSize();
76        if (bulkEncryptionAlgorithm == null) {
77            assertTrue(name, name.contains("_NULL_"));
78            assertEquals(name, 0, blockSize);
79        } else {
80            assertNotNull(name, Cipher.getInstance(cs.getBulkEncryptionAlgorithm()));
81            assertTrue(name, blockSize == 0 || blockSize == 8 || blockSize == 16);
82        }
83
84        String hmacName = cs.getHmacName();
85        assertNotNull(name, hmacName);
86        assertNotNull(name, Mac.getInstance(hmacName));
87
88        String hashName = cs.getHashName();
89        assertNotNull(name, hashName);
90        assertNotNull(name, MessageDigest.getInstance(hashName));
91
92        int macLength = cs.getMACLength();
93        assertTrue(name, macLength == 0 || macLength == 16 || macLength == 20);
94
95        assertTrue(name,
96                   cs.isExportable() == name.contains("_EXPORT_")
97                   || cs.isExportable() == name.contains("_NULL_"));
98
99        String keyType = cs.getServerKeyType();
100        assertEquals(name, cs.isAnonymous(), keyType == null);
101        assertTrue(name, keyType == null || StandardNames.KEY_TYPES.contains(keyType));
102    }
103
104    public void test_getByCode() {
105        // CipherSuite.getByCode is also covered by test_CipherSuite
106        assertUnknown(CipherSuite.getByCode((byte) 0x12, (byte) 0x34));
107        assertUnknown(CipherSuite.getByCode((byte) 0x12, (byte) 0x34, (byte) 0x56));
108        assertUnknown(CipherSuite.getByCode((byte) -1, (byte) -1));
109        assertUnknown(CipherSuite.getByCode((byte) -1, (byte) -1, (byte) -1));
110    }
111    private void assertUnknown(CipherSuite cs) {
112        assertNotNull(cs);
113        assertNotNull(cs.getName().contains("UNKNOWN"));
114    }
115
116    public void test_getSupported() throws Exception {
117        CipherSuite[] suites = CipherSuite.getSupported();
118        List<String> names = new ArrayList<String>(suites.length);
119        for (CipherSuite cs : suites) {
120            test_CipherSuite(cs);
121            names.add(cs.getName());
122        }
123        assertEquals(Arrays.asList(CipherSuite.getSupportedCipherSuiteNames()), names);
124    }
125
126    public void test_getSupportedCipherSuiteNames() throws Exception {
127        String[] names = CipherSuite.getSupportedCipherSuiteNames();
128        StandardNames.assertSupportedCipherSuites(StandardNames.CIPHER_SUITES_SSLENGINE, names);
129        for (String name : names) {
130            test_CipherSuite(name);
131        }
132    }
133
134    public void test_getClientKeyType() throws Exception {
135        byte b = Byte.MIN_VALUE;
136        do {
137            String byteString = Byte.toString(b);
138            String keyType = CipherSuite.getClientKeyType(b);
139            switch (b) {
140                case 1:
141                    assertEquals(byteString, "RSA", keyType);
142                    break;
143                case 2:
144                    assertEquals(byteString, "DSA", keyType);
145                    break;
146                case 3:
147                    assertEquals(byteString, "DH_RSA", keyType);
148                    break;
149                case 4:
150                    assertEquals(byteString, "DH_DSA", keyType);
151                    break;
152                case 64:
153                    assertEquals(byteString, "EC", keyType);
154                    break;
155                case 65:
156                    assertEquals(byteString, "EC_RSA", keyType);
157                    break;
158                case 66:
159                    assertEquals(byteString, "EC_EC", keyType);
160                    break;
161                default:
162                    assertNull(byteString, keyType);
163            }
164            b++;
165        } while (b != Byte.MIN_VALUE);
166    }
167}
168