Migrate ss data to both of system/admin user db
[platform/core/security/key-manager.git] / tests / test-key-provider.cpp
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Kyungwook Tak <k.tak@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  *
18  * @file        test-key-provider.cpp
19  * @author      Kyungwook Tak (k.tak@samsung.com)
20  * @version
21  * @brief
22  */
23 #define BOOST_TEST_MODULE KEY_MANAGER_TEST
24 #include <boost/test/unit_test.hpp>
25 #include <exception.h>
26 #include <key-provider.h>
27 #include <test_common.h>
28 #include <iostream>
29
30 const CKM::Password PASSWORD = "12345TIZEN12345AAAAAAAAA";
31 const CKM::Password INCORRECT_PASSWORD = "AAAAAAAAAAAAAAAAAAAAA";
32 const CKM::Password NEW_PASSWORD = "NEW12345TIZEN12345NEW";
33
34 const std::string USERNAME_SHORT = "AB";
35 const std::string USERNAME_LONG = "SOFTWARE_CENTER_SYSTEM_SW_LAB_SECURITY_PART";
36 const std::string SMACK_LABEL_1 = "SAMPLE_SMACK_LABEL_1";
37 const std::string SMACK_LABEL_2 = "SAMPLE_SMACK_LABEL_2";
38
39 extern bool isLibInitialized;
40
41 BOOST_AUTO_TEST_SUITE(KEY_PROVIDER_TEST)
42 BOOST_AUTO_TEST_CASE(KeyDomainKEK)
43 {
44         BOOST_REQUIRE_MESSAGE(isLibInitialized,
45                                                   "Library is not initialized!");
46         CKM::KeyProvider keyProvider;
47         CKM::RawBuffer rb_test;
48         BOOST_REQUIRE_NO_THROW(rb_test =
49                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
50         BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
51         BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
52                                                   "KeyProvider created, but uninitialized");
53 }
54
55 BOOST_AUTO_TEST_CASE(KeyDomainKekInvalidPassword)
56 {
57         BOOST_REQUIRE_MESSAGE(isLibInitialized,
58                                                   "Library is not initialized!");
59         CKM::KeyProvider keyProvider;
60         CKM::RawBuffer rb_test;
61         BOOST_REQUIRE_NO_THROW(rb_test =
62                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
63         BOOST_REQUIRE_THROW(keyProvider = CKM::KeyProvider(rb_test, INCORRECT_PASSWORD),
64                                                 CKM::Exc::AuthenticationFailed);
65         BOOST_REQUIRE_MESSAGE(!keyProvider.isInitialized(),
66                                                   "KeyProvider not created, but initialized");
67 }
68
69 BOOST_AUTO_TEST_CASE(KeygetPureDomainKEK)
70 {
71         BOOST_REQUIRE_MESSAGE(isLibInitialized,
72                                                   "Library is not initialized!");
73         CKM::KeyProvider keyProvider;
74         CKM::RawBuffer rb_test;
75         BOOST_REQUIRE_NO_THROW(rb_test =
76                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
77         BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
78         BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
79                                                   "KeyProvider created, but uninitialized");
80         BOOST_REQUIRE_NO_THROW(rb_test = keyProvider.getPureDomainKEK());
81 }
82
83 BOOST_AUTO_TEST_CASE(KeyGetWrappedDomainKEK)
84 {
85         BOOST_REQUIRE_MESSAGE(isLibInitialized,
86                                                   "Library is not initialized!");
87         CKM::KeyProvider keyProvider;
88         CKM::RawBuffer rb_test;
89         BOOST_REQUIRE_NO_THROW(rb_test =
90                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
91         BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
92         BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
93                                                   "KeyProvider created, but uninitialized");
94         BOOST_REQUIRE_NO_THROW(rb_test = keyProvider.getWrappedDomainKEK(PASSWORD));
95 }
96
97 BOOST_AUTO_TEST_CASE(KeyGenerateDEK)
98 {
99         BOOST_REQUIRE_MESSAGE(isLibInitialized,
100                                                   "Library is not initialized!");
101         CKM::KeyProvider keyProvider;
102         CKM::RawBuffer rb_test;
103         CKM::RawBuffer rb_DEK1;
104         BOOST_REQUIRE_NO_THROW(rb_test =
105                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
106         BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
107         BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
108                                                   "KeyProvider created, but uninitialized");
109         BOOST_REQUIRE_NO_THROW(rb_DEK1 = keyProvider.generateDEK(SMACK_LABEL_1));
110 }
111
112 BOOST_AUTO_TEST_CASE(KeyGetPureDEK)
113 {
114         BOOST_REQUIRE_MESSAGE(isLibInitialized,
115                                                   "Library is not initialized!");
116         CKM::KeyProvider keyProvider;
117         CKM::RawBuffer rb_pureDEK1;
118         CKM::RawBuffer rb_DEK1;
119         CKM::RawBuffer rb_test;
120         BOOST_REQUIRE_NO_THROW(rb_test =
121                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
122         BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
123         BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
124                                                   "KeyProvider created, but uninitialized");
125         BOOST_REQUIRE_NO_THROW(rb_DEK1 = keyProvider.generateDEK(SMACK_LABEL_1));
126         BOOST_REQUIRE_NO_THROW(rb_pureDEK1 = keyProvider.getPureDEK(rb_DEK1));
127 }
128
129 BOOST_AUTO_TEST_CASE(KeyReencrypt)
130 {
131         BOOST_REQUIRE_MESSAGE(isLibInitialized,
132                                                   "Library is not initialized!");
133         CKM::RawBuffer rb_test;
134         BOOST_REQUIRE_NO_THROW(rb_test =
135                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
136         BOOST_REQUIRE_NO_THROW(CKM::KeyProvider::reencrypt(rb_test, PASSWORD,
137                                                    NEW_PASSWORD));
138 }
139
140 BOOST_AUTO_TEST_CASE(KeyReencrypt_incorrect_password)
141 {
142         BOOST_REQUIRE_MESSAGE(isLibInitialized,
143                                                   "Library is not initialized!");
144         CKM::RawBuffer rb_test;
145         BOOST_REQUIRE_NO_THROW(rb_test =
146                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
147         BOOST_REQUIRE_THROW((rb_test = CKM::KeyProvider::reencrypt(rb_test,
148                                                                    INCORRECT_PASSWORD,
149                                                                    NEW_PASSWORD)), CKM::Exc::AuthenticationFailed);
150 }
151
152 BOOST_AUTO_TEST_CASE(KeyGetPureDEK_after_reencrypt)
153 {
154         BOOST_REQUIRE_MESSAGE(isLibInitialized,
155                                                   "Library is not initialized!");
156         CKM::KeyProvider keyProvider;
157         CKM::RawBuffer rb_DEK1;
158         CKM::RawBuffer rb_test;
159         BOOST_REQUIRE_NO_THROW(rb_test =
160                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
161         BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
162         BOOST_REQUIRE_NO_THROW(rb_DEK1 = keyProvider.generateDEK(SMACK_LABEL_1));
163         BOOST_REQUIRE_NO_THROW(keyProvider.getPureDEK(rb_DEK1));
164 }
165
166 BOOST_AUTO_TEST_SUITE_END()
167