Imported Upstream version 1.1.0
[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 #include "securevirtualresourcetypes.h"
29
30 using namespace std;
31
32 #define PE_UT_TAG "\tPE-UT-message: "
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 #include "policyengine.h"
39 #include "doxmresource.h"
40
41 // test parameters
42 PEContext_t g_peContext;
43
44 #ifdef __cplusplus
45 }
46 #endif
47
48 OicUuid_t g_subjectIdA = {"SubjectA"};
49 OicUuid_t g_subjectIdB = {"SubjectB"};
50 OicUuid_t g_devOwner;
51 char g_resource1[] = "Resource1";
52 char g_resource2[] = "Resource2";
53
54 extern OicSecDoxm_t *gDoxm;
55
56 //Policy Engine Core Tests
57 TEST(PolicyEngineCore, InitPolicyEngine)
58 {
59     EXPECT_EQ(OC_STACK_OK, InitPolicyEngine(&g_peContext));
60 }
61
62 // TODO - in order to unittest this we need InitDoxmResource() to put doxm
63 // into Owned state with a known owner.  This will have to be done post v1.1.
64 TEST(PolicyEngineCore, CheckPermissionNoAcls)
65 {
66     if(OC_STACK_OK == InitDoxmResource())
67     {
68         EXPECT_EQ(ACCESS_DENIED_SUBJECT_NOT_FOUND,
69             CheckPermission(&g_peContext,
70                             &g_subjectIdA,
71                             g_resource1,
72                             PERMISSION_READ));
73     }
74     else
75     {
76         printf("%s WARNING: InitDoxmResource() returned ERROR!\n", \
77             PE_UT_TAG);
78     }
79 }
80
81 // TODO - in order to unittest this we need InitDoxmResource() to put doxm
82 // into Owned state with a known owner.  This will have to be done post v1.1.
83 TEST(PolicyEngineCore, CheckDevOwnerRequest)
84 {
85     if(OC_STACK_OK == InitDoxmResource())
86     {
87         if(OC_STACK_OK == GetDoxmDevOwnerId(&g_devOwner))
88         {
89             printf("%s", PE_UT_TAG);
90             for(int i = 0; i < UUID_LENGTH; i++)
91             {
92                 printf("%d", g_devOwner.id[i]);
93             }
94             printf("\n");
95                 EXPECT_EQ(ACCESS_GRANTED,
96                     CheckPermission(&g_peContext,
97                         &g_devOwner,
98                         g_resource1,
99                         PERMISSION_FULL_CONTROL));
100         }
101         else
102         {
103             printf("%s WARNING: GetDoxmDevOwnerId() returned ERROR!\n", \
104                 PE_UT_TAG);
105         }
106     }
107     else
108     {
109         printf("%s WARNING: InitDoxmResource() returned ERROR!\n", \
110                 PE_UT_TAG);
111     }
112 }
113
114 TEST(PolicyEngineCore, DeInitPolicyEngine)
115 {
116     DeInitPolicyEngine(&g_peContext);
117     EXPECT_EQ(STOPPED, g_peContext.state);
118     EXPECT_EQ((uint16_t)0, g_peContext.permission);
119     EXPECT_FALSE(g_peContext.matchingAclFound);
120     EXPECT_EQ(ACCESS_DENIED_POLICY_ENGINE_ERROR, g_peContext.retVal);
121 }