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 dest[50];
105     const char *src = "Hello, world!";
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 = strcpy_s(dest, sizeof(dest), src);
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 buffer
121     test_id++;
122     res = strcpy_s(dest, 5, src);
123     handler_res = test_handler_called(
124         1, "strcpy_s: dest buffer size insufficent to copy string", test_id);
125     TEST_RES(res != 0, "Copy with insufficient buffer", handler_res, test_id);
126 
127     // Test case 3: Null pointers
128     test_id++;
129     res = strcpy_s(NULL, sizeof(dest), src);
130     handler_res = test_handler_called(1, "strcpy_s: dest is NULL", test_id);
131     TEST_RES(res != 0, "NULL Destination Pointer", handler_res, test_id);
132     res = strcpy_s(dest, sizeof(dest), NULL);
133     handler_res = test_handler_called(1, "strcpy_s: source is NULL", test_id);
134     TEST_RES(res != 0, "NULL Source Pointer", handler_res, test_id);
135 
136     // Test case 4: Copy of empty string
137     test_id++;
138     res = strcpy_s(dest, sizeof(dest), "");
139     handler_res = test_handler_called(0, "", test_id);
140     TEST_RES(res == 0, "Copy of empty string", handler_res, test_id);
141     TEST_RES(strcmp(dest, "") == 0, "Copy of empty string Contents",
142              handler_res, test_id);
143 
144     // Test case 5: Copy to empty buffer
145     test_id++;
146     char buf2[50] = "";
147     res = strcpy_s(buf2, sizeof(buf2), "world");
148     handler_res = test_handler_called(0, "", test_id);
149     TEST_RES(res == 0, "Copy to empty buffer", handler_res, test_id);
150     TEST_RES(strcmp(buf2, "world") == 0, "Copy to empty buffer Contents",
151              handler_res, test_id);
152 
153     printf("All strcpy_s tests passed!\n");
154     return 0;
155 }
156