tizen 2.3 release
[framework/web/wearable/wrt-security.git] / tests / ace / widget_installer.h
1 /*
2  * Copyright (c) 2011 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    widget_installer.h
18  * @author  Tomasz Swierczek (t.swierczek@samsung.com)
19  * @brief   Class that manages widget installation for ACE tests
20  */
21 #ifndef _TESTS_ACE_WIDGET_INSTALLER_H_
22 #define _TESTS_ACE_WIDGET_INSTALLER_H_
23
24 #include <ace-dao-rw/AceDAO.h>
25 #include <ace-dao-ro/common_dao_types.h>
26 #include <sys/time.h>
27 #include <ctime>
28 #include <cstdlib>
29
30 #define INVALID_WIDGET_HANDLE -1
31
32 using namespace AceDB;
33
34 namespace InstallerMockup {
35
36 class WacSecurityMock : public IWacSecurity
37 {
38 public:
39     WacSecurityMock() :
40         mRecognized(false),
41         mDistributorSigned(false),
42         mWacSigned(false)
43     {
44     }
45
46     virtual const WidgetCertificateDataList& getCertificateList() const
47     {
48         return mList;
49     }
50
51     virtual bool isRecognized() const { return mRecognized; }
52     virtual bool isDistributorSigned() const { return mDistributorSigned; }
53     virtual bool isWacSigned() const { return mWacSigned; }
54     virtual void getCertificateChainList(CertificateChainList& /*lst*/) const {}
55     virtual void getCertificateChainList(CertificateChainList&,
56             CertificateSource) const {}
57
58     WidgetCertificateDataList& getCertificateListRef()
59     {
60         return mList;
61     }
62
63     void setRecognized(bool recognized) { mRecognized = recognized; }
64     void setDistributorSigned(bool distributorSigned)
65     {
66         mDistributorSigned = distributorSigned;
67     }
68     void setWacSigned(bool wacSigned) { mWacSigned = wacSigned; }
69
70 private:
71     WidgetCertificateDataList mList;
72     // author signature verified
73     bool mRecognized;
74     // known distribuor
75     bool mDistributorSigned;
76     // distributor is wac
77     bool mWacSigned;
78
79 };
80
81 static WidgetHandle _registerWidget(const WidgetRegisterInfo& regInfo,
82                                     const IWacSecurity& sec,
83                                     int line)
84 {
85     //randomize widget handle
86     WidgetHandle handle = INVALID_WIDGET_HANDLE;
87     struct timeval tv;
88     gettimeofday(&tv, NULL);
89     srand(time(NULL) + tv.tv_usec);
90     do {
91         handle = rand();
92     } while (AceDAOReadOnly::isWidgetInstalled(handle));
93
94     try
95     {
96         auto previous = AceDAOReadOnly::getHandleList();
97
98         // register widget
99         AceDAO::registerWidgetInfo(handle, regInfo, sec.getCertificateList());
100
101         RUNNER_ASSERT_MSG(handle != INVALID_WIDGET_HANDLE,
102                           "(called from line " << line << ")");
103
104         auto current = AceDAOReadOnly::getHandleList();
105         RUNNER_ASSERT_MSG(previous.size()+1 == current.size(),
106                           "(called from line " << line << ")");
107
108         RUNNER_ASSERT_MSG(AceDAOReadOnly::isWidgetInstalled(handle),
109                           "(called from line " << line << ")");
110     } catch (AceDAOReadOnly::Exception::DatabaseError) {
111         RUNNER_ASSERT_MSG(
112                 false,
113                 "Unexpected exception (called from line " << line << ")");
114     }
115     return handle;
116 }
117
118 WidgetHandle registerWidget(AppTypes appType = AppTypes::WAC20)
119 {
120     WacSecurityMock sec;
121     WidgetRegisterInfo regInfo;
122     regInfo.type = appType;
123     return _registerWidget(regInfo, sec, __LINE__);
124 }
125
126 } // namespace
127
128 #endif // _TESTS_ACE_WIDGET_INSTALLER_H_