github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/renter/hostdb/update_test.go (about) 1 package hostdb 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/NebulousLabs/Sia/crypto" 8 "github.com/NebulousLabs/Sia/modules" 9 "github.com/NebulousLabs/Sia/types" 10 ) 11 12 // makeSignedAnnouncement creates a []byte that contains an encoded and signed 13 // host announcement for the given net address. 14 func makeSignedAnnouncement(na modules.NetAddress) ([]byte, error) { 15 sk, pk, err := crypto.GenerateKeyPair() 16 if err != nil { 17 return nil, err 18 } 19 spk := types.SiaPublicKey{ 20 Algorithm: types.SignatureEd25519, 21 Key: pk[:], 22 } 23 return modules.CreateAnnouncement(na, spk, sk) 24 } 25 26 // TestFindHostAnnouncements probes the findHostAnnouncements function 27 func TestFindHostAnnouncements(t *testing.T) { 28 annBytes, err := makeSignedAnnouncement("foo.com:1234") 29 if err != nil { 30 t.Fatal(err) 31 } 32 b := types.Block{ 33 Transactions: []types.Transaction{ 34 { 35 ArbitraryData: [][]byte{annBytes}, 36 }, 37 }, 38 } 39 announcements := findHostAnnouncements(b) 40 if len(announcements) != 1 { 41 t.Error("host announcement not found in block") 42 } 43 44 // Try with an altered prefix 45 b.Transactions[0].ArbitraryData[0][0]++ 46 announcements = findHostAnnouncements(b) 47 if len(announcements) != 0 { 48 t.Error("host announcement found when there was an invalid prefix") 49 } 50 b.Transactions[0].ArbitraryData[0][0]-- 51 52 // Try with an invalid host encoding. 53 b.Transactions[0].ArbitraryData[0][17]++ 54 announcements = findHostAnnouncements(b) 55 if len(announcements) != 0 { 56 t.Error("host announcement found when there was an invalid encoding of a host announcement") 57 } 58 } 59 60 // TestReceiveConsensusSetUpdate probes the ReveiveConsensusSetUpdate method of 61 // the hostdb type. 62 func TestReceiveConsensusSetUpdate(t *testing.T) { 63 // create hostdb 64 hdb := bareHostDB() 65 hdb.persist = &memPersist{} 66 67 // Put a host announcement into a block. 68 annBytes, err := makeSignedAnnouncement("foo.com:1234") 69 if err != nil { 70 t.Fatal(err) 71 } 72 cc := modules.ConsensusChange{ 73 AppliedBlocks: []types.Block{{ 74 Transactions: []types.Transaction{{ 75 ArbitraryData: [][]byte{annBytes}, 76 }}, 77 }}, 78 } 79 80 // call ProcessConsensusChange 81 hdb.ProcessConsensusChange(cc) 82 83 // host should be sent to scanPool 84 select { 85 case <-time.After(time.Second): 86 t.Fatal("announcement not seen") 87 case <-hdb.scanPool: 88 } 89 90 // Check that there is now a host in the hostdb. 91 if len(hdb.AllHosts()) != 1 { 92 t.Fatal("hostdb should have a host after getting a host announcement transcation") 93 } 94 }