55package addressbook_test
66
77import (
8+ "encoding/json"
89 "errors"
910 "testing"
1011
@@ -13,6 +14,7 @@ import (
1314 "github.com/ethersphere/bee/v2/pkg/bzz"
1415 "github.com/ethersphere/bee/v2/pkg/crypto"
1516 "github.com/ethersphere/bee/v2/pkg/statestore/mock"
17+ "github.com/ethersphere/bee/v2/pkg/storage"
1618 "github.com/ethersphere/bee/v2/pkg/swarm"
1719 ma "github.com/multiformats/go-multiaddr"
1820)
@@ -96,3 +98,44 @@ func run(t *testing.T, f bookFunc) {
9698 t .Fatalf ("expected addresses len %v, got %v" , 1 , len (addresses ))
9799 }
98100}
101+
102+ type mockCorruptedStore struct {}
103+
104+ func (m * mockCorruptedStore ) Get (key string , i any ) error {
105+ corruptedJSON := []byte (`{"address": null}` )
106+ return json .Unmarshal (corruptedJSON , i )
107+ }
108+
109+ func (m * mockCorruptedStore ) Put (key string , i any ) error {
110+ return nil
111+ }
112+
113+ func (m * mockCorruptedStore ) Delete (key string ) error {
114+ return nil
115+ }
116+
117+ func (m * mockCorruptedStore ) Iterate (prefix string , fn storage.StateIterFunc ) error {
118+ return nil
119+ }
120+
121+ func (m * mockCorruptedStore ) Close () error {
122+ return nil
123+ }
124+
125+ func TestGetCorruptedNilAddress (t * testing.T ) {
126+ t .Parallel ()
127+
128+ corruptedStore := & mockCorruptedStore {}
129+ book := addressbook .New (corruptedStore )
130+
131+ addr := swarm .NewAddress ([]byte {0 , 1 , 2 , 3 })
132+
133+ v , _ , err := book .Get (addr )
134+ if ! errors .Is (err , addressbook .ErrNotFound ) {
135+ t .Fatalf ("expected ErrNotFound for corrupted entry, got %v" , err )
136+ }
137+
138+ if v != nil {
139+ t .Fatalf ("expected nil address, got %s" , v )
140+ }
141+ }
0 commit comments