Add tests for async ckm API - saveKey
[platform/core/test/security-tests.git] / tests / ckm / async-api.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Bumjin Im <bj.im@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 /*
19  * @file       async-api.cpp
20  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
21  * @version    1.0
22  */
23
24 #include <mutex>
25 #include <condition_variable>
26 #include <chrono>
27 #include <dpl/test/test_runner.h>
28 #include <dpl/test/test_runner_child.h>
29 #include <dpl/log/log.h>
30 #include <ckm/ckm-manager-async.h>
31 #include <ckm/ckm-control.h>
32 #include <tests_common.h>
33 #include <access_provider.h>
34
35 RUNNER_TEST_GROUP_INIT(CKM_ASYNC_API);
36
37 using namespace CKM;
38 using namespace std;
39
40 namespace {
41
42 class MyObserver: public ManagerAsync::Observer
43 {
44 public:
45     MyObserver() :
46             m_finished(false), m_error(0)
47     {
48     }
49
50     void ReceivedError(int error)
51     {
52         LogError("Received error: " << error);
53         m_finished = true;
54         m_error = error;
55         m_cv.notify_one();
56     }
57
58     void ReceivedSaveKey()
59     {
60         LogDebug("Key saved");
61         m_finished = true;
62         m_cv.notify_one();
63     }
64
65     void WaitForResponse()
66     {
67         unique_lock < mutex > lock(m_mutex);
68
69         if (!m_cv.wait_for(lock, std::chrono::seconds(5), [this] {return m_finished;}))
70             RUNNER_ASSERT_MSG(false, "Timeout reached!");
71     }
72
73     bool m_finished;
74     int m_error;
75
76 protected:
77     mutex m_mutex;
78     condition_variable m_cv;
79 };
80
81 const uid_t USER_APP = 5000;
82 const uid_t USER_ROOT = 0;
83 const char* USER_PASS = "user-pass";
84 const char* ROOT_PASS = "test-pass";
85 const char* ALIAS1 = "alias1";
86
87 string keyPem = "-----BEGIN PUBLIC KEY-----\n"
88         "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4\n"
89         "T4tUddtZNi0NVjQn9RFH1NMa220GsRhRO56F77FlSVFKfSfVZKIiWg6C+DVCkcLf\n"
90         "zXJ/Z0pvwOQYBAqVMFjV6efQGN0JzJ1Unu7pPRiZl7RKGEI+cyzzrcDyrLLrQ2W7\n"
91         "0ZySkNEOv6Frx9JgC5NExuYY4lk2fQQa38JXiZkfyzif2em0px7mXbyf5LjccsKq\n"
92         "v1e+XLtMsL0ZefRcqsP++NzQAI8fKX7WBT+qK0HJDLiHrKOTWYzx6CwJ66LD/vvf\n"
93         "j55xtsKDLVDbsotvf8/m6VLMab+vqKk11TP4tq6yo0mwyTADvgl1zowQEO9I1W6o\n"
94         "zQIDAQAB\n"
95         "-----END PUBLIC KEY-----";
96
97 KeyShPtr prepareKey()
98 {
99     RawBuffer buffer(keyPem.begin(), keyPem.end());
100     KeyShPtr k = Key::create(buffer);
101     RUNNER_ASSERT_MSG(k, "Key was not created!");
102     return k;
103 }
104
105 void test_expect_invalid_param(const Alias &alias, const KeyShPtr &key, const Policy &policy)
106 {
107     shared_ptr<MyObserver> obs = make_shared<MyObserver>();
108     ManagerAsync mgr;
109
110     mgr.saveKey(static_pointer_cast < ManagerAsync::Observer > (obs), alias, key, policy);
111     obs->WaitForResponse();
112
113     RUNNER_ASSERT_MSG(obs->m_finished, "Request is not finished!");
114     RUNNER_ASSERT_MSG(obs->m_error == CKM_API_ERROR_INPUT_PARAM,
115                       "Expected " << CKM_API_ERROR_INPUT_PARAM << " error, got: " << obs->m_error);
116 }
117
118 void test_save_key() {
119     shared_ptr<MyObserver> obs = make_shared<MyObserver>();
120     ManagerAsync mgr;
121
122     KeyShPtr key = prepareKey();
123
124     mgr.saveKey(static_pointer_cast < ManagerAsync::Observer > (obs), ALIAS1, key, Policy());
125     obs->WaitForResponse();
126
127     RUNNER_ASSERT_MSG(obs->m_finished, "Request is not finished!");
128     RUNNER_ASSERT_MSG(obs->m_error == 0,
129                       "Request failed " << obs->m_error << ":" << CKM::ErrorToString(obs->m_error));
130 }
131
132 } // namespace anonymous
133
134 // TODO get rid of test dependency
135 RUNNER_TEST(TA0000_init)
136 {
137     int temp;
138     auto control = CKM::Control::create();
139     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->unlockUserKey(USER_APP, USER_PASS)),
140                          "Error=" << CKM::ErrorToString(temp));
141     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->removeUserData(USER_APP)),
142                          "Error=" << CKM::ErrorToString(temp));
143     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->unlockUserKey(USER_ROOT, ROOT_PASS)),
144                          "Error=" << CKM::ErrorToString(temp));
145     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->removeUserData(USER_ROOT)),
146                          "Error=" << CKM::ErrorToString(temp));
147 }
148
149 RUNNER_TEST(TA0010_save_key_no_observer)
150 {
151     ManagerAsync::ObserverPtr obs;
152     ManagerAsync mgr;
153
154     KeyShPtr key = prepareKey();
155
156     try {
157         mgr.saveKey(obs, ALIAS1, key, Policy());
158         RUNNER_ASSERT_MSG(false, "saveKey() should have thrown an exception");
159     } catch (const std::invalid_argument& e) {
160         RUNNER_ASSERT(true);
161     } catch (...) {
162         RUNNER_ASSERT_MSG(false, "Unexpected exception");
163     }
164 }
165
166 RUNNER_TEST(TA0020_save_key_no_alias)
167 {
168     test_expect_invalid_param("", prepareKey(), Policy());
169 }
170
171 RUNNER_TEST(TA0030_save_key_no_key)
172 {
173     KeyShPtr key;
174     test_expect_invalid_param(ALIAS1, key, Policy());
175 }
176
177 RUNNER_TEST(TA0090_save_key_positive)
178 {
179     test_save_key();
180 }
181
182 RUNNER_CHILD_TEST(TA0100_user_save_key_positive)
183 {
184     SecurityServer::AccessProvider ap("mylabel");
185     ap.allowAPI("key-manager::api-storage", "rw");
186     ap.applyAndSwithToUser(USER_APP, USER_APP);
187
188     test_save_key();
189 }
190
191 RUNNER_TEST(TA9999_deinit)
192 {
193     int temp;
194     auto control = CKM::Control::create();
195     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->lockUserKey(USER_APP)),
196                          "Error=" << CKM::ErrorToString(temp));
197     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->removeUserData(USER_APP)),
198                          "Error=" << CKM::ErrorToString(temp));
199     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->lockUserKey(USER_ROOT)),
200                          "Error=" << CKM::ErrorToString(temp));
201     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->removeUserData(USER_ROOT)),
202                              "Error=" << CKM::ErrorToString(temp));
203 }