Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | package org.onosproject.core; |
| 17 | |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 20 | import com.google.common.testing.EqualsTester; |
| 21 | import org.junit.Test; |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 22 | import org.onosproject.security.AppPermission; |
| 23 | import org.onosproject.security.Permission; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 24 | |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 25 | import java.util.ArrayList; |
| 26 | import java.util.HashSet; |
| 27 | import java.util.List; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 28 | import java.util.Optional; |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 29 | import java.util.Set; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 30 | |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 31 | import static org.junit.Assert.assertArrayEquals; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 32 | import static org.junit.Assert.assertEquals; |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 33 | import static org.junit.Assert.assertNotNull; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 34 | import static org.junit.Assert.assertTrue; |
| 35 | import static org.onosproject.app.DefaultApplicationDescriptionTest.*; |
| 36 | |
| 37 | /** |
| 38 | * Basic tests of the default app descriptor. |
| 39 | */ |
| 40 | public class DefaultApplicationTest { |
| 41 | |
| 42 | public static final ApplicationId APP_ID = new DefaultApplicationId(2, APP_NAME); |
| 43 | |
| 44 | @Test |
| 45 | public void basics() { |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 46 | Application app = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN, |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 47 | CATEGORY, URL, README, ICON, ROLE, |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 48 | PERMS, Optional.of(FURL), FEATURES, APPS); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 49 | assertEquals("incorrect id", APP_ID, app.id()); |
| 50 | assertEquals("incorrect version", VER, app.version()); |
| 51 | assertEquals("incorrect description", DESC, app.description()); |
| 52 | assertEquals("incorrect origin", ORIGIN, app.origin()); |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 53 | assertEquals("incorrect category", CATEGORY, app.category()); |
| 54 | assertEquals("incorrect URL", URL, app.url()); |
| 55 | assertEquals("incorrect readme", README, app.readme()); |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 56 | assertArrayEquals("incorrect icon", ICON, app.icon()); |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 57 | assertEquals("incorrect role", ROLE, app.role()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 58 | assertEquals("incorrect permissions", PERMS, app.permissions()); |
| 59 | assertEquals("incorrect features repo", FURL, app.featuresRepo().get()); |
| 60 | assertEquals("incorrect features", FEATURES, app.features()); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 61 | assertEquals("incorrect apps", APPS, app.requiredApps()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 62 | assertTrue("incorrect toString", app.toString().contains(APP_NAME)); |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void testEquality() { |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 67 | Application a1 = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN, |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 68 | CATEGORY, URL, README, ICON, ROLE, |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 69 | PERMS, Optional.of(FURL), FEATURES, APPS); |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 70 | Application a2 = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN, |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 71 | CATEGORY, URL, README, ICON, ROLE, |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 72 | PERMS, Optional.of(FURL), FEATURES, APPS); |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 73 | Application a3 = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN, |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 74 | CATEGORY, URL, README, ICON, ROLE, |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 75 | PERMS, Optional.empty(), FEATURES, APPS); |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 76 | Application a4 = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN + "asd", |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 77 | CATEGORY, URL, README, ICON, ROLE, |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 78 | PERMS, Optional.of(FURL), FEATURES, APPS); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 79 | new EqualsTester().addEqualityGroup(a1, a2) |
| 80 | .addEqualityGroup(a3).addEqualityGroup(a4).testEquals(); |
| 81 | } |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 82 | |
| 83 | |
| 84 | private static final byte[] ICON_ORIG = new byte[] {1, 2, 3, 4}; |
| 85 | |
| 86 | @Test |
| 87 | public void immutableIcon() { |
| 88 | byte[] iconSourceData = ICON_ORIG.clone(); |
| 89 | |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 90 | Application app = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN, |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 91 | CATEGORY, URL, README, iconSourceData, ROLE, |
| 92 | PERMS, Optional.of(FURL), FEATURES, APPS); |
| 93 | |
| 94 | // can we modify the icon after getting a reference to the app? |
| 95 | byte[] icon = app.icon(); |
| 96 | assertArrayEquals("did not start with orig icon", ICON_ORIG, icon); |
| 97 | |
| 98 | // now the hack |
| 99 | for (int i = 0, n = ICON_ORIG.length; i < n; i++) { |
| 100 | icon[i] = 0; |
| 101 | } |
| 102 | // if the reference to the internal array is given out, the hack |
| 103 | // will succeed and this next assertion fails |
| 104 | assertArrayEquals("no longer orig icon", ICON_ORIG, app.icon()); |
| 105 | |
| 106 | // what if we modify the source data? |
| 107 | for (int i = 0, n = ICON_ORIG.length; i < n; i++) { |
| 108 | iconSourceData[i] = 0; |
| 109 | } |
| 110 | // if the application just saved a reference to the given array |
| 111 | // this next assertion fails |
| 112 | assertArrayEquals("modifying source alters appicon", ICON_ORIG, app.icon()); |
| 113 | } |
| 114 | |
| 115 | private static final Permission PERM_W = |
| 116 | new Permission(AppPermission.class.getName(), "FLOWRULE_WRITE"); |
| 117 | private static final Permission PERM_R = |
| 118 | new Permission(AppPermission.class.getName(), "FLOWRULE_READ"); |
| 119 | |
| 120 | private static final Permission JUNK_PERM = new Permission("foo", "bar"); |
| 121 | |
| 122 | private static final Set<Permission> PERMS_ORIG = ImmutableSet.of(PERM_W, PERM_R); |
| 123 | private static final Set<Permission> PERMS_UNSAFE = new HashSet<>(PERMS_ORIG); |
| 124 | |
| 125 | |
| 126 | @Test |
| 127 | public void immutablePermissions() { |
| 128 | // Set<Permission> p = PERMS_ORIG; |
| 129 | Set<Permission> p = PERMS_UNSAFE; |
| 130 | |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 131 | Application app = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN, |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 132 | CATEGORY, URL, README, ICON, ROLE, |
| 133 | p, Optional.of(FURL), FEATURES, APPS); |
| 134 | |
| 135 | Set<Permission> perms = app.permissions(); |
| 136 | try { |
| 137 | perms.add(JUNK_PERM); |
| 138 | } catch (UnsupportedOperationException e) { |
| 139 | // set is immutable |
| 140 | } |
| 141 | assertTrue("no write perm", app.permissions().contains(PERM_W)); |
| 142 | assertTrue("no read perm", app.permissions().contains(PERM_R)); |
| 143 | assertEquals("extra perms", 2, app.permissions().size()); |
| 144 | |
| 145 | // DONE: review - is it sufficient to expect caller to pass in ImmutableSet ? |
| 146 | // Issue Resolved with Immutable collections used during construction. |
| 147 | |
| 148 | // If we just pass in a HashSet, the contents would be modifiable by |
| 149 | // an external party. (Making the field final just means that the |
| 150 | // reference to the set can never change; the contents may still...) |
| 151 | |
| 152 | // Similar reasoning can be applied to these two fields also: |
| 153 | // List<String> features |
| 154 | // List<String> requiredApps |
| 155 | } |
| 156 | |
| 157 | private static final String FOO = "foo"; |
| 158 | private static final String BAR = "bar"; |
| 159 | private static final String FIFI = "fifi"; |
| 160 | private static final String EVIL = "Bwahahahaha!"; |
| 161 | |
| 162 | private static final List<String> FEATURES_ORIG = ImmutableList.of(FOO, BAR); |
| 163 | private static final List<String> FEATURES_UNSAFE = new ArrayList<>(FEATURES_ORIG); |
| 164 | |
| 165 | private static final List<String> REQ_APPS_ORIG = ImmutableList.of(FIFI); |
| 166 | private static final List<String> REQ_APPS_UNSAFE = new ArrayList<>(REQ_APPS_ORIG); |
| 167 | |
| 168 | @Test |
| 169 | public void immutableFeatures() { |
| 170 | // List<String> f = FEATURES_ORIG; |
| 171 | List<String> f = FEATURES_UNSAFE; |
| 172 | |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 173 | Application app = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN, |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 174 | CATEGORY, URL, README, ICON, ROLE, |
| 175 | PERMS, Optional.of(FURL), f, APPS); |
| 176 | |
| 177 | List<String> features = app.features(); |
| 178 | try { |
| 179 | features.add(EVIL); |
| 180 | } catch (UnsupportedOperationException e) { |
| 181 | // list is immutable |
| 182 | } |
| 183 | assertTrue("no foo feature", features.contains(FOO)); |
| 184 | assertTrue("no bar feature", features.contains(BAR)); |
| 185 | assertEquals("extra features!", 2, features.size()); |
| 186 | } |
| 187 | |
| 188 | @Test |
| 189 | public void immutableRequiredApps() { |
| 190 | // List<String> ra = REQ_APPS_ORIG; |
| 191 | List<String> ra = REQ_APPS_UNSAFE; |
| 192 | |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 193 | Application app = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN, |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 194 | CATEGORY, URL, README, ICON, ROLE, |
| 195 | PERMS, Optional.of(FURL), FEATURES, ra); |
| 196 | |
| 197 | List<String> reqApps = app.requiredApps(); |
| 198 | try { |
| 199 | reqApps.add(EVIL); |
| 200 | } catch (UnsupportedOperationException e) { |
| 201 | // list is immutable |
| 202 | } |
| 203 | assertTrue("no fifi required app", reqApps.contains(FIFI)); |
| 204 | assertEquals("extra required apps!", 1, reqApps.size()); |
| 205 | } |
| 206 | |
| 207 | @Test |
| 208 | public void nullIcon() { |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 209 | Application app = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN, |
Simon Hunt | c2da488 | 2016-01-21 13:24:47 -0800 | [diff] [blame] | 210 | CATEGORY, URL, README, null, ROLE, |
| 211 | PERMS, Optional.of(FURL), FEATURES, APPS); |
| 212 | byte[] icon = app.icon(); |
| 213 | assertNotNull("null icon", icon); |
| 214 | assertEquals("unexpected size", 0, icon.length); |
| 215 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 216 | } |