1 // Copyright 2023 Google LLC 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.internal; 18 19 import static com.google.common.truth.Truth.assertThat; 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 final class SlowInputStreamTest { 27 @Test testNoArgsRead_works()28 public void testNoArgsRead_works() throws Exception { 29 byte[] b = new byte[] {(byte) 254, 0, (byte) 255}; 30 SlowInputStream s = SlowInputStream.copyFrom(b); 31 assertThat(s.read()).isEqualTo(254); 32 assertThat(s.read()).isEqualTo(0); 33 assertThat(s.read()).isEqualTo(255); 34 assertThat(s.read()).isEqualTo(-1); 35 } 36 37 @Test testWithBuffer_works()38 public void testWithBuffer_works() throws Exception { 39 byte[] b = new byte[] {(byte) 254, 0, (byte) 255}; 40 SlowInputStream s = SlowInputStream.copyFrom(b); 41 42 byte[] buffer = new byte[10]; 43 assertThat(s.read(buffer, 0, 10)).isEqualTo(1); 44 assertThat(buffer).isEqualTo(new byte[] {(byte) 254, 0, 0, 0, 0, 0, 0, 0, 0, 0}); 45 assertThat(s.read(buffer, 0, 10)).isEqualTo(1); 46 assertThat(buffer).isEqualTo(new byte[] {(byte) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); 47 assertThat(s.read(buffer, 0, 10)).isEqualTo(1); 48 assertThat(buffer).isEqualTo(new byte[] {(byte) 255, 0, 0, 0, 0, 0, 0, 0, 0, 0}); 49 assertThat(s.read(buffer, 0, 10)).isEqualTo(-1); 50 } 51 52 @Test testWithBuffer_len0()53 public void testWithBuffer_len0() throws Exception { 54 byte[] b = new byte[] {(byte) 254, 0, (byte) 255}; 55 SlowInputStream s = SlowInputStream.copyFrom(b); 56 assertThat(s.read(b, 0, 0)).isEqualTo(0); 57 } 58 } 59