• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. 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 distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.common.hash;
16 
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.verifyNoMoreInteractions;
20 import static org.mockito.Mockito.when;
21 
22 import com.google.common.testing.NullPointerTester;
23 import java.io.ByteArrayInputStream;
24 import java.util.Arrays;
25 import junit.framework.TestCase;
26 
27 /**
28  * Tests for {@link HashingInputStream}.
29  *
30  * @author Qian Huang
31  */
32 public class HashingInputStreamTest extends TestCase {
33   private Hasher hasher;
34   private HashFunction hashFunction;
35   private static final byte[] testBytes = new byte[] {'y', 'a', 'm', 's'};
36   private ByteArrayInputStream buffer;
37 
38   @Override
setUp()39   protected void setUp() throws Exception {
40     super.setUp();
41     hasher = mock(Hasher.class);
42     hashFunction = mock(HashFunction.class);
43     buffer = new ByteArrayInputStream(testBytes);
44 
45     when(hashFunction.newHasher()).thenReturn(hasher);
46   }
47 
testRead_putSingleByte()48   public void testRead_putSingleByte() throws Exception {
49     HashingInputStream in = new HashingInputStream(hashFunction, buffer);
50 
51     int b = in.read();
52     assertEquals('y', b);
53 
54     verify(hasher).putByte((byte) 'y');
55     verify(hashFunction).newHasher();
56     verifyNoMoreInteractions(hashFunction, hasher);
57   }
58 
testRead_putByteArray()59   public void testRead_putByteArray() throws Exception {
60     HashingInputStream in = new HashingInputStream(hashFunction, buffer);
61 
62     byte[] buf = new byte[4];
63     int numOfByteRead = in.read(buf, 0, buf.length);
64     assertEquals(4, numOfByteRead);
65     for (int i = 0; i < testBytes.length; i++) {
66       assertEquals(testBytes[i], buf[i]);
67     }
68 
69     verify(hasher).putBytes(testBytes, 0, testBytes.length);
70     verify(hashFunction).newHasher();
71     verifyNoMoreInteractions(hashFunction, hasher);
72   }
73 
testRead_putByteArrayAtPos()74   public void testRead_putByteArrayAtPos() throws Exception {
75     HashingInputStream in = new HashingInputStream(hashFunction, buffer);
76 
77     byte[] buf = new byte[3];
78     int numOfByteRead = in.read(buf, 0, 3);
79     assertEquals(3, numOfByteRead);
80     for (int i = 0; i < numOfByteRead; i++) {
81       assertEquals(testBytes[i], buf[i]);
82     }
83 
84     verify(hasher).putBytes(Arrays.copyOf(testBytes, 3), 0, 3);
85     verify(hashFunction).newHasher();
86     verifyNoMoreInteractions(hashFunction, hasher);
87   }
88 
testRead_putByteArrayOutOfBound()89   public void testRead_putByteArrayOutOfBound() throws Exception {
90     byte[] buf = new byte[100];
91     byte[] expectedBytes = buf.clone();
92     System.arraycopy(testBytes, 0, expectedBytes, 0, testBytes.length);
93 
94     HashingInputStream in = new HashingInputStream(hashFunction, buffer);
95 
96     int numOfByteRead = in.read(buf, 0, 100);
97     assertEquals(4, numOfByteRead);
98     for (int i = 0; i < numOfByteRead; i++) {
99       assertEquals(testBytes[i], buf[i]);
100     }
101 
102     verify(hasher).putBytes(expectedBytes, 0, 4);
103     verify(hashFunction).newHasher();
104     verifyNoMoreInteractions(hashFunction, hasher);
105   }
106 
testHash_hashesCorrectly()107   public void testHash_hashesCorrectly() throws Exception {
108     HashCode expectedHash = Hashing.md5().hashBytes(testBytes);
109     HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer);
110 
111     byte[] buf = new byte[4];
112     int numOfByteRead = in.read(buf, 0, buf.length);
113     assertEquals(4, numOfByteRead);
114 
115     assertEquals(expectedHash, in.hash());
116   }
117 
testHash_hashesCorrectlyReadOutOfBound()118   public void testHash_hashesCorrectlyReadOutOfBound() throws Exception {
119     HashCode expectedHash = Hashing.md5().hashBytes(testBytes);
120     HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer);
121 
122     byte[] buf = new byte[100];
123     int numOfByteRead = in.read(buf, 0, buf.length);
124     assertEquals(-1, in.read()); // additional read
125     assertEquals(4, numOfByteRead);
126 
127     assertEquals(expectedHash, in.hash());
128   }
129 
testHash_hashesCorrectlyForSkipping()130   public void testHash_hashesCorrectlyForSkipping() throws Exception {
131     HashCode expectedHash = Hashing.md5().hashBytes(new byte[] {'m', 's'});
132     HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer);
133 
134     long numOfByteSkipped = in.skip(2);
135     assertEquals(2, numOfByteSkipped);
136 
137     byte[] buf = new byte[4];
138     int numOfByteRead = in.read(buf, 0, buf.length);
139     assertEquals(2, numOfByteRead);
140 
141     assertEquals(expectedHash, in.hash());
142   }
143 
testChecksForNull()144   public void testChecksForNull() throws Exception {
145     NullPointerTester tester = new NullPointerTester();
146 
147     tester.testAllPublicInstanceMethods(new HashingInputStream(Hashing.md5(), buffer));
148     tester.testAllPublicStaticMethods(HashingInputStream.class);
149     tester.testAllPublicConstructors(HashingInputStream.class);
150   }
151 }
152