func TestAddSectorFillLargerFolder(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	cmt, err := newContractManagerTester("TestAddSectorFillLargerFolder")
	if err != nil {
		t.Fatal(err)
	}
	defer cmt.panicClose()
	storageFolderDir := filepath.Join(cmt.persistDir, "storageFolderOne")
	err = os.MkdirAll(storageFolderDir, 0700)
	if err != nil {
		t.Fatal(err)
	}
	err = cmt.cm.AddStorageFolder(storageFolderDir, modules.SectorSize*storageFolderGranularity*2)
	if err != nil {
		t.Fatal(err)
	}
	roots := make([]crypto.Hash, storageFolderGranularity*2+1)
	datas := make([][]byte, storageFolderGranularity*2+1)
	var wg sync.WaitGroup
	for i := 0; i < storageFolderGranularity*2+1; i++ {
		wg.Add(1)
		go func(i int) {
			defer wg.Done()
			root, data := randSector()
			roots[i] = root
			datas[i] = data
		}(i)
	}
	wg.Wait()
	for i := 0; i < storageFolderGranularity*2; i++ {
		wg.Add(1)
		go func(i int) {
			defer wg.Done()
			err := cmt.cm.AddSector(roots[i], datas[i])
			if err != nil {
				t.Fatal(err)
			}
		}(i)
	}
	wg.Wait()
	sfs := cmt.cm.StorageFolders()
	if len(sfs) != 1 {
		t.Fatal("There should be one storage folder in the contract manager", len(sfs))
	}
	if sfs[0].CapacityRemaining != 0 {
		t.Error("Storage folder is supposed to be full", sfs[0].CapacityRemaining)
	}
	err = cmt.cm.AddSector(roots[storageFolderGranularity*2], datas[storageFolderGranularity*2])
	if err == nil {
		t.Error("expecting the storage folder to be full.")
	}
	for i := range roots[:storageFolderGranularity*2] {
		data, err := cmt.cm.ReadSector(roots[i])
		if err != nil {
			t.Fatal(err)
		}
		if !bytes.Equal(data, datas[i]) {
			t.Error("Contract manager returned the wrong data on a sector request")
		}
	}
}
TestAddSectorFillLargerFolder adds sectors to a 128 sector storage folder
until it is full.
package contractmanager
import (
	"bytes"
	"errors"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"testing"
	"github.com/NebulousLabs/Sia/crypto"
	"github.com/NebulousLabs/Sia/modules"
	"github.com/NebulousLabs/fastrand"
)
func randSector() (root crypto.Hash, data []byte) {
	data = fastrand.Bytes(int(modules.SectorSize))
	root = crypto.MerkleRoot(data)
	return
}
func TestAddSector(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	cmt, err := newContractManagerTester("TestAddSector")
	if err != nil {
		t.Fatal(err)
	}
	defer cmt.panicClose()
	storageFolderDir := filepath.Join(cmt.persistDir, "storageFolderOne")
	err = os.MkdirAll(storageFolderDir, 0700)
	if err != nil {
		t.Fatal(err)
	}
	err = cmt.cm.AddStorageFolder(storageFolderDir, modules.SectorSize*64)
	if err != nil {
		t.Fatal(err)
	}
	root, data := randSector()
	err = cmt.cm.AddSector(root, data)
	if err != nil {
		t.Fatal(err)
	}
	sfs := cmt.cm.StorageFolders()
	if len(sfs) != 1 {
		t.Fatal("There should be one storage folder in the contract manager", len(sfs))
	}
	if sfs[0].Capacity != sfs[0].CapacityRemaining+modules.SectorSize {
		t.Error("One sector's worth of capacity should be consumed:", sfs[0].Capacity, sfs[0].CapacityRemaining)
	}
	if len(cmt.cm.sectorLocations) != 1 {
		t.Fatal("there should be one sector reported in the sectorLocations map")
	}
	if len(cmt.cm.storageFolders) != 1 {
		t.Fatal("storage folder not being reported correctly")
	}
	var index uint16
	for _, sf := range cmt.cm.storageFolders {
		index = sf.index
	}
	for _, sl := range cmt.cm.sectorLocations {
		if sl.count != 1 {
			t.Error("Sector location should only be reporting one sector")
		}
		if sl.storageFolder != index {
			t.Error("Sector location is being reported incorrectly - wrong storage folder")
		}
		if sl.index > 64 {
			t.Error("sector index within storage folder also being reported incorrectly")
		}
	}
	found := false
	for _, u := range cmt.cm.storageFolders[index].usage {
		if u != 0 {
			found = true
			break
		}
	}
	if !found {
		t.Error("usage field does not seem to have been updated")
	}
	sectorData, err := cmt.cm.ReadSector(root)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(sectorData, data) {
		t.Fatal("wrong sector provided")
	}
	err = cmt.cm.Close()
	if err != nil {
		t.Fatal(err)
	}
	cmt.cm, err = New(filepath.Join(cmt.persistDir, modules.ContractManagerDir))
	if err != nil {
		t.Fatal(err)
	}
	sfs = cmt.cm.StorageFolders()
	if len(sfs) != 1 {
		t.Fatal("There should be one storage folder in the contract manager", len(sfs))
	}
	if sfs[0].Capacity != sfs[0].CapacityRemaining+modules.SectorSize {
		t.Error("One sector's worth of capacity should be consumed:", sfs[0].Capacity, sfs[0].CapacityRemaining)
	}
	if len(cmt.cm.sectorLocations) != 1 {
		t.Fatal("there should be one sector reported in the sectorLocations map")
	}
	if len(cmt.cm.storageFolders) != 1 {
		t.Fatal("storage folder not being reported correctly")
	}
	for _, sf := range cmt.cm.storageFolders {
		index = sf.index
	}
	for _, sl := range cmt.cm.sectorLocations {
		if sl.count != 1 {
			t.Error("Sector location should only be reporting one sector:", sl.count)
		}
		if sl.storageFolder != index {
			t.Error("Sector location is being reported incorrectly - wrong storage folder", sl.storageFolder, index)
		}
		if sl.index > 64 {
			t.Error("sector index within storage folder also being reported incorrectly")
		}
	}
	found = false
	for _, u := range cmt.cm.storageFolders[index].usage {
		if u != 0 {
			found = true
			break
		}
	}
	if !found {
		t.Error("usage field does not seem to have been updated")
	}
	sectorData, err = cmt.cm.ReadSector(root)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(sectorData, data) {
		t.Fatal("wrong sector provided")
	}
}
func TestAddSectorFillFolder(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	cmt, err := newContractManagerTester("TestAddSectorFillFolder")
	if err != nil {
		t.Fatal(err)
	}
	defer cmt.panicClose()
	storageFolderDir := filepath.Join(cmt.persistDir, "storageFolderOne")
	err = os.MkdirAll(storageFolderDir, 0700)
	if err != nil {
		t.Fatal(err)
	}
	err = cmt.cm.AddStorageFolder(storageFolderDir, modules.SectorSize*storageFolderGranularity)
	if err != nil {
		t.Fatal(err)
	}
	roots := make([]crypto.Hash, storageFolderGranularity+1)
	datas := make([][]byte, storageFolderGranularity+1)
	var wg sync.WaitGroup
	for i := 0; i < storageFolderGranularity+1; i++ {
		wg.Add(1)
		go func(i int) {
			defer wg.Done()
			root, data := randSector()
			roots[i] = root
			datas[i] = data
		}(i)
	}
	wg.Wait()
	for i := 0; i < storageFolderGranularity; i++ {
		wg.Add(1)
		go func(i int) {
			defer wg.Done()
			err := cmt.cm.AddSector(roots[i], datas[i])
			if err != nil {
				t.Fatal(err)
			}
		}(i)
	}
	wg.Wait()
	sfs := cmt.cm.StorageFolders()
	if len(sfs) != 1 {
		t.Fatal("There should be one storage folder in the contract manager", len(sfs))
	}
	if sfs[0].CapacityRemaining != 0 {
		t.Error("Storage folder is supposed to be full", sfs[0].CapacityRemaining)
	}
	err = cmt.cm.AddSector(roots[storageFolderGranularity], datas[storageFolderGranularity])
	if err == nil {
		t.Error("expecting the storage folder to be full.")
	}
	for i := range roots[:storageFolderGranularity] {
		data, err := cmt.cm.ReadSector(roots[i])
		if err != nil {
			t.Fatal(err)
		}
		if !bytes.Equal(data, datas[i]) {
			t.Error("Contract manager returned the wrong data on a sector request")
		}
	}
}