Add encryption scheme tests
[platform/core/security/key-manager.git] / tests / test_encryption-scheme.cpp
1 /*
2  *  Copyright (c) 2015 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 <boost/test/unit_test.hpp>
23 #include <boost/test/results_reporter.hpp>
24
25 #include <scheme-test.h>
26
27 using namespace CKM;
28
29 namespace {
30 // this is done to limit the amount of code included in binary
31 const int OLD_ENC_SCHEME  = 0;
32 const int NEW_ENC_SCHEME  = 1;
33 } // namespace anonymous
34
35
36 BOOST_AUTO_TEST_SUITE(ENCRYPTION_SCHEME_TEST)
37
38 // Test database should have the old scheme
39 BOOST_AUTO_TEST_CASE(T010_Check_old_scheme) {
40     SchemeTest test;
41     test.RestoreDb();
42
43     ItemFilter filter;
44     test.CheckSchemeVersion(filter, OLD_ENC_SCHEME);
45 }
46
47 // Newly written data should use the new scheme
48 BOOST_AUTO_TEST_CASE(T020_Check_new_scheme) {
49     SchemeTest test;
50     test.RemoveUserData();
51     test.FillDb();
52
53     ItemFilter filter;
54     test.CheckSchemeVersion(filter, NEW_ENC_SCHEME);
55 }
56
57 BOOST_AUTO_TEST_CASE(T030_Remove_old_scheme) {
58     SchemeTest test;
59     test.RestoreDb();
60     test.RemoveAll();
61
62     size_t aliases = test.CountObjects();
63     BOOST_REQUIRE_MESSAGE(aliases == 0, "All aliases should be removed");
64 }
65
66 BOOST_AUTO_TEST_CASE(T040_Remove_new_scheme) {
67     SchemeTest test;
68     test.RemoveUserData();
69     test.FillDb();
70     test.RemoveAll();
71
72     size_t aliases = test.CountObjects();
73     BOOST_REQUIRE_MESSAGE(aliases == 0, "All aliases should be removed");
74 }
75
76 // Reading old db should reencrypt objects with new scheme
77 BOOST_AUTO_TEST_CASE(T100_Read) {
78     SchemeTest test;
79     test.RestoreDb();
80     test.ReadAll();
81
82     ItemFilter filter;
83     filter.exportableOnly = true;
84     test.CheckSchemeVersion(filter, NEW_ENC_SCHEME);
85 }
86
87 BOOST_AUTO_TEST_CASE(T110_Count_objects_after_read) {
88     SchemeTest test;
89     test.RestoreDb();
90     size_t orig = test.CountObjects();
91     BOOST_REQUIRE_MESSAGE(orig > 0, "No objects in db");
92
93     test.ReadAll();
94
95     size_t current = test.CountObjects();
96     BOOST_REQUIRE_MESSAGE(current == orig,
97                           "Original number of objects: " << orig << " Current: " << current);
98 }
99
100 // Reading old db with incorrect passwords should leave the scheme unchanged
101 BOOST_AUTO_TEST_CASE(T120_Read_wrong_pass) {
102     SchemeTest test;
103     test.RestoreDb();
104     test.ReadAll(true);
105
106     ItemFilter filter;
107     test.CheckSchemeVersion(filter, OLD_ENC_SCHEME);
108 }
109
110 // Signing/verification should reencrypt objects with new scheme
111 BOOST_AUTO_TEST_CASE(T200_SignVerify) {
112     SchemeTest test;
113     test.RestoreDb();
114     test.SignVerify();
115
116     ItemFilter filter(DataType::KEY_RSA_PUBLIC, DataType::KEY_RSA_PRIVATE);
117     test.CheckSchemeVersion(filter, NEW_ENC_SCHEME);
118 }
119
120 // Encryption/decryption should reencrypt objects with new scheme
121 BOOST_AUTO_TEST_CASE(T210_EncryptDecrypt) {
122     SchemeTest test;
123     test.RestoreDb();
124     test.EncryptDecrypt();
125
126     ItemFilter filter1(DataType::KEY_RSA_PUBLIC, DataType::KEY_RSA_PRIVATE);
127     test.CheckSchemeVersion(filter1, NEW_ENC_SCHEME);
128
129     ItemFilter filter2(DataType::KEY_AES);
130     test.CheckSchemeVersion(filter2, NEW_ENC_SCHEME);
131 }
132
133 // Chain creation should reencrypt objects with new scheme
134 BOOST_AUTO_TEST_CASE(T220_CreateChain) {
135     SchemeTest test;
136     test.RestoreDb();
137     test.CreateChain();
138
139     // non exportable certificates and certificates protected with passwords can't be used for chain
140     // creation
141     ItemFilter filter1(DataType::CERTIFICATE);
142     filter1.exportableOnly = true;
143     filter1.noPassword = true;
144     test.CheckSchemeVersion(filter1, NEW_ENC_SCHEME);
145
146     ItemFilter filter2(DataType::CHAIN_CERT_0, DataType::CHAIN_CERT_15);
147     filter2.exportableOnly = true;
148     filter2.noPassword = true;
149     test.CheckSchemeVersion(filter2, NEW_ENC_SCHEME);
150 }
151
152 BOOST_AUTO_TEST_SUITE_END()