blob: c554db6e1242809636eeb846380b5b2f66f2dbc4 [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
23import com.sun.jersey.api.client.WebResource;
Thomas Vachuska9bb32352015-09-25 11:31:22 -070024import com.sun.jersey.test.framework.AppDescriptor;
25import com.sun.jersey.test.framework.WebAppDescriptor;
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;
Thomas Vachuska9bb32352015-09-25 11:31:22 -070034import org.onosproject.rest.ResourceTest;
Pengfei Lue0c02e22015-07-07 15:41:31 +080035
36import java.io.IOException;
37import java.util.ArrayList;
38import java.util.List;
39import java.util.concurrent.atomic.AtomicLong;
40
41import static org.easymock.EasyMock.*;
42import static org.hamcrest.Matchers.containsString;
43import static org.junit.Assert.assertThat;
44
45/**
46 * Test class for ACL application REST resource.
47 */
48public class AclWebResourceTest extends ResourceTest {
49
50 final AclService mockAclService = createMock(AclService.class);
51 final AclStore mockAclStore = createMock(AclStore.class);
52 final List<AclRule> rules = new ArrayList<>();
53
54 @Before
55 public void setUp() {
56 expect(mockAclService.getAclRules()).andReturn(rules).anyTimes();
57 ServiceDirectory testDirectory = new TestServiceDirectory().add(AclService.class, mockAclService)
58 .add(AclStore.class, mockAclStore);
59 BaseResource.setServiceDirectory(testDirectory);
Thomas Vachuska9bb32352015-09-25 11:31:22 -070060
61 IdGenerator idGenerator = new MockIdGenerator();
62 AclRule.bindIdGenerator(idGenerator);
Pengfei Lue0c02e22015-07-07 15:41:31 +080063 }
64
65 @After
66 public void tearDown() {
67 verify(mockAclService);
68 }
69
70 /**
71 * Mock id generator for testing.
72 */
73 private class MockIdGenerator implements IdGenerator {
74 private AtomicLong nextId = new AtomicLong(0);
75
76 @Override
77 public long getNewId() {
78 return nextId.getAndIncrement();
79 }
80 }
81
Thomas Vachuska9bb32352015-09-25 11:31:22 -070082 @Override
83 public AppDescriptor configure() {
84 return new WebAppDescriptor.Builder("org.onosproject.acl").build();
85 }
86
Pengfei Lue0c02e22015-07-07 15:41:31 +080087 @Test
Thomas Vachuska9bb32352015-09-25 11:31:22 -070088 @Ignore("FIXME: This needs to get reworked")
89 public void addRule() throws IOException {
90 WebResource.Builder rs = resource().path("rules").header("Content-type", "application/json");
Pengfei Lue0c02e22015-07-07 15:41:31 +080091 String response;
92 String json;
Pengfei Lue0c02e22015-07-07 15:41:31 +080093
94 replay(mockAclService);
95
96 // input a invalid JSON string that contains neither nw_src and nw_dst
97 json = "{\"ipProto\":\"TCP\",\"dstTpPort\":\"80\"}";
Thomas Vachuska9bb32352015-09-25 11:31:22 -070098 response = rs.post(String.class, json);
Pengfei Lue0c02e22015-07-07 15:41:31 +080099 assertThat(response, containsString("Failed! Either srcIp or dstIp must be assigned."));
100
101 // input a invalid JSON string that doesn't contain CIDR mask bits
102 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.1\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700103 response = rs.post(String.class, json);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800104 assertThat(response, containsString("Malformed IPv4 prefix string: 10.0.0.1. " +
105 "Address must take form \"x.x.x.x/y\""));
106
107 // input a invalid JSON string that contains a invalid IP address
108 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.256/32\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700109 response = rs.post(String.class, json);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800110 assertThat(response, containsString("Invalid IP address string: 10.0.0.256"));
111
112 // input a invalid JSON string that contains a invalid IP address
113 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.01/32\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700114 response = rs.post(String.class, json);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800115 assertThat(response, containsString("Invalid IP address string: 10.0.01"));
116
117 // input a invalid JSON string that contains a invalid CIDR mask bits
118 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.1/a\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700119 response = rs.post(String.class, json);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800120 assertThat(response, containsString("Failed! For input string: \"a\""));
121
122 // input a invalid JSON string that contains a invalid CIDR mask bits
123 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.1/33\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700124 response = rs.post(String.class, json);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800125 assertThat(response, containsString("Invalid prefix length 33. The value must be in the interval [0, 32]"));
126
127 // input a invalid JSON string that contains a invalid ipProto value
128 json = "{\"ipProto\":\"ARP\",\"srcIp\":\"10.0.0.1/32\",\"dstTpPort\":\"80\",\"action\":\"DENY\"}";
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700129 response = rs.post(String.class, json);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800130 assertThat(response, containsString("ipProto must be assigned to TCP, UDP, or ICMP."));
131
132 // input a invalid JSON string that contains a invalid dstTpPort value
133 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.1/32\",\"dstTpPort\":\"a\",\"action\":\"DENY\"}";
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700134 response = rs.post(String.class, json);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800135 assertThat(response, containsString("dstTpPort must be assigned to a numerical value."));
136
137 // input a invalid JSON string that contains a invalid action value
138 json = "{\"ipProto\":\"TCP\",\"srcIp\":\"10.0.0.1/32\",\"dstTpPort\":\"80\",\"action\":\"PERMIT\"}";
Thomas Vachuska9bb32352015-09-25 11:31:22 -0700139 response = rs.post(String.class, json);
Pengfei Lue0c02e22015-07-07 15:41:31 +0800140 assertThat(response, containsString("action must be assigned to ALLOW or DENY."));
141 }
142}