blob: 328d903bb2c168255bdb408086fdf8cb3e007580 [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");
Ray Milkey47c95412017-09-15 10:40:48 -070072 app = DefaultApplication.builder()
73 .withAppId(appId)
74 .withVersion(Version.version(1, 1, "patch", "build"))
75 .withTitle("testTitle")
76 .withDescription("testDes")
77 .withOrigin("testOri")
78 .withCategory("testCT")
79 .withUrl("testurl")
80 .withReadme("test")
81 .withIcon(null)
82 .withRole(ApplicationRole.ADMIN)
83 .withPermissions(testPermissions)
84 .withFeaturesRepo(Optional.ofNullable(null))
85 .withFeatures(testFeatures)
86 .withRequiredApps(testRequiredApps)
87 .build();
Chanhee Lee94010482017-01-23 23:06:18 -080088
89 store.registerApplication(appId);
90 }
91
92// @Test
93// public void testActivate() {
94// fail("Not yet implemented");
95// }
96//
97// @Test
98// public void testDeactivate() {
99// fail("Not yet implemented");
100// }
101
102 @Test
103 public void testIsSecured() {
104 assertTrue(store.isSecured(appId));
105 }
106
107 @Test
108 public void testReview() {
109 assertEquals(SECURED, store.getState(appId));
110 store.reviewPolicy(appId);
111 assertEquals(REVIEWED, store.getState(appId));
112 }
113
114 @Test
115 public void testAcceptPolicy() {
116 assertEquals(SECURED, store.getState(appId));
117 store.acceptPolicy(appId, getMaximumPermissions(appId));
118 assertEquals(POLICY_VIOLATED, store.getState(appId));
119 }
120
121 @Test
122 public void testRegister() {
123 assertTrue(store.registerApplication(appId));
124 }
125
126 @Test
127 public void testGetPrintableSpecifiedPermissions() {
128 Map<Integer, List<Permission>> result = getPrintablePermissionMap(getMaximumPermissions(appId));
129 assertNotNull(result.get(1).get(0));
130 assertTrue(result.get(1).size() > 0);
131 assertEquals("testNameAdmin", result.get(1).get(0).getName());
132 }
133
134 @Test
135 public void testGetPrintableGrantedPermissions() {
136 Map<Integer, List<Permission>> result = getPrintablePermissionMap(store.getGrantedPermissions(appId));
137 assertNotNull(result.get(2).get(0));
138 assertTrue(result.get(2).size() > 0);
139 assertEquals("testName", result.get(2).get(0).getName());
140 }
141
142 @Test
143 public void testGetPrintableRequestedPermissions() {
144 DefaultApplicationId appIdViolation;
145 appIdViolation = new DefaultApplicationId(2, "violation");
146 Map<Integer, List<Permission>> result =
147 getPrintablePermissionMap(store.getRequestedPermissions(appIdViolation));
148 assertNotNull(result.get(2).get(0));
149 assertTrue(result.get(2).size() > 0);
150 assertEquals("testNameViolation", result.get(2).get(0).getName());
151 }
152
153
154 private Map<Integer, List<Permission>> getPrintablePermissionMap(Set<Permission> perms) {
155 ConcurrentHashMap<Integer, List<Permission>> sortedMap = new ConcurrentHashMap<>();
156 sortedMap.put(0, new ArrayList());
157 sortedMap.put(1, new ArrayList());
158 sortedMap.put(2, new ArrayList());
159 sortedMap.put(3, new ArrayList());
160 sortedMap.put(4, new ArrayList());
161 for (Permission perm : perms) {
162 if (perm.getName().contains("Admin")) {
163 sortedMap.get(1).add(perm);
164 } else {
165 sortedMap.get(2).add(perm);
166 }
167 }
168 return sortedMap;
169 }
170
171 private Set<Permission> getMaximumPermissions(ApplicationId appId) {
172 if (app == null) {
173 return null;
174 }
175 Set<Permission> appPerms;
176 switch (app.role()) {
177 case ADMIN:
178 appPerms = app.permissions();
179 break;
180 case USER:
181 appPerms = app.permissions();
182 break;
183 case UNSPECIFIED:
184 default:
185 appPerms = new HashSet<Permission>();
186 break;
187 }
188
189 return appPerms;
190 }
191}