|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import unittest |
| 19 | +import mock |
| 20 | +from configure import CsForwardingRules |
| 21 | + |
| 22 | + |
| 23 | +class TestCsForwardingRules(unittest.TestCase): |
| 24 | + |
| 25 | + def ftp_helper_rules(self, protocol, public_ports, internal_ports): |
| 26 | + forwarding = CsForwardingRules.__new__(CsForwardingRules) |
| 27 | + forwarding.fw = [] |
| 28 | + forwarding.forward_ftp_helper({"public_ip": "10.0.0.2", "internal_ip": "10.1.1.10", "protocol": protocol, |
| 29 | + "public_ports": public_ports, "internal_ports": internal_ports}) |
| 30 | + return forwarding.fw |
| 31 | + |
| 32 | + def test_ftp_helper_on_other_public_port(self): |
| 33 | + self.assertEqual(self.ftp_helper_rules("tcp", "2121:2121", "21:21"), |
| 34 | + [["raw", "", "-A PREROUTING -d 10.0.0.2/32 -p tcp -m tcp --dport 2121 -j CT --helper ftp"]]) |
| 35 | + |
| 36 | + def test_ftp_helper_on_port_range(self): |
| 37 | + self.assertEqual(self.ftp_helper_rules("tcp", "1020:1030", "20:30"), |
| 38 | + [["raw", "", "-A PREROUTING -d 10.0.0.2/32 -p tcp -m tcp --dport 1021 -j CT --helper ftp"]]) |
| 39 | + |
| 40 | + def test_ftp_helper_on_port_21(self): |
| 41 | + self.assertEqual(self.ftp_helper_rules("tcp", "21:21", "21:21"), |
| 42 | + [["raw", "", "-A PREROUTING -d 10.0.0.2/32 -p tcp -m tcp --dport 21 -j CT --helper ftp"]]) |
| 43 | + |
| 44 | + def test_no_ftp_helper(self): |
| 45 | + self.assertEqual(self.ftp_helper_rules("tcp", "2121:2121", "2121:2121"), []) |
| 46 | + self.assertEqual(self.ftp_helper_rules("udp", "2121:2121", "21:21"), []) |
| 47 | + self.assertEqual(self.ftp_helper_rules("tcp", "any", "any"), []) |
| 48 | + |
| 49 | + @mock.patch.object(CsForwardingRules, 'getStaticRoutes', return_value=[]) |
| 50 | + @mock.patch.object(CsForwardingRules, 'getPrivateGatewayNetworks', return_value=[]) |
| 51 | + @mock.patch.object(CsForwardingRules, 'getGuestIpByIp', return_value="10.1.1.1") |
| 52 | + @mock.patch.object(CsForwardingRules, 'getNetworkByIp', return_value="10.1.1.0/24") |
| 53 | + @mock.patch.object(CsForwardingRules, 'getDeviceByIp', return_value="eth2") |
| 54 | + def test_ftp_helper_on_static_nat(self, *_): |
| 55 | + forwarding = CsForwardingRules.__new__(CsForwardingRules) |
| 56 | + forwarding.fw = [] |
| 57 | + forwarding.processStaticNatRule({"public_ip": "10.0.0.2", "internal_ip": "10.1.1.10"}) |
| 58 | + self.assertIn(["raw", "", "-A PREROUTING -d 10.0.0.2/32 -p tcp -m tcp --dport 21 -j CT --helper ftp"], |
| 59 | + forwarding.fw) |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == '__main__': |
| 63 | + unittest.main() |
0 commit comments