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] = "Hello";
105 const char *src = ", 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 Concatenation
113 test_id++;
114 strcpy(dest, "Hello");
115 res = strncat_s(dest, sizeof(dest), src, 8);
116 handler_res = test_handler_called(0, "", test_id);
117 TEST_RES(res == 0, "Normal Concatenation", handler_res, test_id);
118 TEST_RES(strcmp(dest, "Hello, world!") == 0,
119 "Normal Concatenation Contents", handler_res, test_id);
120
121 // Test case 2: Concatenation with insufficient buffer
122 test_id++;
123 strcpy(dest, "Hello");
124 res = strncat_s(dest, 10, src, 8);
125 handler_res = test_handler_called(
126 1, "strncat_s: dest buffer size insufficent to copy string", test_id);
127 TEST_RES(res != 0, "Concatenation with insufficient buffer", handler_res,
128 test_id);
129
130 // Test case 3: Null pointers
131 test_id++;
132 res = strncat_s(NULL, sizeof(dest), src, 8);
133 handler_res = test_handler_called(1, "strncat_s: dest is NULL", test_id);
134 TEST_RES(res != 0, "NULL Destination Pointer", handler_res, test_id);
135 res = strncat_s(dest, sizeof(dest), NULL, 8);
136 handler_res = test_handler_called(1, "strncat_s: source is NULL", test_id);
137 TEST_RES(res != 0, "NULL Source Pointer", handler_res, test_id);
138
139 // Test case 4: Concatenation of empty source string
140 test_id++;
141 strcpy(dest, "Hello");
142 res = strncat_s(dest, sizeof(dest), "", 0);
143 handler_res = test_handler_called(0, "", test_id);
144 TEST_RES(res == 0, "Concatenation of empty source string", handler_res,
145 test_id);
146 TEST_RES(strcmp(dest, "Hello") == 0,
147 "Concatenation of empty source string Contents", handler_res,
148 test_id);
149
150 // Test case 5: Concatenation with empty destination string
151 test_id++;
152 char buf2[50] = "";
153 res = strncat_s(buf2, sizeof(buf2), src, 8);
154 handler_res = test_handler_called(0, "", test_id);
155 TEST_RES(res == 0, "Concatenation of non-empty source to empty destination",
156 handler_res, test_id);
157 TEST_RES(strcmp(buf2, ", world!") == 0,
158 "Concatenation of non-empty source to empty destination Contents",
159 handler_res, test_id);
160
161 // Test case 6: Concatenation with Zero Characters
162 test_id++;
163 strcpy(dest, "Hello");
164 res = strncat_s(dest, sizeof(dest), src, 0);
165 handler_res = test_handler_called(0, "", test_id);
166 TEST_RES(res == 0, "Concatenation with Zero Characters", handler_res,
167 test_id);
168 TEST_RES(strcmp(dest, "Hello") == 0,
169 "Concatenation with Zero Characters Contents", handler_res,
170 test_id);
171
172 printf("All strncat_s tests passed!\n");
173 return 0;
174 }
175