Replace std::string with CKM::Password
[platform/core/security/key-manager.git] / tests / test-key-provider.cpp
1 #define BOOST_TEST_MODULE KEY_MANAGER_TEST
2 #include <boost/test/unit_test.hpp>
3 #include <key-provider.h>
4 #include <test_common.h>
5 #include <iostream>
6
7 const CKM::Password PASSWORD = "12345TIZEN12345AAAAAAAAA";
8 const CKM::Password INCORRECT_PASSWORD = "AAAAAAAAAAAAAAAAAAAAA";
9 const CKM::Password NEW_PASSWORD = "NEW12345TIZEN12345NEW";
10
11 const std::string USERNAME_SHORT = "AB";
12 const std::string USERNAME_LONG = "SOFTWARE_CENTER_SYSTEM_SW_LAB_SECURITY_PART";
13 const std::string SMACK_LABEL_1 = "SAMPLE_SMACK_LABEL_1";
14 const std::string SMACK_LABEL_2 = "SAMPLE_SMACK_LABEL_2";
15
16 static bool isLibInitialized = false;
17
18 struct KeyProviderLib {
19     KeyProviderLib() {
20         Try {
21             CKM::KeyProvider::initializeLibrary();
22             isLibInitialized = true;
23         }
24         Catch (CKM::Exception) {
25             std::cout << "Library initialization failed!" << std::endl;
26         }
27     }
28     ~KeyProviderLib() {
29         Try { CKM::KeyProvider::closeLibrary(); }
30         Catch (CKM::Exception) {
31             std::cout << "Library deinitialization failed!" << std::endl;
32         }
33     }
34 };
35
36 BOOST_GLOBAL_FIXTURE(TestConfig)
37 BOOST_GLOBAL_FIXTURE(KeyProviderLib)
38
39 BOOST_AUTO_TEST_SUITE(KEY_PROVIDER_TEST)
40 BOOST_AUTO_TEST_CASE(KeyDomainKEK){
41     BOOST_REQUIRE_MESSAGE(isLibInitialized,
42             "Library is not initialized!");
43     CKM::KeyProvider keyProvider;
44     CKM::RawBuffer rb_test;
45     BOOST_REQUIRE_NO_THROW(rb_test =
46             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
47     BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
48     BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
49             "KeyProvider created, but uninitialized");
50 }
51
52 BOOST_AUTO_TEST_CASE(KeyDomainKekInvalidPassword){
53     BOOST_REQUIRE_MESSAGE(isLibInitialized,
54             "Library is not initialized!");
55     CKM::KeyProvider keyProvider;
56     CKM::RawBuffer rb_test;
57     BOOST_REQUIRE_NO_THROW(rb_test =
58             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
59     BOOST_REQUIRE_THROW(keyProvider = CKM::KeyProvider(rb_test, INCORRECT_PASSWORD),
60             CKM::KeyProvider::Exception::UnwrapFailed);
61     BOOST_REQUIRE_MESSAGE(!keyProvider.isInitialized(),
62             "KeyProvider not created, but initialized");
63 }
64
65 BOOST_AUTO_TEST_CASE(KeygetPureDomainKEK){
66     BOOST_REQUIRE_MESSAGE(isLibInitialized,
67             "Library is not initialized!");
68     CKM::KeyProvider keyProvider;
69     CKM::RawBuffer rb_test;
70     BOOST_REQUIRE_NO_THROW(rb_test =
71             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
72     BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
73     BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
74             "KeyProvider created, but uninitialized");
75     BOOST_REQUIRE_NO_THROW(rb_test = keyProvider.getPureDomainKEK());
76 }
77
78 BOOST_AUTO_TEST_CASE(KeyGetWrappedDomainKEK){
79     BOOST_REQUIRE_MESSAGE(isLibInitialized,
80             "Library is not initialized!");
81     CKM::KeyProvider keyProvider;
82     CKM::RawBuffer rb_test;
83     BOOST_REQUIRE_NO_THROW(rb_test =
84             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
85     BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
86     BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
87             "KeyProvider created, but uninitialized");
88     BOOST_REQUIRE_NO_THROW(rb_test = keyProvider.getWrappedDomainKEK(PASSWORD));
89 }
90
91 BOOST_AUTO_TEST_CASE(KeyGenerateDEK){
92     BOOST_REQUIRE_MESSAGE(isLibInitialized,
93             "Library is not initialized!");
94     CKM::KeyProvider keyProvider;
95     CKM::RawBuffer rb_test;
96     CKM::RawBuffer rb_DEK1;
97     BOOST_REQUIRE_NO_THROW(rb_test =
98             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
99     BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
100     BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
101             "KeyProvider created, but uninitialized");
102     BOOST_REQUIRE_NO_THROW(rb_DEK1 = keyProvider.generateDEK(SMACK_LABEL_1));
103 }
104
105 BOOST_AUTO_TEST_CASE(KeyGetPureDEK){
106     BOOST_REQUIRE_MESSAGE(isLibInitialized,
107             "Library is not initialized!");
108     CKM::KeyProvider keyProvider;
109     CKM::RawBuffer rb_pureDEK1;
110     CKM::RawBuffer rb_DEK1;
111     CKM::RawBuffer rb_test;
112     BOOST_REQUIRE_NO_THROW(rb_test =
113             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
114     BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
115     BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
116             "KeyProvider created, but uninitialized");
117     BOOST_REQUIRE_NO_THROW(rb_DEK1 = keyProvider.generateDEK(SMACK_LABEL_1));
118     BOOST_REQUIRE_NO_THROW(rb_pureDEK1 = keyProvider.getPureDEK(rb_DEK1));
119 }
120
121 BOOST_AUTO_TEST_CASE(KeyReencrypt){
122     BOOST_REQUIRE_MESSAGE(isLibInitialized,
123             "Library is not initialized!");
124     CKM::RawBuffer rb_test;
125     BOOST_REQUIRE_NO_THROW(rb_test =
126             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
127     BOOST_REQUIRE_NO_THROW(CKM::KeyProvider::reencrypt(rb_test, PASSWORD,
128             NEW_PASSWORD));
129 }
130
131 BOOST_AUTO_TEST_CASE(KeyReencrypt_incorrect_password){
132     BOOST_REQUIRE_MESSAGE(isLibInitialized,
133             "Library is not initialized!");
134     CKM::RawBuffer rb_test;
135     BOOST_REQUIRE_NO_THROW(rb_test =
136             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
137     BOOST_REQUIRE_THROW((rb_test = CKM::KeyProvider::reencrypt(rb_test, INCORRECT_PASSWORD,
138             NEW_PASSWORD)), CKM::KeyProvider::Exception::UnwrapFailed);
139 }
140
141 BOOST_AUTO_TEST_CASE(KeyGetPureDEK_after_reencrypt){
142     BOOST_REQUIRE_MESSAGE(isLibInitialized,
143             "Library is not initialized!");
144     CKM::KeyProvider keyProvider;
145     CKM::RawBuffer rb_DEK1;
146     CKM::RawBuffer rb_test;
147     BOOST_REQUIRE_NO_THROW(rb_test =
148             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
149     BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
150     BOOST_REQUIRE_NO_THROW(rb_DEK1 = keyProvider.generateDEK(SMACK_LABEL_1));
151     BOOST_REQUIRE_NO_THROW(keyProvider.getPureDEK(rb_DEK1));
152 }
153
154 BOOST_AUTO_TEST_SUITE_END()
155