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.
----------------------------------------------------------------
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]