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