1 // Copyright 2017 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 //////////////////////////////////////////////////////////////////////////////// 16 17 package com.google.crypto.tink.util; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.assertThrows; 21 22 import org.junit.Test; 23 import org.junit.runner.RunWith; 24 import org.junit.runners.JUnit4; 25 26 /** Tests for {@link Bytes} */ 27 @RunWith(JUnit4.class) 28 public class BytesTest { 29 @Test testBasicWorks()30 public void testBasicWorks() throws Exception { 31 byte[] plainArray = new byte[] {0, 1, 2, 3, 4, 5, 6, 7}; 32 Bytes array = Bytes.copyFrom(plainArray); 33 assertThat(array.toByteArray()).isEqualTo(plainArray); 34 } 35 36 @Test testWithRange()37 public void testWithRange() throws Exception { 38 byte[] plainArray = new byte[] {100, 100, 100, 0, 1, 2, 3, 4, 5, 6, 7, 100, 100, 100}; 39 Bytes array = Bytes.copyFrom(plainArray, 3, 8); 40 assertThat(array.toByteArray()).isEqualTo(new byte[] {0, 1, 2, 3, 4, 5, 6, 7}); 41 } 42 43 @Test testWithRange_rangeTooBig()44 public void testWithRange_rangeTooBig() throws Exception { 45 byte[] plainArray = new byte[] {100, 100, 100, 0, 1, 2, 3, 4, 5, 6, 7}; 46 Bytes array = Bytes.copyFrom(plainArray, 3, 100); 47 assertThat(array.toByteArray()).isEqualTo(new byte[] {0, 1, 2, 3, 4, 5, 6, 7}); 48 } 49 50 @Test testGetLength()51 public void testGetLength() throws Exception { 52 byte[] plainArray = new byte[] {100, 100, 100, 0, 1, 2, 3, 4, 5, 6, 7, 100, 100, 100}; 53 Bytes array = Bytes.copyFrom(plainArray, 3, 8); 54 assertThat(array.size()).isEqualTo(8); 55 } 56 57 @Test testImmutability_inputCopied1()58 public void testImmutability_inputCopied1() throws Exception { 59 byte[] plainArray = new byte[] {100, 100, 100, 0, 1, 2, 3, 4, 5, 6, 7, 100, 100, 100}; 60 Bytes array = Bytes.copyFrom(plainArray, 3, 8); 61 plainArray[5] = 55; 62 assertThat(array.toByteArray()).isEqualTo(new byte[] {0, 1, 2, 3, 4, 5, 6, 7}); 63 } 64 65 @Test testImmutability_inputCopied2()66 public void testImmutability_inputCopied2() throws Exception { 67 byte[] plainArray = new byte[] {0, 1, 2, 3, 4, 5, 6, 7}; 68 Bytes array = Bytes.copyFrom(plainArray); 69 plainArray[5] = 55; 70 assertThat(array.toByteArray()).isEqualTo(new byte[] {0, 1, 2, 3, 4, 5, 6, 7}); 71 } 72 73 @Test testImmutability_outputCopied()74 public void testImmutability_outputCopied() throws Exception { 75 byte[] plainArray = new byte[] {100, 100, 100, 0, 1, 2, 3, 4, 5, 6, 7, 100, 100, 100}; 76 Bytes array = Bytes.copyFrom(plainArray, 3, 8); 77 array.toByteArray()[5] = 55; 78 assertThat(array.toByteArray()).isEqualTo(new byte[] {0, 1, 2, 3, 4, 5, 6, 7}); 79 } 80 81 @Test testEquals()82 public void testEquals() throws Exception { 83 byte[] plainArray = new byte[] {1, 2, 3, 1, 2, 3}; 84 Bytes byteArray = Bytes.copyFrom(plainArray); 85 assertThat(byteArray.equals(byteArray)).isTrue(); 86 assertThat(byteArray.equals(Bytes.copyFrom(plainArray))).isTrue(); 87 88 assertThat(byteArray.equals(Bytes.copyFrom(plainArray, 0, 5))).isFalse(); 89 assertThat(byteArray.equals(Bytes.copyFrom(plainArray, 1, 5))).isFalse(); 90 91 assertThat(Bytes.copyFrom(plainArray, 0, 3).equals(Bytes.copyFrom(plainArray, 3, 3))).isTrue(); 92 } 93 94 @Test 95 @SuppressWarnings("EqualsIncompatibleType") testEquals_differentObject()96 public void testEquals_differentObject() throws Exception { 97 assertThat(Bytes.copyFrom(new byte[] {}).equals(Integer.valueOf(0))).isFalse(); 98 } 99 100 @Test testHashCode()101 public void testHashCode() throws Exception { 102 byte[] plainArray = new byte[] {1, 2, 3, 1, 2, 3}; 103 Bytes byteArray = Bytes.copyFrom(plainArray); 104 assertThat(byteArray.hashCode()).isEqualTo(Bytes.copyFrom(plainArray).hashCode()); 105 106 assertThat(Bytes.copyFrom(plainArray, 0, 3).hashCode()) 107 .isEqualTo(Bytes.copyFrom(plainArray, 3, 3).hashCode()); 108 } 109 110 @Test testHashCode_notAlwaysTheSame()111 public void testHashCode_notAlwaysTheSame() throws Exception { 112 int hashCode = Bytes.copyFrom(new byte[] {0}).hashCode(); 113 byte b = 1; 114 while (Bytes.copyFrom(new byte[] {(byte) b}).hashCode() == hashCode && b != 0) { 115 b++; 116 } 117 assertThat(Bytes.copyFrom(new byte[] {(byte) b}).hashCode()).isNotEqualTo(hashCode); 118 } 119 120 @Test testCopyFrom_null_throwsNPE()121 public void testCopyFrom_null_throwsNPE() throws Exception { 122 assertThrows(NullPointerException.class, () -> Bytes.copyFrom(null)); 123 assertThrows(NullPointerException.class, () -> Bytes.copyFrom(null, 0, 0)); 124 } 125 } 126