github.com/JFJun/bsc@v1.0.0/cmd/devp2p/dns_route53_test.go (about)

     1  // Copyright 2020 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/aws/aws-sdk-go/service/route53"
    24  )
    25  
    26  // This test checks that computeChanges/splitChanges create DNS changes in
    27  // leaf-added -> root-changed -> leaf-deleted order.
    28  func TestRoute53ChangeSort(t *testing.T) {
    29  	testTree0 := map[string]recordSet{
    30  		"2kfjogvxdqtxxugbh7gs7naaai.n": {ttl: 3333, values: []string{
    31  			`"enr:-HW4QO1ml1DdXLeZLsUxewnthhUy8eROqkDyoMTyavfks9JlYQIlMFEUoM78PovJDPQrAkrb3LRJ-""vtrymDguKCOIAWAgmlkgnY0iXNlY3AyNTZrMaEDffaGfJzgGhUif1JqFruZlYmA31HzathLSWxfbq_QoQ4"`,
    32  		}},
    33  		"fdxn3sn67na5dka4j2gok7bvqi.n": {ttl: treeNodeTTL, values: []string{`"enrtree-branch:"`}},
    34  		"n":                            {ttl: rootTTL, values: []string{`"enrtree-root:v1 e=2KFJOGVXDQTXXUGBH7GS7NAAAI l=FDXN3SN67NA5DKA4J2GOK7BVQI seq=0 sig=v_-J_q_9ICQg5ztExFvLQhDBGMb0lZPJLhe3ts9LAcgqhOhtT3YFJsl8BWNDSwGtamUdR-9xl88_w-X42SVpjwE"`}},
    35  	}
    36  
    37  	testTree1 := map[string]string{
    38  		"n":                            "enrtree-root:v1 e=JWXYDBPXYWG6FX3GMDIBFA6CJ4 l=C7HRFPF3BLGF3YR4DY5KX3SMBE seq=1 sig=o908WmNp7LibOfPsr4btQwatZJ5URBr2ZAuxvK4UWHlsB9sUOTJQaGAlLPVAhM__XJesCHxLISo94z5Z2a463gA",
    39  		"C7HRFPF3BLGF3YR4DY5KX3SMBE.n": "enrtree://AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YYHIUV7BVDQ5FDPRT2@morenodes.example.org",
    40  		"JWXYDBPXYWG6FX3GMDIBFA6CJ4.n": "enrtree-branch:2XS2367YHAXJFGLZHVAWLQD4ZY,H4FHT4B454P6UXFD7JCYQ5PWDY,MHTDO6TMUBRIA2XWG5LUDACK24",
    41  		"2XS2367YHAXJFGLZHVAWLQD4ZY.n": "enr:-HW4QOFzoVLaFJnNhbgMoDXPnOvcdVuj7pDpqRvh6BRDO68aVi5ZcjB3vzQRZH2IcLBGHzo8uUN3snqmgTiE56CH3AMBgmlkgnY0iXNlY3AyNTZrMaECC2_24YYkYHEgdzxlSNKQEnHhuNAbNlMlWJxrJxbAFvA",
    42  		"H4FHT4B454P6UXFD7JCYQ5PWDY.n": "enr:-HW4QAggRauloj2SDLtIHN1XBkvhFZ1vtf1raYQp9TBW2RD5EEawDzbtSmlXUfnaHcvwOizhVYLtr7e6vw7NAf6mTuoCgmlkgnY0iXNlY3AyNTZrMaECjrXI8TLNXU0f8cthpAMxEshUyQlK-AM0PW2wfrnacNI",
    43  		"MHTDO6TMUBRIA2XWG5LUDACK24.n": "enr:-HW4QLAYqmrwllBEnzWWs7I5Ev2IAs7x_dZlbYdRdMUx5EyKHDXp7AV5CkuPGUPdvbv1_Ms1CPfhcGCvSElSosZmyoqAgmlkgnY0iXNlY3AyNTZrMaECriawHKWdDRk2xeZkrOXBQ0dfMFLHY4eENZwdufn1S1o",
    44  	}
    45  
    46  	wantChanges := []*route53.Change{
    47  		{
    48  			Action: sp("CREATE"),
    49  			ResourceRecordSet: &route53.ResourceRecordSet{
    50  				Name: sp("2xs2367yhaxjfglzhvawlqd4zy.n"),
    51  				ResourceRecords: []*route53.ResourceRecord{{
    52  					Value: sp(`"enr:-HW4QOFzoVLaFJnNhbgMoDXPnOvcdVuj7pDpqRvh6BRDO68aVi5ZcjB3vzQRZH2IcLBGHzo8uUN3snqmgTiE56CH3AMBgmlkgnY0iXNlY3AyNTZrMaECC2_24YYkYHEgdzxlSNKQEnHhuNAbNlMlWJxrJxbAFvA"`),
    53  				}},
    54  				TTL:  ip(treeNodeTTL),
    55  				Type: sp("TXT"),
    56  			},
    57  		},
    58  		{
    59  			Action: sp("CREATE"),
    60  			ResourceRecordSet: &route53.ResourceRecordSet{
    61  				Name: sp("c7hrfpf3blgf3yr4dy5kx3smbe.n"),
    62  				ResourceRecords: []*route53.ResourceRecord{{
    63  					Value: sp(`"enrtree://AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YYHIUV7BVDQ5FDPRT2@morenodes.example.org"`),
    64  				}},
    65  				TTL:  ip(treeNodeTTL),
    66  				Type: sp("TXT"),
    67  			},
    68  		},
    69  		{
    70  			Action: sp("CREATE"),
    71  			ResourceRecordSet: &route53.ResourceRecordSet{
    72  				Name: sp("h4fht4b454p6uxfd7jcyq5pwdy.n"),
    73  				ResourceRecords: []*route53.ResourceRecord{{
    74  					Value: sp(`"enr:-HW4QAggRauloj2SDLtIHN1XBkvhFZ1vtf1raYQp9TBW2RD5EEawDzbtSmlXUfnaHcvwOizhVYLtr7e6vw7NAf6mTuoCgmlkgnY0iXNlY3AyNTZrMaECjrXI8TLNXU0f8cthpAMxEshUyQlK-AM0PW2wfrnacNI"`),
    75  				}},
    76  				TTL:  ip(treeNodeTTL),
    77  				Type: sp("TXT"),
    78  			},
    79  		},
    80  		{
    81  			Action: sp("CREATE"),
    82  			ResourceRecordSet: &route53.ResourceRecordSet{
    83  				Name: sp("jwxydbpxywg6fx3gmdibfa6cj4.n"),
    84  				ResourceRecords: []*route53.ResourceRecord{{
    85  					Value: sp(`"enrtree-branch:2XS2367YHAXJFGLZHVAWLQD4ZY,H4FHT4B454P6UXFD7JCYQ5PWDY,MHTDO6TMUBRIA2XWG5LUDACK24"`),
    86  				}},
    87  				TTL:  ip(treeNodeTTL),
    88  				Type: sp("TXT"),
    89  			},
    90  		},
    91  		{
    92  			Action: sp("CREATE"),
    93  			ResourceRecordSet: &route53.ResourceRecordSet{
    94  				Name: sp("mhtdo6tmubria2xwg5ludack24.n"),
    95  				ResourceRecords: []*route53.ResourceRecord{{
    96  					Value: sp(`"enr:-HW4QLAYqmrwllBEnzWWs7I5Ev2IAs7x_dZlbYdRdMUx5EyKHDXp7AV5CkuPGUPdvbv1_Ms1CPfhcGCvSElSosZmyoqAgmlkgnY0iXNlY3AyNTZrMaECriawHKWdDRk2xeZkrOXBQ0dfMFLHY4eENZwdufn1S1o"`),
    97  				}},
    98  				TTL:  ip(treeNodeTTL),
    99  				Type: sp("TXT"),
   100  			},
   101  		},
   102  		{
   103  			Action: sp("UPSERT"),
   104  			ResourceRecordSet: &route53.ResourceRecordSet{
   105  				Name: sp("n"),
   106  				ResourceRecords: []*route53.ResourceRecord{{
   107  					Value: sp(`"enrtree-root:v1 e=JWXYDBPXYWG6FX3GMDIBFA6CJ4 l=C7HRFPF3BLGF3YR4DY5KX3SMBE seq=1 sig=o908WmNp7LibOfPsr4btQwatZJ5URBr2ZAuxvK4UWHlsB9sUOTJQaGAlLPVAhM__XJesCHxLISo94z5Z2a463gA"`),
   108  				}},
   109  				TTL:  ip(rootTTL),
   110  				Type: sp("TXT"),
   111  			},
   112  		},
   113  		{
   114  			Action: sp("DELETE"),
   115  			ResourceRecordSet: &route53.ResourceRecordSet{
   116  				Name: sp("2kfjogvxdqtxxugbh7gs7naaai.n"),
   117  				ResourceRecords: []*route53.ResourceRecord{
   118  					{Value: sp(`"enr:-HW4QO1ml1DdXLeZLsUxewnthhUy8eROqkDyoMTyavfks9JlYQIlMFEUoM78PovJDPQrAkrb3LRJ-""vtrymDguKCOIAWAgmlkgnY0iXNlY3AyNTZrMaEDffaGfJzgGhUif1JqFruZlYmA31HzathLSWxfbq_QoQ4"`)},
   119  				},
   120  				TTL:  ip(3333),
   121  				Type: sp("TXT"),
   122  			},
   123  		},
   124  		{
   125  			Action: sp("DELETE"),
   126  			ResourceRecordSet: &route53.ResourceRecordSet{
   127  				Name: sp("fdxn3sn67na5dka4j2gok7bvqi.n"),
   128  				ResourceRecords: []*route53.ResourceRecord{{
   129  					Value: sp(`"enrtree-branch:"`),
   130  				}},
   131  				TTL:  ip(treeNodeTTL),
   132  				Type: sp("TXT"),
   133  			},
   134  		},
   135  	}
   136  
   137  	var client route53Client
   138  	changes := client.computeChanges("n", testTree1, testTree0)
   139  	if !reflect.DeepEqual(changes, wantChanges) {
   140  		t.Fatalf("wrong changes (got %d, want %d)", len(changes), len(wantChanges))
   141  	}
   142  
   143  	// Check splitting according to size.
   144  	wantSplit := [][]*route53.Change{
   145  		wantChanges[:4],
   146  		wantChanges[4:6],
   147  		wantChanges[6:],
   148  	}
   149  	split := splitChanges(changes, 600, 4000)
   150  	if !reflect.DeepEqual(split, wantSplit) {
   151  		t.Fatalf("wrong split batches: got %d, want %d", len(split), len(wantSplit))
   152  	}
   153  
   154  	// Check splitting according to count.
   155  	wantSplit = [][]*route53.Change{
   156  		wantChanges[:5],
   157  		wantChanges[5:],
   158  	}
   159  	split = splitChanges(changes, 10000, 6)
   160  	if !reflect.DeepEqual(split, wantSplit) {
   161  		t.Fatalf("wrong split batches: got %d, want %d", len(split), len(wantSplit))
   162  	}
   163  }
   164  
   165  func sp(s string) *string { return &s }
   166  func ip(i int64) *int64   { return &i }