blob: af3f35900390f5576e550433d6bb7ee9c5b53a92 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
Sbhat3599d66962017-06-08 11:25:35 -070031import static org.junit.Assert.*;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080032import static org.onosproject.app.DefaultApplicationDescriptionTest.*;
33
34/**
35 * Basic tests of the default app descriptor.
36 */
37public class DefaultApplicationTest {
38
39 public static final ApplicationId APP_ID = new DefaultApplicationId(2, APP_NAME);
40
41 @Test
42 public void basics() {
Simon Huntafae2f72016-03-04 21:18:23 -080043 Application app = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN,
Jian Lic35415d2016-01-14 17:22:31 -080044 CATEGORY, URL, README, ICON, ROLE,
Thomas Vachuska761f0042015-11-11 19:10:17 -080045 PERMS, Optional.of(FURL), FEATURES, APPS);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080046 assertEquals("incorrect id", APP_ID, app.id());
47 assertEquals("incorrect version", VER, app.version());
Sbhat3599d66962017-06-08 11:25:35 -070048 assertEquals("incorrect title", TITLE, app.title());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080049 assertEquals("incorrect description", DESC, app.description());
50 assertEquals("incorrect origin", ORIGIN, app.origin());
Jian Lic35415d2016-01-14 17:22:31 -080051 assertEquals("incorrect category", CATEGORY, app.category());
52 assertEquals("incorrect URL", URL, app.url());
53 assertEquals("incorrect readme", README, app.readme());
Simon Huntc2da4882016-01-21 13:24:47 -080054 assertArrayEquals("incorrect icon", ICON, app.icon());
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090055 assertEquals("incorrect role", ROLE, app.role());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080056 assertEquals("incorrect permissions", PERMS, app.permissions());
57 assertEquals("incorrect features repo", FURL, app.featuresRepo().get());
58 assertEquals("incorrect features", FEATURES, app.features());
Thomas Vachuska761f0042015-11-11 19:10:17 -080059 assertEquals("incorrect apps", APPS, app.requiredApps());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080060 assertTrue("incorrect toString", app.toString().contains(APP_NAME));
61 }
62
63 @Test
64 public void testEquality() {
Simon Huntafae2f72016-03-04 21:18:23 -080065 Application a1 = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN,
Jian Lic35415d2016-01-14 17:22:31 -080066 CATEGORY, URL, README, ICON, ROLE,
Thomas Vachuska761f0042015-11-11 19:10:17 -080067 PERMS, Optional.of(FURL), FEATURES, APPS);
Simon Huntafae2f72016-03-04 21:18:23 -080068 Application a2 = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN,
Jian Lic35415d2016-01-14 17:22:31 -080069 CATEGORY, URL, README, ICON, ROLE,
Thomas Vachuska761f0042015-11-11 19:10:17 -080070 PERMS, Optional.of(FURL), FEATURES, APPS);
Simon Huntafae2f72016-03-04 21:18:23 -080071 Application a3 = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN,
Jian Lic35415d2016-01-14 17:22:31 -080072 CATEGORY, URL, README, ICON, ROLE,
Thomas Vachuska761f0042015-11-11 19:10:17 -080073 PERMS, Optional.empty(), FEATURES, APPS);
Simon Huntafae2f72016-03-04 21:18:23 -080074 Application a4 = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN + "asd",
Jian Lic35415d2016-01-14 17:22:31 -080075 CATEGORY, URL, README, ICON, ROLE,
Thomas Vachuska761f0042015-11-11 19:10:17 -080076 PERMS, Optional.of(FURL), FEATURES, APPS);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080077 new EqualsTester().addEqualityGroup(a1, a2)
78 .addEqualityGroup(a3).addEqualityGroup(a4).testEquals();
79 }
Simon Huntc2da4882016-01-21 13:24:47 -080080
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 Huntafae2f72016-03-04 21:18:23 -080088 Application app = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN,
Simon Huntc2da4882016-01-21 13:24:47 -080089 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 Huntafae2f72016-03-04 21:18:23 -0800129 Application app = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN,
Simon Huntc2da4882016-01-21 13:24:47 -0800130 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 Huntafae2f72016-03-04 21:18:23 -0800171 Application app = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN,
Simon Huntc2da4882016-01-21 13:24:47 -0800172 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 Huntafae2f72016-03-04 21:18:23 -0800191 Application app = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN,
Simon Huntc2da4882016-01-21 13:24:47 -0800192 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 Huntafae2f72016-03-04 21:18:23 -0800207 Application app = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN,
Simon Huntc2da4882016-01-21 13:24:47 -0800208 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 Vachuska02aeb032015-01-06 22:36:30 -0800214}