Chanhee Lee | 9401048 | 2017-01-23 23:06:18 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Chanhee Lee | 9401048 | 2017-01-23 23:06:18 -0800 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.security.store; |
| 18 | |
| 19 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 20 | import static org.junit.Assert.assertEquals; |
| 21 | import static org.junit.Assert.assertNotNull; |
| 22 | import static org.junit.Assert.assertNull; |
| 23 | import static org.junit.Assert.assertTrue; |
| 24 | import static org.onlab.util.Tools.groupedThreads; |
| 25 | import static org.onosproject.security.store.SecurityModeState.*; |
| 26 | import static org.slf4j.LoggerFactory.getLogger; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.HashSet; |
| 30 | import java.util.List; |
| 31 | import java.util.Optional; |
| 32 | import java.util.Set; |
| 33 | import java.util.concurrent.ConcurrentHashMap; |
| 34 | import java.util.concurrent.ExecutorService; |
| 35 | |
| 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
| 38 | import org.onosproject.core.ApplicationId; |
| 39 | import org.onosproject.core.ApplicationRole; |
| 40 | import org.onosproject.core.DefaultApplication; |
| 41 | import org.onosproject.core.DefaultApplicationId; |
| 42 | import org.onosproject.core.Version; |
| 43 | import org.onosproject.security.Permission; |
| 44 | import org.slf4j.Logger; |
| 45 | |
| 46 | import com.google.common.collect.ImmutableSet; |
| 47 | import com.google.common.collect.Sets; |
| 48 | |
| 49 | /** |
| 50 | * Unit Test on DistributedSecurityModeStore. |
| 51 | */ |
| 52 | public class DistributedSecurityModeStoreTest { |
| 53 | |
| 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
| 56 | private DefaultApplicationId appId; |
| 57 | private DefaultApplication app; |
| 58 | |
| 59 | private Permission testPermission; |
| 60 | private Set<Permission> testPermissions; |
| 61 | private List<String> testFeatures; |
| 62 | private List<String> testRequiredApps; |
| 63 | private Set<String> testLocations; |
| 64 | private ConcurrentHashMap<String, Set<ApplicationId>> localBundleAppDirectory; |
| 65 | private ConcurrentHashMap<ApplicationId, Set<String>> localAppBundleDirectory; |
| 66 | private ConcurrentHashMap<ApplicationId, Set<Permission>> violations; |
| 67 | private SecurityInfo testSecInfo; |
| 68 | private ConcurrentHashMap<ApplicationId, SecurityInfo> states; |
| 69 | |
| 70 | private ExecutorService eventHandler; |
| 71 | |
| 72 | @Before |
| 73 | public void setUp() throws Exception { |
| 74 | appId = new DefaultApplicationId(1, "test"); |
| 75 | testPermissions = new HashSet<Permission>(); |
| 76 | testPermission = new Permission("testClass", "testName"); |
| 77 | testPermissions.add(testPermission); |
| 78 | testFeatures = new ArrayList<String>(); |
| 79 | testFeatures.add("testFeature"); |
| 80 | testRequiredApps = new ArrayList<String>(); |
| 81 | testRequiredApps.add("testRequiredApp"); |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 82 | app = DefaultApplication.builder() |
| 83 | .withAppId(appId) |
| 84 | .withVersion(Version.version(1, 1, "patch", "build")) |
| 85 | .withTitle("testTitle") |
| 86 | .withDescription("testDes") |
| 87 | .withOrigin("testOri") |
| 88 | .withCategory("testCT") |
| 89 | .withUrl("testurl") |
| 90 | .withReadme("test") |
| 91 | .withIcon(null) |
| 92 | .withRole(ApplicationRole.ADMIN) |
| 93 | .withPermissions(testPermissions) |
| 94 | .withFeaturesRepo(Optional.ofNullable(null)) |
| 95 | .withFeatures(testFeatures) |
| 96 | .withRequiredApps(testRequiredApps) |
| 97 | .build(); |
Chanhee Lee | 9401048 | 2017-01-23 23:06:18 -0800 | [diff] [blame] | 98 | |
| 99 | testLocations = new HashSet<String>(); |
| 100 | testLocations.add("locationA"); |
| 101 | testLocations.add("locationB"); |
| 102 | |
| 103 | Set<ApplicationId> appIdSet = new HashSet<ApplicationId>(); |
| 104 | appIdSet.add(appId); |
| 105 | localBundleAppDirectory = new ConcurrentHashMap<>(); |
| 106 | localBundleAppDirectory.put("testLocation", appIdSet); |
| 107 | localAppBundleDirectory = new ConcurrentHashMap<>(); |
| 108 | localAppBundleDirectory.put(appId, testLocations); |
| 109 | |
| 110 | violations = new ConcurrentHashMap<ApplicationId, Set<Permission>>(); |
| 111 | violations.put(appId, testPermissions); |
| 112 | |
| 113 | testSecInfo = new SecurityInfo(testPermissions, SECURED); |
| 114 | states = new ConcurrentHashMap<ApplicationId, SecurityInfo>(); |
| 115 | states.put(appId, testSecInfo); |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void testActivate() { |
| 120 | eventHandler = newSingleThreadExecutor(groupedThreads("onos/security/store", "event-handler", log)); |
| 121 | assertNotNull(eventHandler); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void testDeactivate() { |
| 126 | eventHandler = newSingleThreadExecutor(groupedThreads("onos/security/store", "event-handler", log)); |
| 127 | eventHandler.shutdown(); |
| 128 | assertTrue(eventHandler.isShutdown()); |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void testGetBundleLocations() { |
| 133 | Set<String> locations = localAppBundleDirectory.get(appId); |
| 134 | assertTrue(locations.contains("locationA")); |
| 135 | } |
| 136 | |
| 137 | @Test |
| 138 | public void testGetApplicationIds() { |
| 139 | Set<ApplicationId> appIds = localBundleAppDirectory.get("testLocation"); |
| 140 | assertTrue(appIds.contains(appId)); |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void testGetRequestedPermissions() { |
| 145 | Set<Permission> permissions = violations.get(appId); |
| 146 | assertTrue(permissions.contains(testPermission)); |
| 147 | } |
| 148 | |
| 149 | @Test |
| 150 | public void testGetGrantedPermissions() { |
| 151 | Set<Permission> permissions = states.get(appId).getPermissions(); |
| 152 | assertTrue(permissions.contains(testPermission)); |
| 153 | } |
| 154 | |
| 155 | @Test |
| 156 | public void testRequestPermission() { |
| 157 | states.compute(appId, (id, securityInfo) -> new SecurityInfo(securityInfo.getPermissions(), POLICY_VIOLATED)); |
| 158 | assertEquals(POLICY_VIOLATED, states.get(appId).getState()); |
| 159 | Permission testPermissionB = new Permission("testClassB", "testNameB"); |
| 160 | violations.compute(appId, |
| 161 | (k, v) -> v == null ? Sets.newHashSet(testPermissionB) : addAndGet(v, testPermissionB)); |
| 162 | assertTrue(violations.get(appId).contains(testPermissionB)); |
| 163 | } |
| 164 | |
| 165 | private Set<Permission> addAndGet(Set<Permission> oldSet, Permission newPerm) { |
| 166 | oldSet.add(newPerm); |
| 167 | return oldSet; |
| 168 | } |
| 169 | |
| 170 | @Test |
| 171 | public void testIsSecured() { |
| 172 | SecurityInfo info = states.get(appId); |
| 173 | assertEquals(SECURED, info.getState()); |
| 174 | } |
| 175 | |
| 176 | @Test |
| 177 | public void testReviewPolicy() { |
| 178 | assertEquals(SECURED, states.get(appId).getState()); |
| 179 | states.computeIfPresent(appId, (applicationId, securityInfo) -> { |
| 180 | if (securityInfo.getState().equals(SECURED)) { |
| 181 | return new SecurityInfo(ImmutableSet.of(), REVIEWED); |
| 182 | } |
| 183 | return securityInfo; |
| 184 | }); |
| 185 | assertEquals(REVIEWED, states.get(appId).getState()); |
| 186 | } |
| 187 | |
| 188 | @Test |
| 189 | public void testAcceptPolicy() { |
| 190 | assertEquals(SECURED, states.get(appId).getState()); |
| 191 | states.compute(appId, |
| 192 | (id, securityInfo) -> { |
| 193 | switch (securityInfo.getState()) { |
| 194 | case POLICY_VIOLATED: |
| 195 | return new SecurityInfo(securityInfo.getPermissions(), SECURED); |
| 196 | case SECURED: |
| 197 | return new SecurityInfo(securityInfo.getPermissions(), POLICY_VIOLATED); |
| 198 | case INSTALLED: |
| 199 | return new SecurityInfo(securityInfo.getPermissions(), REVIEWED); |
| 200 | case REVIEWED: |
| 201 | return new SecurityInfo(securityInfo.getPermissions(), INSTALLED); |
| 202 | default: |
| 203 | return securityInfo; |
| 204 | } |
| 205 | }); |
| 206 | assertEquals(POLICY_VIOLATED, states.get(appId).getState()); |
| 207 | } |
| 208 | |
| 209 | @Test |
| 210 | public void testRegisterApplication() { |
| 211 | states.remove(appId); |
| 212 | assertNull(states.get(appId)); |
| 213 | |
| 214 | for (String location : localAppBundleDirectory.get(appId)) { |
| 215 | if (!localBundleAppDirectory.containsKey(location)) { |
| 216 | localBundleAppDirectory.put(location, new HashSet<>()); |
| 217 | } |
| 218 | if (!localBundleAppDirectory.get(location).contains(appId)) { |
| 219 | localBundleAppDirectory.get(location).add(appId); |
| 220 | } |
| 221 | } |
| 222 | states.put(appId, new SecurityInfo(Sets.newHashSet(), INSTALLED)); |
| 223 | assertNotNull(states.get(appId)); |
| 224 | assertEquals(INSTALLED, states.get(appId).getState()); |
| 225 | } |
| 226 | |
| 227 | @Test |
| 228 | public void testUnregisterApplication() { |
| 229 | if (localAppBundleDirectory.containsKey(appId)) { |
| 230 | for (String location : localAppBundleDirectory.get(appId)) { |
| 231 | if (localBundleAppDirectory.get(location) != null) { |
| 232 | if (localBundleAppDirectory.get(location).size() == 1) { |
| 233 | localBundleAppDirectory.remove(location); |
| 234 | } else { |
| 235 | localBundleAppDirectory.get(location).remove(appId); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | localAppBundleDirectory.remove(appId); |
| 240 | } |
| 241 | assertNull(localAppBundleDirectory.get(appId)); |
| 242 | } |
| 243 | |
| 244 | @Test |
| 245 | public void testGetState() { |
| 246 | assertEquals(SECURED, states.get(appId).getState()); |
| 247 | } |
| 248 | |
| 249 | } |