1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.util.zip;
18
19import junit.framework.TestCase;
20
21import java.util.zip.Adler32;
22import java.util.zip.CRC32;
23
24/**
25 * tests for CRC32 and Adler32 checksum algorithms.
26 */
27public class OldAndroidChecksumTest extends TestCase {
28
29    public void testChecksum() throws Exception {
30        /*
31         * Values computed experimentally, using C interfaces.
32         */
33        adler32Test(mTestString, 0x9de210dbL);
34        cRC32Test(mTestString, 0x939f04afL);
35
36        // Test for issue 1016037
37        wrongChecksumWithAdler32Test();
38    }
39
40    private void adler32Test(byte[] values, long expected) {
41        Adler32 adler = new Adler32();
42
43        // try it all at once
44        adler.update(values);
45        assertEquals(adler.getValue(), expected);
46
47        // try resetting and computing one byte at a time
48        adler.reset();
49        for (int i = 0; i < values.length; i++) {
50            adler.update(values[i]);
51        }
52        assertEquals(adler.getValue(), expected);
53    }
54
55    private void cRC32Test(byte[] values, long expected) {
56        CRC32 crc = new CRC32();
57
58        // try it all at once
59        crc.update(values);
60        assertEquals(crc.getValue(), expected);
61
62        // try resetting and computing one byte at a time
63        crc.reset();
64        for (int i = 0; i < values.length; i++) {
65            crc.update(values[i]);
66        }
67        assertEquals(crc.getValue(), expected);
68    }
69
70    // "The quick brown fox jumped over the lazy dogs\n"
71    private static byte[] mTestString = {
72            0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63,
73            0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20,
74            0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70,
75            0x65, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20,
76            0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79,
77            0x20, 0x64, 0x6f, 0x67, 0x73, 0x2e, 0x0a
78    };
79
80
81    // Test for issue 1016037
82    private void wrongChecksumWithAdler32Test() {
83        byte[] bytes = {1, 0, 5, 0, 15, 0, 1, 11, 0, 1};
84        Adler32 adler = new Adler32();
85        adler.update(bytes);
86        long arrayChecksum = adler.getValue();
87        adler.reset();
88        for (int i = 0; i < bytes.length; i++) {
89            adler.update(bytes[i]);
90        }
91        assertEquals("Checksums not equal: expected: " + arrayChecksum +
92                " actual: " + adler.getValue(), arrayChecksum, adler.getValue());
93    }
94}
95
96