github.com/ethersphere/bee/v2@v2.2.0/pkg/swarm/swarm_test.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package swarm_test
     6  
     7  import (
     8  	"encoding/hex"
     9  	"encoding/json"
    10  	"errors"
    11  	"testing"
    12  
    13  	"github.com/ethersphere/bee/v2/pkg/swarm"
    14  )
    15  
    16  func TestAddress(t *testing.T) {
    17  	t.Parallel()
    18  
    19  	for _, tc := range []struct {
    20  		name    string
    21  		hex     string
    22  		want    swarm.Address
    23  		wantErr error
    24  	}{
    25  		{
    26  			name: "blank",
    27  			hex:  "",
    28  			want: swarm.ZeroAddress,
    29  		},
    30  		{
    31  			name:    "odd",
    32  			hex:     "0",
    33  			wantErr: hex.ErrLength,
    34  		},
    35  		{
    36  			name: "zero",
    37  			hex:  "00",
    38  			want: swarm.NewAddress([]byte{0}),
    39  		},
    40  		{
    41  			name: "all zeroes",
    42  			hex:  swarm.EmptyAddress.String(),
    43  			want: swarm.EmptyAddress,
    44  		},
    45  		{
    46  			name: "one",
    47  			hex:  "01",
    48  			want: swarm.NewAddress([]byte{1}),
    49  		},
    50  		{
    51  			name: "arbitrary",
    52  			hex:  "35a26b7bb6455cbabe7a0e05aafbd0b8b26feac843e3b9a649468d0ea37a12b2",
    53  			want: swarm.NewAddress([]byte{0x35, 0xa2, 0x6b, 0x7b, 0xb6, 0x45, 0x5c, 0xba, 0xbe, 0x7a, 0xe, 0x5, 0xaa, 0xfb, 0xd0, 0xb8, 0xb2, 0x6f, 0xea, 0xc8, 0x43, 0xe3, 0xb9, 0xa6, 0x49, 0x46, 0x8d, 0xe, 0xa3, 0x7a, 0x12, 0xb2}),
    54  		},
    55  	} {
    56  		tc := tc
    57  		t.Run(tc.name, func(t *testing.T) {
    58  			t.Parallel()
    59  
    60  			a, err := swarm.ParseHexAddress(tc.hex)
    61  			if !errors.Is(err, tc.wantErr) {
    62  				t.Fatalf("got error %v, want %v", err, tc.wantErr)
    63  			}
    64  			if a.String() != tc.want.String() {
    65  				t.Errorf("got address %#v, want %#v", a, tc.want)
    66  			}
    67  			if !a.Equal(tc.want) {
    68  				t.Errorf("address %v not equal to %v", a, tc.want)
    69  			}
    70  			if a.IsZero() != tc.want.IsZero() {
    71  				t.Errorf("got address as zero=%v, want zero=%v", a.IsZero(), tc.want.IsZero())
    72  			}
    73  			if a.IsEmpty() != tc.want.IsEmpty() {
    74  				t.Errorf("got address as empty=%v, want empty=%v", a.IsEmpty(), tc.want.IsEmpty())
    75  			}
    76  			if a.IsValidLength() != tc.want.IsValidLength() {
    77  				t.Errorf("got address as invalid_size=%v, want valid=%v", a.IsValidLength(), tc.want.IsValidLength())
    78  			}
    79  		})
    80  	}
    81  }
    82  
    83  func TestAddress_jsonMarshalling(t *testing.T) {
    84  	t.Parallel()
    85  
    86  	a1 := swarm.MustParseHexAddress("24798dd5a470e927fa")
    87  
    88  	b, err := json.Marshal(a1)
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  
    93  	var a2 swarm.Address
    94  	if err := json.Unmarshal(b, &a2); err != nil {
    95  		t.Fatal(err)
    96  	}
    97  
    98  	if !a1.Equal(a2) {
    99  		t.Error("unmarshalled address is not equal to the original")
   100  	}
   101  }
   102  
   103  func TestValidSize(t *testing.T) {
   104  	t.Parallel()
   105  
   106  	a1 := swarm.MustParseHexAddress("24798dd5a470e927fa")
   107  	a2 := swarm.MustParseHexAddress("35a26b7bb6455cbabe7a0e05aafbd0b8b26feac843e3b9a649468d0ea37a12b2")
   108  
   109  	if a1.IsValidLength() {
   110  		t.Fatal("wanted invalid size")
   111  	}
   112  
   113  	if !a2.IsValidLength() {
   114  		t.Fatal("wanted valid size")
   115  	}
   116  }
   117  
   118  func TestAddress_MemberOf(t *testing.T) {
   119  	t.Parallel()
   120  
   121  	a1 := swarm.MustParseHexAddress("24798dd5a470e927fa")
   122  	a2 := swarm.MustParseHexAddress("24798dd5a470e927fa")
   123  	a3 := swarm.MustParseHexAddress("24798dd5a470e927fb")
   124  	a4 := swarm.MustParseHexAddress("24798dd5a470e927fc")
   125  
   126  	set1 := []swarm.Address{a2, a3}
   127  	if !a1.MemberOf(set1) {
   128  		t.Fatal("expected addr as member")
   129  	}
   130  
   131  	set2 := []swarm.Address{a3, a4}
   132  	if a1.MemberOf(set2) {
   133  		t.Fatal("expected addr not member")
   134  	}
   135  }
   136  
   137  func TestAddress_Clone(t *testing.T) {
   138  	t.Parallel()
   139  
   140  	original := swarm.MustParseHexAddress("35a26b7bb6455cbabe7a0e05aafbd0b8b26feac843e3b9a649468d0ea37a12b2")
   141  	clone := original.Clone()
   142  
   143  	if !original.Equal(clone) {
   144  		t.Fatal("original should be equal with clone")
   145  	}
   146  
   147  	// if we modify original address, addresses should not be equal
   148  	original.Bytes()[0] = 1
   149  	if original.Equal(clone) {
   150  		t.Fatal("original should not be equal with clone")
   151  	}
   152  }
   153  
   154  func TestCloser(t *testing.T) {
   155  	t.Parallel()
   156  
   157  	a := swarm.MustParseHexAddress("9100000000000000000000000000000000000000000000000000000000000000")
   158  	x := swarm.MustParseHexAddress("8200000000000000000000000000000000000000000000000000000000000000")
   159  	y := swarm.MustParseHexAddress("1200000000000000000000000000000000000000000000000000000000000000")
   160  
   161  	if cmp, _ := x.Closer(a, y); !cmp {
   162  		t.Fatal("x is closer")
   163  	}
   164  }
   165  
   166  func TestParseBitStr(t *testing.T) {
   167  	t.Parallel()
   168  
   169  	for _, tc := range []struct {
   170  		overlay swarm.Address
   171  		bitStr  string
   172  	}{
   173  		{
   174  			swarm.MustParseHexAddress("5c32a2fe3d217af8c943fa665ebcfbdf7ab9af0cf1b2a1c8e5fc163dad2f5c7b"),
   175  			"010111000",
   176  		},
   177  		{
   178  			swarm.MustParseHexAddress("eac0903e59ff1c1a5f1d7d218b33f819b199aa0f68a19fd5fa02b7f84982b55d"),
   179  			"111010101",
   180  		},
   181  		{
   182  			swarm.MustParseHexAddress("70143dd2863ae07edfe7c1bfee75daea06226f0678e1117337d274492226bfe0"),
   183  			"011100000",
   184  		},
   185  	} {
   186  
   187  		if addr, err := swarm.ParseBitStrAddress(tc.bitStr); err != nil {
   188  			t.Fatal(err)
   189  		} else if got := swarm.Proximity(addr.Bytes(), tc.overlay.Bytes()); got < uint8(len(tc.bitStr)) {
   190  			t.Fatalf("got proximiy %d want %d", got, len(tc.bitStr))
   191  		}
   192  	}
   193  }