aboutsummaryrefslogtreecommitdiff
path: root/android/guava-tests/test/com/google/common/hash/HashingInputStreamTest.java
blob: 05351d94cddea2da134665c9f325328b4f6d6212 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (C) 2013 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.common.hash;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import com.google.common.testing.NullPointerTester;
import java.io.ByteArrayInputStream;
import java.util.Arrays;
import junit.framework.TestCase;

/**
 * Tests for {@link HashingInputStream}.
 *
 * @author Qian Huang
 */
public class HashingInputStreamTest extends TestCase {
  private Hasher hasher;
  private HashFunction hashFunction;
  private static final byte[] testBytes = new byte[] {'y', 'a', 'm', 's'};
  private ByteArrayInputStream buffer;

  @SuppressWarnings("DoNotMock")
  @Override
  protected void setUp() throws Exception {
    super.setUp();
    hasher = mock(Hasher.class);
    hashFunction = mock(HashFunction.class);
    buffer = new ByteArrayInputStream(testBytes);

    when(hashFunction.newHasher()).thenReturn(hasher);
  }

  public void testRead_putSingleByte() throws Exception {
    HashingInputStream in = new HashingInputStream(hashFunction, buffer);

    int b = in.read();
    assertEquals('y', b);

    verify(hasher).putByte((byte) 'y');
    verify(hashFunction).newHasher();
    verifyNoMoreInteractions(hashFunction, hasher);
  }

  public void testRead_putByteArray() throws Exception {
    HashingInputStream in = new HashingInputStream(hashFunction, buffer);

    byte[] buf = new byte[4];
    int numOfByteRead = in.read(buf, 0, buf.length);
    assertEquals(4, numOfByteRead);
    for (int i = 0; i < testBytes.length; i++) {
      assertEquals(testBytes[i], buf[i]);
    }

    verify(hasher).putBytes(testBytes, 0, testBytes.length);
    verify(hashFunction).newHasher();
    verifyNoMoreInteractions(hashFunction, hasher);
  }

  public void testRead_putByteArrayAtPos() throws Exception {
    HashingInputStream in = new HashingInputStream(hashFunction, buffer);

    byte[] buf = new byte[3];
    int numOfByteRead = in.read(buf, 0, 3);
    assertEquals(3, numOfByteRead);
    for (int i = 0; i < numOfByteRead; i++) {
      assertEquals(testBytes[i], buf[i]);
    }

    verify(hasher).putBytes(Arrays.copyOf(testBytes, 3), 0, 3);
    verify(hashFunction).newHasher();
    verifyNoMoreInteractions(hashFunction, hasher);
  }

  public void testRead_putByteArrayOutOfBound() throws Exception {
    byte[] buf = new byte[100];
    byte[] expectedBytes = buf.clone();
    System.arraycopy(testBytes, 0, expectedBytes, 0, testBytes.length);

    HashingInputStream in = new HashingInputStream(hashFunction, buffer);

    int numOfByteRead = in.read(buf, 0, 100);
    assertEquals(4, numOfByteRead);
    for (int i = 0; i < numOfByteRead; i++) {
      assertEquals(testBytes[i], buf[i]);
    }

    verify(hasher).putBytes(expectedBytes, 0, 4);
    verify(hashFunction).newHasher();
    verifyNoMoreInteractions(hashFunction, hasher);
  }

  public void testHash_hashesCorrectly() throws Exception {
    HashCode expectedHash = Hashing.md5().hashBytes(testBytes);
    HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer);

    byte[] buf = new byte[4];
    int numOfByteRead = in.read(buf, 0, buf.length);
    assertEquals(4, numOfByteRead);

    assertEquals(expectedHash, in.hash());
  }

  public void testHash_hashesCorrectlyReadOutOfBound() throws Exception {
    HashCode expectedHash = Hashing.md5().hashBytes(testBytes);
    HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer);

    byte[] buf = new byte[100];
    int numOfByteRead = in.read(buf, 0, buf.length);
    assertEquals(-1, in.read()); // additional read
    assertEquals(4, numOfByteRead);

    assertEquals(expectedHash, in.hash());
  }

  public void testHash_hashesCorrectlyForSkipping() throws Exception {
    HashCode expectedHash = Hashing.md5().hashBytes(new byte[] {'m', 's'});
    HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer);

    long numOfByteSkipped = in.skip(2);
    assertEquals(2, numOfByteSkipped);

    byte[] buf = new byte[4];
    int numOfByteRead = in.read(buf, 0, buf.length);
    assertEquals(2, numOfByteRead);

    assertEquals(expectedHash, in.hash());
  }

  public void testChecksForNull() throws Exception {
    NullPointerTester tester = new NullPointerTester();

    tester.testAllPublicInstanceMethods(new HashingInputStream(Hashing.md5(), buffer));
    tester.testAllPublicStaticMethods(HashingInputStream.class);
    tester.testAllPublicConstructors(HashingInputStream.class);
  }
}