package p2p import ( "testing" "time" ) // A node that lost its database gets its own listings back from a peer, also // those in a region it does not follow, and nobody else's. func TestANodeGetsItsOwnListingsBackFromAPeer(t *testing.T) { root := mustRoot(t) a := startNode(t, root, "u1x") // holds the listings (any device of the author may have signed them) for i := 0; i < 7; i++ { // more than two pages of the test's page size 3 a.store(t, a.listing(t, "u1xjhx")) } stranger := startNode(t, nil, "u1x") a.store(t, stranger.listing(t, "u1xjhx")) // The restored node follows another region, so region sync brings nothing. r := startNode(t, root, "u0u") connect(t, r, a) eventually(t, "r gets its 7 listings back", func() bool { return count(r) == 7 }) time.Sleep(300 * time.Millisecond) if got := count(r); got != 7 { t.Fatalf("r holds %d listings; the stranger's must not come along", got) } ls, err := r.s.ByAuthor(r.ctx, stranger.dev.Cert.Author) if err != nil || len(ls) != 0 { t.Fatalf("the stranger's listing was recovered: %v %v", ls, err) } } // Only the first few peers of a run are asked. func TestOnlyTheFirstPeersAreAskedForOwnListings(t *testing.T) { root := mustRoot(t) a := startNode(t, root, "u1x") a.store(t, a.listing(t, "u1xjhx")) r := startNode(t, root, "u0u") r.recoverAsked.Store(recoverPeers) connect(t, r, a) time.Sleep(time.Second) if got := count(r); got != 0 { t.Fatalf("r asked beyond its limit and holds %d", got) } } // An old node does not know the request; the sync with it must still succeed // as far as it goes, and the node asks another peer instead. func TestAnOldNodeCannotAnswerAndTheNextPeerIsAsked(t *testing.T) { root := mustRoot(t) old := startNode(t, root, "u1x") old.oldSync = true good := startNode(t, root, "u1x") good.store(t, good.listing(t, "u1xjhx")) r := startNode(t, root, "u0u") connect(t, r, old) connect(t, r, good) eventually(t, "r gets the listing from the peer that can answer", func() bool { return count(r) == 1 }) } func TestAuthorRequestsNeedAValidKey(t *testing.T) { for _, s := range []string{"", "abc", "zz" + string(make([]byte, 62))} { if validAuthor(s) { t.Errorf("%q accepted", s) } } if !validAuthor("00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff") { t.Error("a 64-digit key was refused") } }