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 buf[50] = "Hello, world!";
105 int test_id = 0;
106 int handler_res = 0;
107 errno_t res;
108
109 set_constraint_handler_s(custom_constraint_handler);
110
111 // Test case 1: Normal Set
112 test_id++;
113 res = memset_s(buf, sizeof(buf), 'A', strlen("Hello, world!"));
114 handler_res = test_handler_called(0, "", test_id);
115 TEST_RES(res == 0, "Normal Set", handler_res, test_id);
116 TEST_RES(strcmp(buf, "AAAAAAAAAAAAA") == 0, "Normal Set Contents",
117 handler_res, test_id);
118
119 // Test case 2: Zero-length Set
120 test_id++;
121 res = memset_s(buf, sizeof(buf), 'B', 0);
122 handler_res = test_handler_called(0, "", test_id);
123 TEST_RES(res == 0, "Zero-length Set", handler_res, test_id);
124 TEST_RES(strcmp(buf, "AAAAAAAAAAAAA") == 0, "Zero-length Set Contents",
125 handler_res, test_id);
126
127 // Test case 3: Null pointers
128 test_id++;
129 res = memset_s(NULL, sizeof(buf), 'C', strlen("Hello, world!"));
130 handler_res = test_handler_called(1, "memset_s: dest is NULL", test_id);
131 TEST_RES(res != 0, "NULL Destination Pointer", handler_res, test_id);
132
133 // Test case 4: Set with zero buffer size
134 test_id++;
135 res = memset_s(buf, 0, 'D', strlen("Hello, world!"));
136 handler_res =
137 test_handler_called(1, "memset_s: count exceeds buffer size", test_id);
138 TEST_RES(res != 0, "Set with zero buffer size", handler_res, test_id);
139
140 printf("All memset_s tests passed!\n");
141 return 0;
142 }
143