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