diff options
author | ambrosin <ambrosin@google.com> | 2023-01-25 01:12:23 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-01-25 01:14:10 -0800 |
commit | 70f226b5793abfa382d365488a17d475a09bbf13 (patch) | |
tree | 16dc0bafe31b4c49964e64849bb6635053048158 /cc/util | |
parent | 28c6858bd4a32dc2c49b51eab4d4ab9efa79cfc9 (diff) | |
download | tink-70f226b5793abfa382d365488a17d475a09bbf13.tar.gz |
Remove usage of `unistd.h` from cc/util/test_util
- Add an internal utility function to create a test file that uses the C++ STD streams-based APIs
- Limit the use of `unistd.h` to util/file_*stream_test.cc
#tinkApiChange
PiperOrigin-RevId: 504491234
Diffstat (limited to 'cc/util')
-rw-r--r-- | cc/util/BUILD.bazel | 8 | ||||
-rw-r--r-- | cc/util/CMakeLists.txt | 8 | ||||
-rw-r--r-- | cc/util/file_input_stream_test.cc | 100 | ||||
-rw-r--r-- | cc/util/file_output_stream_test.cc | 39 | ||||
-rw-r--r-- | cc/util/file_random_access_stream_test.cc | 89 | ||||
-rw-r--r-- | cc/util/test_util.cc | 60 | ||||
-rw-r--r-- | cc/util/test_util.h | 14 |
7 files changed, 181 insertions, 137 deletions
diff --git a/cc/util/BUILD.bazel b/cc/util/BUILD.bazel index 5b74a21e1..d140ff70d 100644 --- a/cc/util/BUILD.bazel +++ b/cc/util/BUILD.bazel @@ -450,6 +450,8 @@ cc_test( ":status", ":test_matchers", ":test_util", + "//internal:test_file_util", + "//subtle:random", "@com_google_absl//absl/memory", "@com_google_absl//absl/status", "@com_google_absl//absl/strings", @@ -462,7 +464,9 @@ cc_test( srcs = ["file_output_stream_test.cc"], deps = [ ":file_output_stream", + ":test_matchers", ":test_util", + "//internal:test_file_util", "//subtle:random", "@com_google_absl//absl/memory", "@com_google_absl//absl/strings", @@ -476,8 +480,12 @@ cc_test( deps = [ ":buffer", ":file_random_access_stream", + ":test_matchers", ":test_util", + "//internal:test_file_util", + "//subtle:random", "@com_google_absl//absl/memory", + "@com_google_absl//absl/status", "@com_google_absl//absl/strings", "@com_google_googletest//:gtest_main", ], diff --git a/cc/util/CMakeLists.txt b/cc/util/CMakeLists.txt index a5f8adbd9..2fbcc9c8f 100644 --- a/cc/util/CMakeLists.txt +++ b/cc/util/CMakeLists.txt @@ -309,6 +309,8 @@ tink_cc_test( absl::memory absl::status absl::strings + tink::internal::test_file_util + tink::subtle::random ) tink_cc_test( @@ -317,10 +319,12 @@ tink_cc_test( file_output_stream_test.cc DEPS tink::util::file_output_stream + tink::util::test_matchers tink::util::test_util gmock absl::memory absl::strings + tink::internal::test_file_util tink::subtle::random ) @@ -331,10 +335,14 @@ tink_cc_test( DEPS tink::util::buffer tink::util::file_random_access_stream + tink::util::test_matchers tink::util::test_util gmock absl::memory + absl::status absl::strings + tink::internal::test_file_util + tink::subtle::random ) tink_cc_test( diff --git a/cc/util/file_input_stream_test.cc b/cc/util/file_input_stream_test.cc index c6e62a061..b8f3586b3 100644 --- a/cc/util/file_input_stream_test.cc +++ b/cc/util/file_input_stream_test.cc @@ -15,8 +15,13 @@ /////////////////////////////////////////////////////////////////////////////// #include "tink/util/file_input_stream.h" +#include <fcntl.h> + #include <algorithm> #include <cstdint> +#include <cstring> +#include <iostream> +#include <ostream> #include <string> #include "gmock/gmock.h" @@ -25,6 +30,8 @@ #include "absl/status/status.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" +#include "tink/internal/test_file_util.h" +#include "tink/subtle/random.h" #include "tink/util/status.h" #include "tink/util/test_matchers.h" #include "tink/util/test_util.h" @@ -39,6 +46,18 @@ using ::crypto::tink::test::StatusIs; constexpr int kDefaultTestStreamSize = 100 * 1024; // 100 KB. +// Opens test file `filename` and returns a file descriptor to it. +util::StatusOr<int> OpenTestFileToRead(absl::string_view filename) { + std::string full_filename = absl::StrCat(test::TmpDir(), "/", filename); + int fd = open(full_filename.c_str(), O_RDONLY); + if (fd == -1) { + return util::Status(absl::StatusCode::kInternal, + absl::StrCat("Cannot open file ", full_filename, + " error: ", std::strerror(errno))); + } + return fd; +} + // Reads the specified `input_stream` until no more bytes can be read, // and puts the read bytes into `contents`. // Returns the status of the last input_stream->Next()-operation. @@ -59,12 +78,13 @@ using FileInputStreamTestDefaultBufferSize = testing::TestWithParam<int>; TEST_P(FileInputStreamTestDefaultBufferSize, ReadAllfFromInputStreamSucceeds) { int stream_size = GetParam(); SCOPED_TRACE(absl::StrCat("stream_size = ", stream_size)); - std::string file_contents; + std::string file_contents = subtle::Random::GetRandomBytes(stream_size); std::string filename = absl::StrCat(stream_size, "_reading_test.bin"); - int input_fd = - test::GetTestFileDescriptor(filename, stream_size, &file_contents); + ASSERT_THAT(internal::CreateTestFile(filename, file_contents), IsOk()); + util::StatusOr<int> input_fd = OpenTestFileToRead(filename); + ASSERT_THAT(input_fd.status(), IsOk()); EXPECT_EQ(stream_size, file_contents.size()); - auto input_stream = absl::make_unique<util::FileInputStream>(input_fd); + auto input_stream = absl::make_unique<util::FileInputStream>(*input_fd); std::string stream_contents; auto status = ReadAll(input_stream.get(), &stream_contents); EXPECT_THAT(status, StatusIs(absl::StatusCode::kOutOfRange)); @@ -83,13 +103,15 @@ TEST_P(FileInputStreamTestCustomBufferSizes, ReadAllWithCustomBufferSizeSucceeds) { int buffer_size = GetParam(); SCOPED_TRACE(absl::StrCat("buffer_size = ", buffer_size)); - std::string file_contents; - std::string filename = absl::StrCat(buffer_size, "_buffer_size_test.bin"); - int input_fd = test::GetTestFileDescriptor(filename, kDefaultTestStreamSize, - &file_contents); + std::string file_contents = + subtle::Random::GetRandomBytes(kDefaultTestStreamSize); + std::string filename = absl::StrCat(buffer_size, "_reading_test.bin"); + ASSERT_THAT(internal::CreateTestFile(filename, file_contents), IsOk()); + util::StatusOr<int> input_fd = OpenTestFileToRead(filename); + ASSERT_THAT(input_fd.status(), IsOk()); EXPECT_EQ(kDefaultTestStreamSize, file_contents.size()); auto input_stream = - absl::make_unique<util::FileInputStream>(input_fd, buffer_size); + absl::make_unique<util::FileInputStream>(*input_fd, buffer_size); const void* buffer; auto next_result = input_stream->Next(&buffer); ASSERT_THAT(next_result, IsOk()); @@ -112,13 +134,15 @@ TEST(FileInputStreamTest, NextFailsIfFdIsInvalid) { TEST(FileInputStreamTest, NextFailsIfDataIsNull) { int buffer_size = 4 * 1024; - std::string file_contents; - std::string filename = absl::StrCat(buffer_size, "_backup_test.bin"); - int input_fd = test::GetTestFileDescriptor(filename, kDefaultTestStreamSize, - &file_contents); + std::string file_contents = + subtle::Random::GetRandomBytes(kDefaultTestStreamSize); + std::string filename = absl::StrCat(buffer_size, "_reading_test.bin"); + ASSERT_THAT(internal::CreateTestFile(filename, file_contents), IsOk()); + util::StatusOr<int> input_fd = OpenTestFileToRead(filename); + ASSERT_THAT(input_fd.status(), IsOk()); EXPECT_EQ(kDefaultTestStreamSize, file_contents.size()); auto input_stream = - absl::make_unique<util::FileInputStream>(input_fd, buffer_size); + absl::make_unique<util::FileInputStream>(*input_fd, buffer_size); EXPECT_THAT(input_stream->Next(nullptr).status(), StatusIs(absl::StatusCode::kInvalidArgument)); @@ -126,13 +150,15 @@ TEST(FileInputStreamTest, NextFailsIfDataIsNull) { TEST(FileInputStreamTest, NextReadsExactlyOneBlockOfData) { int buffer_size = 4 * 1024; - std::string file_contents; - std::string filename = absl::StrCat(buffer_size, "_backup_test.bin"); - int input_fd = test::GetTestFileDescriptor(filename, kDefaultTestStreamSize, - &file_contents); + std::string file_contents = + subtle::Random::GetRandomBytes(kDefaultTestStreamSize); + std::string filename = absl::StrCat(buffer_size, "_reading_test.bin"); + ASSERT_THAT(internal::CreateTestFile(filename, file_contents), IsOk()); + util::StatusOr<int> input_fd = OpenTestFileToRead(filename); + ASSERT_THAT(input_fd.status(), IsOk()); EXPECT_EQ(kDefaultTestStreamSize, file_contents.size()); auto input_stream = - absl::make_unique<util::FileInputStream>(input_fd, buffer_size); + absl::make_unique<util::FileInputStream>(*input_fd, buffer_size); auto expected_file_content_block = absl::string_view(file_contents).substr(0, buffer_size); @@ -147,13 +173,15 @@ TEST(FileInputStreamTest, NextReadsExactlyOneBlockOfData) { TEST(FileInputStreamTest, BackupForNegativeOrZeroBytesIsANoop) { int buffer_size = 4 * 1024; - std::string file_contents; - std::string filename = absl::StrCat(buffer_size, "_backup_test.bin"); - int input_fd = test::GetTestFileDescriptor(filename, kDefaultTestStreamSize, - &file_contents); + std::string file_contents = + subtle::Random::GetRandomBytes(kDefaultTestStreamSize); + std::string filename = absl::StrCat(buffer_size, "_reading_test.bin"); + ASSERT_THAT(internal::CreateTestFile(filename, file_contents), IsOk()); + util::StatusOr<int> input_fd = OpenTestFileToRead(filename); + ASSERT_THAT(input_fd.status(), IsOk()); EXPECT_EQ(kDefaultTestStreamSize, file_contents.size()); auto input_stream = - absl::make_unique<util::FileInputStream>(input_fd, buffer_size); + absl::make_unique<util::FileInputStream>(*input_fd, buffer_size); EXPECT_EQ(input_stream->Position(), 0); auto expected_file_content_block = @@ -183,13 +211,15 @@ TEST(FileInputStreamTest, BackupForNegativeOrZeroBytesIsANoop) { TEST(FileInputStreamTest, BackupForLessThanOneBlockOfData) { int buffer_size = 4 * 1024; - std::string file_contents; - std::string filename = absl::StrCat(buffer_size, "_backup_test.bin"); - int input_fd = test::GetTestFileDescriptor(filename, kDefaultTestStreamSize, - &file_contents); + std::string file_contents = + subtle::Random::GetRandomBytes(kDefaultTestStreamSize); + std::string filename = absl::StrCat(buffer_size, "_reading_test.bin"); + ASSERT_THAT(internal::CreateTestFile(filename, file_contents), IsOk()); + util::StatusOr<int> input_fd = OpenTestFileToRead(filename); + ASSERT_THAT(input_fd.status(), IsOk()); EXPECT_EQ(kDefaultTestStreamSize, file_contents.size()); auto input_stream = - absl::make_unique<util::FileInputStream>(input_fd, buffer_size); + absl::make_unique<util::FileInputStream>(*input_fd, buffer_size); auto expected_file_content_block = absl::string_view(file_contents).substr(0, buffer_size); @@ -231,13 +261,15 @@ TEST(FileInputStreamTest, BackupForLessThanOneBlockOfData) { // of one block. TEST(FileInputStreamTest, BackupAtMostOfOneBlock) { int buffer_size = 4 * 1024; - std::string file_contents; - std::string filename = absl::StrCat(buffer_size, "_backup_test.bin"); - int input_fd = test::GetTestFileDescriptor(filename, kDefaultTestStreamSize, - &file_contents); + std::string file_contents = + subtle::Random::GetRandomBytes(kDefaultTestStreamSize); + std::string filename = absl::StrCat(buffer_size, "_reading_test.bin"); + ASSERT_THAT(internal::CreateTestFile(filename, file_contents), IsOk()); + util::StatusOr<int> input_fd = OpenTestFileToRead(filename); + ASSERT_THAT(input_fd.status(), IsOk()); EXPECT_EQ(kDefaultTestStreamSize, file_contents.size()); auto input_stream = - absl::make_unique<util::FileInputStream>(input_fd, buffer_size); + absl::make_unique<util::FileInputStream>(*input_fd, buffer_size); // Read two blocks of size buffer_size, then back up of more than buffer_size // bytes. diff --git a/cc/util/file_output_stream_test.cc b/cc/util/file_output_stream_test.cc index 2d26a46cb..27e5aec67 100644 --- a/cc/util/file_output_stream_test.cc +++ b/cc/util/file_output_stream_test.cc @@ -16,21 +16,42 @@ #include "tink/util/file_output_stream.h" +#include <fcntl.h> + #include <algorithm> #include <cstring> +#include <iostream> +#include <ostream> #include <string> #include "gtest/gtest.h" #include "absl/memory/memory.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" +#include "tink/internal/test_file_util.h" #include "tink/subtle/random.h" +#include "tink/util/test_matchers.h" #include "tink/util/test_util.h" namespace crypto { namespace tink { namespace { +using ::crypto::tink::test::IsOk; + +// Opens test file `filename` and returns a file descriptor to it. +util::StatusOr<int> OpenTestFileToWrite(absl::string_view filename) { + std::string full_filename = absl::StrCat(test::TmpDir(), "/", filename); + mode_t mode = S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH; + int fd = open(full_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, mode); + if (fd == -1) { + return util::Status(absl::StatusCode::kInternal, + absl::StrCat("Cannot open file ", full_filename, + " error: ", std::strerror(errno))); + } + return fd; +} + // Writes 'contents' the specified 'output_stream', and closes the stream. // Returns the status of output_stream->Close()-operation, or a non-OK status // of a prior output_stream->Next()-operation, if any. @@ -64,8 +85,10 @@ TEST_F(FileOutputStreamTest, WritingStreams) { SCOPED_TRACE(absl::StrCat("stream_size = ", stream_size)); std::string stream_contents = subtle::Random::GetRandomBytes(stream_size); std::string filename = absl::StrCat(stream_size, "_writing_test.bin"); - int output_fd = test::GetTestFileDescriptor(filename); - auto output_stream = absl::make_unique<util::FileOutputStream>(output_fd); + ASSERT_THAT(internal::CreateTestFile(filename, stream_contents), IsOk()); + util::StatusOr<int> output_fd = OpenTestFileToWrite(filename); + ASSERT_THAT(output_fd.status(), IsOk()); + auto output_stream = absl::make_unique<util::FileOutputStream>(*output_fd); auto status = WriteToStream(output_stream.get(), stream_contents); EXPECT_TRUE(status.ok()) << status; std::string file_contents = test::ReadTestFile(filename); @@ -80,9 +103,11 @@ TEST_F(FileOutputStreamTest, CustomBufferSizes) { for (auto buffer_size : {1, 10, 100, 1000, 10000, 100000, 1000000}) { SCOPED_TRACE(absl::StrCat("buffer_size = ", buffer_size)); std::string filename = absl::StrCat(buffer_size, "_buffer_size_test.bin"); - int output_fd = test::GetTestFileDescriptor(filename); + ASSERT_THAT(internal::CreateTestFile(filename, stream_contents), IsOk()); + util::StatusOr<int> output_fd = OpenTestFileToWrite(filename); + ASSERT_THAT(output_fd.status(), IsOk()); auto output_stream = - absl::make_unique<util::FileOutputStream>(output_fd, buffer_size); + absl::make_unique<util::FileOutputStream>(*output_fd, buffer_size); void* buffer; auto next_result = output_stream->Next(&buffer); EXPECT_TRUE(next_result.ok()) << next_result.status(); @@ -103,11 +128,13 @@ TEST_F(FileOutputStreamTest, BackupAndPosition) { void* buffer; std::string stream_contents = subtle::Random::GetRandomBytes(stream_size); std::string filename = absl::StrCat(buffer_size, "_backup_test.bin"); - int output_fd = test::GetTestFileDescriptor(filename); + ASSERT_THAT(internal::CreateTestFile(filename, stream_contents), IsOk()); + util::StatusOr<int> output_fd = OpenTestFileToWrite(filename); + ASSERT_THAT(output_fd.status(), IsOk()); // Prepare the stream and do the first call to Next(). auto output_stream = - absl::make_unique<util::FileOutputStream>(output_fd, buffer_size); + absl::make_unique<util::FileOutputStream>(*output_fd, buffer_size); EXPECT_EQ(0, output_stream->Position()); auto next_result = output_stream->Next(&buffer); EXPECT_TRUE(next_result.ok()) << next_result.status(); diff --git a/cc/util/file_random_access_stream_test.cc b/cc/util/file_random_access_stream_test.cc index 70c510a87..304848f68 100644 --- a/cc/util/file_random_access_stream_test.cc +++ b/cc/util/file_random_access_stream_test.cc @@ -16,15 +16,25 @@ #include "tink/util/file_random_access_stream.h" +#include <fcntl.h> +#include <unistd.h> + +#include <cstring> +#include <iostream> +#include <ostream> #include <string> #include <thread> // NOLINT(build/c++11) #include <utility> #include "gtest/gtest.h" #include "absl/memory/memory.h" +#include "absl/status/status.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" +#include "tink/internal/test_file_util.h" +#include "tink/subtle/random.h" #include "tink/util/buffer.h" +#include "tink/util/test_matchers.h" #include "tink/util/test_util.h" namespace crypto { @@ -32,6 +42,20 @@ namespace tink { namespace util { namespace { +using ::crypto::tink::test::IsOk; + +// Opens test file `filename` and returns a file descriptor to it. +util::StatusOr<int> OpenTestFileToRead(absl::string_view filename) { + std::string full_filename = absl::StrCat(test::TmpDir(), "/", filename); + int fd = open(full_filename.c_str(), O_RDONLY); + if (fd == -1) { + return util::Status(absl::StatusCode::kInternal, + absl::StrCat("Cannot open file ", full_filename, + " error: ", std::strerror(errno))); + } + return fd; +} + // Reads the entire 'ra_stream' in chunks of size 'chunk_size', // until no more bytes can be read, and puts the read bytes into 'contents'. // Returns the status of the last ra_stream->Next()-operation. @@ -79,15 +103,17 @@ void ReadAndVerifyChunk(RandomAccessStream* ra_stream, TEST(FileRandomAccessStreamTest, ReadingStreams) { for (auto stream_size : {1, 10, 100, 1000, 10000, 1000000}) { SCOPED_TRACE(absl::StrCat("stream_size = ", stream_size)); - std::string file_contents; + std::string file_contents = subtle::Random::GetRandomBytes(stream_size); std::string filename = absl::StrCat(stream_size, "_reading_test.bin"); - int input_fd = - test::GetTestFileDescriptor(filename, stream_size, &file_contents); + ASSERT_THAT(crypto::tink::internal::CreateTestFile(filename, file_contents), + IsOk()); + util::StatusOr<int> input_fd = OpenTestFileToRead(filename); + ASSERT_THAT(input_fd.status(), IsOk()); EXPECT_EQ(stream_size, file_contents.size()); - auto ra_stream = absl::make_unique<util::FileRandomAccessStream>(input_fd); + auto ra_stream = absl::make_unique<util::FileRandomAccessStream>(*input_fd); std::string stream_contents; - auto status = ReadAll(ra_stream.get(), 1 + (stream_size / 10), - &stream_contents); + auto status = + ReadAll(ra_stream.get(), 1 + (stream_size / 10), &stream_contents); EXPECT_EQ(absl::StatusCode::kOutOfRange, status.code()); EXPECT_EQ("EOF", status.message()); EXPECT_EQ(file_contents, stream_contents); @@ -98,12 +124,14 @@ TEST(FileRandomAccessStreamTest, ReadingStreams) { TEST(FileRandomAccessStreamTest, ReadingStreamsTillLastByte) { for (auto stream_size : {1, 10, 100, 1000, 10000}) { SCOPED_TRACE(absl::StrCat("stream_size = ", stream_size)); - std::string file_contents; + std::string file_contents = subtle::Random::GetRandomBytes(stream_size); std::string filename = absl::StrCat(stream_size, "_reading_test.bin"); - int input_fd = - test::GetTestFileDescriptor(filename, stream_size, &file_contents); + ASSERT_THAT(crypto::tink::internal::CreateTestFile(filename, file_contents), + IsOk()); + util::StatusOr<int> input_fd = OpenTestFileToRead(filename); + ASSERT_THAT(input_fd.status(), IsOk()); EXPECT_EQ(stream_size, file_contents.size()); - auto ra_stream = absl::make_unique<util::FileRandomAccessStream>(input_fd); + auto ra_stream = absl::make_unique<util::FileRandomAccessStream>(*input_fd); auto buffer = std::move(Buffer::New(stream_size).value()); // Read from the beginning till the last byte. @@ -116,15 +144,16 @@ TEST(FileRandomAccessStreamTest, ReadingStreamsTillLastByte) { } } - TEST(FileRandomAccessStreamTest, ConcurrentReads) { for (auto stream_size : {100, 1000, 10000, 100000}) { - std::string file_contents; + std::string file_contents = subtle::Random::GetRandomBytes(stream_size); std::string filename = absl::StrCat(stream_size, "_reading_test.bin"); - int input_fd = - test::GetTestFileDescriptor(filename, stream_size, &file_contents); + ASSERT_THAT(crypto::tink::internal::CreateTestFile(filename, file_contents), + IsOk()); + util::StatusOr<int> input_fd = OpenTestFileToRead(filename); + ASSERT_THAT(input_fd.status(), IsOk()); EXPECT_EQ(stream_size, file_contents.size()); - auto ra_stream = absl::make_unique<util::FileRandomAccessStream>(input_fd); + auto ra_stream = absl::make_unique<util::FileRandomAccessStream>(*input_fd); std::thread read_0(ReadAndVerifyChunk, ra_stream.get(), 0, stream_size / 2, file_contents); std::thread read_1(ReadAndVerifyChunk, @@ -142,11 +171,13 @@ TEST(FileRandomAccessStreamTest, ConcurrentReads) { TEST(FileRandomAccessStreamTest, NegativeReadPosition) { for (auto stream_size : {0, 10, 100, 1000, 10000}) { - std::string file_contents; + std::string file_contents = subtle::Random::GetRandomBytes(stream_size); std::string filename = absl::StrCat(stream_size, "_reading_test.bin"); - int input_fd = - test::GetTestFileDescriptor(filename, stream_size, &file_contents); - auto ra_stream = absl::make_unique<util::FileRandomAccessStream>(input_fd); + ASSERT_THAT(crypto::tink::internal::CreateTestFile(filename, file_contents), + IsOk()); + util::StatusOr<int> input_fd = OpenTestFileToRead(filename); + ASSERT_THAT(input_fd.status(), IsOk()); + auto ra_stream = absl::make_unique<util::FileRandomAccessStream>(*input_fd); int count = 42; auto buffer = std::move(Buffer::New(count).value()); for (auto position : {-100, -10, -1}) { @@ -161,11 +192,13 @@ TEST(FileRandomAccessStreamTest, NegativeReadPosition) { TEST(FileRandomAccessStreamTest, NotPositiveReadCount) { for (auto stream_size : {0, 10, 100, 1000, 10000}) { - std::string file_contents; + std::string file_contents = subtle::Random::GetRandomBytes(stream_size); std::string filename = absl::StrCat(stream_size, "_reading_test.bin"); - int input_fd = - test::GetTestFileDescriptor(filename, stream_size, &file_contents); - auto ra_stream = absl::make_unique<util::FileRandomAccessStream>(input_fd); + ASSERT_THAT(crypto::tink::internal::CreateTestFile(filename, file_contents), + IsOk()); + util::StatusOr<int> input_fd = OpenTestFileToRead(filename); + ASSERT_THAT(input_fd.status(), IsOk()); + auto ra_stream = absl::make_unique<util::FileRandomAccessStream>(*input_fd); auto buffer = std::move(Buffer::New(42).value()); int64_t position = 0; for (auto count : {-100, -10, -1, 0}) { @@ -179,11 +212,13 @@ TEST(FileRandomAccessStreamTest, NotPositiveReadCount) { TEST(FileRandomAccessStreamTest, ReadPositionAfterEof) { for (auto stream_size : {0, 10, 100, 1000, 10000}) { - std::string file_contents; + std::string file_contents = subtle::Random::GetRandomBytes(stream_size); std::string filename = absl::StrCat(stream_size, "_reading_test.bin"); - int input_fd = - test::GetTestFileDescriptor(filename, stream_size, &file_contents); - auto ra_stream = absl::make_unique<util::FileRandomAccessStream>(input_fd); + ASSERT_THAT(crypto::tink::internal::CreateTestFile(filename, file_contents), + IsOk()); + util::StatusOr<int> input_fd = OpenTestFileToRead(filename); + ASSERT_THAT(input_fd.status(), IsOk()); + auto ra_stream = absl::make_unique<util::FileRandomAccessStream>(*input_fd); int count = 42; auto buffer = std::move(Buffer::New(count).value()); for (auto position : {stream_size + 1, stream_size + 10}) { diff --git a/cc/util/test_util.cc b/cc/util/test_util.cc index 9802c9df8..5413bba5b 100644 --- a/cc/util/test_util.cc +++ b/cc/util/test_util.cc @@ -16,10 +16,8 @@ #include "tink/util/test_util.h" -#include <fcntl.h> #include <stdarg.h> #include <stdlib.h> -#include <unistd.h> #include <cmath> #include <cstdint> @@ -75,54 +73,6 @@ namespace crypto { namespace tink { namespace test { -int GetTestFileDescriptor(absl::string_view filename, int size, - std::string* file_contents) { - (*file_contents) = subtle::Random::GetRandomBytes(size); - return GetTestFileDescriptor(filename, *file_contents); -} - -int GetTestFileDescriptor( - absl::string_view filename, absl::string_view file_contents) { - std::string full_filename = - absl::StrCat(crypto::tink::test::TmpDir(), "/", filename); - mode_t mode = S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH; - int fd = open(full_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, mode); - if (fd == -1) { - std::clog << "Cannot create file " << full_filename - << " error: " << errno << std::endl; - exit(1); - } - auto size = file_contents.size(); - if (write(fd, file_contents.data(), size) != size) { - std::clog << "Failed to write " << size << " bytes to file " - << full_filename << " error: " << errno << std::endl; - - exit(1); - } - close(fd); - fd = open(full_filename.c_str(), O_RDONLY); - if (fd == -1) { - std::clog << "Cannot re-open file " << full_filename - << " error: " << errno << std::endl; - exit(1); - } - return fd; -} - - -int GetTestFileDescriptor(absl::string_view filename) { - std::string full_filename = - absl::StrCat(crypto::tink::test::TmpDir(), "/", filename); - mode_t mode = S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH; - int fd = open(full_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, mode); - if (fd == -1) { - std::clog << "Cannot create file " << full_filename - << " error: " << errno << std::endl; - exit(1); - } - return fd; -} - std::string ReadTestFile(absl::string_view filename) { std::string full_filename = absl::StrCat(test::TmpDir(), "/", filename); std::ifstream input_stream(full_filename, std::ios::binary); @@ -186,12 +136,10 @@ std::string TmpDir() { return "/tmp"; } -void AddKeyData( - const google::crypto::tink::KeyData& key_data, - uint32_t key_id, - google::crypto::tink::OutputPrefixType output_prefix, - google::crypto::tink::KeyStatusType key_status, - google::crypto::tink::Keyset* keyset) { +void AddKeyData(const google::crypto::tink::KeyData& key_data, uint32_t key_id, + google::crypto::tink::OutputPrefixType output_prefix, + google::crypto::tink::KeyStatusType key_status, + google::crypto::tink::Keyset* keyset) { Keyset::Key* key = keyset->add_key(); key->set_output_prefix_type(output_prefix); key->set_key_id(key_id); diff --git a/cc/util/test_util.h b/cc/util/test_util.h index 4e2657d86..cba8433c1 100644 --- a/cc/util/test_util.h +++ b/cc/util/test_util.h @@ -64,20 +64,6 @@ namespace test { // Various utilities for testing. /////////////////////////////////////////////////////////////////////////////// -// Creates a new test file with the specified 'filename', writes 'size' random -// bytes to the file, and returns a file descriptor for reading from the file. -// A copy of the bytes written to the file is returned in 'file_contents'. -int GetTestFileDescriptor(absl::string_view filename, int size, - std::string* file_contents); - -// Creates a new test file with the specified 'filename', with contents from -// 'file_contents', and returns a file descriptor for reading from the file. -int GetTestFileDescriptor(absl::string_view filename, - absl::string_view file_contents); - -// Creates a new test file with the specified 'filename', ready for writing. -int GetTestFileDescriptor(absl::string_view filename); - // Reads the test file specified by `filename`, and returns its contents. std::string ReadTestFile(absl::string_view filename); |