1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 // Android-added: package declaration 25 package test.java.util.zip; 26 27 /** 28 * Base class for Checksum tests 29 */ 30 import java.nio.ByteBuffer; 31 import java.nio.ByteOrder; 32 import java.nio.charset.StandardCharsets; 33 import java.util.zip.Checksum; 34 35 public class ChecksumBase { 36 37 private static final byte[] BYTES_123456789 = "123456789".getBytes(StandardCharsets.US_ASCII); 38 testAll(Checksum checksum, long expected)39 public static void testAll(Checksum checksum, long expected) { 40 testBytes(checksum, expected); 41 testByteArray(checksum, expected); 42 testWrappedByteBuffer(checksum, expected); 43 testReadonlyByteBuffer(checksum, expected); 44 testDirectByteBuffer(checksum, expected); 45 testByteArrayOffset(checksum, expected); 46 testDirectByteBufferOffset(checksum, expected); 47 testLittleEndianDirectByteBufferOffset(checksum, expected); 48 testWrappedByteBufferOffset(checksum, expected); 49 testLittleEndianWrappedByteBufferOffset(checksum, expected); 50 testReadonlyByteBufferOffset(checksum, expected); 51 testLittleEndianReadonlyByteBufferOffset(checksum, expected); 52 } 53 testBytes(Checksum checksum, long expected)54 private static void testBytes(Checksum checksum, long expected) { 55 checksum.reset(); 56 for (byte bits : BYTES_123456789) { 57 checksum.update(bits); 58 } 59 checkChecksum(checksum, expected); 60 } 61 testByteArray(Checksum checksum, long expected)62 private static void testByteArray(Checksum checksum, long expected) { 63 checksum.reset(); 64 checksum.update(BYTES_123456789); 65 checkChecksum(checksum, expected); 66 } 67 testWrappedByteBuffer(Checksum checksum, long expected)68 private static void testWrappedByteBuffer(Checksum checksum, long expected) { 69 checksum.reset(); 70 ByteBuffer bb = ByteBuffer.wrap(BYTES_123456789); 71 checksum.update(bb); 72 checkChecksum(checksum, expected); 73 } 74 testReadonlyByteBuffer(Checksum checksum, long expected)75 private static void testReadonlyByteBuffer(Checksum checksum, long expected) { 76 checksum.reset(); 77 ByteBuffer bb = ByteBuffer.wrap(BYTES_123456789).asReadOnlyBuffer(); 78 checksum.update(bb); 79 checkChecksum(checksum, expected); 80 } 81 testDirectByteBuffer(Checksum checksum, long expected)82 private static void testDirectByteBuffer(Checksum checksum, long expected) { 83 checksum.reset(); 84 ByteBuffer bb = ByteBuffer.allocateDirect(BYTES_123456789.length); 85 bb.put(BYTES_123456789); 86 bb.rewind(); 87 checksum.update(bb); 88 checkChecksum(checksum, expected); 89 } 90 checkChecksum(Checksum checksum, long expected)91 private static void checkChecksum(Checksum checksum, long expected) { 92 if (checksum.getValue() != expected) { 93 throw new RuntimeException("Calculated checksum result was invalid." 94 + " Expected " + Long.toHexString(expected) 95 + ", but got " + Long.toHexString(checksum.getValue()) + "."); 96 } 97 } 98 testByteArrayOffset(Checksum checksum, long expected)99 private static void testByteArrayOffset(Checksum checksum, long expected) { 100 byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64]; 101 for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) { 102 checksum.reset(); 103 System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length); 104 checksum.update(unaligned_bytes_123456789, i, BYTES_123456789.length); 105 checkChecksumOffset(checksum, expected, i); 106 } 107 } 108 testDirectByteBufferOffset(Checksum checksum, long expected)109 private static void testDirectByteBufferOffset(Checksum checksum, long expected) { 110 byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64]; 111 for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) { 112 checksum.reset(); 113 ByteBuffer bb = ByteBuffer.allocateDirect(unaligned_bytes_123456789.length); 114 System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length); 115 bb.put(unaligned_bytes_123456789); 116 bb.position(i); 117 bb.limit(i + BYTES_123456789.length); 118 checksum.update(bb); 119 checkChecksumOffset(checksum, expected, i); 120 } 121 } 122 testLittleEndianDirectByteBufferOffset(Checksum checksum, long expected)123 private static void testLittleEndianDirectByteBufferOffset(Checksum checksum, long expected) { 124 byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64]; 125 for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) { 126 checksum.reset(); 127 ByteBuffer bb = ByteBuffer.allocateDirect(unaligned_bytes_123456789.length); 128 bb.order(ByteOrder.LITTLE_ENDIAN); 129 System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length); 130 bb.put(unaligned_bytes_123456789); 131 bb.position(i); 132 bb.limit(i + BYTES_123456789.length); 133 checksum.update(bb); 134 checkChecksumOffset(checksum, expected, i); 135 } 136 } 137 testWrappedByteBufferOffset(Checksum checksum, long expected)138 private static void testWrappedByteBufferOffset(Checksum checksum, long expected) { 139 byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64]; 140 for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) { 141 checksum.reset(); 142 System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length); 143 ByteBuffer bb = ByteBuffer.wrap(unaligned_bytes_123456789); 144 bb.position(i); 145 bb.limit(i + BYTES_123456789.length); 146 checksum.update(bb); 147 checkChecksumOffset(checksum, expected, i); 148 } 149 } 150 testLittleEndianWrappedByteBufferOffset(Checksum checksum, long expected)151 private static void testLittleEndianWrappedByteBufferOffset(Checksum checksum, long expected) { 152 byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64]; 153 for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) { 154 checksum.reset(); 155 System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length); 156 ByteBuffer bb = ByteBuffer.wrap(unaligned_bytes_123456789); 157 bb.order(ByteOrder.LITTLE_ENDIAN); 158 bb.position(i); 159 bb.limit(i + BYTES_123456789.length); 160 checksum.update(bb); 161 checkChecksumOffset(checksum, expected, i); 162 } 163 } 164 testReadonlyByteBufferOffset(Checksum checksum, long expected)165 private static void testReadonlyByteBufferOffset(Checksum checksum, long expected) { 166 byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64]; 167 for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) { 168 checksum.reset(); 169 System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length); 170 ByteBuffer bb = ByteBuffer.wrap(unaligned_bytes_123456789).asReadOnlyBuffer(); 171 bb.position(i); 172 bb.limit(i + BYTES_123456789.length); 173 checksum.update(bb); 174 checkChecksumOffset(checksum, expected, i); 175 } 176 } 177 testLittleEndianReadonlyByteBufferOffset(Checksum checksum, long expected)178 private static void testLittleEndianReadonlyByteBufferOffset(Checksum checksum, long expected) { 179 byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64]; 180 for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) { 181 checksum.reset(); 182 System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length); 183 ByteBuffer bb = ByteBuffer.wrap(unaligned_bytes_123456789).asReadOnlyBuffer(); 184 bb.order(ByteOrder.LITTLE_ENDIAN); 185 bb.position(i); 186 bb.limit(i + BYTES_123456789.length); 187 checksum.update(bb); 188 checkChecksumOffset(checksum, expected, i); 189 } 190 } 191 checkChecksumOffset(Checksum checksum, long expected, int offset)192 private static void checkChecksumOffset(Checksum checksum, long expected, int offset) { 193 if (checksum.getValue() != expected) { 194 throw new RuntimeException("Calculated CRC32C result was invalid. Array offset " 195 + offset + ". Expected: " + Long.toHexString(expected) + ", Got: " 196 + Long.toHexString(checksum.getValue())); 197 } 198 } 199 } 200