github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/modules/renter/contractor/persist_test.go (about) 1 package contractor 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "SiaPrime/build" 10 "SiaPrime/modules" 11 "SiaPrime/modules/renter/proto" 12 "SiaPrime/types" 13 ) 14 15 // memPersist implements the persister interface in-memory. 16 type memPersist contractorPersist 17 18 func (m *memPersist) save(data contractorPersist) error { *m = memPersist(data); return nil } 19 func (m memPersist) load(data *contractorPersist) error { *data = contractorPersist(m); return nil } 20 21 // TestSaveLoad tests that the contractor can save and load itself. 22 func TestSaveLoad(t *testing.T) { 23 // create contractor with mocked persist dependency 24 c := &Contractor{ 25 persist: new(memPersist), 26 } 27 28 c.oldContracts = map[types.FileContractID]modules.RenterContract{ 29 {0}: {ID: types.FileContractID{0}, HostPublicKey: types.SiaPublicKey{Key: []byte("foo")}}, 30 {1}: {ID: types.FileContractID{1}, HostPublicKey: types.SiaPublicKey{Key: []byte("bar")}}, 31 {2}: {ID: types.FileContractID{2}, HostPublicKey: types.SiaPublicKey{Key: []byte("baz")}}, 32 } 33 34 c.renewedFrom = map[types.FileContractID]types.FileContractID{ 35 {1}: {2}, 36 } 37 c.renewedTo = map[types.FileContractID]types.FileContractID{ 38 {1}: {2}, 39 } 40 41 // save, clear, and reload 42 err := c.save() 43 if err != nil { 44 t.Fatal(err) 45 } 46 c.hdb = stubHostDB{} 47 c.oldContracts = make(map[types.FileContractID]modules.RenterContract) 48 c.renewedFrom = make(map[types.FileContractID]types.FileContractID) 49 c.renewedTo = make(map[types.FileContractID]types.FileContractID) 50 err = c.load() 51 if err != nil { 52 t.Fatal(err) 53 } 54 // Check that all fields were restored 55 _, ok0 := c.oldContracts[types.FileContractID{0}] 56 _, ok1 := c.oldContracts[types.FileContractID{1}] 57 _, ok2 := c.oldContracts[types.FileContractID{2}] 58 if !ok0 || !ok1 || !ok2 { 59 t.Fatal("oldContracts were not restored properly:", c.oldContracts) 60 } 61 id := types.FileContractID{2} 62 if c.renewedFrom[types.FileContractID{1}] != id { 63 t.Fatal("renewedFrom not restored properly:", c.renewedFrom) 64 } 65 if c.renewedTo[types.FileContractID{1}] != id { 66 t.Fatal("renewedTo not restored properly:", c.renewedTo) 67 } 68 // use stdPersist instead of mock 69 c.persist = NewPersist(build.TempDir("contractor", t.Name())) 70 os.MkdirAll(build.TempDir("contractor", t.Name()), 0700) 71 72 // save, clear, and reload 73 err = c.save() 74 if err != nil { 75 t.Fatal(err) 76 } 77 c.oldContracts = make(map[types.FileContractID]modules.RenterContract) 78 c.renewedFrom = make(map[types.FileContractID]types.FileContractID) 79 c.renewedTo = make(map[types.FileContractID]types.FileContractID) 80 err = c.load() 81 if err != nil { 82 t.Fatal(err) 83 } 84 // check that all fields were restored 85 _, ok0 = c.oldContracts[types.FileContractID{0}] 86 _, ok1 = c.oldContracts[types.FileContractID{1}] 87 _, ok2 = c.oldContracts[types.FileContractID{2}] 88 if !ok0 || !ok1 || !ok2 { 89 t.Fatal("oldContracts were not restored properly:", c.oldContracts) 90 } 91 if c.renewedFrom[types.FileContractID{1}] != id { 92 t.Fatal("renewedFrom not restored properly:", c.renewedFrom) 93 } 94 if c.renewedTo[types.FileContractID{1}] != id { 95 t.Fatal("renewedTo not restored properly:", c.renewedTo) 96 } 97 } 98 99 // TestConvertPersist tests that contracts previously stored in the 100 // .journal format can be converted to the .contract format. 101 func TestConvertPersist(t *testing.T) { 102 dir := build.TempDir(filepath.Join("contractor", t.Name())) 103 os.MkdirAll(dir, 0700) 104 // copy the test data into the temp folder 105 testdata, err := ioutil.ReadFile(filepath.Join("testdata", "TestConvertPersist.journal")) 106 if err != nil { 107 t.Fatal(err) 108 } 109 err = ioutil.WriteFile(filepath.Join(dir, "contractor.journal"), testdata, 0600) 110 if err != nil { 111 t.Fatal(err) 112 } 113 114 // convert the journal 115 err = convertPersist(dir) 116 if err != nil { 117 t.Fatal(err) 118 } 119 120 // load the persist 121 var p contractorPersist 122 err = NewPersist(dir).load(&p) 123 if err != nil { 124 t.Fatal(err) 125 } 126 if !p.Allowance.Funds.Equals64(10) || p.Allowance.Hosts != 7 || p.Allowance.Period != 3 || p.Allowance.RenewWindow != 20 { 127 t.Fatal("recovered allowance was wrong:", p.Allowance) 128 } 129 130 // load the contracts 131 cs, err := proto.NewContractSet(filepath.Join(dir, "contracts"), modules.ProdDependencies) 132 if err != nil { 133 t.Fatal(err) 134 } 135 if cs.Len() != 1 { 136 t.Fatal("expected 1 contract, got", cs.Len()) 137 } 138 m := cs.ViewAll()[0] 139 if m.ID.String() != "792b5eec683819d78416a9e80cba454ebcb5a52eeac4f17b443d177bd425fc5c" { 140 t.Fatal("recovered contract has wrong ID", m.ID) 141 } 142 }