xref: /isa-l_crypto/rolling_hash/rolling_hash2_param_test.c (revision 37c1320fef459b6ac01a1fae6f17aa16f54f55fb)
1 /**********************************************************************
2   Copyright(c) 2024 Intel Corporation All rights reserved.
3   Redistribution and use in source and binary forms, with or without
4   modification, are permitted provided that the following conditions
5   are met:
6     * Redistributions of source code must retain the above copyright
7       notice, this list of conditions and the following disclaimer.
8     * Redistributions in binary form must reproduce the above copyright
9       notice, this list of conditions and the following disclaimer in
10       the documentation and/or other materials provided with the
11       distribution.
12     * Neither the name of Intel Corporation nor the names of its
13       contributors may be used to endorse or promote products derived
14       from this software without specific prior written permission.
15   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 **********************************************************************/
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include "isal_crypto_api.h"
31 #include "rolling_hashx.h"
32 #include "multi_buffer.h"
33 #include "test.h"
34 
35 #ifdef SAFE_PARAM
36 
37 static int
38 test_rolling_hash2_init_api(void)
39 {
40         int ret = -1;
41         const char *fn_name = "isal_rolling_hash2_init";
42         struct rh_state2 state = { 0 };
43 
44         // check NULL state
45         CHECK_RETURN_GOTO(isal_rolling_hash2_init(NULL, 32), ISAL_CRYPTO_ERR_NULL_CTX, fn_name,
46                           end_init);
47 
48         // check invalid window size
49         CHECK_RETURN_GOTO(isal_rolling_hash2_init(&state, 500), ISAL_CRYPTO_ERR_WINDOW_SIZE,
50                           fn_name, end_init);
51 
52         // check valid args
53         CHECK_RETURN_GOTO(isal_rolling_hash2_init(&state, 5), ISAL_CRYPTO_ERR_NONE, fn_name,
54                           end_init);
55 
56         ret = 0;
57 end_init:
58         return ret;
59 }
60 
61 static int
62 test_rolling_hash2_reset_api(void)
63 {
64         int ret = -1;
65         const char *fn_name = "isal_rolling_hash2_reset";
66         struct rh_state2 state = { 0 };
67         uint8_t init_bytes[64] = { 0 };
68 
69         // check NULL state
70         CHECK_RETURN_GOTO(isal_rolling_hash2_reset(NULL, init_bytes), ISAL_CRYPTO_ERR_NULL_CTX,
71                           fn_name, end_reset);
72 
73         // check NULL init bytes
74         CHECK_RETURN_GOTO(isal_rolling_hash2_reset(&state, NULL), ISAL_CRYPTO_ERR_HASH_INIT_VAL,
75                           fn_name, end_reset);
76 
77         // check valid args
78         CHECK_RETURN_GOTO(isal_rolling_hash2_reset(&state, init_bytes), ISAL_CRYPTO_ERR_NONE,
79                           fn_name, end_reset);
80 
81         ret = 0;
82 end_reset:
83         return ret;
84 }
85 
86 static int
87 test_rolling_hash2_run_api(void)
88 {
89         int ret = -1;
90         const char *fn_name = "isal_rolling_hash2_run";
91         struct rh_state2 state = { 0 };
92         uint8_t buffer[64] = { 0 };
93         uint32_t len = (uint32_t) sizeof(buffer);
94         uint32_t mask = 0xffff0;
95         uint32_t trigger = 0x3df0;
96         uint32_t offset = 0;
97         int match = -1;
98 
99         // check NULL state
100         CHECK_RETURN_GOTO(isal_rolling_hash2_run(NULL, buffer, len, mask, trigger, &offset, &match),
101                           ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_run);
102 
103         // check NULL source buffer
104         CHECK_RETURN_GOTO(isal_rolling_hash2_run(&state, NULL, len, mask, trigger, &offset, &match),
105                           ISAL_CRYPTO_ERR_NULL_SRC, fn_name, end_run);
106 
107         // check NULL offset
108         CHECK_RETURN_GOTO(isal_rolling_hash2_run(&state, buffer, len, mask, trigger, NULL, &match),
109                           ISAL_CRYPTO_ERR_NULL_OFFSET, fn_name, end_run);
110 
111         // check NULL match
112         CHECK_RETURN_GOTO(isal_rolling_hash2_run(&state, buffer, len, mask, trigger, &offset, NULL),
113                           ISAL_CRYPTO_ERR_NULL_MATCH, fn_name, end_run);
114 
115         // check valid args
116         CHECK_RETURN_GOTO(
117                 isal_rolling_hash2_run(&state, buffer, len, mask, trigger, &offset, &match),
118                 ISAL_CRYPTO_ERR_NONE, fn_name, end_run);
119 
120         ret = 0;
121 end_run:
122         return ret;
123 }
124 
125 static int
126 test_rolling_hashx_mask_gen_api(void)
127 {
128         int ret = -1;
129         const char *fn_name = "isal_rolling_hashx_mask_gen";
130         uint32_t mean = 0;
131         uint32_t shift = 0;
132         uint32_t mask = FINGERPRINT_RET_OTHER + 1;
133 
134         // check NULL mask
135         CHECK_RETURN_GOTO(isal_rolling_hashx_mask_gen(mean, shift, NULL), ISAL_CRYPTO_ERR_NULL_MASK,
136                           fn_name, end_mask_gen);
137 
138         // check valid args
139         CHECK_RETURN_GOTO(isal_rolling_hashx_mask_gen(mean, shift, &mask), ISAL_CRYPTO_ERR_NONE,
140                           fn_name, end_mask_gen);
141 
142         // check mask was set to valid value
143         if (mask >= FINGERPRINT_RET_OTHER) {
144                 printf("test: %s() - unexpected mask set\n", fn_name);
145                 goto end_mask_gen;
146         }
147 
148         ret = 0;
149 end_mask_gen:
150         return ret;
151 }
152 
153 #endif /* SAFE_PARAM */
154 
155 int
156 main(void)
157 {
158         int fail = 0;
159 
160 #ifdef SAFE_PARAM
161         fail |= test_rolling_hash2_init_api();
162         fail |= test_rolling_hash2_reset_api();
163         fail |= test_rolling_hash2_run_api();
164         fail |= test_rolling_hashx_mask_gen_api();
165 
166         printf(fail ? "Fail\n" : "Pass\n");
167 #else
168         printf("Not Executed\n");
169 #endif
170         return fail;
171 }
172