blob: c3693cb8318fba1211905c50bbbc757a017a5b1f [file] [log] [blame]
Pengfei Lue0c02e22015-07-07 15:41:31 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Pengfei Lue0c02e22015-07-07 15:41:31 +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.
Ray Milkey2d572dd2017-04-14 10:01:24 -070015 *
16 * Originally created by Pengfei Lu, Network and Cloud Computing Laboratory, Dalian University of Technology, China
17 * Advisers: Keqiu Li and Heng Qi
18 * This work is supported by the State Key Program of National Natural Science of China(Grant No. 61432002)
19 * and Prospective Research Project on Future Networks in Jiangsu Future Networks Innovation Institute.
Pengfei Lue0c02e22015-07-07 15:41:31 +080020 */
21
Thomas Vachuska9bb32352015-09-25 11:31:22 -070022package org.onosproject.acl;
Pengfei Lue0c02e22015-07-07 15:41:31 +080023
Jian Li9d616492016-03-09 10:52:49 -080024import org.glassfish.jersey.server.ResourceConfig;
Pengfei Lue0c02e22015-07-07 15:41:31 +080025import org.junit.After;
26import org.junit.Before;
27import org.junit.Test;
Ray Milkey094a1352018-01-22 14:03:54 -080028import org.onlab.junit.TestUtils;
Pengfei Lue0c02e22015-07-07 15:41:31 +080029import org.onlab.osgi.ServiceDirectory;
Thomas Vachuska9bb32352015-09-25 11:31:22 -070030import org.onlab.osgi.TestServiceDirectory;
Pengfei Lue0c02e22015-07-07 15:41:31 +080031import org.onlab.rest.BaseResource;
Pengfei Lue0c02e22015-07-07 15:41:31 +080032import org.onosproject.core.IdGenerator;
debmaitif6091982019-04-12 14:51:57 +053033import org.onosproject.rest.resources.ResourceTest;
Pengfei Lue0c02e22015-07-07 15:41:31 +080034
Jian Li9d616492016-03-09 10:52:49 -080035import javax.ws.rs.client.Entity;
36import javax.ws.rs.client.WebTarget;
debmaitif6091982019-04-12 14:51:57 +053037import javax.ws.rs.core.MediaType;
38import javax.ws.rs.core.Response;
39import java.io.InputStream;
Pengfei Lue0c02e22015-07-07 15:41:31 +080040import java.util.ArrayList;
41import java.util.List;
42import java.util.concurrent.atomic.AtomicLong;
43
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070044import static org.easymock.EasyMock.*;
Pengfei Lue0c02e22015-07-07 15:41:31 +080045import static org.hamcrest.Matchers.containsString;
debmaitif6091982019-04-12 14:51:57 +053046import static org.junit.Assert.assertEquals;
Pengfei Lue0c02e22015-07-07 15:41:31 +080047import static org.junit.Assert.assertThat;
48
49/**
50 * Test class for ACL application REST resource.
51 */
debmaitif6091982019-04-12 14:51:57 +053052public class AclWebResourceTest extends ResourceTest {
Pengfei Lue0c02e22015-07-07 15:41:31 +080053
54 final AclService mockAclService = createMock(AclService.class);
55 final AclStore mockAclStore = createMock(AclStore.class);
56 final List<AclRule> rules = new ArrayList<>();
57
Jian Li9d616492016-03-09 10:52:49 -080058 /**
59 * Constructs a control metrics collector resource test instance.
60 */
61 public AclWebResourceTest() {
62 super(ResourceConfig.forApplicationClass(AclWebApplication.class));
63 }
64
Pengfei Lue0c02e22015-07-07 15:41:31 +080065 @Before
Jian Li9d616492016-03-09 10:52:49 -080066 public void setUpMock() {
Pengfei Lue0c02e22015-07-07 15:41:31 +080067 expect(mockAclService.getAclRules()).andReturn(rules).anyTimes();
Jian Li9d616492016-03-09 10:52:49 -080068 ServiceDirectory testDirectory = new TestServiceDirectory()
69 .add(AclService.class, mockAclService)
Pengfei Lue0c02e22015-07-07 15:41:31 +080070 .add(AclStore.class, mockAclStore);
debmaitif6091982019-04-12 14:51:57 +053071 setServiceDirectory(testDirectory);
Ray Milkey094a1352018-01-22 14:03:54 -080072 TestUtils.setField(BaseResource.class, "services", testDirectory);
Ray Milkey06297ed2018-01-22 17:13:41 -080073 AclRule.idGenerator = new MockIdGenerator();
Pengfei Lue0c02e22015-07-07 15:41:31 +080074 }
75
76 @After
77 public void tearDown() {
78 verify(mockAclService);
79 }
80
81 /**
82 * Mock id generator for testing.
83 */
84 private class MockIdGenerator implements IdGenerator {
85 private AtomicLong nextId = new AtomicLong(0);
86
87 @Override
88 public long getNewId() {
89 return nextId.getAndIncrement();
90 }
91 }
92
93 @Test
debmaitif6091982019-04-12 14:51:57 +053094 public void addInvalidRule() {
Jian Li9d616492016-03-09 10:52:49 -080095 WebTarget wt = target();
Pengfei Lue0c02e22015-07-07 15:41:31 +080096 String response;
debmaitif6091982019-04-12 14:51:57 +053097 InputStream jsonStream;
Pengfei Lue0c02e22015-07-07 15:41:31 +080098
99 replay(mockAclService);
100
101 // input a invalid JSON string that contains neither nw_src and nw_dst
debmaitif6091982019-04-12 14:51:57 +0530102 jsonStream = AclWebResourceTest.class
103 .getResourceAsStream("post-invalid-acl.json");
104 response = wt.path("rules").request(MediaType.APPLICATION_JSON_TYPE)
105 .post(Entity.json(jsonStream), String.class);
106 assertThat(response.toString(), containsString("Either srcIp or dstIp must be assigned."));
107
Pengfei Lue0c02e22015-07-07 15:41:31 +0800108
109 // input a invalid JSON string that doesn't contain CIDR mask bits
debmaitif6091982019-04-12 14:51:57 +0530110 jsonStream = AclWebResourceTest.class
111 .getResourceAsStream("post-invalid-ip-1.json");
112 response = wt.path("rules").request(MediaType.APPLICATION_JSON_TYPE)
113 .post(Entity.json(jsonStream), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800114 assertThat(response, containsString("Malformed IPv4 prefix string: 10.0.0.1. " +
115 "Address must take form \"x.x.x.x/y\""));
116
117 // input a invalid JSON string that contains a invalid IP address
debmaitif6091982019-04-12 14:51:57 +0530118 jsonStream = AclWebResourceTest.class
119 .getResourceAsStream("post-invalid-ip-2.json");
120 response = wt.path("rules").request(MediaType.APPLICATION_JSON_TYPE)
121 .post(Entity.json(jsonStream), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800122 assertThat(response, containsString("Invalid IP address string: 10.0.0.256"));
123
124 // input a invalid JSON string that contains a invalid IP address
debmaitif6091982019-04-12 14:51:57 +0530125
126 jsonStream = AclWebResourceTest.class
127 .getResourceAsStream("post-invalid-ip-3.json");
128 response = wt.path("rules").request(MediaType.APPLICATION_JSON_TYPE)
129 .post(Entity.json(jsonStream), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800130 assertThat(response, containsString("Invalid IP address string: 10.0.01"));
131
132 // input a invalid JSON string that contains a invalid CIDR mask bits
debmaitif6091982019-04-12 14:51:57 +0530133 jsonStream = AclWebResourceTest.class
134 .getResourceAsStream("post-invalid-cidr-1.json");
135 response = wt.path("rules").request(MediaType.APPLICATION_JSON_TYPE)
136 .post(Entity.json(jsonStream), String.class);
137 assertThat(response, containsString("For input string: \"a\""));
Pengfei Lue0c02e22015-07-07 15:41:31 +0800138
139 // input a invalid JSON string that contains a invalid CIDR mask bits
debmaitif6091982019-04-12 14:51:57 +0530140 jsonStream = AclWebResourceTest.class
141 .getResourceAsStream("post-invalid-cidr-2.json");
142 response = wt.path("rules").request(MediaType.APPLICATION_JSON_TYPE)
143 .post(Entity.json(jsonStream), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800144 assertThat(response, containsString("Invalid prefix length 33. The value must be in the interval [0, 32]"));
145
146 // input a invalid JSON string that contains a invalid ipProto value
debmaitif6091982019-04-12 14:51:57 +0530147 jsonStream = AclWebResourceTest.class
148 .getResourceAsStream("post-invalid-proto.json");
149 response = wt.path("rules").request(MediaType.APPLICATION_JSON_TYPE)
150 .post(Entity.json(jsonStream), String.class);
151 assertThat(response, containsString("ipProto must be assigned to TCP, UDP, or ICMP"));
Pengfei Lue0c02e22015-07-07 15:41:31 +0800152
153 // input a invalid JSON string that contains a invalid action value
debmaitif6091982019-04-12 14:51:57 +0530154 jsonStream = AclWebResourceTest.class
155 .getResourceAsStream("post-invalid-action.json");
156 response = wt.path("rules").request(MediaType.APPLICATION_JSON_TYPE)
157 .post(Entity.json(jsonStream), String.class);
158 assertThat(response, containsString("action must be ALLOW or DENY"));
Pengfei Lue0c02e22015-07-07 15:41:31 +0800159 }
debmaitif6091982019-04-12 14:51:57 +0530160
161 @Test
162 public void addRule() {
163 mockAclService.addAclRule(anyObject());
164 expectLastCall().andReturn(true).anyTimes();
165 replay(mockAclService);
166
167 WebTarget wt = target();
168 InputStream jsonStream;
169
170 jsonStream = AclWebResourceTest.class
171 .getResourceAsStream("post-valid-acl.json");
172 Response response = wt.path("rules").request(MediaType.APPLICATION_JSON_TYPE)
173 .post(Entity.json(jsonStream));
174 assertEquals(response.getLocation().getPath(), "/0x0");
175
176 }
177
178
Pengfei Lue0c02e22015-07-07 15:41:31 +0800179}