gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/renter/hostdb/update_test.go (about)

     1  package hostdb
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gitlab.com/SiaPrime/SiaPrime/crypto"
     7  	"gitlab.com/SiaPrime/SiaPrime/modules"
     8  	"gitlab.com/SiaPrime/SiaPrime/types"
     9  )
    10  
    11  // makeSignedAnnouncement creates a []byte that contains an encoded and signed
    12  // host announcement for the given net address.
    13  func makeSignedAnnouncement(na modules.NetAddress) ([]byte, error) {
    14  	sk, pk := crypto.GenerateKeyPair()
    15  	spk := types.SiaPublicKey{
    16  		Algorithm: types.SignatureEd25519,
    17  		Key:       pk[:],
    18  	}
    19  	return modules.CreateAnnouncement(na, spk, sk)
    20  }
    21  
    22  // TestFindHostAnnouncements probes the findHostAnnouncements function
    23  func TestFindHostAnnouncements(t *testing.T) {
    24  	annBytes, err := makeSignedAnnouncement("foo.com:1234")
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	b := types.Block{
    29  		Transactions: []types.Transaction{
    30  			{
    31  				ArbitraryData: [][]byte{annBytes},
    32  			},
    33  		},
    34  	}
    35  	announcements := findHostAnnouncements(b)
    36  	if len(announcements) != 1 {
    37  		t.Error("host announcement not found in block")
    38  	}
    39  
    40  	// Try with an altered prefix
    41  	b.Transactions[0].ArbitraryData[0][0]++
    42  	announcements = findHostAnnouncements(b)
    43  	if len(announcements) != 0 {
    44  		t.Error("host announcement found when there was an invalid prefix")
    45  	}
    46  	b.Transactions[0].ArbitraryData[0][0]--
    47  
    48  	// Try with an invalid host encoding.
    49  	b.Transactions[0].ArbitraryData[0][17]++
    50  	announcements = findHostAnnouncements(b)
    51  	if len(announcements) != 0 {
    52  		t.Error("host announcement found when there was an invalid encoding of a host announcement")
    53  	}
    54  }