github.com/ethersphere/bee/v2@v2.2.0/pkg/resolver/cidv1/cidv1_test.go (about)

     1  // Copyright 2022 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 cidv1_test
     6  
     7  import (
     8  	"errors"
     9  	"testing"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/resolver"
    12  	"github.com/ethersphere/bee/v2/pkg/resolver/cidv1"
    13  	"github.com/ethersphere/bee/v2/pkg/util/testutil"
    14  )
    15  
    16  func TestCIDResolver(t *testing.T) {
    17  	t.Parallel()
    18  
    19  	r := cidv1.Resolver{}
    20  	testutil.CleanupCloser(t, r)
    21  
    22  	t.Run("resolve manifest CID", func(t *testing.T) {
    23  		t.Parallel()
    24  
    25  		addr, err := r.Resolve("bah5acgzazjrvpieogf6rl3cwb7xtjzgel6hrt4a4g4vkody5u4v7u7y2im4a")
    26  		if err != nil {
    27  			t.Fatal(err)
    28  		}
    29  
    30  		expected := "ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338"
    31  		if addr.String() != expected {
    32  			t.Fatalf("unexpected address resolved exp %s found %s", expected, addr.String())
    33  		}
    34  	})
    35  	t.Run("resolve feed CID", func(t *testing.T) {
    36  		t.Parallel()
    37  
    38  		addr, err := r.Resolve("bah5qcgzazjrvpieogf6rl3cwb7xtjzgel6hrt4a4g4vkody5u4v7u7y2im4a")
    39  		if err != nil {
    40  			t.Fatal(err)
    41  		}
    42  
    43  		expected := "ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338"
    44  		if addr.String() != expected {
    45  			t.Fatalf("unexpected address resolved exp %s found %s", expected, addr.String())
    46  		}
    47  	})
    48  	t.Run("fail other codecs", func(t *testing.T) {
    49  		t.Parallel()
    50  
    51  		_, err := r.Resolve("bafybeiekkklkqtypmqav6ytqjbdqucxfwuk5cgige4245d2qhkccuyfnly")
    52  		if err == nil {
    53  			t.Fatal("expected error")
    54  		}
    55  	})
    56  	t.Run("fail on invalid CID", func(t *testing.T) {
    57  		t.Parallel()
    58  
    59  		_, err := r.Resolve("bafybeiekk")
    60  		if !errors.Is(err, resolver.ErrParse) {
    61  			t.Fatal("expected error", resolver.ErrParse, "got", err)
    62  		}
    63  	})
    64  }