aes_cipher_test.cc 3.5 KB
Newer Older
Y
Yanghello 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 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>

M
MRXLT 已提交
21
#include <fstream>
Y
Yanghello 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#include <string>
#include <vector>
#include "paddle/fluid/framework/io/crypto/cipher.h"
#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