1 /*
2  * PROJECT:         ReactOS API tests
3  * LICENSE:         LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4  * PURPOSE:         Tests for _mbsstr
5  * COPYRIGHT:       Copyright 2024 Thomas Faber (thomas.faber@reactos.org)
6  */
7 
8 #include <apitest.h>
9 #include <mbstring.h>
10 #define WIN32_NO_STATUS
11 #include <pseh/pseh2.h>
12 #include <ndk/mmfuncs.h>
13 
14 START_TEST(_mbsstr)
15 {
16     unsigned char *haystack;
17     unsigned char *ret;
18 
19     /* NULL pointers are not handled */
20     StartSeh()
21         (void)_mbsstr(NULL, NULL);
22     EndSeh(STATUS_ACCESS_VIOLATION);
23 
24     StartSeh()
25         haystack = (unsigned char *)"hello";
26         (void)_mbsstr(haystack, NULL);
27     EndSeh(STATUS_ACCESS_VIOLATION);
28 
29     StartSeh()
30         haystack = (unsigned char *)"";
31         (void)_mbsstr(haystack, NULL);
32     EndSeh(STATUS_ACCESS_VIOLATION);
33 
34     /* Empty needle returns haystack, empty haystack returns NULL... */
35     haystack = (unsigned char *)"hello";
36     ret = _mbsstr(haystack, (const unsigned char *)"");
37     ok(ret == haystack, "ret = %p, haystack = %p\n", ret, haystack);
38 
39     haystack = (unsigned char *)"";
40     ret = _mbsstr(haystack, (const unsigned char *)"a");
41     ok(ret == NULL, "ret = %p, haystack = %p\n", ret, haystack);
42 
43     /* ... but if both are empty, behavior differs */
44     haystack = (unsigned char *)"";
45     ret = _mbsstr(haystack, (const unsigned char *)"");
46 #ifdef TEST_CRTDLL
47     ok(ret == NULL, "ret = %p, haystack = %p\n", ret, haystack);
48 #else
49     ok(ret == haystack, "ret = %p, haystack = %p\n", ret, haystack);
50 #endif
51 
52     /* Simple "found" cases */
53     haystack = (unsigned char *)"abcdefg";
54     ret = _mbsstr(haystack, (const unsigned char *)"abc");
55     ok(ret == haystack, "ret = %p, haystack = %p\n", ret, haystack);
56 
57     haystack = (unsigned char *)"abcdefg";
58     ret = _mbsstr(haystack, (const unsigned char *)"g");
59     ok(ret == haystack + 6, "ret = %p, haystack = %p\n", ret, haystack);
60 
61     haystack = (unsigned char *)"abcdefg";
62     ret = _mbsstr(haystack, (const unsigned char *)"abcdefg");
63     ok(ret == haystack, "ret = %p, haystack = %p\n", ret, haystack);
64 
65     /* Simple "not found" cases */
66     haystack = (unsigned char *)"abcdefg";
67     ret = _mbsstr(haystack, (const unsigned char *)"h");
68     ok(ret == NULL, "ret = %p, haystack = %p\n", ret, haystack);
69 
70     haystack = (unsigned char *)"abcdefg";
71     ret = _mbsstr(haystack, (const unsigned char *)"gh");
72     ok(ret == NULL, "ret = %p, haystack = %p\n", ret, haystack);
73 
74     haystack = (unsigned char *)"abcdefg";
75     ret = _mbsstr(haystack, (const unsigned char *)"abcD");
76     ok(ret == NULL, "ret = %p, haystack = %p\n", ret, haystack);
77 
78     /* Needle longer than haystack */
79     haystack = (unsigned char *)"abcdefg";
80     ret = _mbsstr(haystack, (const unsigned char *)"abcdefgh");
81     ok(ret == NULL, "ret = %p, haystack = %p\n", ret, haystack);
82 
83     haystack = (unsigned char *)"abcdefg";
84     ret = _mbsstr(haystack, (const unsigned char *)"xxxxxxxx");
85     ok(ret == NULL, "ret = %p, haystack = %p\n", ret, haystack);
86 }
87 
88