Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / csdk / security / unittest / policyengine.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "gtest/gtest.h"
22 #include <pwd.h>
23 #include <grp.h>
24 #include <linux/limits.h>
25 #include "ocstack.h"
26 #include "cainterface.h"
27 #include "srmresourcestrings.h"
28
29 using namespace std;
30
31 #define PE_UT_TAG "\tPE-UT-message: "
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 #include "policyengine.h"
38 #include "doxmresource.h"
39
40 // test parameters
41 PEContext_t g_peContext;
42
43 #ifdef __cplusplus
44 }
45 #endif
46
47 OicUuid_t g_subjectIdA = {"SubjectA"};
48 OicUuid_t g_subjectIdB = {"SubjectB"};
49 OicUuid_t g_devOwner;
50 char g_resource1[] = "Resource1";
51 char g_resource2[] = "Resource2";
52
53 //Policy Engine Core Tests
54 TEST(PolicyEngineCore, InitPolicyEngine)
55 {
56     EXPECT_EQ(OC_STACK_OK, InitPolicyEngine(&g_peContext));
57 }
58
59 TEST(PolicyEngineCore, CheckPermissionNoAcls)
60 {
61     EXPECT_EQ(ACCESS_DENIED_SUBJECT_NOT_FOUND,
62         CheckPermission(&g_peContext,
63                         &g_subjectIdA,
64                         g_resource1,
65                         PERMISSION_READ));
66 }
67
68 //TODO This won't work until we figure out how to OcInit() or equivalent.
69 TEST(PolicyEngineCore, CheckDevOwnerRequest)
70 {
71     if(OC_STACK_OK == InitDoxmResource())
72     {
73         if(OC_STACK_OK == GetDoxmDevOwnerId(&g_devOwner))
74         {
75             printf("%s", PE_UT_TAG);
76             for(int i = 0; i < UUID_LENGTH; i++)
77             {
78                 printf("%d", g_devOwner.id[i]);
79             }
80             printf("\n");
81                 EXPECT_EQ(ACCESS_GRANTED,
82                     CheckPermission(&g_peContext,
83                         &g_devOwner,
84                         g_resource1,
85                         PERMISSION_FULL_CONTROL));
86         }
87         else
88         {
89             printf("%s WARNING: InitDoxmResource() returned ERROR!\n", \
90                 PE_UT_TAG);
91         }
92     }
93     else
94     {
95         printf("%s WARNING: GetDoxmDevOwnerId() returned ERROR!\n", PE_UT_TAG);
96     }
97
98
99 }
100
101 TEST(PolicyEngineCore, DeInitPolicyEngine)
102 {
103     DeInitPolicyEngine(&g_peContext);
104     EXPECT_EQ(STOPPED, g_peContext.state);
105     EXPECT_EQ(NULL, g_peContext.subject);
106     EXPECT_EQ(NULL, g_peContext.resource);
107     EXPECT_EQ((uint16_t)0, g_peContext.permission);
108     EXPECT_FALSE(g_peContext.matchingAclFound);
109     EXPECT_EQ(ACCESS_DENIED_POLICY_ENGINE_ERROR, g_peContext.retVal);
110 }