File: test_wcsnset_s.c

package info (click to toggle)
safeclib 3.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,832 kB
  • sloc: ansic: 52,639; makefile: 1,271; perl: 528; sh: 518
file content (127 lines) | stat: -rw-r--r-- 3,189 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*------------------------------------------------------------------
 * test_wcsnset_s
 * File 'wcsnset_s.c'
 * Lines executed:85.71% of 21
 *
 *------------------------------------------------------------------
 */

#include "test_private.h"
#include "safe_str_lib.h"

#define LEN (128)

int main(void) {
    errno_t rc;
    rsize_t max_len;
    wchar_t str1[LEN];
    wchar_t value = L'\0';
    uint32_t j;
    int errs = 0;

    /*--------------------------------------------------*/

#ifndef HAVE_CT_BOS_OVR
    EXPECT_BOS("empty dest")
    rc = wcsnset_s(NULL, 5, value, 5);
    ERR(ESNULLP)

    EXPECT_BOS("empty dest or dmax")
    rc = wcsnset_s(str1, 0, 0, 5);
    ERR(ESZEROL)

#if SIZEOF_WCHAR_T > 2
    EXPECT_BOS("value overflow >0x10ffff")
    rc = wcsset_s(str1, LEN, 0x110000);
    ERR(ESLEMAX);
#endif

    EXPECT_BOS("dest overflow")
    rc = wcsnset_s(str1, RSIZE_MAX_WSTR + 1, 0, 5);
    ERR(ESLEMAX);

#ifdef HAVE___BUILTIN_OBJECT_SIZE
    EXPECT_BOS("dest overflow")
    rc = wcsnset_s(str1, LEN + 1, 0, 5);
    ERR(EOVERFLOW);
    WCHECK_SLACK(str1, LEN);
#endif
#endif

    rc = wcsnset_s(str1, LEN - 1, L' ', LEN);
    ERR(ESNOSPC);
    WCHECK_SLACK(str1, LEN - 1);

    /*--------------------------------------------------*/

    wcscpy(str1, L"abc");
    value = L' ';
    max_len = 1;
    rc = wcsnset_s(str1, LEN, value, max_len);
    ERR(EOK)
    for (j = 0; j < max_len; j++) {
        if (str1[j] != value) {
            debug_printf("%s %u   Error rc=%u \n", __FUNCTION__, __LINE__, rc);
            errs++;
        }
    }

    /*--------------------------------------------------*/

    wcscpy(str1, L"abc");
    value = 0;
    max_len = 2;
    rc = wcsnset_s(str1, LEN, value, max_len);
    ERR(EOK)
    for (j = 0; j < max_len; j++) {
        if (str1[j] != value) {
            debug_printf("%s %u   Error rc=%u \n", __FUNCTION__, __LINE__, rc);
            errs++;
        }
    }

    /*--------------------------------------------------*/

    wcscpy(str1, L"abc");
    max_len = 3;
    rc = wcsnset_s(str1, LEN, value, max_len);
    ERR(EOK)
    for (j = 0; j < max_len; j++) {
        if (str1[j] != value) {
            debug_printf("%s %u   Error rc=%u \n", __FUNCTION__, __LINE__, rc);
            errs++;
        }
    }
    WCHECK_SLACK(&str1[3], LEN - 3);

    /*--------------------------------------------------*/

    wcscpy(str1, L"abc");
    max_len = LEN;
    rc = wcsnset_s(str1, LEN, value, max_len);
    ERR(EOK)
    for (j = 0; j < 3; j++) {
        if (str1[j] != value) {
            debug_printf("%s %u   Error rc=%u \n", __FUNCTION__, __LINE__, rc);
            errs++;
        }
    }
    WCHECK_SLACK(&str1[3], LEN - 3);

    /*--------------------------------------------------*/

    wcscpy(str1, L"Now is the time for all data to be zeroed");
    max_len = wcslen(L"Now is the ");

    rc = wcsnset_s(str1, LEN, 0, max_len);
    ERR(EOK)
    if (wcscmp(&str1[max_len], L"time for all data to be zeroed")) {
        debug_printf("%s %u ERROR  --%ls-- \n", __FUNCTION__, __LINE__,
                     &str1[max_len]);
        errs++;
    }

    /*--------------------------------------------------*/

    return (errs);
}