blob: 13c8201217065cefbcfae48281de93d77abd2845 [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;
25import org.glassfish.jersey.test.JerseyTest;
Pengfei Lue0c02e22015-07-07 15:41:31 +080026import org.junit.After;
27import org.junit.Before;
Thomas Vachuska9bb32352015-09-25 11:31:22 -070028import org.junit.Ignore;
Pengfei Lue0c02e22015-07-07 15:41:31 +080029import org.junit.Test;
30import org.onlab.osgi.ServiceDirectory;
Thomas Vachuska9bb32352015-09-25 11:31:22 -070031import org.onlab.osgi.TestServiceDirectory;
Pengfei Lue0c02e22015-07-07 15:41:31 +080032import org.onlab.rest.BaseResource;
Pengfei Lue0c02e22015-07-07 15:41:31 +080033import org.onosproject.core.IdGenerator;
34
Jian Li9d616492016-03-09 10:52:49 -080035import javax.ws.rs.client.Entity;
36import javax.ws.rs.client.WebTarget;
Pengfei Lue0c02e22015-07-07 15:41:31 +080037import java.io.IOException;
38import java.util.ArrayList;
39import java.util.List;
40import java.util.concurrent.atomic.AtomicLong;
41
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070042import static org.easymock.EasyMock.*;
Pengfei Lue0c02e22015-07-07 15:41:31 +080043import static org.hamcrest.Matchers.containsString;
44import static org.junit.Assert.assertThat;
45
46/**
47 * Test class for ACL application REST resource.
48 */
Jian Li9d616492016-03-09 10:52:49 -080049public class AclWebResourceTest extends JerseyTest {
Pengfei Lue0c02e22015-07-07 15:41:31 +080050
51 final AclService mockAclService = createMock(AclService.class);
52 final AclStore mockAclStore = createMock(AclStore.class);
53 final List<AclRule> rules = new ArrayList<>();
54
Jian Li9d616492016-03-09 10:52:49 -080055 /**
56 * Constructs a control metrics collector resource test instance.
57 */
58 public AclWebResourceTest() {
59 super(ResourceConfig.forApplicationClass(AclWebApplication.class));
60 }
61
Pengfei Lue0c02e22015-07-07 15:41:31 +080062 @Before
Jian Li9d616492016-03-09 10:52:49 -080063 public void setUpMock() {
Pengfei Lue0c02e22015-07-07 15:41:31 +080064 expect(mockAclService.getAclRules()).andReturn(rules).anyTimes();
Jian Li9d616492016-03-09 10:52:49 -080065 ServiceDirectory testDirectory = new TestServiceDirectory()
66 .add(AclService.class, mockAclService)
Pengfei Lue0c02e22015-07-07 15:41:31 +080067 .add(AclStore.class, mockAclStore);
68 BaseResource.setServiceDirectory(testDirectory);
Thomas Vachuska9bb32352015-09-25 11:31:22 -070069
70 IdGenerator idGenerator = new MockIdGenerator();
71 AclRule.bindIdGenerator(idGenerator);
Pengfei Lue0c02e22015-07-07 15:41:31 +080072 }
73
74 @After
75 public void tearDown() {
76 verify(mockAclService);
77 }
78
79 /**
80 * Mock id generator for testing.
81 */
82 private class MockIdGenerator implements IdGenerator {
83 private AtomicLong nextId = new AtomicLong(0);
84
85 @Override
86 public long getNewId() {
87 return nextId.getAndIncrement();
88 }
89 }
90
91 @Test
Thomas Vachuska9bb32352015-09-25 11:31:22 -070092 @Ignore("FIXME: This needs to get reworked")
93 public void addRule() throws IOException {
Jian Li9d616492016-03-09 10:52:49 -080094 WebTarget wt = target();
Pengfei Lue0c02e22015-07-07 15:41:31 +080095 String response;
96 String json;
Pengfei Lue0c02e22015-07-07 15:41:31 +080097
98 replay(mockAclService);
99
Jian Li9d616492016-03-09 10:52:49 -0800100 // FIXME: following code snippet requires refactoring by extracting
101 // json string as a separated file
102
Pengfei Lue0c02e22015-07-07 15:41:31 +0800103 // input a invalid JSON string that contains neither nw_src and nw_dst
104 json = "{\"ipProto\":\"TCP\",\"dstTpPort\":\"80\"}";
Jian Li9d616492016-03-09 10:52:49 -0800105 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800106 assertThat(response, containsString("Failed! Either srcIp or dstIp must be assigned."));
107
108 // input a invalid JSON string that doesn't contain CIDR mask bits
109 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.1\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Jian Li9d616492016-03-09 10:52:49 -0800110 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800111 assertThat(response, containsString("Malformed IPv4 prefix string: 10.0.0.1. " +
112 "Address must take form \"x.x.x.x/y\""));
113
114 // input a invalid JSON string that contains a invalid IP address
115 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.256/32\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Jian Li9d616492016-03-09 10:52:49 -0800116 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800117 assertThat(response, containsString("Invalid IP address string: 10.0.0.256"));
118
119 // input a invalid JSON string that contains a invalid IP address
120 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.01/32\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Jian Li9d616492016-03-09 10:52:49 -0800121 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800122 assertThat(response, containsString("Invalid IP address string: 10.0.01"));
123
124 // input a invalid JSON string that contains a invalid CIDR mask bits
125 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.1/a\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Jian Li9d616492016-03-09 10:52:49 -0800126 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800127 assertThat(response, containsString("Failed! For input string: \"a\""));
128
129 // input a invalid JSON string that contains a invalid CIDR mask bits
130 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.1/33\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Jian Li9d616492016-03-09 10:52:49 -0800131 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800132 assertThat(response, containsString("Invalid prefix length 33. The value must be in the interval [0, 32]"));
133
134 // input a invalid JSON string that contains a invalid ipProto value
135 json = "{\"ipProto\":\"ARP\",\"srcIp\":\"10.0.0.1/32\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Jian Li9d616492016-03-09 10:52:49 -0800136 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800137 assertThat(response, containsString("ipProto must be assigned to TCP, UDP, or ICMP."));
138
139 // input a invalid JSON string that contains a invalid dstTpPort value
140 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.1/32\",\"dstTpPort\":\"a\",\"action\":\"DENY\"}";
Jian Li9d616492016-03-09 10:52:49 -0800141 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800142 assertThat(response, containsString("dstTpPort must be assigned to a numerical value."));
143
144 // input a invalid JSON string that contains a invalid action value
145 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.1/32\",\"dstTpPort\":\"80\",\"action\":\"PERMIT\"}";
Jian Li9d616492016-03-09 10:52:49 -0800146 response = wt.request().post(Entity.json(json), String.class);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800147 assertThat(response, containsString("action must be assigned to ALLOW or DENY."));
148 }
149}