Debian Codegraph @

testcases/unit/buffertest.c

raw
1 /*
2 * COPYRIGHT (c) International Business Machines Corp. 2022
3 *
4 * This program is provided under the terms of the Common Public License,
5 * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
6 * software constitutes recipient's acceptance of CPL-1.0 terms which can be
7 * found in the file LICENSE file or at
8 * https://opensource.org/licenses/cpl1.0.php
9 */
10 #include <stdio.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #include <buffer.h>
15 #include "unittest.h"
16
17 #define STDERR_RC_UNEXP(n, func, rc, exp_rc) \
18 fprintf(stderr, "[%d] %s: %s (curr: %ld, expected: %ld)\n", \
19 n, func, "unexpected return value", rc, exp_rc)
20
21 static char *short_string = "abc";
22 static char *long_string = "abcdefghijklmnopqrstuvwxyz"
23 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
24 "abcdefghijklmnopqrstuvwxyz"
25 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
26 "abcdefghijklmnopqrstuvwxyz"
27 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
28 "abcdefghijklmnopqrstuvwxyz"
29 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
30 "abcdefghijklmnopqrstuvwxyz"
31 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
32 "abcdefghijklmnopqrstuvwxyz"
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
34
35 static int test_buffer_api(char *s)
36 {
37 int result = 0;
38 long rc, exp_rc;
39 size_t s_len = strlen(s);
40 p11_buffer_t *buf = p11_buffer_new();
41
42 /* check for buffer length and size */
43 if ((*p11_buffer_char(buf) != '\0') ||
44 (p11_buffer_char(buf) == NULL) ||
45 (p11_buffer_size(buf) == 0) ||
46 (strlen(p11_buffer_char(buf)) != 0)) {
47 fprintf(stderr, "[%d] %s: %s\n",
48 0, "p11_buffer_new()",
49 "wrong initial buffer values");
50 result++;
51 }
52
53 /* check corner case s == NULL */
54 exp_rc = strlen(p11_buffer_char(buf));
55 rc = p11_buffer_append(buf, NULL);
56 if (rc != exp_rc) {
57 STDERR_RC_UNEXP(1, "p11_buffer_append()", rc, exp_rc);
58 result++;
59 }
60
61 /* check corner case s == NULL, len == 0 */
62 exp_rc = strlen(p11_buffer_char(buf));
63 rc = p11_buffer_append_len(buf, NULL, 0);
64 if (rc != exp_rc) {
65 STDERR_RC_UNEXP(2, "p11_buffer_append_len()", rc, exp_rc);
66 result++;
67 }
68
69 /* check corner case len == 0 */
70 exp_rc = strlen(p11_buffer_char(buf));
71 rc = p11_buffer_append_len(buf, s, 0);
72 if (rc != exp_rc) {
73 STDERR_RC_UNEXP(3, "p11_buffer_append_len()", rc, exp_rc);
74 result++;
75 }
76
77 /* normal append */
78 exp_rc = strlen(p11_buffer_char(buf)) + s_len;
79 rc = p11_buffer_append(buf, s);
80 if (rc != exp_rc) {
81 STDERR_RC_UNEXP(4, "p11_buffer_append()", rc, exp_rc);
82 result++;
83 }
84
85 /* normal append with length */
86 exp_rc = strlen(p11_buffer_char(buf)) + s_len;
87 rc = p11_buffer_append_len(buf, s, s_len);
88 if (rc != exp_rc) {
89 STDERR_RC_UNEXP(5, "p11_buffer_append()", rc, exp_rc);
90 result++;
91 }
92
93 /* normal append with printf */
94 exp_rc = strlen(p11_buffer_char(buf)) + 14 + s_len;
95 rc = p11_buffer_append_printf(buf, "append_prinf(%s)", s);
96 if (rc != exp_rc) {
97 STDERR_RC_UNEXP(6, "p11_buffer_append()", rc, exp_rc);
98 result++;
99 }
100
101 /* reset buffer */
102 p11_buffer_reset(buf);
103 if ((*p11_buffer_char(buf) != '\0') ||
104 (strlen(p11_buffer_char(buf)) != 0)) {
105 fprintf(stderr, "[%d] %s: %s\n",
106 7, "p11_buffer_reset()",
107 "wrong buffer values after reset");
108 result++;
109 }
110
111 p11_buffer_free(buf);
112 return result;
113 }
114
115 int main(void)
116 {
117 if (test_buffer_api(short_string))
118 return TEST_FAIL;
119 if (test_buffer_api(long_string))
120 return TEST_FAIL;
121
122 return TEST_PASS;
123 }