aes_cipher_test.cc 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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
/* 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 <glog/logging.h>
#include <gtest/gtest.h>

#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.");
Y
yangqingyou 已提交
48
  bool is_throw = false;
49 50 51 52 53 54 55 56 57
  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) {
Y
yangqingyou 已提交
58
      is_throw = true;
59 60
      LOG(ERROR) << e.what();
    }
Y
yangqingyou 已提交
61
    EXPECT_FALSE(is_throw);
62 63 64 65 66 67 68 69
  }
}

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};
Y
yangqingyou 已提交
70
  bool is_throw = false;
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  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) {
Y
yangqingyou 已提交
86
      is_throw = true;
87 88
      LOG(ERROR) << e.what();
    }
Y
yangqingyou 已提交
89
    EXPECT_FALSE(is_throw);
90 91 92 93 94 95 96 97 98
  }
}

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");
Y
yangqingyou 已提交
99
  bool is_throw = false;
100 101 102 103 104 105 106 107
  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) {
Y
yangqingyou 已提交
108
      is_throw = true;
109 110
      LOG(ERROR) << e.what();
    }
Y
yangqingyou 已提交
111
    EXPECT_FALSE(is_throw);
112 113 114 115 116
  }
}

}  // namespace framework
}  // namespace paddle