blob: c0fbf9cc9028b5c4aabeb956945c34b03888146e [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
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 */
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
Simon Huntc2da4882016-01-21 13:24:47 -080031import static org.junit.Assert.assertArrayEquals;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080032import static org.junit.Assert.assertEquals;
Simon Huntc2da4882016-01-21 13:24:47 -080033import static org.junit.Assert.assertNotNull;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080034import static org.junit.Assert.assertTrue;
35import static org.onosproject.app.DefaultApplicationDescriptionTest.*;
36
37/**
38 * Basic tests of the default app descriptor.
39 */
40public class DefaultApplicationTest {
41
42 public static final ApplicationId APP_ID = new DefaultApplicationId(2, APP_NAME);
43
44 @Test
45 public void basics() {
Jian Lic35415d2016-01-14 17:22:31 -080046 Application app = new DefaultApplication(APP_ID, VER, DESC, ORIGIN,
47 CATEGORY, URL, README, ICON, ROLE,
Thomas Vachuska761f0042015-11-11 19:10:17 -080048 PERMS, Optional.of(FURL), FEATURES, APPS);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080049 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 Lic35415d2016-01-14 17:22:31 -080053 assertEquals("incorrect category", CATEGORY, app.category());
54 assertEquals("incorrect URL", URL, app.url());
55 assertEquals("incorrect readme", README, app.readme());
Simon Huntc2da4882016-01-21 13:24:47 -080056 assertArrayEquals("incorrect icon", ICON, app.icon());
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090057 assertEquals("incorrect role", ROLE, app.role());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080058 assertEquals("incorrect permissions", PERMS, app.permissions());
59 assertEquals("incorrect features repo", FURL, app.featuresRepo().get());
60 assertEquals("incorrect features", FEATURES, app.features());
Thomas Vachuska761f0042015-11-11 19:10:17 -080061 assertEquals("incorrect apps", APPS, app.requiredApps());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080062 assertTrue("incorrect toString", app.toString().contains(APP_NAME));
63 }
64
65 @Test
66 public void testEquality() {
Jian Lic35415d2016-01-14 17:22:31 -080067 Application a1 = new DefaultApplication(APP_ID, VER, DESC, ORIGIN,
68 CATEGORY, URL, README, ICON, ROLE,
Thomas Vachuska761f0042015-11-11 19:10:17 -080069 PERMS, Optional.of(FURL), FEATURES, APPS);
Jian Lic35415d2016-01-14 17:22:31 -080070 Application a2 = new DefaultApplication(APP_ID, VER, DESC, ORIGIN,
71 CATEGORY, URL, README, ICON, ROLE,
Thomas Vachuska761f0042015-11-11 19:10:17 -080072 PERMS, Optional.of(FURL), FEATURES, APPS);
Jian Lic35415d2016-01-14 17:22:31 -080073 Application a3 = new DefaultApplication(APP_ID, VER, DESC, ORIGIN,
74 CATEGORY, URL, README, ICON, ROLE,
Thomas Vachuska761f0042015-11-11 19:10:17 -080075 PERMS, Optional.empty(), FEATURES, APPS);
Jian Lic35415d2016-01-14 17:22:31 -080076 Application a4 = new DefaultApplication(APP_ID, VER, DESC, ORIGIN + "asd",
77 CATEGORY, URL, README, ICON, ROLE,
Thomas Vachuska761f0042015-11-11 19:10:17 -080078 PERMS, Optional.of(FURL), FEATURES, APPS);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080079 new EqualsTester().addEqualityGroup(a1, a2)
80 .addEqualityGroup(a3).addEqualityGroup(a4).testEquals();
81 }
Simon Huntc2da4882016-01-21 13:24:47 -080082
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
90 Application app = new DefaultApplication(APP_ID, VER, DESC, ORIGIN,
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
131 Application app = new DefaultApplication(APP_ID, VER, DESC, ORIGIN,
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
173 Application app = new DefaultApplication(APP_ID, VER, DESC, ORIGIN,
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
193 Application app = new DefaultApplication(APP_ID, VER, DESC, ORIGIN,
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() {
209 Application app = new DefaultApplication(APP_ID, VER, DESC, ORIGIN,
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 Vachuska02aeb032015-01-06 22:36:30 -0800216}