|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +module CloudController |
| 4 | + RSpec.describe RuleValidator do |
| 5 | + describe '.validate_destination' do |
| 6 | + subject { described_class.validate_destination(destination) } |
| 7 | + |
| 8 | + before do |
| 9 | + TestConfig.override( |
| 10 | + enable_ipv6: true, |
| 11 | + security_groups: { enable_comma_delimited_destinations: false } |
| 12 | + ) |
| 13 | + end |
| 14 | + |
| 15 | + context 'with a single valid IPv4 address' do |
| 16 | + let(:destination) { '192.168.10.2' } |
| 17 | + |
| 18 | + it { is_expected.to be true } |
| 19 | + end |
| 20 | + |
| 21 | + context 'with a valid CIDR' do |
| 22 | + let(:destination) { '10.0.0.0/8' } |
| 23 | + |
| 24 | + it { is_expected.to be true } |
| 25 | + end |
| 26 | + |
| 27 | + context 'with a valid ascending range' do |
| 28 | + let(:destination) { '192.168.10.2-192.168.15.254' } |
| 29 | + |
| 30 | + it { is_expected.to be true } |
| 31 | + end |
| 32 | + |
| 33 | + context 'with an inverted range' do |
| 34 | + let(:destination) { '200.0.0.0-150.0.0.0' } |
| 35 | + |
| 36 | + it { is_expected.to be false } |
| 37 | + end |
| 38 | + |
| 39 | + context 'with a range whose second endpoint is a CIDR' do |
| 40 | + let(:destination) { '1.1.1.1-2.2.2.2/30' } |
| 41 | + |
| 42 | + it { is_expected.to be false } |
| 43 | + end |
| 44 | + |
| 45 | + context 'with a malformed address' do |
| 46 | + let(:destination) { '999.999.999.999' } |
| 47 | + |
| 48 | + it { is_expected.to be false } |
| 49 | + end |
| 50 | + |
| 51 | + # Zero-padded IPv4 octets are ambiguous (decimal vs. octal) and are rejected downstream by Diego and vxlan-policy-agent. |
| 52 | + context 'with leading zeros in an IPv4 address' do |
| 53 | + context 'in the first octet' do |
| 54 | + let(:destination) { '010.0.0.53' } |
| 55 | + |
| 56 | + it { is_expected.to be false } |
| 57 | + end |
| 58 | + |
| 59 | + context 'in every octet' do |
| 60 | + let(:destination) { '03.005.010.02' } |
| 61 | + |
| 62 | + it { is_expected.to be false } |
| 63 | + end |
| 64 | + |
| 65 | + context 'in a CIDR' do |
| 66 | + let(:destination) { '010.000.000.000/24' } |
| 67 | + |
| 68 | + it { is_expected.to be false } |
| 69 | + end |
| 70 | + |
| 71 | + context 'in a range endpoint' do |
| 72 | + let(:destination) { '010.0.0.1-10.0.0.9' } |
| 73 | + |
| 74 | + it { is_expected.to be false } |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + context 'with a valid IPv6 address' do |
| 79 | + let(:destination) { '2001:db8::1' } |
| 80 | + |
| 81 | + it { is_expected.to be true } |
| 82 | + end |
| 83 | + |
| 84 | + # Zero-padded IPv6 hextets are valid (RFC 4291); exhaustive coverage lives in the v3 spec (#4367). |
| 85 | + context 'with a zero-padded IPv6 segment' do |
| 86 | + let(:destination) { '2001:0db8::1' } |
| 87 | + |
| 88 | + it { is_expected.to be true } |
| 89 | + end |
| 90 | + |
| 91 | + # A zero-padded embedded octet carries the same decimal/octal ambiguity as a bare IPv4 address. |
| 92 | + context 'with an IPv4-mapped IPv6 address' do |
| 93 | + context 'with a plain embedded octet' do |
| 94 | + let(:destination) { '::ffff:10.0.0.1' } |
| 95 | + |
| 96 | + it { is_expected.to be true } |
| 97 | + end |
| 98 | + |
| 99 | + context 'with a zero-padded embedded octet' do |
| 100 | + let(:destination) { '::ffff:010.0.0.1' } |
| 101 | + |
| 102 | + it { is_expected.to be false } |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + context 'with a valid IPv6 range' do |
| 107 | + let(:destination) { '2001:db8::1-2001:db8::ff' } |
| 108 | + |
| 109 | + it { is_expected.to be true } |
| 110 | + end |
| 111 | + |
| 112 | + context 'with an inverted IPv6 range' do |
| 113 | + let(:destination) { '2001:db8::ff-2001:db8::1' } |
| 114 | + |
| 115 | + it { is_expected.to be false } |
| 116 | + end |
| 117 | + end |
| 118 | + end |
| 119 | +end |
0 commit comments