blob: b52312608da34794f10b8cf9d300caaba993c043 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska02aeb032015-01-06 22:36:30 -08003 *
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 */
16package org.onosproject.core;
17
Simon Huntc2da4882016-01-21 13:24:47 -080018import com.google.common.collect.ImmutableList;
19import com.google.common.collect.ImmutableSet;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080020import com.google.common.testing.EqualsTester;
21import org.junit.Test;
Simon Huntc2da4882016-01-21 13:24:47 -080022import org.onosproject.security.AppPermission;
23import org.onosproject.security.Permission;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080024
Simon Huntc2da4882016-01-21 13:24:47 -080025import java.util.ArrayList;
26import java.util.HashSet;
27import java.util.List;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080028import java.util.Optional;
Simon Huntc2da4882016-01-21 13:24:47 -080029import java.util.Set;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080030
Ray Milkey47c95412017-09-15 10:40:48 -070031import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
32import static org.onosproject.core.DefaultApplication.Builder;
33
Sbhat3599d66962017-06-08 11:25:35 -070034import static org.junit.Assert.*;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080035import static org.onosproject.app.DefaultApplicationDescriptionTest.*;
36
37/**
38 * Basic tests of the default app descriptor.
39 */
40public class DefaultApplicationTest {
41
Ray Milkey47c95412017-09-15 10:40:48 -070042 /**
43 * Checks that the DefaultApplication class is immutable.
44 */
45 @Test
46 public void testImmutability() {
47 assertThatClassIsImmutable(DefaultApplication.class);
48 }
49
Thomas Vachuska02aeb032015-01-06 22:36:30 -080050 public static final ApplicationId APP_ID = new DefaultApplicationId(2, APP_NAME);
Ray Milkey47c95412017-09-15 10:40:48 -070051 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 Vachuska02aeb032015-01-06 22:36:30 -080066
67 @Test
68 public void basics() {
Ray Milkey47c95412017-09-15 10:40:48 -070069 Application app = baseBuilder.build();
70
Thomas Vachuska02aeb032015-01-06 22:36:30 -080071 assertEquals("incorrect id", APP_ID, app.id());
72 assertEquals("incorrect version", VER, app.version());
Sbhat3599d66962017-06-08 11:25:35 -070073 assertEquals("incorrect title", TITLE, app.title());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080074 assertEquals("incorrect description", DESC, app.description());
75 assertEquals("incorrect origin", ORIGIN, app.origin());
Jian Lic35415d2016-01-14 17:22:31 -080076 assertEquals("incorrect category", CATEGORY, app.category());
77 assertEquals("incorrect URL", URL, app.url());
78 assertEquals("incorrect readme", README, app.readme());
Simon Huntc2da4882016-01-21 13:24:47 -080079 assertArrayEquals("incorrect icon", ICON, app.icon());
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090080 assertEquals("incorrect role", ROLE, app.role());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080081 assertEquals("incorrect permissions", PERMS, app.permissions());
82 assertEquals("incorrect features repo", FURL, app.featuresRepo().get());
83 assertEquals("incorrect features", FEATURES, app.features());
Thomas Vachuska761f0042015-11-11 19:10:17 -080084 assertEquals("incorrect apps", APPS, app.requiredApps());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080085 assertTrue("incorrect toString", app.toString().contains(APP_NAME));
86 }
87
88 @Test
89 public void testEquality() {
Ray Milkey47c95412017-09-15 10:40:48 -070090 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 Vachuska02aeb032015-01-06 22:36:30 -0800104 }
Simon Huntc2da4882016-01-21 13:24:47 -0800105
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 Milkey47c95412017-09-15 10:40:48 -0700113 Application app = DefaultApplication.builder(baseBuilder)
114 .withIcon(iconSourceData).build();
Simon Huntc2da4882016-01-21 13:24:47 -0800115
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 Milkey47c95412017-09-15 10:40:48 -0700153 Application app = baseBuilder.build();
Simon Huntc2da4882016-01-21 13:24:47 -0800154
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 Milkey47c95412017-09-15 10:40:48 -0700193 Application app = DefaultApplication.builder(baseBuilder).withFeatures(f).build();
Simon Huntc2da4882016-01-21 13:24:47 -0800194
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 Milkey47c95412017-09-15 10:40:48 -0700211 Application app = DefaultApplication.builder(baseBuilder).withRequiredApps(ra).build();
Simon Huntc2da4882016-01-21 13:24:47 -0800212
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 Milkey47c95412017-09-15 10:40:48 -0700225 Application app = DefaultApplication.builder(baseBuilder).withIcon(null).build();
Simon Huntc2da4882016-01-21 13:24:47 -0800226 byte[] icon = app.icon();
227 assertNotNull("null icon", icon);
228 assertEquals("unexpected size", 0, icon.length);
229 }
Ray Milkey47c95412017-09-15 10:40:48 -0700230}