diff options
author | Jack Lloyd <[email protected]> | 2017-09-21 19:19:28 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-09-21 19:19:28 -0400 |
commit | bba14785c7b146e477e350a250010b2d4a200bf9 (patch) | |
tree | 54a1573c5290b41080ae00e2746b2edc5442272e /src | |
parent | 933d384010284a2f85a02f24904aceb1fb476880 (diff) |
Add a test of Pipe io on Unix file descriptors
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/filters/fd_unix/fd_unix.cpp | 18 | ||||
-rw-r--r-- | src/tests/test_filters.cpp | 37 |
2 files changed, 47 insertions, 8 deletions
diff --git a/src/lib/filters/fd_unix/fd_unix.cpp b/src/lib/filters/fd_unix/fd_unix.cpp index 53f396cab..5ce29e875 100644 --- a/src/lib/filters/fd_unix/fd_unix.cpp +++ b/src/lib/filters/fd_unix/fd_unix.cpp @@ -23,11 +23,12 @@ int operator<<(int fd, Pipe& pipe) size_t position = 0; while(got) { - ssize_t ret = write(fd, &buffer[position], got); - if(ret == -1) + ssize_t ret = ::write(fd, &buffer[position], got); + if(ret < 0) throw Stream_IO_Error("Pipe output operator (unixfd) has failed"); - position += ret; - got -= ret; + + position += static_cast<size_t>(ret); + got -= static_cast<size_t>(ret); } } return fd; @@ -41,11 +42,12 @@ int operator>>(int fd, Pipe& pipe) secure_vector<uint8_t> buffer(DEFAULT_BUFFERSIZE); while(true) { - ssize_t ret = read(fd, buffer.data(), buffer.size()); - if(ret == 0) break; - if(ret == -1) + ssize_t ret = ::read(fd, buffer.data(), buffer.size()); + if(ret < 0) throw Stream_IO_Error("Pipe input operator (unixfd) has failed"); - pipe.write(buffer.data(), ret); + else if(ret == 0) + break; + pipe.write(buffer.data(), static_cast<size_t>(ret)); } return fd; } diff --git a/src/tests/test_filters.cpp b/src/tests/test_filters.cpp index b024140e5..fdadbb6c6 100644 --- a/src/tests/test_filters.cpp +++ b/src/tests/test_filters.cpp @@ -23,6 +23,11 @@ #include <botan/b64_filt.h> #endif +#if defined(BOTAN_HAS_PIPE_UNIXFD_IO) + #include <botan/fd_unix.h> + #include <unistd.h> +#endif + namespace Botan_Tests { #if defined(BOTAN_HAS_FILTERS) @@ -50,6 +55,10 @@ class Filter_Tests : public Test results.push_back(test_chain()); results.push_back(test_threaded_fork()); +#if defined(BOTAN_HAS_PIPE_UNIXFD_IO) + results.push_back(test_pipe_fd_io()); +#endif + return results; } @@ -593,6 +602,34 @@ class Filter_Tests : public Test return result; } +#if defined(BOTAN_HAS_PIPE_UNIXFD_IO) + Test::Result test_pipe_fd_io() + { + Test::Result result("Pipe file descriptor IO"); + + int fd[2]; + if(::pipe(fd) != 0) + return result; // pipe unavailable? + + Botan::Pipe hex_enc(new Botan::Hex_Encoder); + Botan::Pipe hex_dec(new Botan::Hex_Decoder); + + hex_enc.process_msg("hi chappy"); + fd[1] << hex_enc; + ::close(fd[1]); + + hex_dec.start_msg(); + fd[0] >> hex_dec; + hex_dec.end_msg(); + ::close(fd[0]); + + std::string dec = hex_dec.read_all_as_string(); + + result.test_eq("IO through Unix pipe works", dec, "hi chappy"); + + return result; + } +#endif Test::Result test_threaded_fork() { |