• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.core;
18 
19 import junit.framework.TestCase;
20 
21 import java.util.zip.Adler32;
22 import java.util.zip.CRC32;
23 import android.test.suitebuilder.annotation.SmallTest;
24 
25 /**
26  * tests for CRC32 and Adler32 checksum algorithms.
27  */
28 public class ChecksumTest extends TestCase {
29 
30     @SmallTest
testChecksum()31     public void testChecksum() throws Exception {
32         /*
33          * Values computed experimentally, using C interfaces.
34          */
35         adler32Test(mTestString, 0x9de210dbL);
36         cRC32Test(mTestString, 0x939f04afL);
37 
38         // Test for issue 1016037
39         wrongChecksumWithAdler32Test();
40     }
41 
adler32Test(byte[] values, long expected)42     private void adler32Test(byte[] values, long expected) {
43         Adler32 adler = new Adler32();
44 
45         // try it all at once
46         adler.update(values);
47         assertEquals(adler.getValue(), expected);
48 
49         // try resetting and computing one byte at a time
50         adler.reset();
51         for (int i = 0; i < values.length; i++) {
52             adler.update(values[i]);
53         }
54         assertEquals(adler.getValue(), expected);
55     }
56 
cRC32Test(byte[] values, long expected)57     private void cRC32Test(byte[] values, long expected) {
58         CRC32 crc = new CRC32();
59 
60         // try it all at once
61         crc.update(values);
62         assertEquals(crc.getValue(), expected);
63 
64         // try resetting and computing one byte at a time
65         crc.reset();
66         for (int i = 0; i < values.length; i++) {
67             crc.update(values[i]);
68         }
69         assertEquals(crc.getValue(), expected);
70     }
71 
72     // "The quick brown fox jumped over the lazy dogs\n"
73     private static byte[] mTestString = {
74             0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63,
75             0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20,
76             0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70,
77             0x65, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20,
78             0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79,
79             0x20, 0x64, 0x6f, 0x67, 0x73, 0x2e, 0x0a
80     };
81 
82 
83     // Test for issue 1016037
wrongChecksumWithAdler32Test()84     private void wrongChecksumWithAdler32Test() {
85         byte[] bytes = {1, 0, 5, 0, 15, 0, 1, 11, 0, 1};
86         Adler32 adler = new Adler32();
87         adler.update(bytes);
88         long arrayChecksum = adler.getValue();
89         adler.reset();
90         for (int i = 0; i < bytes.length; i++) {
91             adler.update(bytes[i]);
92         }
93         assertEquals("Checksums not equal: expected: " + arrayChecksum +
94                 " actual: " + adler.getValue(), arrayChecksum, adler.getValue());
95     }
96 }
97 
98