Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/addressbook/addressbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func (s *store) Get(overlay swarm.Address) (*bzz.Address, bool, error) {
}
return nil, false, err
}
if v.Address == nil {
_ = s.store.Delete(key)
return nil, false, ErrNotFound
Comment thread
akrem-chabchoub marked this conversation as resolved.
}
return v.Address, v.Verified, nil
}

Expand Down
43 changes: 43 additions & 0 deletions pkg/addressbook/addressbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package addressbook_test

import (
"encoding/json"
"errors"
"testing"

Expand All @@ -13,6 +14,7 @@ import (
"github.com/ethersphere/bee/v2/pkg/bzz"
"github.com/ethersphere/bee/v2/pkg/crypto"
"github.com/ethersphere/bee/v2/pkg/statestore/mock"
"github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/swarm"
ma "github.com/multiformats/go-multiaddr"
)
Expand Down Expand Up @@ -96,3 +98,44 @@ func run(t *testing.T, f bookFunc) {
t.Fatalf("expected addresses len %v, got %v", 1, len(addresses))
}
}

type mockCorruptedStore struct{}

func (m *mockCorruptedStore) Get(key string, i any) error {
corruptedJSON := []byte(`{"address": null}`)
return json.Unmarshal(corruptedJSON, i)
}

func (m *mockCorruptedStore) Put(key string, i any) error {
return nil
}

func (m *mockCorruptedStore) Delete(key string) error {
return nil
}

func (m *mockCorruptedStore) Iterate(prefix string, fn storage.StateIterFunc) error {
return nil
}

func (m *mockCorruptedStore) Close() error {
return nil
}

func TestGetCorruptedNilAddress(t *testing.T) {
t.Parallel()

corruptedStore := &mockCorruptedStore{}
book := addressbook.New(corruptedStore)

addr := swarm.NewAddress([]byte{0, 1, 2, 3})

v, _, err := book.Get(addr)
if !errors.Is(err, addressbook.ErrNotFound) {
t.Fatalf("expected ErrNotFound for corrupted entry, got %v", err)
}

if v != nil {
t.Fatalf("expected nil address, got %s", v)
}
}
Loading