1 //**********************************************************************************
2 //EncryptPad Copyright 2016 Evgeny Pokhilko
3 //<evpomail@gmail.com> <http://www.evpo.net/encryptpad>
4 //
5 //This file is part of EncryptPad
6 //
7 //EncryptPad is free software: you can redistribute it and/or modify
8 //it under the terms of the GNU General Public License as published by
9 //the Free Software Foundation, either version 2 of the License, or
10 //(at your option) any later version.
11 //
12 //EncryptPad is distributed in the hope that it will be useful,
13 //but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //GNU General Public License for more details.
16 //
17 //You should have received a copy of the GNU General Public License
18 //along with EncryptPad.  If not, see <http://www.gnu.org/licenses/>.
19 //**********************************************************************************
20 #include "gtest/gtest.h"
21 #include <cstring>
22 #include <string>
23 #include <algorithm>
24 #include "file_encryption.h"
25 #include "algo_defaults.h"
26 
27 using namespace EncryptPad;
28 using namespace EncryptMsg;
29 using namespace Botan;
30 
31 struct ParamItem
32 {
33     std::string content;
34     Compression compression;
35     bool is_armor;
36 };
37 
GetParameters()38 std::vector<ParamItem> &GetParameters()
39 {
40     static std::vector<ParamItem> parameters =
41     {
42         {"test01", Compression::ZLIB, false},
43         {"", Compression::ZLIB, false},
44         {"da", Compression::Uncompressed, false},
45         {"test01", Compression::ZLIB, true},
46         {"", Compression::ZLIB, true},
47     };
48     return parameters;
49 }
50 
51 // PacketStream tests
52 class FileEncryptionFixture : public ::testing::TestWithParam<ParamItem>
53 {
54     protected:
55         std::string passphrase_;
56         PacketMetadata metadata_;
57         EncryptParams enc_params_;
58         KeyService key_service_;
59         SecureVector<byte> in_buffer_;
60         SecureVector<byte> out_buffer_;
61         SecureVector<byte> result_buffer_;
62 
AssignBuffer(const std::string & text)63         void AssignBuffer(const std::string &text)
64         {
65             in_buffer_.resize(text.size());
66             std::copy(reinterpret_cast<const byte*>(text.data()), reinterpret_cast<const byte*>(text.data() + text.size()), in_buffer_.data());
67         }
68 
ResultString()69         std::string ResultString()
70         {
71             return std::string(
72                     reinterpret_cast<const char*>(result_buffer_.data()),
73                     reinterpret_cast<const char*>(result_buffer_.data() + result_buffer_.size()));
74         }
75 
SetUp()76         virtual void SetUp()
77         {
78             passphrase_ = "HwEj+1wSGpy|";
79             metadata_.compression = Compression::ZLIB;
80             metadata_.cipher_algo = CipherAlgo::AES256;
81             metadata_.hash_algo = HashAlgo::SHA256;
82             metadata_.file_name = "_CONSOLE";
83             metadata_.iterations = kDefaultIterations;
84 
85             key_service_.ChangePassphrase(
86                     passphrase_, metadata_.hash_algo, GetAlgoSpec(metadata_.cipher_algo).key_size, metadata_.iterations);
87 
88             enc_params_.key_service = &key_service_;
89         }
90 
TearDown()91         virtual void TearDown()
92         {
93             key_service_.Clear();
94             in_buffer_.clear();
95             out_buffer_.clear();
96             result_buffer_.clear();
97         }
98 };
99 
100 INSTANTIATE_TEST_CASE_P(Common, FileEncryptionFixture,
101         ::testing::ValuesIn(GetParameters()));
102 
TEST_P(FileEncryptionFixture,When_text_encrypted_then_decrypted_It_is_the_same)103 TEST_P(FileEncryptionFixture, When_text_encrypted_then_decrypted_It_is_the_same)
104 {
105     // Prepare
106     auto param_item = GetParam();
107     std::string armor_head = "-----BEGIN PGP";
108     AssignBuffer(param_item.content);
109     metadata_.compression = param_item.compression;
110     metadata_.is_armor = param_item.is_armor;
111 
112     // Act
113     EpadResult encrypt_result = EncryptBuffer(in_buffer_, enc_params_, out_buffer_, metadata_);
114     EpadResult decrypt_result = DecryptBuffer(out_buffer_, enc_params_, result_buffer_, metadata_);
115 
116     // Assert
117     if(metadata_.is_armor)
118     {
119         EXPECT_TRUE(out_buffer_.size() >= armor_head.size());
120         EXPECT_TRUE(std::equal(armor_head.begin(), armor_head.end(), out_buffer_.begin()));
121     }
122     ASSERT_EQ(EpadResult::Success, encrypt_result);
123     ASSERT_EQ(EpadResult::Success, decrypt_result);
124     ASSERT_EQ(param_item.content, ResultString());
125 }
126