1 /*
2  * Copyright (c) 2014, The University of Oxford
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  * 3. Neither the name of the University of Oxford nor the names of its
13  *    contributors may be used to endorse or promote products derived from this
14  *    software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <gtest/gtest.h>
30 
31 #include <cstdio>
32 #include <cstdlib>
33 #include <cstring>
34 
35 #include "binary/oskar_crc.h"
36 
TEST(crc,crc32_standard)37 TEST(crc, crc32_standard)
38 {
39     const char data[] = "123456789";
40 
41     // Test IEEE CRC-32.
42     oskar_CRC* crc_data = oskar_crc_create(OSKAR_CRC_32);
43     EXPECT_EQ(0xcbf43926uL, oskar_crc_compute(crc_data, data, strlen(data)));
44     oskar_crc_free(crc_data);
45 
46     // Test Castagnoli CRC-32C.
47     crc_data = oskar_crc_create(OSKAR_CRC_32C);
48     EXPECT_EQ(0xe3069283uL, oskar_crc_compute(crc_data, data, strlen(data)));
49     oskar_crc_free(crc_data);
50 }
51 
TEST(crc,crc32_string)52 TEST(crc, crc32_string)
53 {
54     const char data[] = "The quick brown fox jumps over the lazy dog";
55 
56     // Evaluate CRC.
57     oskar_CRC* crc_data = oskar_crc_create(OSKAR_CRC_32);
58     unsigned long oskar_crc = oskar_crc_compute(crc_data, data, strlen(data));
59     oskar_crc_free(crc_data);
60 
61     // Check.
62     EXPECT_EQ(0x414fa339uL, oskar_crc);
63 }
64 
TEST(crc,crc32_string_incremental)65 TEST(crc, crc32_string_incremental)
66 {
67     const char data1[] = "The quick brown fox ";
68     const char data2[] = "jumps over the lazy dog";
69 
70     // Evaluate CRC.
71     oskar_CRC* crc_data = oskar_crc_create(OSKAR_CRC_32);
72     unsigned long crc = oskar_crc_compute(crc_data, data1, strlen(data1));
73     crc = oskar_crc_update(crc_data, crc, data2, strlen(data2));
74     oskar_crc_free(crc_data);
75 
76     // Check.
77     EXPECT_EQ(0x414fa339uL, crc);
78 }
79 
TEST(crc,crc32_corrupted)80 TEST(crc, crc32_corrupted)
81 {
82     // Create the lookup table.
83     oskar_CRC* crc_data = oskar_crc_create(OSKAR_CRC_32C);
84 
85     // Create test data.
86     size_t length = 1uL * 1024uL * 1024uL;
87     size_t bytes = sizeof(int) * length;
88     unsigned char* data = (unsigned char*) malloc(bytes);
89     unsigned int* d = (unsigned int*) data;
90     for (size_t i = 0; i < length; ++i)
91     {
92         d[i] = (unsigned int) i;
93     }
94 
95     // Get CRC of original data.
96     unsigned long crc1 = oskar_crc_compute(crc_data, data, bytes);
97 
98     // Toggle a bit somewhere.
99     data[bytes >> 2] ^= (1 << 4);
100 
101     // Get CRC of corrupted data and check values are different.
102     unsigned long crc2 = oskar_crc_compute(crc_data, data, bytes);
103     EXPECT_NE(crc1, crc2);
104 
105     // Toggle a bit somewhere else.
106     data[(bytes >> 2) + 1234] ^= (1 << 4);
107 
108     // Get CRC of corrupted data and check values are different.
109     unsigned long crc3 = oskar_crc_compute(crc_data, data, bytes);
110     EXPECT_NE(crc1, crc3);
111 
112     // Cleanup.
113     oskar_crc_free(crc_data);
114     free(data);
115 }
116 
TEST(crc,crc8_standard)117 TEST(crc, crc8_standard)
118 {
119     const char data[] = "123456789";
120     oskar_CRC* crc_data = oskar_crc_create(OSKAR_CRC_8_EBU);
121     EXPECT_EQ(0x97uL, oskar_crc_compute(crc_data, data, strlen(data)));
122     oskar_crc_free(crc_data);
123 }
124 
TEST(crc,crc8_consistency)125 TEST(crc, crc8_consistency)
126 {
127     oskar_CRC* crc_data = oskar_crc_create(OSKAR_CRC_8_EBU);
128     char data[] = {
129             0x00, 0x00, 0x00, 0x00,
130             0x00, 0x00, 0x00, 0x00,
131             0x00, 0x00, 0x00, 0x00,
132             0x00, 0x00, 0x00, 0x00,
133             0x00, 0x00, 0x00, 0x00,
134             0x00, 0x00, 0x00
135     };
136 
137     // Test 0.
138     EXPECT_NE(0uL, oskar_crc_compute(crc_data, data, sizeof(data)));
139 
140     // Test 1.
141     data[0] = 0x01;
142     EXPECT_EQ(0x32uL, oskar_crc_compute(crc_data, data, sizeof(data)));
143 
144     // Test 2.
145     data[0] = 0x3d;
146     data[1] = 0x02;
147     data[4] = 0x02;
148     EXPECT_EQ(0x9BuL, oskar_crc_compute(crc_data, data, sizeof(data)));
149 
150     // Test 3.
151     unsigned char crc = oskar_crc_compute(crc_data, data, 10);
152     crc = oskar_crc_update(crc_data, crc, data + 10, sizeof(data) - 10);
153     EXPECT_EQ(0x9B, crc);
154 
155     // Cleanup.
156     oskar_crc_free(crc_data);
157 }
158