Commit summary:
[platform/core/security/suspicious-activity-monitor.git] / utest / test_policyhandler.cpp
1 /**
2  * Samsung Ukraine R&D Center (SRK under a contract between)
3  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License
17  */
18 #include <gmock/gmock.h>
19
20 #include <dpm/device-policy-manager.h>
21 #include <dpm/administration.h>
22 #include <dpm/application.h>
23 #include <dpm/bluetooth.h>
24 #include <dpm/restriction.h>
25 #include <dpm/security.h>
26 #include <dpm/wifi.h>
27 #include <chrono>
28
29 #include "connection.h"
30 #include "settings.h"
31 #include "policyhandler.h"
32 #include "integer_property_policy.h"
33 #include "restservicemock.h"
34 #include "device_policy_manager_mock.h"
35
36 using ::testing::_;
37 using ::testing::Eq;
38 using ::testing::Return;
39 using ::testing::Throw;
40 using ::testing::DoAll;
41 using ::testing::Invoke;
42 using ::testing::WithArgs;
43 using ::testing::TypedEq;
44
45 namespace
46 {
47 const std::string TEST_POLICY_URI{"policy-uri"};
48 const std::string TEST_POLICY_CURI{"policy-curi"};
49 const std::string TEST_POLICY_NAME = "camera";
50 const std::string TEST_POLICY{R"-=(
51     {
52         "type":"policy",
53         "data": [
54             {
55                 "name":"camera",
56                 "value":1
57             }
58         ]
59     }
60 )-="};
61 }
62
63 TEST(TestPolicyHandler, test_accept)
64 {
65     try {
66         agent::Settings& settings = agent::Settings::instance();
67         RestServiceMock rest;
68         communication::Connection conn("", std::chrono::milliseconds(10), &rest, [](){return true;});
69         agent::PolicyHandler policy(conn);
70         DPMMock dpm;
71
72         dpm::IntegerPropertyPolicy ipp(dpm::PolicyEnforce::GetInstance(), TEST_POLICY_NAME, dpm_restriction_set_camera_state);
73
74         Json::Value root;
75         root["type"] = "policy";
76         root["uri"] = TEST_POLICY_URI;
77         root["curi"] = TEST_POLICY_CURI;
78         communication::Event event1(root, conn);
79         communication::Event event2(root, conn);
80
81         EXPECT_CALL(dpm, dpm_manager_create()).WillOnce(Return(&dpm));
82         EXPECT_CALL(dpm, dpm_restriction_set_camera_state(1)).WillOnce(Return(0));
83         EXPECT_CALL(dpm, dpm_manager_destroy()).Times(1);
84         EXPECT_CALL(rest, doGet(_, Eq(TEST_POLICY_URI)))
85                 .WillOnce(Return(TEST_POLICY));
86         EXPECT_CALL(rest, doPost(_, Eq(TEST_POLICY_CURI), TypedEq<const std::string&>(std::string{})))
87                 .Times(1);
88
89         settings.setLock(false);
90         policy.accept(event1);
91         settings.setLock(true);
92         policy.accept(event2);
93         settings.setLock(false);
94         policy.disable();
95         policy.accept(event2);
96     } catch (std::exception& e) {
97         FAIL() << e.what();
98     }
99 }