summaryrefslogtreecommitdiff
path: root/test/test_misc.cpp
blob: 80716a0f5d55b3314dbacf414a79dbf723ca2ff9 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright (c) 2020-2022 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * This file is licensed under the terms of MIT License or the Apache License
 * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
 * See the LICENSE file or the notice below for Apache License Version 2.0
 * details.
 *
 * 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_misc.cpp
 * @author      Tomasz Świerczek (t.swierczek@samsung.com)
 * @version     1.0
 * @brief       Tests of functions & classes from utils.cpp file and other smaller files
 */

#include <algorithm>
#include <credentials.h>
#include <string>
#include <stdexcept>
#include <dpl/exception.h>
#include <security-manager-types.h>
#include <sys/types.h>
#include <utils.h>
#include <testmacros.h>
#include <tzplatform-config.h>

using namespace SecurityManager;

BOOST_AUTO_TEST_SUITE(UTILS_TEST)

namespace {

int noThrowFunc()
{
    ScopedTimeStamper __stamper("a location", Credentials::getCredentialsFromSelf());
    return SECURITY_MANAGER_SUCCESS;
}

template<class E> int throwFunc()
{
    throw E();
    return SECURITY_MANAGER_SUCCESS;
}

int throwExFunc()
{
    throw Exception("","",0,"");
    return SECURITY_MANAGER_SUCCESS;
}

} // namespace anonymous

POSITIVE_TEST_CASE(T280_ScopedTimeStamper_try_catch)
{
    BOOST_REQUIRE_MESSAGE(try_catch(noThrowFunc) == SECURITY_MANAGER_SUCCESS, "Invalid return from try_catch()");
}

NEGATIVE_TEST_CASE(T282_try_catch)
{
    BOOST_REQUIRE_MESSAGE(try_catch(throwExFunc) == SECURITY_MANAGER_ERROR_UNKNOWN, "Invalid return value from try_catch()");
}

NEGATIVE_TEST_CASE(T283_try_catch)
{
    BOOST_REQUIRE_MESSAGE(try_catch(throwFunc<std::bad_alloc>) == SECURITY_MANAGER_ERROR_MEMORY, "Invalid return value from try_catch()");
}

NEGATIVE_TEST_CASE(T284_try_catch)
{
    BOOST_REQUIRE_MESSAGE(try_catch(throwFunc<std::system_error>) == SECURITY_MANAGER_ERROR_UNKNOWN, "Invalid return value from try_catch()");
}

NEGATIVE_TEST_CASE(T285_try_catch)
{
    BOOST_REQUIRE_MESSAGE(try_catch(throwFunc<std::exception>) == SECURITY_MANAGER_ERROR_UNKNOWN, "Invalid return value from try_catch()");
}

NEGATIVE_TEST_CASE(T286_try_catch)
{
    BOOST_REQUIRE_MESSAGE(try_catch(throwFunc<std::string>) == SECURITY_MANAGER_ERROR_UNKNOWN, "Invalid return value from try_catch()");
}

POSITIVE_TEST_CASE(T287_groups_loading)
{
    std::vector<gid_t> vgroups;
    std::vector<std::string> privileges;
    gid_t *groups = static_cast<gid_t*>(malloc(sizeof(gid_t)));
    BOOST_REQUIRE_MESSAGE(groups, "Memory allocation problem");
    auto gPtr = makeUnique(groups, free);
    size_t groups_count = 1;
    BOOST_REQUIRE_NO_THROW(group_vector_to_array(vgroups, &groups, &groups_count));
    BOOST_REQUIRE_MESSAGE(!groups && !groups_count, "Invalid output from group_vector_to_array");
    BOOST_REQUIRE_NO_THROW(loadGroups(vgroups));
    // current configuration has some groups and some privileges
    BOOST_REQUIRE_MESSAGE(vgroups.size() > 0, "Invalid output from loadGroups");
    BOOST_REQUIRE_NO_THROW(group_vector_to_array(vgroups, &groups, &groups_count));
    auto gPtr2 = makeUnique(groups, free);
    BOOST_REQUIRE_MESSAGE(groups, "NULL returned from group_vector_to_array");
    BOOST_REQUIRE_MESSAGE(groups_count ==  vgroups.size(), "Invalid number of groups");
    BOOST_REQUIRE_MESSAGE(std::is_permutation(vgroups.begin(), vgroups.end(),
                          groups, groups + groups_count),
                          "Invalid return from group_vector_to_array");
}

POSITIVE_TEST_CASE(T288_monotonic_clock)
{
    time_t t;
    BOOST_REQUIRE_NO_THROW(t = monotonicNow());
    BOOST_REQUIRE_MESSAGE(t > 0, "Invalid monotonic time value");
}

NEGATIVE_TEST_CASE(T289_creds)
{
    BOOST_REQUIRE_THROW(Credentials::getCredentialsFromSocket(-1), Credentials::Exception::SocketError);
}

NEGATIVE_TEST_CASE(T290_creds)
{
    BOOST_REQUIRE_THROW(Credentials::getCredentialsFromFd(-1), Credentials::Exception::FdError);
}

POSITIVE_TEST_CASE(T291_tzplatform_config)
{
    TizenPlatformConfig cfg(0);
    BOOST_REQUIRE_NO_THROW(cfg.ctxMakePath(
                    TizenPlatformConfig::getId("TZ_USER_HOME"),
                    "/just a string 1/"));
    BOOST_REQUIRE_NO_THROW(cfg.ctxMakePath(
                    TizenPlatformConfig::getId("TZ_USER_HOME"),
                    "/just a string 1/", "/just a string 2/"));
    BOOST_REQUIRE_NO_THROW(cfg.ctxMakePath(
                    TizenPlatformConfig::getId("TZ_USER_HOME"),
                    "/just a string 1/", "/just a string 2/", "/just a string 3/"));

    BOOST_REQUIRE_NO_THROW(TizenPlatformConfig::getId("TZ_USER_HOME"));
    BOOST_REQUIRE_NO_THROW(TizenPlatformConfig::makePath(
                                TizenPlatformConfig::getId("TZ_USER_HOME"),
                                "/just a string 1/"));
    BOOST_REQUIRE_NO_THROW(TizenPlatformConfig::makePath(
                                TizenPlatformConfig::getId("TZ_USER_HOME"),
                                "/just a string 1/", "/just a string 2/"));

    BOOST_REQUIRE_NO_THROW(TizenPlatformConfig::makePath(
                                TizenPlatformConfig::getId("TZ_USER_HOME"),
                                "/just a string 1/", "/just a string 2/", "/just a string 3/"));
}

POSITIVE_TEST_CASE(T292_exception_class)
{
        Exception e("path", "function", 1, "message");
        std::string s = Exception::KnownExceptionToString(e);
        BOOST_REQUIRE_MESSAGE(!s.empty(), "Empty message passed from KnownExceptionToString");

        std::string s1 = Exception::UnknownExceptionToString();
        BOOST_REQUIRE_MESSAGE(!s1.empty(), "Empty message passed from UnknownExceptionToString");

        Exception e2(e);
        BOOST_REQUIRE_NO_THROW(Exception::DisplayKnownException(e2));
        BOOST_REQUIRE_NO_THROW(Exception::DisplayUnknownException());
}

POSITIVE_TEST_CASE(T293_possiblyUnterminatedArrayToString)
{
    char a[] = {'k', 'o', 't', 'a'};
    BOOST_REQUIRE_EQUAL("kota", possiblyUnterminatedArrayToString(a));
    a[3] = '\0';
    BOOST_REQUIRE_EQUAL("kot", possiblyUnterminatedArrayToString(a));
    a[2] = '\0';
    BOOST_REQUIRE_EQUAL("ko", possiblyUnterminatedArrayToString(a));
    a[1] = '\0';
    BOOST_REQUIRE_EQUAL("k", possiblyUnterminatedArrayToString(a));
    a[0] = '\0';
    BOOST_REQUIRE_EQUAL("", possiblyUnterminatedArrayToString(a));
}

BOOST_AUTO_TEST_SUITE_END()