Merge "Increase backlog for listening sockets" into tizen
[platform/core/security/key-manager.git] / tests / test_encryption-scheme.cpp
1 /*
2  *  Copyright (c) 2015 -2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 /*
17  * @file       test_encryption-scheme.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include <fstream>
23 #include <string>
24
25 #include <boost/test/unit_test.hpp>
26 #include <boost/test/results_reporter.hpp>
27
28 #include <scheme-test.h>
29
30 using namespace CKM;
31
32 namespace {
33 // this is done to limit the amount of code included in binary
34 const int OLD_ENC_SCHEME  = 0;
35 const int NEW_ENC_SCHEME  = 1;
36
37 const char* const ONLYCAP = "/sys/fs/smackfs/onlycap";
38
39 class OnlycapFixture {
40 public:
41         OnlycapFixture() {
42                 std::fstream file(ONLYCAP);
43                 std::string tmp;
44                 while(file >> tmp) {
45                         m_oldOnlycap += tmp;
46                         m_oldOnlycap += " ";
47                 }
48                 file.clear();
49                 file << " ";
50         };
51
52         ~OnlycapFixture() {
53                 try {
54                         std::ofstream file(ONLYCAP);
55                         file << m_oldOnlycap;
56                 } catch(...) {}
57         };
58 private:
59         std::string m_oldOnlycap;
60 };
61
62 } // namespace anonymous
63
64 BOOST_FIXTURE_TEST_SUITE(ENCRYPTION_SCHEME_TEST, OnlycapFixture)
65
66 // Test database should have the old scheme
67 BOOST_AUTO_TEST_CASE(T010_Check_old_scheme)
68 {
69         SchemeTest test;
70         test.RestoreDb();
71
72         ItemFilter filter;
73         test.CheckSchemeVersion(filter, OLD_ENC_SCHEME);
74 }
75
76 BOOST_AUTO_TEST_CASE(T1111_Alias_Info_old_scheme)
77 {
78         SchemeTest test;
79         test.RestoreDb();
80
81         ItemFilter filter;
82         test.CheckSchemeVersion(filter, OLD_ENC_SCHEME);
83         test.CheckAliasInfo();
84         test.ReadAll();
85         test.CheckAliasInfo();
86 }
87
88 // Newly written data should use the new scheme
89 BOOST_AUTO_TEST_CASE(T020_Check_new_scheme)
90 {
91         SchemeTest test;
92         test.RemoveUserData();
93         test.FillDb();
94
95         ItemFilter filter;
96         test.CheckSchemeVersion(filter, NEW_ENC_SCHEME);
97 }
98
99 BOOST_AUTO_TEST_CASE(T030_Remove_old_scheme)
100 {
101         SchemeTest test;
102         test.RestoreDb();
103         test.RemoveAll();
104
105         size_t aliases = test.CountObjects();
106         BOOST_REQUIRE_MESSAGE(aliases == 0, "All aliases should be removed");
107 }
108
109 BOOST_AUTO_TEST_CASE(T040_Remove_new_scheme)
110 {
111         SchemeTest test;
112         test.RemoveUserData();
113         test.FillDb();
114         test.RemoveAll();
115
116         size_t aliases = test.CountObjects();
117         BOOST_REQUIRE_MESSAGE(aliases == 0, "All aliases should be removed");
118 }
119
120 // Reading old db should reencrypt objects with new scheme
121 BOOST_AUTO_TEST_CASE(T100_Read)
122 {
123         SchemeTest test;
124         test.RestoreDb();
125         test.ReadAll();
126
127         ItemFilter filter;
128         filter.exportableOnly = true;
129         test.CheckSchemeVersion(filter, NEW_ENC_SCHEME);
130 }
131
132 BOOST_AUTO_TEST_CASE(T110_Count_objects_after_read)
133 {
134         SchemeTest test;
135         test.RestoreDb();
136         size_t orig = test.CountObjects();
137         BOOST_REQUIRE_MESSAGE(orig > 0, "No objects in db");
138
139         test.ReadAll();
140
141         size_t current = test.CountObjects();
142         BOOST_REQUIRE_MESSAGE(current == orig,
143                                                   "Original number of objects: " << orig << " Current: " << current);
144 }
145
146 // Reading old db with incorrect passwords should leave the scheme unchanged
147 BOOST_AUTO_TEST_CASE(T120_Read_wrong_pass)
148 {
149         SchemeTest test;
150         test.RestoreDb();
151         test.ReadAll(true);
152
153         ItemFilter filter;
154         test.CheckSchemeVersion(filter, OLD_ENC_SCHEME);
155 }
156
157 // Signing/verification should reencrypt objects with new scheme
158 BOOST_AUTO_TEST_CASE(T200_SignVerify)
159 {
160         SchemeTest test;
161         test.RestoreDb();
162         test.SignVerify();
163
164         ItemFilter filter(DataType::KEY_RSA_PUBLIC, DataType::KEY_RSA_PRIVATE);
165         test.CheckSchemeVersion(filter, NEW_ENC_SCHEME);
166 }
167
168 // Encryption/decryption should reencrypt objects with new scheme
169 BOOST_AUTO_TEST_CASE(T210_EncryptDecrypt)
170 {
171         SchemeTest test;
172         test.RestoreDb();
173         test.EncryptDecrypt();
174
175         ItemFilter filter1(DataType::KEY_RSA_PUBLIC, DataType::KEY_RSA_PRIVATE);
176         test.CheckSchemeVersion(filter1, NEW_ENC_SCHEME);
177
178         ItemFilter filter2(DataType::KEY_AES);
179         test.CheckSchemeVersion(filter2, NEW_ENC_SCHEME);
180 }
181
182 // Chain creation should reencrypt objects with new scheme
183 BOOST_AUTO_TEST_CASE(T220_CreateChain)
184 {
185         SchemeTest test;
186         test.RestoreDb();
187         test.CreateChain();
188
189         // non exportable certificates and certificates protected with passwords can't be used for chain
190         // creation
191         ItemFilter filter1(DataType::CERTIFICATE);
192         filter1.exportableOnly = true;
193         filter1.noPassword = true;
194         test.CheckSchemeVersion(filter1, NEW_ENC_SCHEME);
195
196         ItemFilter filter2(DataType::CHAIN_CERT_0, DataType::CHAIN_CERT_15);
197         filter2.exportableOnly = true;
198         filter2.noPassword = true;
199         test.CheckSchemeVersion(filter2, NEW_ENC_SCHEME);
200 }
201
202 BOOST_AUTO_TEST_SUITE_END()