sigs.k8s.io/external-dns@v0.14.1/pkg/rfc2317/arpa_test.go (about)

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     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.
    15  */
    16  
    17  package rfc2317
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  )
    23  
    24  func TestCidrToInAddr(t *testing.T) {
    25  	var tests = []struct {
    26  		in     string
    27  		out    string
    28  		errmsg string
    29  	}{
    30  
    31  		{"174.136.107.0/24", "107.136.174.in-addr.arpa", ""},
    32  		{"174.136.107.1/24", "107.136.174.in-addr.arpa", "CIDR 174.136.107.1/24 has 1 bits beyond the mask"},
    33  
    34  		{"174.136.0.0/16", "136.174.in-addr.arpa", ""},
    35  		{"174.136.43.0/16", "136.174.in-addr.arpa", "CIDR 174.136.43.0/16 has 1 bits beyond the mask"},
    36  
    37  		{"174.0.0.0/8", "174.in-addr.arpa", ""},
    38  		{"174.136.43.0/8", "174.in-addr.arpa", "CIDR 174.136.43.0/8 has 1 bits beyond the mask"},
    39  		{"174.136.0.44/8", "174.in-addr.arpa", "CIDR 174.136.0.44/8 has 1 bits beyond the mask"},
    40  		{"174.136.45.45/8", "174.in-addr.arpa", "CIDR 174.136.45.45/8 has 1 bits beyond the mask"},
    41  
    42  		{"2001::/16", "1.0.0.2.ip6.arpa", ""},
    43  		{"2001:0db8:0123:4567:89ab:cdef:1234:5670/124", "7.6.5.4.3.2.1.f.e.d.c.b.a.9.8.7.6.5.4.3.2.1.0.8.b.d.0.1.0.0.2.ip6.arpa", ""},
    44  
    45  		{"174.136.107.14/32", "14.107.136.174.in-addr.arpa", ""},
    46  		{"2001:0db8:0123:4567:89ab:cdef:1234:5678/128", "8.7.6.5.4.3.2.1.f.e.d.c.b.a.9.8.7.6.5.4.3.2.1.0.8.b.d.0.1.0.0.2.ip6.arpa", ""},
    47  
    48  		// IPv4 "Classless in-addr.arpa delegation" RFC2317.
    49  		// From examples in the RFC:
    50  		{"192.0.2.0/25", "0/25.2.0.192.in-addr.arpa", ""},
    51  		{"192.0.2.128/26", "128/26.2.0.192.in-addr.arpa", ""},
    52  		{"192.0.2.192/26", "192/26.2.0.192.in-addr.arpa", ""},
    53  		// All the base cases:
    54  		{"174.1.0.0/25", "0/25.0.1.174.in-addr.arpa", ""},
    55  		{"174.1.0.0/26", "0/26.0.1.174.in-addr.arpa", ""},
    56  		{"174.1.0.0/27", "0/27.0.1.174.in-addr.arpa", ""},
    57  		{"174.1.0.0/28", "0/28.0.1.174.in-addr.arpa", ""},
    58  		{"174.1.0.0/29", "0/29.0.1.174.in-addr.arpa", ""},
    59  		{"174.1.0.0/30", "0/30.0.1.174.in-addr.arpa", ""},
    60  		{"174.1.0.0/31", "0/31.0.1.174.in-addr.arpa", ""},
    61  		// /25 (all cases)
    62  		{"174.1.0.0/25", "0/25.0.1.174.in-addr.arpa", ""},
    63  		{"174.1.0.128/25", "128/25.0.1.174.in-addr.arpa", ""},
    64  		// /26 (all cases)
    65  		{"174.1.0.0/26", "0/26.0.1.174.in-addr.arpa", ""},
    66  		{"174.1.0.64/26", "64/26.0.1.174.in-addr.arpa", ""},
    67  		{"174.1.0.128/26", "128/26.0.1.174.in-addr.arpa", ""},
    68  		{"174.1.0.192/26", "192/26.0.1.174.in-addr.arpa", ""},
    69  		// /27 (all cases)
    70  		{"174.1.0.0/27", "0/27.0.1.174.in-addr.arpa", ""},
    71  		{"174.1.0.32/27", "32/27.0.1.174.in-addr.arpa", ""},
    72  		{"174.1.0.64/27", "64/27.0.1.174.in-addr.arpa", ""},
    73  		{"174.1.0.96/27", "96/27.0.1.174.in-addr.arpa", ""},
    74  		{"174.1.0.128/27", "128/27.0.1.174.in-addr.arpa", ""},
    75  		{"174.1.0.160/27", "160/27.0.1.174.in-addr.arpa", ""},
    76  		{"174.1.0.192/27", "192/27.0.1.174.in-addr.arpa", ""},
    77  		{"174.1.0.224/27", "224/27.0.1.174.in-addr.arpa", ""},
    78  		// /28 (first 2, last 2)
    79  		{"174.1.0.0/28", "0/28.0.1.174.in-addr.arpa", ""},
    80  		{"174.1.0.16/28", "16/28.0.1.174.in-addr.arpa", ""},
    81  		{"174.1.0.224/28", "224/28.0.1.174.in-addr.arpa", ""},
    82  		{"174.1.0.240/28", "240/28.0.1.174.in-addr.arpa", ""},
    83  		// /29 (first 2 cases)
    84  		{"174.1.0.0/29", "0/29.0.1.174.in-addr.arpa", ""},
    85  		{"174.1.0.8/29", "8/29.0.1.174.in-addr.arpa", ""},
    86  		// /30 (first 2 cases)
    87  		{"174.1.0.0/30", "0/30.0.1.174.in-addr.arpa", ""},
    88  		{"174.1.0.4/30", "4/30.0.1.174.in-addr.arpa", ""},
    89  		// /31 (first 2 cases)
    90  		{"174.1.0.0/31", "0/31.0.1.174.in-addr.arpa", ""},
    91  		{"174.1.0.2/31", "2/31.0.1.174.in-addr.arpa", ""},
    92  
    93  		// IPv4-mapped IPv6 addresses:
    94  		{"::ffff:174.136.107.15", "15.107.136.174.in-addr.arpa", ""},
    95  
    96  		// Error Cases:
    97  		{"0.0.0.0/0", "", "cannot use /0 in reverse CIDR"},
    98  		{"2001::/0", "", "CIDR 2001::/0 has 1 bits beyond the mask"},
    99  		{"4.5/16", "", "invalid CIDR address: 4.5/16"},
   100  		{"foo.com", "", "invalid CIDR address: foo.com"},
   101  	}
   102  	for i, tst := range tests {
   103  		t.Run(fmt.Sprintf("%d--%s", i, tst.in), func(t *testing.T) {
   104  			d, err := CidrToInAddr(tst.in)
   105  
   106  			if tst.errmsg == "" {
   107  				// We DO NOT expect an error.
   108  				if err != nil {
   109  					// ...but we got one.
   110  					t.Errorf("Expected '%s' but got ERROR('%s')", tst.out, err)
   111  				} else if (tst.errmsg == "") && d != tst.out {
   112  					// but the expected output was wrong
   113  					t.Errorf("Expected '%s' but got '%s'", tst.out, d)
   114  				}
   115  			} else {
   116  				// We DO expect an error.
   117  				if err == nil {
   118  					// ...but we didn't get one.
   119  					t.Errorf("Expected ERROR('%s') but got result '%s'", tst.errmsg, d)
   120  				} else if err.Error() != tst.errmsg {
   121  					// ...but not the right error.
   122  					t.Errorf("Expected ERROR('%s') but got ERROR('%s')", tst.errmsg, err)
   123  				}
   124  
   125  			}
   126  		})
   127  	}
   128  }