lordgamez commented on a change in pull request #987:
URL: https://github.com/apache/nifi-minifi-cpp/pull/987#discussion_r570186050



##########
File path: libminifi/test/unit/FileStreamTests.cpp
##########
@@ -263,3 +267,75 @@ TEST_CASE("Read zero bytes") {
   minifi::io::FileStream stream(utils::file::concat_path(dir, "test.txt"), 0, 
true);
   REQUIRE(stream.read(nullptr, 0) == 0);
 }
+
+TEST_CASE("Non-existing file read/write test") {
+  TestController test_controller;
+  char format[] = "/tmp/gt.XXXXXX";
+  auto dir = test_controller.createTempDirectory(format);
+  minifi::io::FileStream stream(utils::file::concat_path(dir, 
"non_existing_file.txt"), 0, true);
+  REQUIRE(test_controller.getLog().getInstance().contains("Error opening 
file", std::chrono::seconds(0)));
+  REQUIRE(test_controller.getLog().getInstance().contains("No such file or 
directory", std::chrono::seconds(0)));
+  REQUIRE(stream.write("lorem ipsum", false) == -1);
+  REQUIRE(test_controller.getLog().getInstance().contains("Error writing to 
file: invalid file stream", std::chrono::seconds(0)));
+  std::vector<uint8_t> readBuffer;
+  stream.seek(0);
+  REQUIRE(stream.read(readBuffer, 1) == -1);
+  REQUIRE(test_controller.getLog().getInstance().contains("Error reading from 
file: invalid file stream", std::chrono::seconds(0)));
+}
+
+TEST_CASE("Existing file read/write test") {
+  TestController test_controller;
+  char format[] = "/tmp/gt.XXXXXX";
+  auto dir = test_controller.createTempDirectory(format);
+  std::string path_to_existing_file(utils::file::concat_path(dir, 
"existing_file.txt"));
+  {
+    std::ofstream outfile(path_to_existing_file);
+    outfile << "lorem ipsum" << std::endl;
+    outfile.close();
+  }
+  minifi::io::FileStream stream(path_to_existing_file, 0, true);
+  REQUIRE_FALSE(test_controller.getLog().getInstance().contains("Error opening 
file", std::chrono::seconds(0)));
+  REQUIRE_FALSE(stream.write("dolor sit amet", false) == -1);
+  REQUIRE_FALSE(test_controller.getLog().getInstance().contains("Error writing 
to file", std::chrono::seconds(0)));
+  std::vector<uint8_t> readBuffer;
+  stream.seek(0);
+  REQUIRE_FALSE(stream.read(readBuffer, 11) == -1);
+  REQUIRE_FALSE(test_controller.getLog().getInstance().contains("Error reading 
from file", std::chrono::seconds(0)));
+  stream.seek(0);
+  REQUIRE(stream.read(nullptr, 11) == -1);
+  REQUIRE(test_controller.getLog().getInstance().contains("Error reading from 
file: invalid buffer", std::chrono::seconds(0)));
+}
+
+#ifdef USE_BOOST
+TEST_CASE("Opening file without permission creates error logs") {
+  TestController test_controller;
+  char format[] = "/tmp/gt.XXXXXX";
+  auto dir = test_controller.createTempDirectory(format);
+  std::string path_to_permissionless_file(utils::file::concat_path(dir, 
"permissionless_file.txt"));
+  {
+    std::ofstream outfile(path_to_permissionless_file);
+    outfile << "this file has been just created" << std::endl;
+    outfile.close();
+    // This could be done with C++17 std::filesystem
+    boost::filesystem::permissions(path_to_permissionless_file, 
boost::filesystem::no_perms);

Review comment:
       I think that's a good idea trying both. I just really didn't insist on 
running it on Windows as currently we do not have boost installed on our 
Windows CI runners.

##########
File path: libminifi/src/io/FileStream.cpp
##########
@@ -77,10 +101,16 @@ void FileStream::close() {
 
 void FileStream::seek(uint64_t offset) {
   std::lock_guard<std::mutex> lock(file_lock_);
+  if (file_stream_ == nullptr || !file_stream_->is_open()) {
+    logging::LOG_ERROR(logger_) << seek_error << invalid_file_stream_error_msg;
+    return;
+  }
   offset_ = gsl::narrow<size_t>(offset);
   file_stream_->clear();
-  file_stream_->seekg(offset_);
-  file_stream_->seekp(offset_);
+  if (!file_stream_->seekg(offset_))
+    logging::LOG_ERROR(logger_) << seek_error << seekg_call_error_msg;

Review comment:
       Okay, makes sense




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to