Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 3 | * |
| 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 Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 15 | * |
| 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 Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 20 | */ |
| 21 | |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 22 | package org.onosproject.acl; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 23 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 24 | import org.glassfish.jersey.server.ResourceConfig; |
| 25 | import org.glassfish.jersey.test.JerseyTest; |
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; |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame] | 30 | import org.onlab.junit.TestUtils; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 31 | import org.onlab.osgi.ServiceDirectory; |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 32 | import org.onlab.osgi.TestServiceDirectory; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 33 | import org.onlab.rest.BaseResource; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 34 | import org.onosproject.core.IdGenerator; |
| 35 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 36 | import javax.ws.rs.client.Entity; |
| 37 | import javax.ws.rs.client.WebTarget; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 38 | import java.io.IOException; |
| 39 | import java.util.ArrayList; |
| 40 | import java.util.List; |
| 41 | import java.util.concurrent.atomic.AtomicLong; |
| 42 | |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 43 | import static org.easymock.EasyMock.*; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 44 | import static org.hamcrest.Matchers.containsString; |
| 45 | import static org.junit.Assert.assertThat; |
| 46 | |
| 47 | /** |
| 48 | * Test class for ACL application REST resource. |
| 49 | */ |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 50 | public class AclWebResourceTest extends JerseyTest { |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 51 | |
| 52 | final AclService mockAclService = createMock(AclService.class); |
| 53 | final AclStore mockAclStore = createMock(AclStore.class); |
| 54 | final List<AclRule> rules = new ArrayList<>(); |
| 55 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 56 | /** |
| 57 | * Constructs a control metrics collector resource test instance. |
| 58 | */ |
| 59 | public AclWebResourceTest() { |
| 60 | super(ResourceConfig.forApplicationClass(AclWebApplication.class)); |
| 61 | } |
| 62 | |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 63 | @Before |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 64 | public void setUpMock() { |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 65 | expect(mockAclService.getAclRules()).andReturn(rules).anyTimes(); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 66 | ServiceDirectory testDirectory = new TestServiceDirectory() |
| 67 | .add(AclService.class, mockAclService) |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 68 | .add(AclStore.class, mockAclStore); |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame] | 69 | TestUtils.setField(BaseResource.class, "services", testDirectory); |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 70 | |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 71 | AclRule.idGenerator = new MockIdGenerator(); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 72 | } |
| 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 Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 92 | @Ignore("FIXME: This needs to get reworked") |
| 93 | public void addRule() throws IOException { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 94 | WebTarget wt = target(); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 95 | String response; |
| 96 | String json; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 97 | |
| 98 | replay(mockAclService); |
| 99 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 100 | // FIXME: following code snippet requires refactoring by extracting |
| 101 | // json string as a separated file |
| 102 | |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 103 | // input a invalid JSON string that contains neither nw_src and nw_dst |
| 104 | json = "{\"ipProto\":\"TCP\",\"dstTpPort\":\"80\"}"; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 105 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 106 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 110 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 111 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 116 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 117 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 121 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 122 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 126 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 127 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 131 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 132 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 136 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 137 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 141 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 142 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 146 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 147 | assertThat(response, containsString("action must be assigned to ALLOW or DENY.")); |
| 148 | } |
| 149 | } |