Merge branch 'tizen' into yaca
[platform/core/test/security-tests.git] / src / ode / ode-tests-common.cpp
1 /*
2  *  Copyright (c) 2018 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  *  @file   ode-tests-common.cpp
17  *  @author Pawel Kowalski (p.kowalski2@partner.samsung.com)
18  *  @brief  Tests for the ODE API
19  */
20
21 #include <unistd.h>
22
23 #include <iostream>
24 #include <string>
25 #include <utility>
26
27 #include <dpl/log/log.h>
28
29 #include <ode/keys.h>
30 #include <ode/internal-encryption.h>
31 #include "ode-tests-common.h"
32
33 #define ERRORDESCRIBE(name) case name: return #name
34
35 const char* ODEErrorToString(int error) {
36     switch(error) {
37         ERRORDESCRIBE(ODE_ERROR_NONE);
38         ERRORDESCRIBE(ODE_ERROR_INVALID_PARAMETER);
39         ERRORDESCRIBE(ODE_ERROR_CONNECTION_REFUSED);
40         ERRORDESCRIBE(ODE_ERROR_PERMISSION_DENIED);
41         ERRORDESCRIBE(ODE_ERROR_NO_SUCH_FILE);
42         ERRORDESCRIBE(ODE_ERROR_NO_SUCH_DEVICE);
43         ERRORDESCRIBE(ODE_ERROR_KEY_REJECTED);
44         ERRORDESCRIBE(ODE_ERROR_NO_DATA);
45         ERRORDESCRIBE(ODE_ERROR_RESOURCE_BUSY);
46         ERRORDESCRIBE(ODE_ERROR_UNKNOWN);
47         default: return "Error not defined";
48     }
49 }
50
51 #undef ERRORDESCRIBE
52
53 // helper classes
54
55 HelperKeys::HelperKeys(const char* device, const char* password, bool initialize) {
56     dev = device;
57     pass = password;
58
59     if (initialize) {
60         bool result;
61         assert_positive(ode_key_is_initialized, dev, &result);
62
63         if (!result) {
64             assert_positive(ode_key_init, dev, pass, ODE_KEY_DEFAULT_256BIT);
65         }
66     }
67 }
68
69 HelperKeys::~HelperKeys() {
70     ode_key_remove_master_key(dev);
71     ode_key_remove(dev, pass);
72 }
73
74 HelperInternalEncryption::HelperInternalEncryption(const char* password) {
75     pass = password;
76
77     bool result;
78     assert_positive(ode_internal_encryption_is_password_initialized, &result);
79
80     if (!result) {
81         assert_positive(ode_internal_encryption_init_password, pass);
82     }
83 }
84
85 HelperInternalEncryption::~HelperInternalEncryption() {
86     ode_internal_encryption_umount();
87     ode_internal_encryption_clean_password(pass);
88 }
89
90 std::unique_ptr<ServiceManager, std::function<void(ServiceManager*)>> createScopedOdeStopper() {
91     auto sm_start = [](ServiceManager* sm) {
92         try {
93             sm->startService(true);
94             delete sm;
95             sleep(1);
96             //TODO: startService should wait until the sm is really started but it does not
97             // because of that the sleep is needed
98         } catch (...) {
99             RUNNER_ERROR_MSG("Unexpected exception during service startup");
100         }
101     };
102     std::unique_ptr<ServiceManager, decltype(sm_start)> sm_stop(
103             new ServiceManager("ode.service", { "ode.socket" }),
104             std::move(sm_start));
105
106     sm_stop->stopService(true);
107
108     return sm_stop;
109 }