Fix potential buffer overflow error CID: 40674
[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 <exception.h>
4 #include <key-provider.h>
5 #include <test_common.h>
6 #include <iostream>
7
8 const CKM::Password PASSWORD = "12345TIZEN12345AAAAAAAAA";
9 const CKM::Password INCORRECT_PASSWORD = "AAAAAAAAAAAAAAAAAAAAA";
10 const CKM::Password NEW_PASSWORD = "NEW12345TIZEN12345NEW";
11
12 const std::string USERNAME_SHORT = "AB";
13 const std::string USERNAME_LONG = "SOFTWARE_CENTER_SYSTEM_SW_LAB_SECURITY_PART";
14 const std::string SMACK_LABEL_1 = "SAMPLE_SMACK_LABEL_1";
15 const std::string SMACK_LABEL_2 = "SAMPLE_SMACK_LABEL_2";
16
17 extern bool isLibInitialized;
18
19 BOOST_AUTO_TEST_SUITE(KEY_PROVIDER_TEST)
20 BOOST_AUTO_TEST_CASE(KeyDomainKEK){
21     BOOST_REQUIRE_MESSAGE(isLibInitialized,
22             "Library is not initialized!");
23     CKM::KeyProvider keyProvider;
24     CKM::RawBuffer rb_test;
25     BOOST_REQUIRE_NO_THROW(rb_test =
26             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
27     BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
28     BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
29             "KeyProvider created, but uninitialized");
30 }
31
32 BOOST_AUTO_TEST_CASE(KeyDomainKekInvalidPassword){
33     BOOST_REQUIRE_MESSAGE(isLibInitialized,
34             "Library is not initialized!");
35     CKM::KeyProvider keyProvider;
36     CKM::RawBuffer rb_test;
37     BOOST_REQUIRE_NO_THROW(rb_test =
38             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
39     BOOST_REQUIRE_THROW(keyProvider = CKM::KeyProvider(rb_test, INCORRECT_PASSWORD),
40             CKM::Exc::AuthenticationFailed);
41     BOOST_REQUIRE_MESSAGE(!keyProvider.isInitialized(),
42             "KeyProvider not created, but initialized");
43 }
44
45 BOOST_AUTO_TEST_CASE(KeygetPureDomainKEK){
46     BOOST_REQUIRE_MESSAGE(isLibInitialized,
47             "Library is not initialized!");
48     CKM::KeyProvider keyProvider;
49     CKM::RawBuffer rb_test;
50     BOOST_REQUIRE_NO_THROW(rb_test =
51             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
52     BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
53     BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
54             "KeyProvider created, but uninitialized");
55     BOOST_REQUIRE_NO_THROW(rb_test = keyProvider.getPureDomainKEK());
56 }
57
58 BOOST_AUTO_TEST_CASE(KeyGetWrappedDomainKEK){
59     BOOST_REQUIRE_MESSAGE(isLibInitialized,
60             "Library is not initialized!");
61     CKM::KeyProvider keyProvider;
62     CKM::RawBuffer rb_test;
63     BOOST_REQUIRE_NO_THROW(rb_test =
64             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
65     BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
66     BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
67             "KeyProvider created, but uninitialized");
68     BOOST_REQUIRE_NO_THROW(rb_test = keyProvider.getWrappedDomainKEK(PASSWORD));
69 }
70
71 BOOST_AUTO_TEST_CASE(KeyGenerateDEK){
72     BOOST_REQUIRE_MESSAGE(isLibInitialized,
73             "Library is not initialized!");
74     CKM::KeyProvider keyProvider;
75     CKM::RawBuffer rb_test;
76     CKM::RawBuffer rb_DEK1;
77     BOOST_REQUIRE_NO_THROW(rb_test =
78             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
79     BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
80     BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
81             "KeyProvider created, but uninitialized");
82     BOOST_REQUIRE_NO_THROW(rb_DEK1 = keyProvider.generateDEK(SMACK_LABEL_1));
83 }
84
85 BOOST_AUTO_TEST_CASE(KeyGetPureDEK){
86     BOOST_REQUIRE_MESSAGE(isLibInitialized,
87             "Library is not initialized!");
88     CKM::KeyProvider keyProvider;
89     CKM::RawBuffer rb_pureDEK1;
90     CKM::RawBuffer rb_DEK1;
91     CKM::RawBuffer rb_test;
92     BOOST_REQUIRE_NO_THROW(rb_test =
93             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
94     BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
95     BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
96             "KeyProvider created, but uninitialized");
97     BOOST_REQUIRE_NO_THROW(rb_DEK1 = keyProvider.generateDEK(SMACK_LABEL_1));
98     BOOST_REQUIRE_NO_THROW(rb_pureDEK1 = keyProvider.getPureDEK(rb_DEK1));
99 }
100
101 BOOST_AUTO_TEST_CASE(KeyReencrypt){
102     BOOST_REQUIRE_MESSAGE(isLibInitialized,
103             "Library is not initialized!");
104     CKM::RawBuffer rb_test;
105     BOOST_REQUIRE_NO_THROW(rb_test =
106             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
107     BOOST_REQUIRE_NO_THROW(CKM::KeyProvider::reencrypt(rb_test, PASSWORD,
108             NEW_PASSWORD));
109 }
110
111 BOOST_AUTO_TEST_CASE(KeyReencrypt_incorrect_password){
112     BOOST_REQUIRE_MESSAGE(isLibInitialized,
113             "Library is not initialized!");
114     CKM::RawBuffer rb_test;
115     BOOST_REQUIRE_NO_THROW(rb_test =
116             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
117     BOOST_REQUIRE_THROW((rb_test = CKM::KeyProvider::reencrypt(rb_test, INCORRECT_PASSWORD,
118             NEW_PASSWORD)), CKM::Exc::AuthenticationFailed);
119 }
120
121 BOOST_AUTO_TEST_CASE(KeyGetPureDEK_after_reencrypt){
122     BOOST_REQUIRE_MESSAGE(isLibInitialized,
123             "Library is not initialized!");
124     CKM::KeyProvider keyProvider;
125     CKM::RawBuffer rb_DEK1;
126     CKM::RawBuffer rb_test;
127     BOOST_REQUIRE_NO_THROW(rb_test =
128             CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
129     BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
130     BOOST_REQUIRE_NO_THROW(rb_DEK1 = keyProvider.generateDEK(SMACK_LABEL_1));
131     BOOST_REQUIRE_NO_THROW(keyProvider.getPureDEK(rb_DEK1));
132 }
133
134 BOOST_AUTO_TEST_SUITE_END()
135