1316557Sngie/*- 2316557Sngie * Copyright (c) 2017 Ngie Cooper <ngie@freebsd.org> 3316557Sngie * All rights reserved. 4316557Sngie * 5316557Sngie * Redistribution and use in source and binary forms, with or without 6316557Sngie * modification, are permitted provided that the following conditions 7316557Sngie * are met: 8316557Sngie * 1. Redistributions of source code must retain the above copyright 9316557Sngie * notice, this list of conditions and the following disclaimer. 10316557Sngie * 2. Redistributions in binary form must reproduce the above copyright 11316557Sngie * notice, this list of conditions and the following disclaimer in the 12316557Sngie * documentation and/or other materials provided with the distribution. 13316557Sngie * 14316557Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15316557Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16316557Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17316557Sngie * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18316557Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19316557Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20316557Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21316557Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22316557Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23316557Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24316557Sngie * SUCH DAMAGE. 25316557Sngie */ 26316557Sngie 27321118Sngie#include <sys/cdefs.h> 28321118Sngie__FBSDID("$FreeBSD: stable/10/lib/libsbuf/tests/sbuf_stdio_test.c 321118 2017-07-18 08:30:58Z ngie $"); 29321118Sngie 30316557Sngie#include <sys/param.h> 31316557Sngie#include <sys/sbuf.h> 32316557Sngie#include <errno.h> 33316557Sngie#include <stdarg.h> 34316557Sngie#include <stdio.h> 35316557Sngie#include <stdlib.h> 36316557Sngie#include <string.h> 37316557Sngie#include <unistd.h> 38316557Sngie 39316557Sngie#include <atf-c.h> 40316557Sngie 41316557Sngie#include "sbuf_test_common.h" 42316557Sngie 43316557Sngiestatic char test_string[] = "this is a test string"; 44316557Sngie 45316557Sngie#define MESSAGE_FORMAT "message: %s\n" 46316557Sngie#define MESSAGE_SEPARATOR ';' 47316557Sngie 48316557Sngiestatic int 49316557Sngiesbuf_vprintf_helper(struct sbuf *sb, const char * restrict format, ...) 50316557Sngie{ 51316557Sngie va_list ap; 52316557Sngie int rc; 53316557Sngie 54316557Sngie va_start(ap, format); 55316557Sngie 56316557Sngie rc = sbuf_vprintf(sb, format, ap); 57316557Sngie 58316557Sngie va_end(ap); 59316557Sngie 60316557Sngie return (rc); 61316557Sngie} 62316557Sngie 63316557SngieATF_TC_WITHOUT_HEAD(sbuf_printf_test); 64316557SngieATF_TC_BODY(sbuf_printf_test, tc) 65316557Sngie{ 66316557Sngie struct sbuf *sb; 67316557Sngie char *test_string_tmp; 68316557Sngie 69316557Sngie asprintf(&test_string_tmp, "%s%c" MESSAGE_FORMAT, 70316557Sngie test_string, MESSAGE_SEPARATOR, test_string); 71316557Sngie ATF_REQUIRE_MSG(test_string_tmp != NULL, "asprintf failed"); 72316557Sngie 73316557Sngie sb = sbuf_new_auto(); 74316557Sngie ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", 75316557Sngie strerror(errno)); 76316557Sngie 77316557Sngie ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed"); 78316557Sngie ATF_REQUIRE_MSG(sbuf_putc(sb, MESSAGE_SEPARATOR) == 0, 79316557Sngie "sbuf_putc failed"); 80316557Sngie 81316557Sngie ATF_REQUIRE_MSG(sbuf_printf(sb, MESSAGE_FORMAT, test_string) == 0, 82316557Sngie "sbuf_printf failed"); 83316557Sngie 84316557Sngie ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", 85316557Sngie strerror(errno)); 86316557Sngie 87316557Sngie ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), test_string_tmp, 88316557Sngie "sbuf (\"%s\") != test string (\"%s\")", sbuf_data(sb), 89316557Sngie test_string_tmp); 90316557Sngie 91316557Sngie sbuf_delete(sb); 92316557Sngie 93316557Sngie free(test_string_tmp); 94316557Sngie} 95316557Sngie 96316557SngieATF_TC_WITHOUT_HEAD(sbuf_putbuf_test); 97316557SngieATF_TC_BODY(sbuf_putbuf_test, tc) 98316557Sngie{ 99316557Sngie struct sbuf *sb; 100316557Sngie pid_t child_proc; 101316557Sngie 102316557Sngie sb = sbuf_new_auto(); 103316557Sngie ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", 104316557Sngie strerror(errno)); 105316557Sngie 106316557Sngie ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed"); 107316557Sngie 108316557Sngie child_proc = atf_utils_fork(); 109316557Sngie if (child_proc == 0) { 110316557Sngie sbuf_putbuf(sb); 111316557Sngie exit(0); 112316557Sngie } 113316557Sngie atf_utils_wait(child_proc, 0, test_string, ""); 114316557Sngie 115316557Sngie ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", 116316557Sngie strerror(errno)); 117316557Sngie 118316557Sngie sbuf_delete(sb); 119316557Sngie} 120316557Sngie 121316557SngieATF_TC_WITHOUT_HEAD(sbuf_vprintf_test); 122316557SngieATF_TC_BODY(sbuf_vprintf_test, tc) 123316557Sngie{ 124316557Sngie struct sbuf *sb; 125316557Sngie char *test_string_tmp; 126316557Sngie int rc; 127316557Sngie 128316557Sngie asprintf(&test_string_tmp, "%s%c" MESSAGE_FORMAT, 129316557Sngie test_string, MESSAGE_SEPARATOR, test_string); 130316557Sngie ATF_REQUIRE_MSG(test_string_tmp != NULL, "asprintf failed"); 131316557Sngie 132316557Sngie sb = sbuf_new_auto(); 133316557Sngie ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", 134316557Sngie strerror(errno)); 135316557Sngie 136316557Sngie ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed"); 137316557Sngie ATF_REQUIRE_MSG(sbuf_putc(sb, MESSAGE_SEPARATOR) == 0, 138316557Sngie "sbuf_putc failed"); 139316557Sngie 140316557Sngie rc = sbuf_vprintf_helper(sb, MESSAGE_FORMAT, test_string); 141316557Sngie ATF_REQUIRE_MSG(rc == 0, "sbuf_vprintf failed"); 142316557Sngie 143316557Sngie ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", 144316557Sngie strerror(errno)); 145316557Sngie 146316557Sngie ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), test_string_tmp, 147316557Sngie "sbuf (\"%s\") != test string (\"%s\")", sbuf_data(sb), 148316557Sngie test_string_tmp); 149316557Sngie 150316557Sngie sbuf_delete(sb); 151316557Sngie} 152316557Sngie 153316557SngieATF_TP_ADD_TCS(tp) 154316557Sngie{ 155316557Sngie 156316557Sngie ATF_TP_ADD_TC(tp, sbuf_printf_test); 157316557Sngie ATF_TP_ADD_TC(tp, sbuf_putbuf_test); 158316557Sngie ATF_TP_ADD_TC(tp, sbuf_vprintf_test); 159316557Sngie 160316557Sngie return (atf_no_error()); 161316557Sngie} 162