summaryrefslogtreecommitdiff
path: root/test/test_privilege_db_transactions.cpp
blob: cbfa273022eabb234b437260b1320513c8684d51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 *  Copyright (c) 2016 - 2018 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */
/*
 * @file       test_privilege_db_transactions.cpp
 * @author     Radoslaw Bartosiak (r.bartosiak@samsung.com)
 * @version    1.0
 */

#include <stdlib.h>
#include <string>
#include <sys/types.h>

#include <boost/test/unit_test.hpp>
#include <boost/test/results_reporter.hpp>
#include <boost/test/utils/wrap_stringstream.hpp>

#include <config.h>
#include <filesystem.h>

#include "privilege_db.h"
#include "privilege_db_fixture.h"


class Empty {}; //to overwrite the suite fixture

BOOST_FIXTURE_TEST_SUITE(PRIVILEGE_DB_TEST_TRANSACTIONS, PrivilegeDBFixture)

// Constructor

BOOST_FIXTURE_TEST_CASE(T100_privilegedb_constructor, Empty)
{
    std::unique_ptr<PrivilegeDb> testPrivDb;

    purgeDb();
    // db init must fail w/ no loader having run beforehand
    BOOST_REQUIRE_THROW(testPrivDb.reset(new PrivilegeDb(PrivilegeDb::Offline::no, PrivilegeDb::Db::test)),
        PrivilegeDb::Exception::IOError);
    requireNoDb();
}

// Transactions

BOOST_AUTO_TEST_CASE(T200_transaction_rollback_commit)
{
    BOOST_REQUIRE_NO_THROW(getPrivDb()->BeginTransaction());
    addAppSuccess(app(1), pkg(1), uid(1), tizenVer(1), author(1), NotHybrid);

    BOOST_REQUIRE_NO_THROW(getPrivDb()->RollbackTransaction());
    BOOST_REQUIRE_MESSAGE(!getPrivDb()->AppNameExists(app(1)),
        "AppNameExists wrongly reported " << app(1) << " as existing, despite a rollback");
    BOOST_REQUIRE_THROW(getPrivDb()->CommitTransaction(),
        PrivilegeDb::Exception::InternalError);
}

BOOST_AUTO_TEST_CASE(T210_transaction_double_rollback)
{
    BOOST_REQUIRE_NO_THROW(getPrivDb()->BeginTransaction());
    addAppSuccess(app(1), pkg(1), uid(1), tizenVer(1), author(1), NotHybrid);

    BOOST_REQUIRE_NO_THROW(getPrivDb()->RollbackTransaction());
    BOOST_REQUIRE_THROW(getPrivDb()->RollbackTransaction(),
        PrivilegeDb::Exception::InternalError);
}

BOOST_AUTO_TEST_CASE(T220_commit_without_begin)
{
    BOOST_REQUIRE_THROW(getPrivDb()->CommitTransaction(), PrivilegeDb::Exception::InternalError);
}

BOOST_AUTO_TEST_CASE(T230_rollback_without_begin)
{
    BOOST_REQUIRE_THROW(getPrivDb()->RollbackTransaction(), PrivilegeDb::Exception::InternalError);
}

BOOST_AUTO_TEST_CASE(T240_transaction)
{
    BOOST_REQUIRE_NO_THROW(getPrivDb()->BeginTransaction());
    addAppSuccess(app(1), pkg(1), uid(1), tizenVer(1), author(1), NotHybrid);

    BOOST_REQUIRE_NO_THROW(getPrivDb()->CommitTransaction());
    BOOST_REQUIRE_NO_THROW(getPrivDb()->BeginTransaction());
    BOOST_REQUIRE_NO_THROW(getPrivDb()->RollbackTransaction());
    BOOST_REQUIRE_MESSAGE(getPrivDb()->AppNameExists(app(1)),
        "AppNameExists wrongly not reported " << app(1) << " as existing application name");
}

BOOST_AUTO_TEST_CASE(T250_transaction_double_begin)
{
    BOOST_REQUIRE_NO_THROW(getPrivDb()->BeginTransaction());
    addAppSuccess(app(1), pkg(1), uid(1), tizenVer(1), author(1), NotHybrid);

    BOOST_REQUIRE_THROW(getPrivDb()->BeginTransaction(),
        PrivilegeDb::Exception::InternalError);
}

BOOST_AUTO_TEST_CASE(T260_transaction_double_commit)
{
    BOOST_REQUIRE_NO_THROW(getPrivDb()->BeginTransaction());
    addAppSuccess(app(1), pkg(1), uid(1), tizenVer(1), author(1), NotHybrid);

    BOOST_REQUIRE_NO_THROW(getPrivDb()->CommitTransaction());
    BOOST_REQUIRE_THROW(getPrivDb()->CommitTransaction(),
        PrivilegeDb::Exception::InternalError);
}

BOOST_AUTO_TEST_SUITE_END()