6d020b71900079a519cd847dff89d3f6e8e6315a
[platform/core/security/key-manager.git] / tests / encryption-scheme / scheme-test.h
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       scheme-test.h
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #pragma once
23
24 #include <memory>
25 #include <string>
26 #include <vector>
27
28 #include <ckm/ckm-control.h>
29 #include <ckm/ckm-manager.h>
30
31 #include <data-type.h>
32
33 namespace CKM {
34 namespace DB {
35 class Crypto;
36 } // DB
37 } // CKM
38
39 struct Item {
40     Item() {}
41     Item(const CKM::Alias& alias,
42          const CKM::DataType::Type type,
43          const CKM::Policy& policy)
44     : alias(alias), type(type), policy(policy)
45     {
46     }
47
48     CKM::Alias alias;
49     CKM::DataType::Type type;
50     CKM::Policy policy;
51 };
52
53 typedef std::vector<Item> Items;
54
55 struct ItemFilter {
56     ItemFilter() :
57         typeFrom(CKM::DataType::DB_FIRST),
58         typeTo(CKM::DataType::DB_LAST),
59         exportableOnly(false),
60         noPassword(false)
61     {}
62
63     explicit ItemFilter(CKM::DataType::Type type) :
64         typeFrom(type),
65         typeTo(type),
66         exportableOnly(false),
67         noPassword(false)
68     {}
69
70     ItemFilter(CKM::DataType::Type typeFrom, CKM::DataType::Type typeTo) :
71         typeFrom(typeFrom),
72         typeTo(typeTo),
73         exportableOnly(false),
74         noPassword(false)
75     {}
76
77     bool Matches(const Item& item) const {
78         if(item.type < typeFrom || item.type > typeTo)
79             return false;
80         if(exportableOnly && !item.policy.extractable)
81             return false;
82         if(noPassword && !item.policy.password.empty())
83             return false;
84         return true;
85     }
86
87     CKM::DataType::Type typeFrom;
88     CKM::DataType::Type typeTo;
89     bool exportableOnly;
90     bool noPassword;
91 };
92
93 class SchemeTest {
94 public:
95     SchemeTest();
96     ~SchemeTest();
97
98     void RemoveUserData();
99     void FillDb();
100     void ReadAll(bool useWrongPass = false);
101     void SignVerify();
102     void EncryptDecrypt();
103     void CreateChain();
104     void RemoveAll();
105     size_t CountObjects();
106     void RestoreDb();
107     void CheckSchemeVersion(const ItemFilter& filter, int version);
108
109 private:
110     void SwitchToUser();
111     void SwitchToRoot();
112     void EnableDirectDbAccess();
113     void SignVerifyItem(const Item& itemPrv, const Item& itemPub);
114     void EncryptDecryptItem(const Item& item);
115     void EncryptDecryptItem(const Item& itemPrv, const Item& itemPub);
116     void CreateChainItem(const Item& leaf, const Items& certs);
117
118     CKM::ControlShPtr m_control;
119     CKM::ManagerShPtr m_mgr;
120     std::string m_origLabel;
121     bool m_userChanged;
122
123     std::unique_ptr<CKM::DB::Crypto> m_db;
124     bool m_directAccessEnabled;
125 };