github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/merkletree2/skippoiners_test.go (about)

     1  package merkletree2
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestSkipPointersForSeqno(t *testing.T) {
    11  
    12  	tests := []struct {
    13  		s        Seqno
    14  		pointers []Seqno
    15  	}{
    16  		{1, []Seqno{}},
    17  		{2, []Seqno{1}},
    18  		{3, []Seqno{2}},
    19  		{4, []Seqno{2, 3}},
    20  		{5, []Seqno{4}},
    21  		{10, []Seqno{8, 9}},
    22  		{26, []Seqno{16, 24, 25}},
    23  		{30, []Seqno{16, 24, 28, 29}},
    24  		{2048, []Seqno{1024, 1536, 1792, 1920, 1984, 2016, 2032, 2040, 2044, 2046, 2047}},
    25  		{2049, []Seqno{2048}},
    26  	}
    27  
    28  	for _, test := range tests {
    29  		t.Run(fmt.Sprintf("skips for %v", test.s), func(t *testing.T) {
    30  			skips := SkipPointersForSeqno(test.s)
    31  			require.EqualValues(t, test.pointers, skips)
    32  		})
    33  	}
    34  
    35  }
    36  
    37  func TestSkipPointersPath(t *testing.T) {
    38  
    39  	tests := []struct {
    40  		s        Seqno
    41  		e        Seqno
    42  		pointers []Seqno
    43  	}{
    44  		{1, 1, []Seqno{1}},
    45  		{1, 2, []Seqno{2}},
    46  		{1, 3, []Seqno{2, 3}},
    47  		{1, 4, []Seqno{2, 4}},
    48  		{1, 5, []Seqno{2, 4, 5}},
    49  		{1, 10, []Seqno{2, 4, 8, 10}},
    50  		{1, 26, []Seqno{2, 4, 8, 16, 26}},
    51  		{1, 30, []Seqno{2, 4, 8, 16, 30}},
    52  		{1, 2048, []Seqno{2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048}},
    53  		{5, 8, []Seqno{6, 8}},
    54  		{31, 65, []Seqno{32, 64, 65}},
    55  		{1023, 2049, []Seqno{1024, 2048, 2049}},
    56  	}
    57  
    58  	for _, test := range tests {
    59  		t.Run(fmt.Sprintf("skips for %v->%v", test.s, test.e), func(t *testing.T) {
    60  			skips, err := SkipPointersPath(test.s, test.e)
    61  			require.NoError(t, err)
    62  			require.EqualValues(t, test.pointers, skips)
    63  		})
    64  	}
    65  
    66  	_, err := SkipPointersPath(3, 2)
    67  	require.Error(t, err)
    68  	require.Contains(t, err.Error(), "start > end")
    69  }
    70  
    71  func TestComputeRootHashesNeededInExtensionProof(t *testing.T) {
    72  
    73  	tests := []struct {
    74  		s        Seqno
    75  		e        Seqno
    76  		pointers []Seqno
    77  	}{
    78  		{1, 1, []Seqno{}},
    79  		{1, 2, []Seqno{}},
    80  		{1, 3, []Seqno{}},
    81  		{1, 4, []Seqno{3}},
    82  		{1, 5, []Seqno{3}},
    83  		{1, 10, []Seqno{3, 6, 7, 9}},
    84  		{2, 15, []Seqno{3, 6, 7, 12, 14}},
    85  		{11, 30, []Seqno{8, 10, 14, 15, 24, 28, 29}},
    86  		{1, 128, []Seqno{3, 6, 7, 12, 14, 15, 24, 28, 30, 31, 48, 56, 60, 62, 63, 96, 112, 120, 124, 126, 127}},
    87  		{63, 128, []Seqno{32, 48, 56, 60, 62, 96, 112, 120, 124, 126, 127}},
    88  		{64, 128, []Seqno{96, 112, 120, 124, 126, 127}},
    89  	}
    90  
    91  	for _, test := range tests {
    92  		t.Run(fmt.Sprintf("skips for %v->%v", test.s, test.e), func(t *testing.T) {
    93  			skips, err := ComputeRootHashSeqnosNeededInExtensionProof(test.s, test.e)
    94  			require.NoError(t, err)
    95  			require.EqualValues(t, test.pointers, skips)
    96  		})
    97  	}
    98  
    99  	_, err := ComputeRootHashSeqnosNeededInExtensionProof(3, 2)
   100  	require.Error(t, err)
   101  	require.Contains(t, err.Error(), "start > end")
   102  }