gitlab.com/SkynetLabs/skyd@v1.6.9/skymodules/renter/proto/session_test.go (about)

     1  package proto
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"go.sia.tech/siad/crypto"
     8  	"go.sia.tech/siad/modules"
     9  )
    10  
    11  func TestCalculateProofRanges(t *testing.T) {
    12  	tests := []struct {
    13  		desc       string
    14  		numSectors uint64
    15  		actions    []modules.LoopWriteAction
    16  		exp        []crypto.ProofRange
    17  	}{
    18  		{
    19  			desc:       "Append",
    20  			numSectors: 2,
    21  			actions: []modules.LoopWriteAction{
    22  				{Type: modules.WriteActionAppend, Data: []byte{1, 2, 3}},
    23  			},
    24  			exp: []crypto.ProofRange{},
    25  		},
    26  		{
    27  			desc:       "Swap",
    28  			numSectors: 3,
    29  			actions: []modules.LoopWriteAction{
    30  				{Type: modules.WriteActionSwap, A: 1, B: 2},
    31  			},
    32  			exp: []crypto.ProofRange{
    33  				{Start: 1, End: 2},
    34  				{Start: 2, End: 3},
    35  			},
    36  		},
    37  		{
    38  			desc:       "Trim",
    39  			numSectors: 3,
    40  			actions: []modules.LoopWriteAction{
    41  				{Type: modules.WriteActionTrim, A: 1},
    42  			},
    43  			exp: []crypto.ProofRange{
    44  				{Start: 2, End: 3},
    45  			},
    46  		},
    47  		{
    48  			desc:       "AppendSwapTrim",
    49  			numSectors: 12,
    50  			actions: []modules.LoopWriteAction{
    51  				{Type: modules.WriteActionAppend, Data: []byte{1, 2, 3}},
    52  				{Type: modules.WriteActionSwap, A: 5, B: 12},
    53  				{Type: modules.WriteActionTrim, A: 1},
    54  			},
    55  			exp: []crypto.ProofRange{
    56  				{Start: 5, End: 6},
    57  			},
    58  		},
    59  		{
    60  			desc:       "SwapTrimSwapAppendAppend",
    61  			numSectors: 12,
    62  			actions: []modules.LoopWriteAction{
    63  				{Type: modules.WriteActionSwap, A: 6, B: 11},
    64  				{Type: modules.WriteActionTrim, A: 1},
    65  				{Type: modules.WriteActionSwap, A: 7, B: 10},
    66  				{Type: modules.WriteActionAppend, Data: []byte{1, 2, 3}},
    67  				{Type: modules.WriteActionAppend, Data: []byte{4, 5, 6}},
    68  			},
    69  			exp: []crypto.ProofRange{
    70  				{Start: 6, End: 7},
    71  				{Start: 7, End: 8},
    72  				{Start: 10, End: 11},
    73  				{Start: 11, End: 12},
    74  			},
    75  		},
    76  	}
    77  	for _, test := range tests {
    78  		t.Run(test.desc, func(t *testing.T) {
    79  			res := calculateProofRanges(test.actions, test.numSectors)
    80  			if !reflect.DeepEqual(res, test.exp) {
    81  				t.Errorf("incorrect ranges: expected %v, got %v", test.exp, res)
    82  			}
    83  		})
    84  	}
    85  }
    86  
    87  func TestModifyProofRanges(t *testing.T) {
    88  	tests := []struct {
    89  		desc        string
    90  		numSectors  uint64
    91  		actions     []modules.LoopWriteAction
    92  		proofRanges []crypto.ProofRange
    93  		exp         []crypto.ProofRange
    94  	}{
    95  		{
    96  			desc:       "Append",
    97  			numSectors: 2,
    98  			actions: []modules.LoopWriteAction{
    99  				{Type: modules.WriteActionAppend, Data: []byte{1, 2, 3}},
   100  			},
   101  			proofRanges: nil,
   102  			exp: []crypto.ProofRange{
   103  				{Start: 2, End: 3},
   104  			},
   105  		},
   106  		{
   107  			desc:       "Swap",
   108  			numSectors: 3,
   109  			actions: []modules.LoopWriteAction{
   110  				{Type: modules.WriteActionSwap, A: 1, B: 2},
   111  			},
   112  			proofRanges: []crypto.ProofRange{
   113  				{Start: 1, End: 2},
   114  				{Start: 2, End: 3},
   115  			},
   116  			exp: []crypto.ProofRange{
   117  				{Start: 1, End: 2},
   118  				{Start: 2, End: 3},
   119  			},
   120  		},
   121  		{
   122  			desc:       "Trim",
   123  			numSectors: 3,
   124  			actions: []modules.LoopWriteAction{
   125  				{Type: modules.WriteActionTrim, A: 1},
   126  			},
   127  			proofRanges: []crypto.ProofRange{
   128  				{Start: 2, End: 3},
   129  			},
   130  			exp: []crypto.ProofRange{},
   131  		},
   132  		{
   133  			desc:       "AppendSwapTrim",
   134  			numSectors: 12,
   135  			actions: []modules.LoopWriteAction{
   136  				{Type: modules.WriteActionAppend, Data: []byte{1, 2, 3}},
   137  				{Type: modules.WriteActionSwap, A: 5, B: 12},
   138  				{Type: modules.WriteActionTrim, A: 1},
   139  			},
   140  			proofRanges: []crypto.ProofRange{
   141  				{Start: 5, End: 6},
   142  			},
   143  			exp: []crypto.ProofRange{
   144  				{Start: 5, End: 6},
   145  			},
   146  		},
   147  		{
   148  			desc:       "SwapTrimSwapAppendAppend",
   149  			numSectors: 12,
   150  			actions: []modules.LoopWriteAction{
   151  				{Type: modules.WriteActionSwap, A: 6, B: 11},
   152  				{Type: modules.WriteActionTrim, A: 1},
   153  				{Type: modules.WriteActionSwap, A: 7, B: 10},
   154  				{Type: modules.WriteActionAppend, Data: []byte{1, 2, 3}},
   155  				{Type: modules.WriteActionAppend, Data: []byte{4, 5, 6}},
   156  			},
   157  			proofRanges: []crypto.ProofRange{
   158  				{Start: 6, End: 7},
   159  				{Start: 7, End: 8},
   160  				{Start: 10, End: 11},
   161  				{Start: 11, End: 12},
   162  			},
   163  			exp: []crypto.ProofRange{
   164  				{Start: 6, End: 7},
   165  				{Start: 7, End: 8},
   166  				{Start: 10, End: 11},
   167  				{Start: 11, End: 12},
   168  				{Start: 12, End: 13},
   169  			},
   170  		},
   171  	}
   172  	for _, test := range tests {
   173  		t.Run(test.desc, func(t *testing.T) {
   174  			res := modifyProofRanges(test.proofRanges, test.actions, test.numSectors)
   175  			if !reflect.DeepEqual(res, test.exp) {
   176  				t.Errorf("incorrect modification: expected %v, got %v", test.exp, res)
   177  			}
   178  		})
   179  	}
   180  }
   181  
   182  func TestModifyLeafHashes(t *testing.T) {
   183  	tests := []struct {
   184  		desc       string
   185  		numSectors uint64
   186  		actions    []modules.LoopWriteAction
   187  		leaves     []crypto.Hash
   188  		exp        []crypto.Hash
   189  	}{
   190  		{
   191  			desc:       "Append",
   192  			numSectors: 2,
   193  			actions: []modules.LoopWriteAction{
   194  				{Type: modules.WriteActionAppend, Data: []byte{1, 2, 3}},
   195  			},
   196  			leaves: nil,
   197  			exp:    []crypto.Hash{crypto.MerkleRoot([]byte{1, 2, 3})},
   198  		},
   199  		{
   200  			desc:       "Swap",
   201  			numSectors: 3,
   202  			actions: []modules.LoopWriteAction{
   203  				{Type: modules.WriteActionSwap, A: 1, B: 2},
   204  			},
   205  			leaves: []crypto.Hash{{1}, {2}},
   206  			exp:    []crypto.Hash{{2}, {1}},
   207  		},
   208  		{
   209  			desc:       "Trim",
   210  			numSectors: 3,
   211  			actions: []modules.LoopWriteAction{
   212  				{Type: modules.WriteActionTrim, A: 1},
   213  			},
   214  			leaves: []crypto.Hash{{1}},
   215  			exp:    []crypto.Hash{},
   216  		},
   217  		{
   218  			desc:       "AppendSwapTrim",
   219  			numSectors: 12,
   220  			actions: []modules.LoopWriteAction{
   221  				{Type: modules.WriteActionAppend, Data: []byte{1, 2, 3}},
   222  				{Type: modules.WriteActionSwap, A: 5, B: 12},
   223  				{Type: modules.WriteActionTrim, A: 1},
   224  			},
   225  			leaves: []crypto.Hash{{1}},
   226  			exp:    []crypto.Hash{crypto.MerkleRoot([]byte{1, 2, 3})},
   227  		},
   228  		{
   229  			desc:       "SwapTrimSwapAppendAppend",
   230  			numSectors: 12,
   231  			actions: []modules.LoopWriteAction{
   232  				{Type: modules.WriteActionSwap, A: 6, B: 11},
   233  				{Type: modules.WriteActionTrim, A: 1},
   234  				{Type: modules.WriteActionSwap, A: 7, B: 10},
   235  				{Type: modules.WriteActionAppend, Data: []byte{1, 2, 3}},
   236  				{Type: modules.WriteActionAppend, Data: []byte{4, 5, 6}},
   237  			},
   238  			leaves: []crypto.Hash{{1}, {2}, {3}, {4}},
   239  			exp:    []crypto.Hash{{4}, {3}, {2}, crypto.MerkleRoot([]byte{1, 2, 3}), crypto.MerkleRoot([]byte{4, 5, 6})},
   240  		},
   241  	}
   242  	for _, test := range tests {
   243  		t.Run(test.desc, func(t *testing.T) {
   244  			res := modifyLeaves(test.leaves, test.actions, test.numSectors)
   245  			if !reflect.DeepEqual(res, test.exp) {
   246  				t.Errorf("incorrect modification: expected %v, got %v", test.exp, res)
   247  			}
   248  		})
   249  	}
   250  }