github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/renter/contractor/upload_test.go (about)

     1  package contractor
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/NebulousLabs/Sia/crypto"
     7  	"github.com/NebulousLabs/Sia/modules"
     8  	"github.com/NebulousLabs/Sia/types"
     9  )
    10  
    11  // editorHostDB is used to test the Editor method.
    12  type editorHostDB struct {
    13  	stubHostDB
    14  	hosts map[modules.NetAddress]modules.HostDBEntry
    15  }
    16  
    17  func (hdb editorHostDB) Host(addr modules.NetAddress) (modules.HostDBEntry, bool) {
    18  	h, ok := hdb.hosts[addr]
    19  	return h, ok
    20  }
    21  
    22  // TestEditor tests the failure conditions of the Editor method. The method is
    23  // more fully tested in the host integration test.
    24  func TestEditor(t *testing.T) {
    25  	// use a mock hostdb to supply hosts
    26  	hdb := &editorHostDB{
    27  		hosts: make(map[modules.NetAddress]modules.HostDBEntry),
    28  	}
    29  	c := &Contractor{
    30  		hdb: hdb,
    31  	}
    32  
    33  	// empty contract
    34  	_, err := c.Editor(modules.RenterContract{})
    35  	if err == nil {
    36  		t.Error("expected error, got nil")
    37  	}
    38  
    39  	// expired contract
    40  	c.blockHeight = 3
    41  	_, err = c.Editor(modules.RenterContract{})
    42  	if err == nil {
    43  		t.Error("expected error, got nil")
    44  	}
    45  	c.blockHeight = 0
    46  
    47  	// expensive host
    48  	_, hostPublicKey := crypto.GenerateKeyPairDeterministic([32]byte{})
    49  	dbe := modules.HostDBEntry{
    50  		PublicKey: types.SiaPublicKey{
    51  			Algorithm: types.SignatureEd25519,
    52  			Key:       hostPublicKey[:],
    53  		},
    54  	}
    55  	dbe.AcceptingContracts = true
    56  	dbe.StoragePrice = types.NewCurrency64(^uint64(0))
    57  	hdb.hosts["foo"] = dbe
    58  	_, err = c.Editor(modules.RenterContract{NetAddress: "foo"})
    59  	if err == nil {
    60  		t.Error("expected error, got nil")
    61  	}
    62  
    63  	// invalid contract
    64  	dbe.StoragePrice = types.NewCurrency64(500)
    65  	hdb.hosts["bar"] = dbe
    66  	_, err = c.Editor(modules.RenterContract{NetAddress: "bar"})
    67  	if err == nil {
    68  		t.Error("expected error, got nil")
    69  	}
    70  
    71  	// spent contract
    72  	contract := modules.RenterContract{
    73  		NetAddress: "bar",
    74  		LastRevision: types.FileContractRevision{
    75  			NewValidProofOutputs: []types.SiacoinOutput{
    76  				{Value: types.NewCurrency64(0)},
    77  				{Value: types.NewCurrency64(^uint64(0))},
    78  			},
    79  		},
    80  	}
    81  	_, err = c.Editor(contract)
    82  	if err == nil {
    83  		t.Error("expected error, got nil")
    84  	}
    85  }