Path: blob/main/contrib/kyua/utils/process/systembuf_test.cpp
48199 views
// Copyright 2010 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "utils/process/systembuf.hpp"2930extern "C" {31#include <sys/stat.h>3233#include <fcntl.h>34#include <unistd.h>35}3637#include <fstream>3839#include <atf-c++.hpp>4041using utils::process::systembuf;424344static void45check_data(std::istream& is, std::size_t length)46{47char ch = 'A', chr;48std::size_t cnt = 0;49while (is >> chr) {50ATF_REQUIRE_EQ(ch, chr);51if (ch == 'Z')52ch = 'A';53else54ch++;55cnt++;56}57ATF_REQUIRE_EQ(cnt, length);58}596061static void62write_data(std::ostream& os, std::size_t length)63{64char ch = 'A';65for (std::size_t i = 0; i < length; i++) {66os << ch;67if (ch == 'Z')68ch = 'A';69else70ch++;71}72os.flush();73}747576static void77test_read(std::size_t length, std::size_t bufsize)78{79std::ofstream f("test_read.txt");80write_data(f, length);81f.close();8283int fd = ::open("test_read.txt", O_RDONLY);84ATF_REQUIRE(fd != -1);85systembuf sb(fd, bufsize);86std::istream is(&sb);87check_data(is, length);88::close(fd);89::unlink("test_read.txt");90}919293static void94test_write(std::size_t length, std::size_t bufsize)95{96int fd = ::open("test_write.txt", O_WRONLY | O_CREAT | O_TRUNC,97S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);98ATF_REQUIRE(fd != -1);99systembuf sb(fd, bufsize);100std::ostream os(&sb);101write_data(os, length);102::close(fd);103104std::ifstream is("test_write.txt");105check_data(is, length);106is.close();107::unlink("test_write.txt");108}109110111ATF_TEST_CASE(short_read);112ATF_TEST_CASE_HEAD(short_read)113{114set_md_var("descr", "Tests that a short read (one that fits in the "115"internal buffer) works when using systembuf");116}117ATF_TEST_CASE_BODY(short_read)118{119test_read(64, 1024);120}121122123ATF_TEST_CASE(long_read);124ATF_TEST_CASE_HEAD(long_read)125{126set_md_var("descr", "Tests that a long read (one that does not fit in "127"the internal buffer) works when using systembuf");128}129ATF_TEST_CASE_BODY(long_read)130{131test_read(64 * 1024, 1024);132}133134135ATF_TEST_CASE(short_write);136ATF_TEST_CASE_HEAD(short_write)137{138set_md_var("descr", "Tests that a short write (one that fits in the "139"internal buffer) works when using systembuf");140}141ATF_TEST_CASE_BODY(short_write)142{143test_write(64, 1024);144}145146147ATF_TEST_CASE(long_write);148ATF_TEST_CASE_HEAD(long_write)149{150set_md_var("descr", "Tests that a long write (one that does not fit "151"in the internal buffer) works when using systembuf");152}153ATF_TEST_CASE_BODY(long_write)154{155test_write(64 * 1024, 1024);156}157158159ATF_INIT_TEST_CASES(tcs)160{161ATF_ADD_TEST_CASE(tcs, short_read);162ATF_ADD_TEST_CASE(tcs, long_read);163ATF_ADD_TEST_CASE(tcs, short_write);164ATF_ADD_TEST_CASE(tcs, long_write);165}166167168