chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
Licensed 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 "paddle/fluid/framework/io/crypto/aes_cipher.h"
|
||||
|
||||
#include <cryptopp/cryptlib.h>
|
||||
#include <glog/logging.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
#include "paddle/fluid/framework/io/crypto/cipher_utils.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
|
||||
class AESTest : public ::testing::Test {
|
||||
public:
|
||||
std::string key;
|
||||
|
||||
void SetUp() override { key = CipherUtils::GenKey(256); }
|
||||
static void GenConfigFile(const std::string& cipher_name);
|
||||
};
|
||||
|
||||
void AESTest::GenConfigFile(const std::string& cipher_name) {
|
||||
std::ofstream fout("aes_test.conf");
|
||||
fout << "cipher_name : " << cipher_name << std::endl;
|
||||
fout.close();
|
||||
}
|
||||
|
||||
TEST_F(AESTest, security_string) {
|
||||
std::vector<std::string> name_list({"AES_CTR_NoPadding",
|
||||
"AES_CBC_PKCSPadding",
|
||||
"AES_ECB_PKCSPadding",
|
||||
"AES_GCM_NoPadding"});
|
||||
const std::string plaintext("hello world.");
|
||||
bool is_throw = false;
|
||||
for (auto& i : name_list) {
|
||||
AESTest::GenConfigFile(i);
|
||||
try {
|
||||
auto cipher = CipherFactory::CreateCipher("aes_test.conf");
|
||||
std::string ciphertext = cipher->Encrypt(plaintext, AESTest::key);
|
||||
|
||||
std::string plaintext1 = cipher->Decrypt(ciphertext, AESTest::key);
|
||||
EXPECT_EQ(plaintext, plaintext1);
|
||||
} catch (CryptoPP::Exception& e) {
|
||||
is_throw = true;
|
||||
LOG(ERROR) << e.what();
|
||||
}
|
||||
EXPECT_FALSE(is_throw);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(AESTest, security_vector) {
|
||||
std::vector<std::string> name_list({"AES_CTR_NoPadding",
|
||||
"AES_CBC_PKCSPadding",
|
||||
"AES_ECB_PKCSPadding",
|
||||
"AES_GCM_NoPadding"});
|
||||
std::vector<int> input{1, 2, 3, 4};
|
||||
bool is_throw = false;
|
||||
for (auto& i : name_list) {
|
||||
AESTest::GenConfigFile(i);
|
||||
try {
|
||||
auto cipher = CipherFactory::CreateCipher("aes_test.conf");
|
||||
for (auto& i : input) {
|
||||
std::string ciphertext =
|
||||
cipher->Encrypt(std::to_string(i), AESTest::key);
|
||||
|
||||
std::string plaintext = cipher->Decrypt(ciphertext, AESTest::key);
|
||||
|
||||
int output = std::stoi(plaintext);
|
||||
|
||||
EXPECT_EQ(i, output);
|
||||
}
|
||||
} catch (CryptoPP::Exception& e) {
|
||||
is_throw = true;
|
||||
LOG(ERROR) << e.what();
|
||||
}
|
||||
EXPECT_FALSE(is_throw);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(AESTest, encrypt_to_file) {
|
||||
std::vector<std::string> name_list({"AES_CTR_NoPadding",
|
||||
"AES_CBC_PKCSPadding",
|
||||
"AES_ECB_PKCSPadding",
|
||||
"AES_GCM_NoPadding"});
|
||||
const std::string plaintext("hello world.");
|
||||
std::string filename("aes_test.ciphertext");
|
||||
bool is_throw = false;
|
||||
for (auto& i : name_list) {
|
||||
AESTest::GenConfigFile(i);
|
||||
try {
|
||||
auto cipher = CipherFactory::CreateCipher("aes_test.conf");
|
||||
cipher->EncryptToFile(plaintext, AESTest::key, filename);
|
||||
std::string plaintext1 = cipher->DecryptFromFile(AESTest::key, filename);
|
||||
EXPECT_EQ(plaintext, plaintext1);
|
||||
} catch (CryptoPP::Exception& e) {
|
||||
is_throw = true;
|
||||
LOG(ERROR) << e.what();
|
||||
}
|
||||
EXPECT_FALSE(is_throw);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
||||
@@ -0,0 +1,77 @@
|
||||
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
Licensed 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 "paddle/fluid/framework/io/crypto/cipher_utils.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
|
||||
TEST(CipherUtils, load_config) {
|
||||
std::string filename("cryptotest_config_file.conf");
|
||||
|
||||
std::ofstream fout(filename, std::ios::out);
|
||||
fout << "# annotation test line:"
|
||||
" must have two space along ':'."
|
||||
<< std::endl;
|
||||
std::vector<std::string> key_value;
|
||||
key_value.emplace_back("key_str : ciphername");
|
||||
key_value.emplace_back("key_int : 1");
|
||||
key_value.emplace_back("key_bool : true");
|
||||
key_value.emplace_back("key_bool1 : false");
|
||||
key_value.emplace_back("key_bool2 : 0");
|
||||
for (auto& i : key_value) {
|
||||
fout << i << std::endl;
|
||||
}
|
||||
fout.close();
|
||||
|
||||
auto config = CipherUtils::LoadConfig(filename);
|
||||
|
||||
std::string out_str;
|
||||
EXPECT_TRUE(CipherUtils::GetValue<std::string>(config, "key_str", &out_str));
|
||||
EXPECT_EQ(out_str, std::string("ciphername"));
|
||||
|
||||
int out_int = 0;
|
||||
EXPECT_TRUE(CipherUtils::GetValue<int>(config, "key_int", &out_int));
|
||||
EXPECT_EQ(out_int, 1);
|
||||
|
||||
bool out_bool = false;
|
||||
EXPECT_TRUE(CipherUtils::GetValue<bool>(config, "key_bool", &out_bool));
|
||||
EXPECT_EQ(out_bool, true);
|
||||
|
||||
bool out_bool1 = false;
|
||||
EXPECT_TRUE(CipherUtils::GetValue<bool>(config, "key_bool1", &out_bool1));
|
||||
EXPECT_EQ(out_bool1, false);
|
||||
|
||||
bool out_bool2 = false;
|
||||
EXPECT_TRUE(CipherUtils::GetValue<bool>(config, "key_bool2", &out_bool2));
|
||||
EXPECT_EQ(out_bool2, false);
|
||||
}
|
||||
|
||||
TEST(CipherUtils, gen_key) {
|
||||
std::string filename("test_keyfile");
|
||||
std::string key = CipherUtils::GenKey(256);
|
||||
std::string key1 = CipherUtils::GenKeyToFile(256, filename);
|
||||
EXPECT_NE(key, key1);
|
||||
std::string key2 = CipherUtils::ReadKeyFromFile(filename);
|
||||
EXPECT_EQ(key1, key2);
|
||||
EXPECT_EQ(static_cast<int>(key.size()), 32);
|
||||
}
|
||||
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
||||
@@ -0,0 +1,62 @@
|
||||
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed 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 <gtest/gtest.h>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "paddle/fluid/framework/io/fs.h"
|
||||
|
||||
#if defined _WIN32 || defined __APPLE__
|
||||
#else
|
||||
#define _LINUX
|
||||
#endif
|
||||
|
||||
TEST(FS, mv) {
|
||||
#ifdef _LINUX
|
||||
std::ofstream out("src.txt");
|
||||
out.close();
|
||||
paddle::framework::fs_mv("src.txt", "dest.txt");
|
||||
paddle::framework::hdfs_mv("", "");
|
||||
paddle::framework::localfs_mv("", "");
|
||||
try {
|
||||
paddle::framework::hdfs_mv("afs:/none", "afs:/none");
|
||||
} catch (...) {
|
||||
VLOG(3) << "test hdfs_mv, catch expected errors of unknown path";
|
||||
}
|
||||
try {
|
||||
paddle::framework::fs_mv("afs:/none", "afs:/none");
|
||||
} catch (...) {
|
||||
VLOG(3) << "test hdfs_mv, catch expected errors of unknown path";
|
||||
}
|
||||
try {
|
||||
paddle::framework::hdfs_mv("unknown:/none", "unknown:/none");
|
||||
} catch (...) {
|
||||
VLOG(3) << "test hdfs_mv, catch expected errors of unknown prefix";
|
||||
}
|
||||
|
||||
try {
|
||||
paddle::framework::dataset_hdfs_set_command(
|
||||
"hadoop -D hadoop.job.ugi=anotherxxx fs -text");
|
||||
int err_no = 0;
|
||||
paddle::framework::hdfs_open_read("afs:/none.gz", &err_no, "", true);
|
||||
paddle::framework::hdfs_open_read("afs:/none.gz", &err_no, "", false);
|
||||
paddle::framework::hdfs_open_read("afs:/none", &err_no, "", true);
|
||||
paddle::framework::hdfs_open_read("afs:/none", &err_no, "", false);
|
||||
} catch (...) {
|
||||
VLOG(3) << "test hdfs_open_read, catch expected errors of unknown path";
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user