tizen 2.3.1 release
[framework/web/wearable/wrt-commons.git] / tests / dao / TestCases_CertificateDAO.cpp
1 /*
2  * Copyright (c) 2013 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   TestCases_CertificateDAO.cpp
18  * @author  Slawomir Pajak (s.pajak@partner.samsung.com)
19  * @version 1.0
20  * @brief   This file contains tests for certificate dao class.
21  */
22
23 #include <dpl/test/test_runner.h>
24 #include <wrt-commons/certificate-dao/certificate_dao.h>
25
26 RUNNER_TEST_GROUP_INIT(DAO)
27
28 using namespace CertificateDB;
29
30 namespace {
31
32 class CertificateDAOWrapper {
33 public:
34     static CertificateDAO* getCertificateDAO()
35     {
36         static CertificateDAOPtr certificateDAO(
37                 new CertificateDB::CertificateDAO(DPL::FromASCIIString("testWidget123")));
38         return certificateDAO.get();
39     }
40 };
41 }
42
43 /**
44  * Name: certificate_dao_save_remove_by_certdata
45  * Description: Tests saves certificateDao object, reads it, and removes it at the end
46  *              using certificate data value.
47  * Expected: All operations should succeed.
48  */
49 RUNNER_TEST(certificate_dao_save_remove_by_certdata)
50 {
51     CertificateDAO* certificateDAO = CertificateDAOWrapper::getCertificateDAO();
52
53     CertificateDataList dataList = certificateDAO->getCertificateDataList();
54     RUNNER_ASSERT_MSG(dataList.empty(), "CertificateDataList should be empty");
55
56     CertificateData certData(DPL::FromASCIIString("sampleCetificate"));
57     certificateDAO->setCertificateData(certData, RESULT_ALLOW_ONCE);
58
59     dataList = certificateDAO->getCertificateDataList();
60
61     RUNNER_ASSERT_MSG(dataList.size() == 1, "CertificateDataList should not be empty");
62     RUNNER_ASSERT_MSG((*dataList.begin())->certificate == DPL::FromASCIIString("sampleCetificate"),
63             "Wrong element in table");
64
65     RUNNER_ASSERT_MSG(certificateDAO->getResult(**dataList.begin()) == RESULT_ALLOW_ONCE, "Wrong result");
66
67     certificateDAO->removeCertificateData(certData);
68
69     dataList = certificateDAO->getCertificateDataList();
70     RUNNER_ASSERT_MSG(dataList.empty(), "CertificateDataList should be empty");
71
72 }
73
74 /**
75  * Name: certificate_dao_save_remove_by_result
76  * Description: Tests saves certificateDao object, reads it, and removes it at the end
77  *              using result value.
78  * Expected: All operations should succeed.
79  */
80 RUNNER_TEST(certificate_dao_save_remove_by_result)
81 {
82     CertificateDAO* certificateDAO = CertificateDAOWrapper::getCertificateDAO();
83
84     CertificateDataList dataList = certificateDAO->getCertificateDataList();
85     RUNNER_ASSERT_MSG(dataList.empty(), "CertificateDataList should be empty");
86
87     CertificateData certData(DPL::FromASCIIString("sampleCetificate1"));
88     certificateDAO->setCertificateData(certData, RESULT_ALLOW_ALWAYS);
89
90     CertificateData certData2(DPL::FromASCIIString("sampleCetificate2"));
91     certificateDAO->setCertificateData(certData2, RESULT_ALLOW_ALWAYS);
92
93     dataList = certificateDAO->getCertificateDataList();
94     RUNNER_ASSERT_MSG(dataList.size() == 2, "CertificateDataList should not be empty");
95
96     certificateDAO->removeCertificateData(RESULT_ALLOW_ALWAYS);
97     dataList = certificateDAO->getCertificateDataList();
98     RUNNER_ASSERT_MSG(dataList.empty(), "CertificateDataList should be empty");
99 }
100
101 /**
102  * Name: certificate_dao_update
103  * Description: Tests update of certificateDao object.
104  * Expected: All operations should succeed.
105  */
106 RUNNER_TEST(certificate_dao_update)
107 {
108     CertificateDAO* certificateDAO = CertificateDAOWrapper::getCertificateDAO();
109
110     CertificateDataList dataList = certificateDAO->getCertificateDataList();
111     RUNNER_ASSERT_MSG(dataList.empty(), "CertificateDataList should be empty");
112
113     CertificateData certData(DPL::FromASCIIString("sampleCetificate3"));
114     certificateDAO->setCertificateData(certData, RESULT_ALLOW_ONCE);
115
116     dataList = certificateDAO->getCertificateDataList();
117     RUNNER_ASSERT_MSG(dataList.size() == 1, "CertificateDataList should not be empty");
118     RUNNER_ASSERT_MSG((*dataList.begin())->certificate == DPL::FromASCIIString("sampleCetificate3"),
119             "Wrong element in table");
120     RUNNER_ASSERT_MSG(certificateDAO->getResult(**dataList.begin()) == RESULT_ALLOW_ONCE, "Wrong result");
121
122     certificateDAO->setCertificateData(certData, RESULT_DENY_ALWAYS);
123     dataList = certificateDAO->getCertificateDataList();
124     RUNNER_ASSERT_MSG(dataList.size() == 1, "CertificateDataList should not be empty");
125     RUNNER_ASSERT_MSG((*dataList.begin())->certificate == DPL::FromASCIIString("sampleCetificate3"),
126             "Wrong element in table");
127     RUNNER_ASSERT_MSG(certificateDAO->getResult(**dataList.begin()) == RESULT_DENY_ALWAYS, "Wrong result");
128
129     certificateDAO->removeCertificateData(certData);
130     dataList = certificateDAO->getCertificateDataList();
131     RUNNER_ASSERT_MSG(dataList.empty(), "CertificateDataList should be empty");
132 }
133
134 /**
135  * Name: certificate_dao_equality_operator
136  * Description: Tests CertificateData equality operators.
137  * Expected: All operations should succeed.
138  */
139 RUNNER_TEST(certificate_dao_equality_operator)
140 {
141     CertificateData certData1(DPL::FromASCIIString("sampleCetificate1"));
142     CertificateData certData2(DPL::FromASCIIString("sampleCetificate1"));
143     CertificateData certData3(DPL::FromASCIIString("sampleCetificate2"));
144
145     RUNNER_ASSERT_MSG(certData1 == certData2, "Object should be equal");
146     RUNNER_ASSERT_MSG(certData1 != certData3, "Object should not be equal");
147     RUNNER_ASSERT_MSG(certData2 != certData3, "Object should not be equal");
148 }
149