Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 1 | /* |
| 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 Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 21 | package org.onosproject.acl; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 22 | |
| 23 | import com.sun.jersey.api.client.WebResource; |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 24 | import com.sun.jersey.test.framework.AppDescriptor; |
| 25 | import com.sun.jersey.test.framework.WebAppDescriptor; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 26 | import org.junit.After; |
| 27 | import org.junit.Before; |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 28 | import org.junit.Ignore; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 29 | import org.junit.Test; |
| 30 | import org.onlab.osgi.ServiceDirectory; |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 31 | import org.onlab.osgi.TestServiceDirectory; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 32 | import org.onlab.rest.BaseResource; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 33 | import org.onosproject.core.IdGenerator; |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 34 | import org.onosproject.rest.ResourceTest; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 35 | |
| 36 | import java.io.IOException; |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.List; |
| 39 | import java.util.concurrent.atomic.AtomicLong; |
| 40 | |
| 41 | import static org.easymock.EasyMock.*; |
| 42 | import static org.hamcrest.Matchers.containsString; |
| 43 | import static org.junit.Assert.assertThat; |
| 44 | |
| 45 | /** |
| 46 | * Test class for ACL application REST resource. |
| 47 | */ |
| 48 | public 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 Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 60 | |
| 61 | IdGenerator idGenerator = new MockIdGenerator(); |
| 62 | AclRule.bindIdGenerator(idGenerator); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 63 | } |
| 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 Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 82 | @Override |
| 83 | public AppDescriptor configure() { |
| 84 | return new WebAppDescriptor.Builder("org.onosproject.acl").build(); |
| 85 | } |
| 86 | |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 87 | @Test |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 88 | @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 Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 91 | String response; |
| 92 | String json; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 93 | |
| 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 Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 98 | response = rs.post(String.class, json); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 99 | 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 Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 103 | response = rs.post(String.class, json); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 104 | 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 Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 109 | response = rs.post(String.class, json); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 110 | 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 Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 114 | response = rs.post(String.class, json); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 115 | 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 Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 119 | response = rs.post(String.class, json); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 120 | 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 Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 124 | response = rs.post(String.class, json); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 125 | 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 Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 129 | response = rs.post(String.class, json); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 130 | 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 Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 134 | response = rs.post(String.class, json); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 135 | 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 Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 139 | response = rs.post(String.class, json); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 140 | assertThat(response, containsString("action must be assigned to ALLOW or DENY.")); |
| 141 | } |
| 142 | } |