1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright © 2024, Synopsys Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above
14  *    copyright notice, this list of conditions and the following
15  *    disclaimer in the documentation and/or other materials provided
16  *    with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #define __STDC_WANT_LIB_EXT1__ 1
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
41 
42 #define MAX_ERROR_MSG 100
43 
44 char handler_msg[MAX_ERROR_MSG] = "";
45 
46 static void
custom_constraint_handler(const char * restrict msg,void * restrict ptr,errno_t error)47 custom_constraint_handler(const char *restrict msg, void *restrict ptr,
48                           errno_t error)
49 {
50     (void)ptr;
51     (void)error;
52     strcpy(handler_msg, msg);
53 }
54 
55 #define TEST_RES(cond, msg, handler_res, test_id)                              \
56     if ((!(cond)) || (handler_res == 1)) {                                     \
57         printf("Test %d Failed: %s\n", test_id, msg);                          \
58         return 1;                                                              \
59     } else {                                                                   \
60         printf("Test %d Passed: %s\n", test_id, msg);                          \
61     }
62 
63 static int
test_handler_called(int handler_called,char * expected_msg,int test_id)64 test_handler_called(int handler_called, char *expected_msg, int test_id)
65 {
66     int ret = 0;
67     if (handler_called == 0) {
68         (void)expected_msg;
69         if (handler_msg[0] != '\0') {
70             printf(
71                 "ERROR: Custom constraint handler called without error detiction!\n");
72             printf("Test %d Failed: Error msg is incorrect\n", test_id);
73             ret = 1;
74         }
75     } else {
76         if (handler_msg[0] == '\0') {
77             (void)expected_msg;
78             printf("ERROR: Custom constraint handler not called\n");
79             printf("Test %d Failed: Error msg is incorrect\n", test_id);
80             ret = 1;
81         } else {
82             if (strcmp(expected_msg, handler_msg) != 0) {
83                 printf(
84                     "ERROR: Custom constraint handler called with incorrect msg: %s\n",
85                     handler_msg);
86                 printf("Test %d Failed: Error msg is incorrect\n", test_id);
87                 ret = 1;
88             } else {
89                 (void)expected_msg;
90                 printf(
91                     "Custom constraint handler called with correct msg: %s\n",
92                     handler_msg);
93                 handler_msg[0] = '\0';
94                 ret = 0;
95             }
96         }
97     }
98     return ret;
99 }
100 
101 int
main(void)102 main(void)
103 {
104     char src[] = "Hello, world!";
105     char dest[50];
106     int test_id = 0;
107     int handler_res = 0;
108     errno_t res;
109 
110     set_constraint_handler_s(custom_constraint_handler);
111 
112     // Test case 1: Normal copy
113     test_id++;
114     res = memcpy_s(dest, sizeof(dest), src, strlen(src) + 1);
115     handler_res = test_handler_called(0, "", test_id);
116     TEST_RES(res == 0, "Normal Copy", handler_res, test_id);
117     TEST_RES(strcmp(dest, "Hello, world!") == 0, "Normal Copy Contents",
118              handler_res, test_id);
119 
120     // Test case 2: Copy with insufficient destination size
121     test_id++;
122     res = memcpy_s(dest, 5, src, strlen(src) + 1);
123     handler_res = test_handler_called(
124         1, "memcpy_s: copy count exceeds buffer size", test_id);
125     TEST_RES(res != 0, "Copy with insufficient destination size", handler_res,
126              test_id);
127 
128     // Test case 3: Copy with Null destination
129     test_id++;
130     res = memcpy_s(NULL, sizeof(dest), src, strlen(src) + 1);
131     handler_res = test_handler_called(1, "memcpy_s: dest is NULL", test_id);
132     TEST_RES(res != 0, "Copy with Null destination", handler_res, test_id);
133 
134     // Test case 4: Copy with Null source
135     test_id++;
136     res = memcpy_s(dest, sizeof(dest), NULL, strlen(src) + 1);
137     handler_res = test_handler_called(1, "memcpy_s: source is NULL", test_id);
138     TEST_RES(res != 0, "Copy with Null source", handler_res, test_id);
139 
140     // Test case 5: Copy with zero length
141     test_id++;
142     strcpy(dest, "");
143     res = memcpy_s(dest, sizeof(dest), src, 0);
144     handler_res = test_handler_called(0, "", test_id);
145     TEST_RES(res == 0, "Copy with zero length", handler_res, test_id);
146     TEST_RES(dest[0] == '\0', "Copy with zero length Contents", handler_res,
147              test_id);
148 
149     printf("All memcpy_s tests passed!\n");
150     return 0;
151 }
152