Skip to content

Commit

Permalink
test: add tests for most functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Sep 5, 2023
1 parent 268007c commit 1bf3f67
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 42 deletions.
5 changes: 2 additions & 3 deletions gateway/blocks_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,12 @@ func (bb *BlocksBackend) GetCAR(ctx context.Context, p path.ImmutablePath, param
// Setup the UnixFS resolver.
f := newNodeGetterFetcherSingleUseFactory(ctx, blockGetter)
pathResolver := resolver.NewBasicResolver(f)
ip := ipfspath.FromString(p.String())
_, _, err = pathResolver.ResolveToLastNode(ctx, ip)
_, _, err = pathResolver.ResolveToLastNode(ctx, p)

Check warning on line 262 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L262

Added line #L262 was not covered by tests

if isErrNotFound(err) {
return ContentPathMetadata{
PathSegmentRoots: nil,
LastSegment: ifacepath.NewResolvedPath(ip, rootCid, rootCid, ""),
LastSegment: path.NewIPFSPath(rootCid),

Check warning on line 267 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L267

Added line #L267 was not covered by tests
ContentType: "",
}, io.NopCloser(&buf), nil
}
Expand Down
6 changes: 3 additions & 3 deletions gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func TestGatewayGet(t *testing.T) {
text string
}{
{"127.0.0.1:8080", "/", http.StatusNotFound, "404 page not found\n"},
{"127.0.0.1:8080", "/ipfs", http.StatusBadRequest, "invalid path \"/ipfs/\": not enough path components\n"},
{"127.0.0.1:8080", "/ipns", http.StatusBadRequest, "invalid path \"/ipns/\": not enough path components\n"},
{"127.0.0.1:8080", "/ipfs", http.StatusBadRequest, "invalid path \"/ipfs/\": path does not have enough components\n"},
{"127.0.0.1:8080", "/ipns", http.StatusBadRequest, "invalid path \"/ipns/\": path does not have enough components\n"},
{"127.0.0.1:8080", "/" + k.Cid().String(), http.StatusNotFound, "404 page not found\n"},
{"127.0.0.1:8080", "/ipfs/this-is-not-a-cid", http.StatusBadRequest, "invalid path \"/ipfs/this-is-not-a-cid\": invalid CID: invalid cid: illegal base32 data at input byte 3\n"},
{"127.0.0.1:8080", "/ipfs/this-is-not-a-cid", http.StatusBadRequest, "invalid path \"/ipfs/this-is-not-a-cid\": invalid cid: illegal base32 data at input byte 3\n"},
{"127.0.0.1:8080", k.String(), http.StatusOK, "fnord"},
{"127.0.0.1:8080", "/ipns/nxdomain.example.com", http.StatusInternalServerError, "failed to resolve /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"},
{"127.0.0.1:8080", "/ipns/%0D%0A%0D%0Ahello", http.StatusInternalServerError, "failed to resolve /ipns/\\r\\n\\r\\nhello: " + namesys.ErrResolveFailed.Error() + "\n"},
Expand Down
15 changes: 11 additions & 4 deletions path/error.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package path

import (
"errors"
"fmt"
)

var (
ErrExpectedImmutable = errors.New("path was expected to be immutable")
ErrInsufficientComponents = errors.New("path does not have enough components")
ErrUnknownNamespace = errors.New("unknown namespace")
)

type ErrInvalidPath struct {
error error
path string
err error
path string
}

func (e ErrInvalidPath) Error() string {
return fmt.Sprintf("invalid path %q: %s", e.path, e.error)
return fmt.Sprintf("invalid path %q: %s", e.path, e.err)
}

func (e ErrInvalidPath) Unwrap() error {
return e.error
return e.err
}

func (e ErrInvalidPath) Is(err error) bool {
Expand Down
4 changes: 2 additions & 2 deletions path/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
)

func TestErrorIs(t *testing.T) {
if !errors.Is(ErrInvalidPath{path: "foo", error: errors.New("bar")}, ErrInvalidPath{}) {
if !errors.Is(ErrInvalidPath{path: "foo", err: errors.New("bar")}, ErrInvalidPath{}) {
t.Fatal("error must be error")
}

if !errors.Is(&ErrInvalidPath{path: "foo", error: errors.New("bar")}, ErrInvalidPath{}) {
if !errors.Is(&ErrInvalidPath{path: "foo", err: errors.New("bar")}, ErrInvalidPath{}) {
t.Fatal("pointer to error must be error")
}
}
22 changes: 11 additions & 11 deletions path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ type immutablePath struct {

func NewImmutablePath(p Path) (ImmutablePath, error) {
if p.Namespace().Mutable() {
return nil, fmt.Errorf("path was expected to be immutable: %s", p.String())
return nil, ErrInvalidPath{err: ErrExpectedImmutable, path: p.String()}
}

segments := p.Segments()
cid, err := cid.Decode(segments[1])
if err != nil {
return nil, &ErrInvalidPath{error: fmt.Errorf("invalid CID: %w", err), path: p.String()}
return nil, &ErrInvalidPath{err: err, path: p.String()}

Check warning on line 120 in path/path.go

View check run for this annotation

Codecov / codecov/patch

path/path.go#L120

Added line #L120 was not covered by tests
}

return immutablePath{path: p, cid: cid}, nil
Expand Down Expand Up @@ -149,7 +149,7 @@ func (ip immutablePath) Remainder() string {

// NewIPFSPath returns a new "/ipfs" path with the provided CID.
func NewIPFSPath(cid cid.Cid) ImmutablePath {
return &immutablePath{
return immutablePath{
path: path{
str: fmt.Sprintf("/%s/%s", IPFSNamespace, cid.String()),
namespace: IPFSNamespace,
Expand All @@ -160,7 +160,7 @@ func NewIPFSPath(cid cid.Cid) ImmutablePath {

// NewIPLDPath returns a new "/ipld" path with the provided CID.
func NewIPLDPath(cid cid.Cid) ImmutablePath {
return &immutablePath{
return immutablePath{
path: path{
str: fmt.Sprintf("/%s/%s", IPLDNamespace, cid.String()),
namespace: IPLDNamespace,
Expand All @@ -169,10 +169,10 @@ func NewIPLDPath(cid cid.Cid) ImmutablePath {
}
}

// NewPath takes the given string and returns a well-forme and sanitized [Path].
// NewPath takes the given string and returns a well-formed and sanitized [Path].
// The given string is cleaned through [gopath.Clean], but preserving the final
// trailing slash. This function returns an error when the given string is not
// a valid path.
// a valid content path.
func NewPath(str string) (Path, error) {
cleaned := gopath.Clean(str)
components := strings.Split(cleaned, "/")
Expand All @@ -186,18 +186,18 @@ func NewPath(str string) (Path, error) {
// components: [" " "{namespace}" "{element}"]. The first component must therefore
// be empty.
if len(components) < 3 || components[0] != "" {
return nil, &ErrInvalidPath{error: fmt.Errorf("not enough path components"), path: str}
return nil, &ErrInvalidPath{err: ErrInsufficientComponents, path: str}
}

switch components[1] {
case "ipfs", "ipld":
if components[2] == "" {
return nil, &ErrInvalidPath{error: fmt.Errorf("not enough path components"), path: str}
return nil, &ErrInvalidPath{err: ErrInsufficientComponents, path: str}
}

Check warning on line 196 in path/path.go

View check run for this annotation

Codecov / codecov/patch

path/path.go#L195-L196

Added lines #L195 - L196 were not covered by tests

cid, err := cid.Decode(components[2])
if err != nil {
return nil, &ErrInvalidPath{error: fmt.Errorf("invalid CID: %w", err), path: str}
return nil, &ErrInvalidPath{err: err, path: str}
}

ns := IPFSNamespace
Expand All @@ -214,15 +214,15 @@ func NewPath(str string) (Path, error) {
}, nil
case "ipns":
if components[2] == "" {
return nil, &ErrInvalidPath{error: fmt.Errorf("not enough path components"), path: str}
return nil, &ErrInvalidPath{err: ErrInsufficientComponents, path: str}
}

Check warning on line 218 in path/path.go

View check run for this annotation

Codecov / codecov/patch

path/path.go#L217-L218

Added lines #L217 - L218 were not covered by tests

return path{
str: cleaned,
namespace: IPNSNamespace,
}, nil
default:
return nil, &ErrInvalidPath{error: fmt.Errorf("unknown namespace %q", components[1]), path: str}
return nil, &ErrInvalidPath{err: fmt.Errorf("%w: %q", ErrUnknownNamespace, components[1]), path: str}
}
}

Expand Down
Loading

0 comments on commit 1bf3f67

Please sign in to comment.