xref: /openbsd-src/regress/lib/libutil/imsg/ibuf_test.c (revision 8dfe214903ce3625c937d5fad2469e8a0d1d4d71)
1*8dfe2149Sclaudio /* $OpenBSD: ibuf_test.c,v 1.5 2023/12/29 16:02:29 claudio Exp $ */
2c1a45aedStobias /*
3c1a45aedStobias  * Copyright (c) Tobias Stoeckmann <tobias@openbsd.org>
4c1a45aedStobias  *
5c1a45aedStobias  * Permission to use, copy, modify, and distribute this software for any
6c1a45aedStobias  * purpose with or without fee is hereby granted, provided that the above
7c1a45aedStobias  * copyright notice and this permission notice appear in all copies.
8c1a45aedStobias  *
9c1a45aedStobias  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10c1a45aedStobias  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11c1a45aedStobias  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12c1a45aedStobias  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13c1a45aedStobias  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14c1a45aedStobias  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15c1a45aedStobias  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16c1a45aedStobias  */
17c1a45aedStobias 
18c1a45aedStobias #include <sys/queue.h>
19c1a45aedStobias #include <sys/types.h>
20c1a45aedStobias 
21c1a45aedStobias #include <imsg.h>
22c1a45aedStobias #include <limits.h>
23c1a45aedStobias #include <stdint.h>
24c1a45aedStobias #include <stdio.h>
25c1a45aedStobias 
26c1a45aedStobias int
test_ibuf_open(void)27820acc68Stb test_ibuf_open(void)
28820acc68Stb {
29c1a45aedStobias 	struct ibuf *buf;
30c1a45aedStobias 
31fc405d53Sclaudio 	if ((buf = ibuf_open(1)) == NULL)
32c1a45aedStobias 		return 1;
33c1a45aedStobias 
34c1a45aedStobias 	ibuf_free(buf);
35c1a45aedStobias 	return 0;
36c1a45aedStobias }
37c1a45aedStobias 
38c1a45aedStobias int
test_ibuf_dynamic(void)39820acc68Stb test_ibuf_dynamic(void)
40820acc68Stb {
41c1a45aedStobias 	struct ibuf *buf;
42c1a45aedStobias 
43c1a45aedStobias 	if (ibuf_dynamic(100, 0) != NULL)
44c1a45aedStobias 		return 1;
45c1a45aedStobias 
46c1a45aedStobias 	if ((buf = ibuf_dynamic(10, SIZE_MAX)) == NULL)
47c1a45aedStobias 		return 1;
48c1a45aedStobias 
49c1a45aedStobias 	ibuf_free(buf);
50c1a45aedStobias 	return 0;
51c1a45aedStobias }
52c1a45aedStobias 
53c1a45aedStobias int
test_ibuf_reserve(void)54820acc68Stb test_ibuf_reserve(void)
55820acc68Stb {
56c1a45aedStobias 	struct ibuf *buf;
57c1a45aedStobias 	int ret;
58c1a45aedStobias 
59c1a45aedStobias 	if ((buf = ibuf_dynamic(10, SIZE_MAX)) == NULL) {
60c1a45aedStobias 		return 1;
61c1a45aedStobias 	}
62c1a45aedStobias 
63c1a45aedStobias 	if (ibuf_reserve(buf, SIZE_MAX) != NULL) {
64c1a45aedStobias 		ibuf_free(buf);
65c1a45aedStobias 		return 1;
66c1a45aedStobias 	}
67c1a45aedStobias 
68c1a45aedStobias 	if (ibuf_reserve(buf, 10) == NULL) {
69c1a45aedStobias 		ibuf_free(buf);
70c1a45aedStobias 		return 1;
71c1a45aedStobias 	}
72c1a45aedStobias 
73c1a45aedStobias 	ret = (ibuf_reserve(buf, SIZE_MAX) != NULL);
74c1a45aedStobias 
75c1a45aedStobias 	ibuf_free(buf);
76c1a45aedStobias 	return ret;
77c1a45aedStobias }
78c1a45aedStobias 
79c1a45aedStobias int
test_ibuf_seek(void)80820acc68Stb test_ibuf_seek(void)
81820acc68Stb {
82c1a45aedStobias 	struct ibuf *buf;
83c1a45aedStobias 	int ret;
84c1a45aedStobias 
85c1a45aedStobias 	if ((buf = ibuf_open(10)) == NULL)
86c1a45aedStobias 		return 1;
87c1a45aedStobias 
88c1a45aedStobias 	ret = (ibuf_seek(buf, 1, SIZE_MAX) != NULL);
89c1a45aedStobias 
90c1a45aedStobias 	ibuf_free(buf);
91c1a45aedStobias 	return ret;
92c1a45aedStobias }
93c1a45aedStobias 
94c1a45aedStobias int
main(void)95c1a45aedStobias main(void)
96c1a45aedStobias {
97c1a45aedStobias 	extern char *__progname;
98c1a45aedStobias 
99c1a45aedStobias 	int ret = 0;
100c1a45aedStobias 
101c1a45aedStobias 	if (test_ibuf_open() != 0) {
102c1a45aedStobias 		printf("FAILED: test_ibuf_open\n");
103c1a45aedStobias 		ret = 1;
104c1a45aedStobias 	} else
105c1a45aedStobias 		printf("SUCCESS: test_ibuf_open\n");
106c1a45aedStobias 
107c1a45aedStobias 	if (test_ibuf_dynamic() != 0) {
108c1a45aedStobias 		printf("FAILED: test_ibuf_dynamic\n");
109c1a45aedStobias 		ret = 1;
110c1a45aedStobias 	} else
111c1a45aedStobias 		printf("SUCCESS: test_ibuf_dynamic\n");
112c1a45aedStobias 
113c1a45aedStobias 	if (test_ibuf_reserve() != 0) {
114c1a45aedStobias 		printf("FAILED: test_ibuf_reserve\n");
115c1a45aedStobias 		ret = 1;
116c1a45aedStobias 	} else
117c1a45aedStobias 		printf("SUCCESS: test_ibuf_reserve\n");
118c1a45aedStobias 
119c1a45aedStobias 	if (test_ibuf_seek() != 0) {
120c1a45aedStobias 		printf("FAILED: test_ibuf_seek\n");
121c1a45aedStobias 		ret = 1;
122c1a45aedStobias 	} else
123c1a45aedStobias 		printf("SUCCESS: test_ibuf_seek\n");
124c1a45aedStobias 
125c1a45aedStobias 	if (ret != 0) {
126c1a45aedStobias 		printf("FAILED: %s\n", __progname);
127c1a45aedStobias 		return 1;
128c1a45aedStobias 	}
129c1a45aedStobias 
130c1a45aedStobias 	return 0;
131c1a45aedStobias }
132