Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addrmgr: Test removal of corrupt peers file. #1129

Merged
merged 1 commit into from
Mar 6, 2018
Merged
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
2 changes: 1 addition & 1 deletion addrmgr/addrmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (a *AddrManager) updateAddress(netAddr, srcAddr *wire.NetAddress) {
// TODO(oga) only update addresses periodically.
// Update the last seen time and services.
// note that to prevent causing excess garbage on getaddr
// messages the netaddresses in addrmaanger are *immutable*,
// messages the netaddresses in addrmanager are *immutable*,
// if we need to change them then we replace the pointer with a
// new copy so that we don't have to copy every na for getaddr.
if netAddr.Timestamp.After(ka.na.Timestamp) ||
Expand Down
25 changes: 24 additions & 1 deletion addrmgr/addrmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestAddAddressByIP(t *testing.T) {
// make sure the peers file has been written
peersFile := filepath.Join(dir, addrmgr.PeersFilename)
if _, err := os.Stat(peersFile); err != nil {
t.Fatalf("Peer file does not exist: %s", peersFile)
t.Fatalf("Peers file does not exist: %s", peersFile)
}

// start address manager again to read peers file
Expand Down Expand Up @@ -498,3 +498,26 @@ func TestNetAddressKey(t *testing.T) {
}

}

func TestCorruptPeersFile(t *testing.T) {
dir, err := ioutil.TempDir("", "testcorruptpeersfile")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
peersFile := filepath.Join(dir, addrmgr.PeersFilename)
// create corrupt (empty) peers file
fp, err := os.Create(peersFile)
if err != nil {
t.Fatalf("Could not create empty peers file: %s", peersFile)
}
if err := fp.Close(); err != nil {
t.Fatalf("Could not write empty peers file: %s", peersFile)
}
amgr := addrmgr.New(dir, nil)
amgr.Start()
amgr.Stop()
if _, err := os.Stat(peersFile); err != nil {
t.Fatalf("Corrupt peers file has not been removed: %s", peersFile)
}
}
4 changes: 2 additions & 2 deletions addrmgr/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ be trusted to send valid peers nor attempt to provide you with only peers they
control with malicious intent.

With that in mind, this package provides a concurrency safe address manager for
caching and selecting peers in a non-determinstic manner. The general idea is
caching and selecting peers in a non-deterministic manner. The general idea is
the caller adds addresses to the address manager and notifies it when addresses
are connected, known good, and attempted. The caller also requests addresses as
it needs them.
Expand All @@ -34,6 +34,6 @@ hard to only return routable addresses. In addition, it uses the information
provided by the caller about connected, known good, and attempted addresses to
periodically purge peers which no longer appear to be good peers as well as
bias the selection toward known good peers. The general idea is to make a best
effort at only providing usuable addresses.
effort at only providing usable addresses.
*/
package addrmgr