1 /* 2 * Copyright (C) 2017 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 17 package org.conscrypt; 18 19 import static org.junit.Assert.assertArrayEquals; 20 21 import org.junit.Test; 22 import org.junit.runner.RunWith; 23 import org.junit.runners.JUnit4; 24 25 @RunWith(JUnit4.class) 26 public class SSLUtilsTest { 27 private static final byte[] VALID_CHARACTERS = 28 "0123456789abcdefghijklmnopqrstuvwxyz".getBytes(); 29 30 @Test noProtocolsShouldSucceed()31 public void noProtocolsShouldSucceed() { 32 byte[] expected = new byte[0]; 33 byte[] actual = SSLUtils.toLengthPrefixedList(); 34 assertArrayEquals(expected, actual); 35 } 36 37 @Test(expected = IllegalArgumentException.class) emptyProtocolShouldThrow()38 public void emptyProtocolShouldThrow() { 39 SSLUtils.toLengthPrefixedList(""); 40 } 41 42 @Test(expected = IllegalArgumentException.class) longProtocolShouldThrow()43 public void longProtocolShouldThrow() { 44 SSLUtils.toLengthPrefixedList(new String(newValidProtocol(256))); 45 } 46 47 @Test(expected = IllegalArgumentException.class) protocolWithInvalidCharacterShouldThrow()48 public void protocolWithInvalidCharacterShouldThrow() { 49 SSLUtils.toLengthPrefixedList("This is a bad character: €"); 50 } 51 52 @Test validProtocolsShouldSucceed()53 public void validProtocolsShouldSucceed() { 54 byte[][] protocols = new byte[][]{ 55 "protocol-1".getBytes(), 56 "protocol-2".getBytes(), 57 "protocol-3".getBytes(), 58 }; 59 byte[] expected = getExpectedEncodedBytes(protocols); 60 byte[] actual = SSLUtils.toLengthPrefixedList(toStrings(protocols)); 61 assertArrayEquals(expected, actual); 62 } 63 toStrings(byte[][] protocols)64 private static String[] toStrings(byte[][] protocols) { 65 int numProtocols = protocols.length; 66 String[] out = new String[numProtocols]; 67 for(int i = 0; i < numProtocols; ++i) { 68 out[i] = new String(protocols[i]); 69 } 70 return out; 71 } 72 getExpectedEncodedBytes(byte[][] protocols)73 private static byte[] getExpectedEncodedBytes(byte[][] protocols) { 74 int numProtocols = protocols.length; 75 int encodedLength = numProtocols; 76 for (byte[] protocol : protocols) { 77 encodedLength += protocol.length; 78 } 79 byte[] encoded = new byte[encodedLength]; 80 for(int encodedIndex = 0, i = 0; i < numProtocols; ++i) { 81 byte[] protocol = protocols[i]; 82 encoded[encodedIndex++] = (byte) protocol.length; 83 System.arraycopy(protocol, 0, encoded, encodedIndex, protocol.length); 84 encodedIndex += protocol.length; 85 } 86 return encoded; 87 } 88 newValidProtocol(int length)89 private static byte[] newValidProtocol(int length) { 90 byte[] chars = new byte[length]; 91 for (int i = 0; i < length; ++i) { 92 int charIndex = i % VALID_CHARACTERS.length; 93 chars[i] = VALID_CHARACTERS[charIndex]; 94 } 95 return chars; 96 } 97 } 98