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.impl; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertNotNull; |
| 21 | import static org.junit.Assert.assertTrue; |
| 22 | import static org.onosproject.security.store.SecurityModeState.POLICY_VIOLATED; |
| 23 | import static org.onosproject.security.store.SecurityModeState.REVIEWED; |
| 24 | import static org.onosproject.security.store.SecurityModeState.SECURED; |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.HashSet; |
| 28 | import java.util.List; |
| 29 | import java.util.Map; |
| 30 | import java.util.Optional; |
| 31 | import java.util.Set; |
| 32 | import java.util.concurrent.ConcurrentHashMap; |
| 33 | |
| 34 | import org.junit.Before; |
| 35 | import org.junit.Test; |
| 36 | import org.onosproject.core.ApplicationId; |
| 37 | import org.onosproject.core.ApplicationRole; |
| 38 | import org.onosproject.core.DefaultApplication; |
| 39 | import org.onosproject.core.DefaultApplicationId; |
| 40 | import org.onosproject.core.Version; |
| 41 | import org.onosproject.security.Permission; |
| 42 | import org.onosproject.security.store.SecurityModeStoreAdapter; |
| 43 | |
| 44 | /** |
| 45 | * Unit Test on SecurityModeManager. |
| 46 | */ |
| 47 | public 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 Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 72 | 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 Lee | 9401048 | 2017-01-23 23:06:18 -0800 | [diff] [blame] | 88 | |
| 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 | } |