1 /* $OpenBSD: ibuf_test.c,v 1.2 2023/05/23 09:32:37 claudio Exp $ 2 */ 3 /* 4 * Copyright (c) Tobias Stoeckmann <tobias@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/queue.h> 20 #include <sys/types.h> 21 #include <sys/uio.h> 22 23 #include <imsg.h> 24 #include <limits.h> 25 #include <stdint.h> 26 #include <stdio.h> 27 28 int 29 test_ibuf_open(void) { 30 struct ibuf *buf; 31 32 if ((buf = ibuf_open(1)) == NULL) 33 return 1; 34 35 ibuf_free(buf); 36 return 0; 37 } 38 39 int 40 test_ibuf_dynamic(void) { 41 struct ibuf *buf; 42 43 if (ibuf_dynamic(100, 0) != NULL) 44 return 1; 45 46 if ((buf = ibuf_dynamic(10, SIZE_MAX)) == NULL) 47 return 1; 48 49 ibuf_free(buf); 50 return 0; 51 } 52 53 int 54 test_ibuf_reserve(void) { 55 struct ibuf *buf; 56 int ret; 57 58 if ((buf = ibuf_dynamic(10, SIZE_MAX)) == NULL) { 59 return 1; 60 } 61 62 if (ibuf_reserve(buf, SIZE_MAX) != NULL) { 63 ibuf_free(buf); 64 return 1; 65 } 66 67 if (ibuf_reserve(buf, 10) == NULL) { 68 ibuf_free(buf); 69 return 1; 70 } 71 72 ret = (ibuf_reserve(buf, SIZE_MAX) != NULL); 73 74 ibuf_free(buf); 75 return ret; 76 } 77 78 int 79 test_ibuf_seek(void) { 80 struct ibuf *buf; 81 int ret; 82 83 if ((buf = ibuf_open(10)) == NULL) 84 return 1; 85 86 ret = (ibuf_seek(buf, 1, SIZE_MAX) != NULL); 87 88 ibuf_free(buf); 89 return ret; 90 } 91 92 int 93 test_msgbuf_drain(void) { 94 struct msgbuf msgbuf; 95 struct ibuf *buf; 96 97 msgbuf_init(&msgbuf); 98 99 if ((buf = ibuf_open(4)) == NULL) 100 return 1; 101 if (ibuf_add(buf, "test", 4) != 0) { 102 ibuf_free(buf); 103 return 1; 104 } 105 ibuf_close(&msgbuf, buf); 106 107 if (msgbuf.queued != 1) { 108 ibuf_free(buf); 109 return 1; 110 } 111 112 msgbuf_drain(&msgbuf, 1); 113 114 if (msgbuf.queued != 1) { 115 ibuf_free(buf); 116 return 1; 117 } 118 119 msgbuf_drain(&msgbuf, SIZE_MAX); 120 121 if (msgbuf.queued != 0) { 122 ibuf_free(buf); 123 return 1; 124 } 125 126 return 0; 127 } 128 129 int 130 main(void) 131 { 132 extern char *__progname; 133 134 int ret = 0; 135 136 if (test_ibuf_open() != 0) { 137 printf("FAILED: test_ibuf_open\n"); 138 ret = 1; 139 } else 140 printf("SUCCESS: test_ibuf_open\n"); 141 142 if (test_ibuf_dynamic() != 0) { 143 printf("FAILED: test_ibuf_dynamic\n"); 144 ret = 1; 145 } else 146 printf("SUCCESS: test_ibuf_dynamic\n"); 147 148 if (test_ibuf_reserve() != 0) { 149 printf("FAILED: test_ibuf_reserve\n"); 150 ret = 1; 151 } else 152 printf("SUCCESS: test_ibuf_reserve\n"); 153 154 if (test_ibuf_seek() != 0) { 155 printf("FAILED: test_ibuf_seek\n"); 156 ret = 1; 157 } else 158 printf("SUCCESS: test_ibuf_seek\n"); 159 160 if (test_msgbuf_drain() != 0) { 161 printf("FAILED: test_msgbuf_drain\n"); 162 ret = 1; 163 } else 164 printf("SUCCESS: test_msgbuf_drain\n"); 165 166 if (ret != 0) { 167 printf("FAILED: %s\n", __progname); 168 return 1; 169 } 170 171 return 0; 172 } 173