Release 0.1.54.9
[platform/core/security/key-manager.git] / common / DBFixture.h
1 /*
2  *  Copyright (c) 2016-2020 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        DBFixture.h
19  * @author      Maciej Karpiuk (m.karpiuk2@samsung.com)
20  * @version
21  * @brief
22  */
23 #pragma once
24
25 #include <ckm/ckm-type.h>
26 #include <protocols.h>
27 #include <db-row.h>
28 #include <data-type.h>
29 #include <db-crypto.h>
30
31 class DBFixture {
32 public:
33         enum class DBCryptoThrows : bool { no, yes };
34         DBFixture(DBCryptoThrows dbCryptoThrows = DBCryptoThrows::no);
35         explicit DBFixture(const char *legacy_db_fname, const char *db_fname,
36                                            DBCryptoThrows dbCryptoThrows = DBCryptoThrows::no);
37
38         constexpr static const char *m_default_name = "name";
39         constexpr static const char *m_default_owner = "label";
40
41         // ::::::::::::::::::::::::: helper methods :::::::::::::::::::::::::
42         static void generate_name(unsigned int id, CKM::Name &output);
43         static void generate_owner(unsigned int id, CKM::ClientId &output);
44         static CKM::DB::Row create_default_row(CKM::DataType type =
45                         CKM::DataType::BINARY_DATA);
46         static CKM::DB::Row create_default_row(const CKM::Name &name,
47                                                                                    const CKM::ClientId &owner,
48                                                                                    CKM::DataType type = CKM::DataType::BINARY_DATA);
49         static CKM::DB::Row create_default_binary_row();
50         static void compare_row(const CKM::DB::Row &lhs, const CKM::DB::Row &rhs);
51
52         // ::::::::::::::::::::::::: DB :::::::::::::::::::::::::
53         void generate_db(unsigned int num_name, unsigned int names_per_owner);
54         long add_full_access_rights(unsigned int num_name,
55                                                                 unsigned int num_names_per_owner);
56         void check_DB_integrity(const CKM::DB::Row &rowPattern);
57         void insert_row();
58         void insert_row(const CKM::Name &name, const CKM::ClientId &owner);
59         void delete_row(const CKM::Name &name, const CKM::ClientId &owner);
60         void add_permission(const CKM::Name &name, const CKM::ClientId &owner,
61                                                 const CKM::ClientId &accessor);
62         void read_row_expect_success(const CKM::Name &name,
63                                                                  const CKM::ClientId &owner);
64
65         CKM::DB::Crypto    m_db;
66
67 private:
68         void    init(DBCryptoThrows dbCryptoThrows);
69         static void unlinkDb();
70
71         constexpr static const char *m_crypto_legacy_db_fname = "/tmp/testme.db";
72         constexpr static const char *m_crypto_db_fname = "/tmp/testme0.db";
73 };