blob: 34765a43e9ffdbe8bc509390166753b06cd7492e [file] [log] [blame]
Pengfei Lue0c02e22015-07-07 15:41:31 +08001/*
2 * Copyright 2015 Open Networking Laboratory
3 * Originally created by Pengfei Lu, Network and Cloud Computing Laboratory, Dalian University of Technology, China
4 * Advisers: Keqiu Li and Heng Qi
5 * This work is supported by the State Key Program of National Natural Science of China(Grant No. 61432002)
6 * and Prospective Research Project on Future Networks in Jiangsu Future Networks Innovation Institute.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
Thomas Vachuska9bb32352015-09-25 11:31:22 -070021package org.onosproject.acl;
Pengfei Lue0c02e22015-07-07 15:41:31 +080022
Jian Li9d616492016-03-09 10:52:49 -080023import org.glassfish.jersey.server.ResourceConfig;
24import org.glassfish.jersey.test.JerseyTest;
Pengfei Lue0c02e22015-07-07 15:41:31 +080025import org.junit.After;
26import org.junit.Before;
Thomas Vachuska9bb32352015-09-25 11:31:22 -070027import org.junit.Ignore;
Pengfei Lue0c02e22015-07-07 15:41:31 +080028import org.junit.Test;
29import 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;
33
Jian Li9d616492016-03-09 10:52:49 -080034import javax.ws.rs.client.Entity;
35import javax.ws.rs.client.WebTarget;
Pengfei Lue0c02e22015-07-07 15:41:31 +080036import java.io.IOException;
37import java.util.ArrayList;
38import java.util.List;
39import java.util.concurrent.atomic.AtomicLong;
40
Jian Li9d616492016-03-09 10:52:49 -080041import static org.easymock.EasyMock.createMock;
42import static org.easymock.EasyMock.expect;
43import static org.easymock.EasyMock.replay;
44import static org.easymock.EasyMock.verify;
Pengfei Lue0c02e22015-07-07 15:41:31 +080045import static org.hamcrest.Matchers.containsString;
46import static org.junit.Assert.assertThat;
47
48/**
49 * Test class for ACL application REST resource.
50 */
Jian Li9d616492016-03-09 10:52:49 -080051public class AclWebResourceTest extends JerseyTest {
Pengfei Lue0c02e22015-07-07 15:41:31 +080052
53 final AclService mockAclService = createMock(AclService.class);
54 final AclStore mockAclStore = createMock(AclStore.class);
55 final List<AclRule> rules = new ArrayList<>();
56
Jian Li9d616492016-03-09 10:52:49 -080057 /**
58 * Constructs a control metrics collector resource test instance.
59 */
60 public AclWebResourceTest() {
61 super(ResourceConfig.forApplicationClass(AclWebApplication.class));
62 }
63
Pengfei Lue0c02e22015-07-07 15:41:31 +080064 @Before
Jian Li9d616492016-03-09 10:52:49 -080065 public void setUpMock() {
Pengfei Lue0c02e22015-07-07 15:41:31 +080066 expect(mockAclService.getAclRules()).andReturn(rules).anyTimes();
Jian Li9d616492016-03-09 10:52:49 -080067 ServiceDirectory testDirectory = new TestServiceDirectory()
68 .add(AclService.class, mockAclService)
Pengfei Lue0c02e22015-07-07 15:41:31 +080069 .add(AclStore.class, mockAclStore);
70 BaseResource.setServiceDirectory(testDirectory);
Thomas Vachuska9bb32352015-09-25 11:31:22 -070071
72 IdGenerator idGenerator = new MockIdGenerator();
73 AclRule.bindIdGenerator(idGenerator);
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
Thomas Vachuska9bb32352015-09-25 11:31:22 -070094 @Ignore("FIXME: This needs to get reworked")
95 public void addRule() throws IOException {
Jian Li9d616492016-03-09 10:52:49 -080096 WebTarget wt = target();
Pengfei Lue0c02e22015-07-07 15:41:31 +080097 String response;
98 String json;
Pengfei Lue0c02e22015-07-07 15:41:31 +080099
100 replay(mockAclService);
101
Jian Li9d616492016-03-09 10:52:49 -0800102 // FIXME: following code snippet requires refactoring by extracting
103 // json string as a separated file
104
Pengfei Lue0c02e22015-07-07 15:41:31 +0800105 // input a invalid JSON string that contains neither nw_src and nw_dst
106 json = "{\"ipProto\":\"TCP\",\"dstTpPort\":\"80\"}";
Jian Li9d616492016-03-09 10:52:49 -0800107 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800108 assertThat(response, containsString("Failed! Either srcIp or dstIp must be assigned."));
109
110 // input a invalid JSON string that doesn't contain CIDR mask bits
111 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.1\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Jian Li9d616492016-03-09 10:52:49 -0800112 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800113 assertThat(response, containsString("Malformed IPv4 prefix string: 10.0.0.1. " +
114 "Address must take form \"x.x.x.x/y\""));
115
116 // input a invalid JSON string that contains a invalid IP address
117 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.256/32\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Jian Li9d616492016-03-09 10:52:49 -0800118 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800119 assertThat(response, containsString("Invalid IP address string: 10.0.0.256"));
120
121 // input a invalid JSON string that contains a invalid IP address
122 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.01/32\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Jian Li9d616492016-03-09 10:52:49 -0800123 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800124 assertThat(response, containsString("Invalid IP address string: 10.0.01"));
125
126 // input a invalid JSON string that contains a invalid CIDR mask bits
127 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.1/a\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Jian Li9d616492016-03-09 10:52:49 -0800128 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800129 assertThat(response, containsString("Failed! For input string: \"a\""));
130
131 // input a invalid JSON string that contains a invalid CIDR mask bits
132 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.1/33\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Jian Li9d616492016-03-09 10:52:49 -0800133 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800134 assertThat(response, containsString("Invalid prefix length 33. The value must be in the interval [0, 32]"));
135
136 // input a invalid JSON string that contains a invalid ipProto value
137 json = "{\"ipProto\":\"ARP\",\"srcIp\":\"10.0.0.1/32\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Jian Li9d616492016-03-09 10:52:49 -0800138 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800139 assertThat(response, containsString("ipProto must be assigned to TCP, UDP, or ICMP."));
140
141 // input a invalid JSON string that contains a invalid dstTpPort value
142 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.1/32\",\"dstTpPort\":\"a\",\"action\":\"DENY\"}";
Jian Li9d616492016-03-09 10:52:49 -0800143 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800144 assertThat(response, containsString("dstTpPort must be assigned to a numerical value."));
145
146 // input a invalid JSON string that contains a invalid action value
147 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.1/32\",\"dstTpPort\":\"80\",\"action\":\"PERMIT\"}";
Jian Li9d616492016-03-09 10:52:49 -0800148 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800149 assertThat(response, containsString("action must be assigned to ALLOW or DENY."));
150 }
151}