1 /********************************************************************** 2 Copyright(c) 2011-2017 Intel Corporation All rights reserved. 3 4 Redistribution and use in source and binary forms, with or without 5 modification, are permitted provided that the following conditions 6 are met: 7 * Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 * Redistributions in binary form must reproduce the above copyright 10 notice, this list of conditions and the following disclaimer in 11 the documentation and/or other materials provided with the 12 distribution. 13 * Neither the name of Intel Corporation nor the names of its 14 contributors may be used to endorse or promote products derived 15 from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 **********************************************************************/ 29 30 #include <stdio.h> 31 #include <string.h> 32 #include <stdint.h> 33 #include <stdlib.h> 34 #include <assert.h> 35 #include "crc.h" 36 37 #ifndef RANDOMS 38 # define RANDOMS 20 39 #endif 40 #ifndef TEST_SEED 41 # define TEST_SEED 0x1234 42 #endif 43 44 #define MAX_BUF 2345 45 #define TEST_SIZE 217 46 #define TEST_LEN (8 * 1024) 47 48 typedef uint16_t u16; 49 typedef uint8_t u8; 50 51 void rand_buffer(unsigned char *buf, long buffer_size) 52 { 53 long i; 54 for (i = 0; i < buffer_size; i++) 55 buf[i] = rand(); 56 } 57 58 int memtst(unsigned char *buf, unsigned char c, int len) 59 { 60 int i; 61 for (i = 0; i < len; i++) 62 if (*buf++ != c) 63 return 1; 64 65 return 0; 66 } 67 68 int crc_copy_check(const char *description, u8 * dst, u8 * src, u8 dst_fill_val, int len, 69 int tot) 70 { 71 u16 seed; 72 int rem; 73 74 assert(tot >= len); 75 seed = rand(); 76 rem = tot - len; 77 memset(dst, dst_fill_val, tot); 78 u16 crc_dut = crc16_t10dif_copy(seed, dst, src, len); 79 u16 crc_ref = crc16_t10dif(seed, src, len); 80 if (crc_dut != crc_ref) { 81 printf("%s, crc gen fail: 0x%4x 0x%4x len=%d\n", description, crc_dut, 82 crc_ref, len); 83 return 1; 84 } else if (memcmp(dst, src, len)) { 85 printf("%s, copy fail: len=%d\n", description, len); 86 return 1; 87 } else if (memtst(&dst[len], dst_fill_val, rem)) { 88 printf("%s, writeover fail: len=%d\n", description, len); 89 return 1; 90 } 91 return 0; 92 } 93 94 int main(int argc, char *argv[]) 95 { 96 int r = 0; 97 int i; 98 int len, tot; 99 u8 *src_raw, *dst_raw; 100 u8 *src, *dst; 101 102 printf("Test crc16_t10dif_copy_test:\n"); 103 src_raw = (u8 *) malloc(TEST_LEN); 104 dst_raw = (u8 *) malloc(TEST_LEN); 105 if (NULL == src_raw || NULL == dst_raw) { 106 printf("alloc error: Fail"); 107 return -1; 108 } 109 src = src_raw; 110 dst = dst_raw; 111 112 srand(TEST_SEED); 113 114 // Test of all zeros 115 memset(src, 0, TEST_LEN); 116 r |= crc_copy_check("zero tst", dst, src, 0x5e, MAX_BUF, TEST_LEN); 117 118 // Another simple test pattern 119 memset(src, 0xff, TEST_LEN); 120 r |= crc_copy_check("simp tst", dst, src, 0x5e, MAX_BUF, TEST_LEN); 121 122 // Do a few short len random data tests 123 rand_buffer(src, TEST_LEN); 124 rand_buffer(dst, TEST_LEN); 125 for (i = 0; i < MAX_BUF; i++) { 126 r |= crc_copy_check("short len", dst, src, rand(), i, MAX_BUF); 127 } 128 printf("."); 129 130 // Do a few longer tests, random data 131 for (i = TEST_LEN; i >= (TEST_LEN - TEST_SIZE); i--) { 132 r |= crc_copy_check("long len", dst, src, rand(), i, TEST_LEN); 133 } 134 printf("."); 135 136 // Do random size, random data 137 for (i = 0; i < RANDOMS; i++) { 138 len = rand() % TEST_LEN; 139 r |= crc_copy_check("rand len", dst, src, rand(), len, TEST_LEN); 140 } 141 printf("."); 142 143 // Run tests at end of buffer 144 for (i = 0; i < RANDOMS; i++) { 145 len = rand() % TEST_LEN; 146 src = &src_raw[TEST_LEN - len - 1]; 147 dst = &dst_raw[TEST_LEN - len - 1]; 148 tot = len; 149 r |= crc_copy_check("end of buffer", dst, src, rand(), len, tot); 150 } 151 printf("."); 152 153 printf("Test done: %s\n", r ? "Fail" : "Pass"); 154 return r; 155 } 156