adamdebreceni commented on a change in pull request #937:
URL: https://github.com/apache/nifi-minifi-cpp/pull/937#discussion_r526895679



##########
File path: libminifi/src/utils/file/FileSystem.cpp
##########
@@ -0,0 +1,80 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string>
+#include <fstream>
+#include "utils/file/FileSystem.h"
+#include "utils/OptionalUtils.h"
+#include "utils/EncryptionProvider.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+namespace file {
+
+FileSystem::FileSystem(bool should_encrypt, 
utils::optional<utils::crypto::EncryptionProvider> encryptor)
+    : should_encrypt_(should_encrypt),
+      encryptor_(std::move(encryptor)) {
+  if (should_encrypt_ && !encryptor) {
+    std::string err_message = "Requested file encryption but no encryption 
utility was provided";
+    logger_->log_error(err_message.c_str());
+    throw std::invalid_argument(err_message);
+  }
+}
+
+utils::optional<std::string> FileSystem::read(const std::string& file_name) {
+  std::ifstream input{file_name, std::ios::binary};
+  std::string content{std::istreambuf_iterator<char>(input), {}};

Review comment:
       done, you are right, that it should return `nullopt` if there is no such 
file, not if there was any other kind of error

##########
File path: libminifi/test/unit/FileSystemTests.cpp
##########
@@ -0,0 +1,90 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string>
+#include <fstream>
+#include <iterator>
+#include "../TestBase.h"
+#include "utils/file/FileSystem.h"
+
+using utils::crypto::EncryptionProvider;
+using utils::file::FileSystem;
+
+utils::crypto::Bytes encryption_key = 
utils::crypto::stringToBytes(utils::StringUtils::from_hex(
+    "4024b327fdc987ce3eb43dd1f690b9987e4072e0020e3edf4349ce1ad91a4e38"));
+
+struct FSTest : TestController {
+  FSTest() {
+    char format[] = "/var/tmp/fs.XXXXXX";
+    dir = createTempDirectory(format);
+    encrypted_file = utils::file::FileUtils::concat_path(dir, "encrypted.txt");
+    raw_file = utils::file::FileUtils::concat_path(dir, "raw.txt");
+    new_file = utils::file::FileUtils::concat_path(dir, "new.txt");
+
+    std::ofstream{encrypted_file, std::ios::binary} << 
crypto.encrypt("banana");
+    std::ofstream{raw_file, std::ios::binary} << "banana";
+  }
+
+  EncryptionProvider crypto{encryption_key};
+  std::string encrypted_file;
+  std::string raw_file;
+  std::string new_file;
+  std::string dir;
+};
+
+TEST_CASE_METHOD(FSTest, "Can read encrypted or non-encrypted file", "[fs]") {
+  FileSystem fs{true, crypto};
+  REQUIRE(fs.read(encrypted_file) == "banana");
+  REQUIRE(fs.read(raw_file) == "banana");
+}
+
+TEST_CASE_METHOD(FSTest, "Write encrypted file", "[fs]") {
+  FileSystem fs{true, crypto};
+
+  fs.write(new_file, "red lorry, yellow lorry");
+
+  std::ifstream file{new_file, std::ios::binary};
+  std::string file_content{std::istreambuf_iterator<char>(file), {}};
+  REQUIRE(crypto.decrypt(file_content) == "red lorry, yellow lorry");
+}
+
+TEST_CASE_METHOD(FSTest, "Can read encrypted but writes non-encrypted", 
"[fs]") {
+  FileSystem fs{false, crypto};
+  REQUIRE(fs.read(encrypted_file) == "banana");
+
+  fs.write(new_file, "red lorry, yellow lorry");
+
+  std::ifstream file{new_file, std::ios::binary};
+  std::string file_content{std::istreambuf_iterator<char>(file), {}};
+  REQUIRE(file_content == "red lorry, yellow lorry");
+}
+
+TEST_CASE_METHOD(FSTest, "Can't read/write encrypted", "[fs]") {

Review comment:
       done

##########
File path: libminifi/test/unit/StringViewTests.cpp
##########
@@ -0,0 +1,114 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string>
+#include <vector>
+#include <tuple>
+#include "utils/StringView.h"
+#include "utils/StringViewUtils.h"
+#include "../TestBase.h"
+
+using utils::StringView;
+using utils::StringViewUtils;
+
+TEST_CASE("Comparision") {
+  std::string a = "a";
+  std::string b = "b";
+
+  REQUIRE(StringView(a) == "a");
+  REQUIRE(StringView(a) != "b");
+  REQUIRE(StringView("a") == "a");
+  REQUIRE(StringView("abcd") != "a");
+  REQUIRE(StringView(a) == a);
+  REQUIRE(StringView(a) != b);

Review comment:
       added

##########
File path: libminifi/test/unit/StringViewTests.cpp
##########
@@ -0,0 +1,114 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string>
+#include <vector>
+#include <tuple>
+#include "utils/StringView.h"
+#include "utils/StringViewUtils.h"
+#include "../TestBase.h"
+
+using utils::StringView;
+using utils::StringViewUtils;
+
+TEST_CASE("Comparision") {
+  std::string a = "a";
+  std::string b = "b";
+
+  REQUIRE(StringView(a) == "a");
+  REQUIRE(StringView(a) != "b");
+  REQUIRE(StringView("a") == "a");
+  REQUIRE(StringView("abcd") != "a");
+  REQUIRE(StringView(a) == a);
+  REQUIRE(StringView(a) != b);
+  REQUIRE(StringView(a) == StringView(a));
+  REQUIRE(StringView(a) != StringView(b));
+  REQUIRE(!(StringView(a) == StringView(b)));
+}
+
+TEST_CASE("trimLeft") {
+  std::vector<std::pair<std::string, std::string>> cases{
+      {"", ""},
+      {"abcd", "abcd"},
+      {" abc", "abc"},
+      {"  abc", "abc"},
+      {" ", ""},
+      {"abc ", "abc "}

Review comment:
       added




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