• 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   @SuppressWarnings("DoNotMock")
39   @Override
setUp()40   protected void setUp() throws Exception {
41     super.setUp();
42     hasher = mock(Hasher.class);
43     hashFunction = mock(HashFunction.class);
44     buffer = new ByteArrayInputStream(testBytes);
45 
46     when(hashFunction.newHasher()).thenReturn(hasher);
47   }
48 
testRead_putSingleByte()49   public void testRead_putSingleByte() throws Exception {
50     HashingInputStream in = new HashingInputStream(hashFunction, buffer);
51 
52     int b = in.read();
53     assertEquals('y', b);
54 
55     verify(hasher).putByte((byte) 'y');
56     verify(hashFunction).newHasher();
57     verifyNoMoreInteractions(hashFunction, hasher);
58   }
59 
testRead_putByteArray()60   public void testRead_putByteArray() throws Exception {
61     HashingInputStream in = new HashingInputStream(hashFunction, buffer);
62 
63     byte[] buf = new byte[4];
64     int numOfByteRead = in.read(buf, 0, buf.length);
65     assertEquals(4, numOfByteRead);
66     for (int i = 0; i < testBytes.length; i++) {
67       assertEquals(testBytes[i], buf[i]);
68     }
69 
70     verify(hasher).putBytes(testBytes, 0, testBytes.length);
71     verify(hashFunction).newHasher();
72     verifyNoMoreInteractions(hashFunction, hasher);
73   }
74 
testRead_putByteArrayAtPos()75   public void testRead_putByteArrayAtPos() throws Exception {
76     HashingInputStream in = new HashingInputStream(hashFunction, buffer);
77 
78     byte[] buf = new byte[3];
79     int numOfByteRead = in.read(buf, 0, 3);
80     assertEquals(3, numOfByteRead);
81     for (int i = 0; i < numOfByteRead; i++) {
82       assertEquals(testBytes[i], buf[i]);
83     }
84 
85     verify(hasher).putBytes(Arrays.copyOf(testBytes, 3), 0, 3);
86     verify(hashFunction).newHasher();
87     verifyNoMoreInteractions(hashFunction, hasher);
88   }
89 
testRead_putByteArrayOutOfBound()90   public void testRead_putByteArrayOutOfBound() throws Exception {
91     byte[] buf = new byte[100];
92     byte[] expectedBytes = buf.clone();
93     System.arraycopy(testBytes, 0, expectedBytes, 0, testBytes.length);
94 
95     HashingInputStream in = new HashingInputStream(hashFunction, buffer);
96 
97     int numOfByteRead = in.read(buf, 0, 100);
98     assertEquals(4, numOfByteRead);
99     for (int i = 0; i < numOfByteRead; i++) {
100       assertEquals(testBytes[i], buf[i]);
101     }
102 
103     verify(hasher).putBytes(expectedBytes, 0, 4);
104     verify(hashFunction).newHasher();
105     verifyNoMoreInteractions(hashFunction, hasher);
106   }
107 
testHash_hashesCorrectly()108   public void testHash_hashesCorrectly() throws Exception {
109     HashCode expectedHash = Hashing.md5().hashBytes(testBytes);
110     HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer);
111 
112     byte[] buf = new byte[4];
113     int numOfByteRead = in.read(buf, 0, buf.length);
114     assertEquals(4, numOfByteRead);
115 
116     assertEquals(expectedHash, in.hash());
117   }
118 
testHash_hashesCorrectlyReadOutOfBound()119   public void testHash_hashesCorrectlyReadOutOfBound() throws Exception {
120     HashCode expectedHash = Hashing.md5().hashBytes(testBytes);
121     HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer);
122 
123     byte[] buf = new byte[100];
124     int numOfByteRead = in.read(buf, 0, buf.length);
125     assertEquals(-1, in.read()); // additional read
126     assertEquals(4, numOfByteRead);
127 
128     assertEquals(expectedHash, in.hash());
129   }
130 
testHash_hashesCorrectlyForSkipping()131   public void testHash_hashesCorrectlyForSkipping() throws Exception {
132     HashCode expectedHash = Hashing.md5().hashBytes(new byte[] {'m', 's'});
133     HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer);
134 
135     long numOfByteSkipped = in.skip(2);
136     assertEquals(2, numOfByteSkipped);
137 
138     byte[] buf = new byte[4];
139     int numOfByteRead = in.read(buf, 0, buf.length);
140     assertEquals(2, numOfByteRead);
141 
142     assertEquals(expectedHash, in.hash());
143   }
144 
testChecksForNull()145   public void testChecksForNull() throws Exception {
146     NullPointerTester tester = new NullPointerTester();
147 
148     tester.testAllPublicInstanceMethods(new HashingInputStream(Hashing.md5(), buffer));
149     tester.testAllPublicStaticMethods(HashingInputStream.class);
150     tester.testAllPublicConstructors(HashingInputStream.class);
151   }
152 }
153