Commit 0f035326 authored by Janos Guljas's avatar Janos Guljas

add swarm Address tests

parent f8886987
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package swarm_test
import (
"bytes"
"encoding/hex"
"errors"
"testing"
"github.com/ethersphere/bee/pkg/swarm"
)
func TestAddress(t *testing.T) {
for _, tc := range []struct {
name string
hex string
want swarm.Address
wantErr error
}{
{
name: "blank",
hex: "",
want: swarm.ZeroAddress,
},
{
name: "odd",
hex: "0",
wantErr: hex.ErrLength,
},
{
name: "zero",
hex: "00",
want: []byte{0},
},
{
name: "one",
hex: "01",
want: []byte{1},
},
{
name: "arbitrary",
hex: "35a26b7bb6455cbabe7a0e05aafbd0b8b26feac843e3b9a649468d0ea37a12b2",
want: []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},
},
} {
t.Run(tc.name, func(t *testing.T) {
a, err := swarm.NewAddress(tc.hex)
if !errors.Is(err, tc.wantErr) {
t.Fatalf("got error %v, want %v", err, tc.wantErr)
}
if !bytes.Equal(a, tc.want) {
t.Errorf("got address %#v, want %#v", a, tc.want)
}
if !a.Equal(tc.want) {
t.Errorf("address %v not equal to %v", a, tc.want)
}
if a.IsZero() != tc.want.IsZero() {
t.Errorf("got address as zero=%v, want zero=%v", a.IsZero(), tc.want.IsZero())
}
})
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment