blob: 15155ff379bb4a46233463ef60afa1270ba922ee [file] [log] [blame]
Chanhee Lee94010482017-01-23 23:06:18 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Chanhee Lee94010482017-01-23 23:06:18 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.security.impl;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertNotNull;
21import static org.junit.Assert.assertTrue;
22import static org.onosproject.security.store.SecurityModeState.POLICY_VIOLATED;
23import static org.onosproject.security.store.SecurityModeState.REVIEWED;
24import static org.onosproject.security.store.SecurityModeState.SECURED;
25
26import java.util.ArrayList;
27import java.util.HashSet;
28import java.util.List;
29import java.util.Map;
30import java.util.Optional;
31import java.util.Set;
32import java.util.concurrent.ConcurrentHashMap;
33
34import org.junit.Before;
35import org.junit.Test;
36import org.onosproject.core.ApplicationId;
37import org.onosproject.core.ApplicationRole;
38import org.onosproject.core.DefaultApplication;
39import org.onosproject.core.DefaultApplicationId;
40import org.onosproject.core.Version;
41import org.onosproject.security.Permission;
42import org.onosproject.security.store.SecurityModeStoreAdapter;
43
44/**
45 * Unit Test on SecurityModeManager.
46 */
47public class SecurityModeManagerTest {
48
49 private SecurityModeStoreAdapter store;
50
51 private DefaultApplicationId appId;
52 private DefaultApplication app;
53
54 private Permission testPermission;
55 private Set<Permission> testPermissions;
56 private List<String> testFeatures;
57 private List<String> testRequiredApps;
58 private Set<String> testLocations;
59
60 @Before
61 public void setUp() throws Exception {
62 store = new SecurityModeStoreAdapter();
63
64 appId = new DefaultApplicationId(1, "test");
65 testPermissions = new HashSet<Permission>();
66 testPermission = new Permission("testClass", "testNameAdmin");
67 testPermissions.add(testPermission);
68 testFeatures = new ArrayList<String>();
69 testFeatures.add("testFeature");
70 testRequiredApps = new ArrayList<String>();
71 testRequiredApps.add("testRequiredApp");
72 app = new DefaultApplication(appId, Version.version(1, 1, "patch", "build"), "testTitle",
73 "testDes", "testOri", "testCT",
74 "testurl", "test", null,
75 ApplicationRole.ADMIN, testPermissions,
76 Optional.ofNullable(null), testFeatures, testRequiredApps);
77
78 store.registerApplication(appId);
79 }
80
81// @Test
82// public void testActivate() {
83// fail("Not yet implemented");
84// }
85//
86// @Test
87// public void testDeactivate() {
88// fail("Not yet implemented");
89// }
90
91 @Test
92 public void testIsSecured() {
93 assertTrue(store.isSecured(appId));
94 }
95
96 @Test
97 public void testReview() {
98 assertEquals(SECURED, store.getState(appId));
99 store.reviewPolicy(appId);
100 assertEquals(REVIEWED, store.getState(appId));
101 }
102
103 @Test
104 public void testAcceptPolicy() {
105 assertEquals(SECURED, store.getState(appId));
106 store.acceptPolicy(appId, getMaximumPermissions(appId));
107 assertEquals(POLICY_VIOLATED, store.getState(appId));
108 }
109
110 @Test
111 public void testRegister() {
112 assertTrue(store.registerApplication(appId));
113 }
114
115 @Test
116 public void testGetPrintableSpecifiedPermissions() {
117 Map<Integer, List<Permission>> result = getPrintablePermissionMap(getMaximumPermissions(appId));
118 assertNotNull(result.get(1).get(0));
119 assertTrue(result.get(1).size() > 0);
120 assertEquals("testNameAdmin", result.get(1).get(0).getName());
121 }
122
123 @Test
124 public void testGetPrintableGrantedPermissions() {
125 Map<Integer, List<Permission>> result = getPrintablePermissionMap(store.getGrantedPermissions(appId));
126 assertNotNull(result.get(2).get(0));
127 assertTrue(result.get(2).size() > 0);
128 assertEquals("testName", result.get(2).get(0).getName());
129 }
130
131 @Test
132 public void testGetPrintableRequestedPermissions() {
133 DefaultApplicationId appIdViolation;
134 appIdViolation = new DefaultApplicationId(2, "violation");
135 Map<Integer, List<Permission>> result =
136 getPrintablePermissionMap(store.getRequestedPermissions(appIdViolation));
137 assertNotNull(result.get(2).get(0));
138 assertTrue(result.get(2).size() > 0);
139 assertEquals("testNameViolation", result.get(2).get(0).getName());
140 }
141
142
143 private Map<Integer, List<Permission>> getPrintablePermissionMap(Set<Permission> perms) {
144 ConcurrentHashMap<Integer, List<Permission>> sortedMap = new ConcurrentHashMap<>();
145 sortedMap.put(0, new ArrayList());
146 sortedMap.put(1, new ArrayList());
147 sortedMap.put(2, new ArrayList());
148 sortedMap.put(3, new ArrayList());
149 sortedMap.put(4, new ArrayList());
150 for (Permission perm : perms) {
151 if (perm.getName().contains("Admin")) {
152 sortedMap.get(1).add(perm);
153 } else {
154 sortedMap.get(2).add(perm);
155 }
156 }
157 return sortedMap;
158 }
159
160 private Set<Permission> getMaximumPermissions(ApplicationId appId) {
161 if (app == null) {
162 return null;
163 }
164 Set<Permission> appPerms;
165 switch (app.role()) {
166 case ADMIN:
167 appPerms = app.permissions();
168 break;
169 case USER:
170 appPerms = app.permissions();
171 break;
172 case UNSPECIFIED:
173 default:
174 appPerms = new HashSet<Permission>();
175 break;
176 }
177
178 return appPerms;
179 }
180}