1 /* 2 * Copyright (c) 2013, 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 /* 25 @test 26 @bug 7088419 27 @summary Ensure that the byte-at-a-time, byte array, and DirectByteBuffer 28 methods of CRC32 and Adler32 are consistent across a range of inputs. 29 */ 30 31 import java.nio.ByteBuffer; 32 import java.util.zip.Adler32; 33 import java.util.zip.CRC32; 34 import java.util.zip.Checksum; 35 36 public class CRCandAdlerTest { 37 38 public static void main(String[] args) throws Exception { 39 40 byte[] b = initializedBytes(4096 * 4096); 41 42 { 43 CRC32 crc1 = new CRC32(); 44 CRC32 crc2 = new CRC32(); 45 CRC32 crc3 = new CRC32(); 46 CRC32 crc4 = new CRC32(); 47 48 crc1.update(b, 0, b.length); 49 updateSerial(crc2, b, 0, b.length); 50 updateDirect(crc3, b, 0, b.length); 51 updateSerialSlow(crc4, b, 0, b.length); 52 53 check(crc1, crc2); 54 check(crc3, crc4); 55 check(crc1, crc3); 56 57 crc1.update(17); 58 crc2.update(17); 59 crc3.update(17); 60 crc4.update(17); 61 62 crc1.update(b, 1, b.length-2); 63 updateSerial(crc2, b, 1, b.length-2); 64 updateDirect(crc3, b, 1, b.length-2); 65 updateSerialSlow(crc4, b, 1, b.length-2); 66 67 check(crc1, crc2); 68 check(crc3, crc4); 69 check(crc1, crc3); 70 71 report("finished huge crc", crc1, crc2, crc3, crc4); 72 73 for (int i = 0; i < 256; i++) { 74 for (int j = 0; j < 256; j += 1) { 75 crc1.update(b, i, j); 76 updateSerial(crc2, b, i, j); 77 updateDirect(crc3, b, i, j); 78 updateSerialSlow(crc4, b, i, j); 79 80 check(crc1, crc2); 81 check(crc3, crc4); 82 check(crc1, crc3); 83 84 } 85 } 86 87 report("finished small survey crc", crc1, crc2, crc3, crc4); 88 } 89 90 { 91 Adler32 crc1 = new Adler32(); 92 Adler32 crc2 = new Adler32(); 93 Adler32 crc3 = new Adler32(); 94 Adler32 crc4 = new Adler32(); 95 // Test long CRC. 96 97 crc1.update(b, 0, b.length); 98 updateSerial(crc2, b, 0, b.length); 99 updateDirect(crc3, b, 0, b.length); 100 updateSerialSlow(crc4, b, 0, b.length); 101 102 check(crc1, crc2); 103 check(crc3, crc4); 104 check(crc1, crc3); 105 106 crc1.update(17); 107 crc2.update(17); 108 crc3.update(17); 109 crc4.update(17); 110 111 crc1.update(b, 1, b.length-2); 112 updateSerial(crc2, b, 1, b.length-2); 113 updateDirect(crc3, b, 1, b.length-2); 114 updateSerialSlow(crc4, b, 1, b.length-2); 115 116 check(crc1, crc2); 117 check(crc3, crc4); 118 check(crc1, crc3); 119 120 report("finished huge adler32", crc1, crc2, crc3, crc4); 121 122 for (int i = 0; i < 256; i++) { 123 for (int j = 0; j < 256; j += 1) { 124 crc1.update(b, i, j); 125 updateSerial(crc2, b, i, j); 126 updateDirect(crc3, b, i, j); 127 updateSerialSlow(crc4, b, i, j); 128 129 check(crc1, crc2); 130 check(crc3, crc4); 131 check(crc1, crc3); 132 133 } 134 } 135 136 report("finished small survey adler32", crc1, crc2, crc3, crc4); 137 } 138 } 139 140 141 private static void report(String s, Checksum crc1, Checksum crc2, 142 Checksum crc3, Checksum crc4) { 143 System.out.println(s + ", crc1 = " + crc1.getValue() + 144 ", crc2 = " + crc2.getValue()+ 145 ", crc3 = " + crc3.getValue()+ 146 ", crc4 = " + crc4.getValue()); 147 } 148 149 private static void check(Checksum crc1, Checksum crc2) throws Exception { 150 if (crc1.getValue() != crc2.getValue()) { 151 String s = "value 1 = " + crc1.getValue() + ", value 2 = " + crc2.getValue(); 152 System.err.println(s); 153 throw new Exception(s); 154 } 155 } 156 157 private static byte[] initializedBytes(int M) { 158 byte[] bytes = new byte[M]; 159 for (int i = 0; i < bytes.length; i++) { 160 bytes[i] = (byte) i; 161 } 162 return bytes; 163 } 164 165 private static void updateSerial(Checksum crc, byte[] b, int start, int length) { 166 for (int i = 0; i < length; i++) 167 crc.update(b[i+start]); 168 } 169 170 private static void updateSerialSlow(Checksum crc, byte[] b, int start, int length) { 171 for (int i = 0; i < length; i++) 172 crc.update(b[i+start]); 173 crc.getValue(); 174 } 175 176 private static void updateDirect(CRC32 crc3, byte[] b, int start, int length) { 177 ByteBuffer buf = ByteBuffer.allocateDirect(length); 178 buf.put(b, start, length); 179 buf.flip(); 180 crc3.update(buf); 181 } 182 183 private static void updateDirect(Adler32 crc3, byte[] b, int start, int length) { 184 ByteBuffer buf = ByteBuffer.allocateDirect(length); 185 buf.put(b, start, length); 186 buf.flip(); 187 crc3.update(buf); 188 } 189 190 }