Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 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 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 23 | import org.glassfish.jersey.server.ResourceConfig; |
| 24 | import org.glassfish.jersey.test.JerseyTest; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 27 | import org.junit.Ignore; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 28 | import org.junit.Test; |
| 29 | import org.onlab.osgi.ServiceDirectory; |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 30 | import org.onlab.osgi.TestServiceDirectory; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 31 | import org.onlab.rest.BaseResource; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 32 | import org.onosproject.core.IdGenerator; |
| 33 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 34 | import javax.ws.rs.client.Entity; |
| 35 | import javax.ws.rs.client.WebTarget; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 36 | import java.io.IOException; |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.List; |
| 39 | import java.util.concurrent.atomic.AtomicLong; |
| 40 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 41 | import static org.easymock.EasyMock.createMock; |
| 42 | import static org.easymock.EasyMock.expect; |
| 43 | import static org.easymock.EasyMock.replay; |
| 44 | import static org.easymock.EasyMock.verify; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 45 | import static org.hamcrest.Matchers.containsString; |
| 46 | import static org.junit.Assert.assertThat; |
| 47 | |
| 48 | /** |
| 49 | * Test class for ACL application REST resource. |
| 50 | */ |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 51 | public class AclWebResourceTest extends JerseyTest { |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 52 | |
| 53 | final AclService mockAclService = createMock(AclService.class); |
| 54 | final AclStore mockAclStore = createMock(AclStore.class); |
| 55 | final List<AclRule> rules = new ArrayList<>(); |
| 56 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 57 | /** |
| 58 | * Constructs a control metrics collector resource test instance. |
| 59 | */ |
| 60 | public AclWebResourceTest() { |
| 61 | super(ResourceConfig.forApplicationClass(AclWebApplication.class)); |
| 62 | } |
| 63 | |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 64 | @Before |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 65 | public void setUpMock() { |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 66 | expect(mockAclService.getAclRules()).andReturn(rules).anyTimes(); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 67 | ServiceDirectory testDirectory = new TestServiceDirectory() |
| 68 | .add(AclService.class, mockAclService) |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 69 | .add(AclStore.class, mockAclStore); |
| 70 | BaseResource.setServiceDirectory(testDirectory); |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 71 | |
| 72 | IdGenerator idGenerator = new MockIdGenerator(); |
| 73 | AclRule.bindIdGenerator(idGenerator); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 74 | } |
| 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 Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 94 | @Ignore("FIXME: This needs to get reworked") |
| 95 | public void addRule() throws IOException { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 96 | WebTarget wt = target(); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 97 | String response; |
| 98 | String json; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 99 | |
| 100 | replay(mockAclService); |
| 101 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 102 | // FIXME: following code snippet requires refactoring by extracting |
| 103 | // json string as a separated file |
| 104 | |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 105 | // input a invalid JSON string that contains neither nw_src and nw_dst |
| 106 | json = "{\"ipProto\":\"TCP\",\"dstTpPort\":\"80\"}"; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 107 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 108 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 112 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 113 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 118 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 119 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 123 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 124 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 128 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 129 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 133 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 134 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 138 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 139 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 143 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 144 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 148 | response = wt.request().post(Entity.json(json), String.class); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 149 | assertThat(response, containsString("action must be assigned to ALLOW or DENY.")); |
| 150 | } |
| 151 | } |