...

Source file src/github.com/theupdateframework/go-tuf/pkg/targets/hash_bins_test.go

Documentation: github.com/theupdateframework/go-tuf/pkg/targets

     1  package targets
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func BenchmarkHexEncode1(b *testing.B) {
    10  	for n := 0; n <= b.N; n++ {
    11  		for x := uint64(0); x <= 0xf; x += 1 {
    12  			hexEncode(x, 1)
    13  		}
    14  	}
    15  }
    16  
    17  func BenchmarkHexEncode4(b *testing.B) {
    18  	for n := 0; n <= b.N; n++ {
    19  		for x := uint64(0); x <= 0xffff; x += 1 {
    20  			hexEncode(x, 4)
    21  		}
    22  	}
    23  }
    24  
    25  func TestHashBin(t *testing.T) {
    26  	tcs := []struct {
    27  		hb           *HashBin
    28  		roleName     string
    29  		hashPrefixes []string
    30  	}{
    31  		{
    32  			hb: &HashBin{
    33  				rolePrefix:  "abc_",
    34  				hexDigitLen: 1,
    35  				first:       0x0,
    36  				last:        0x7,
    37  			},
    38  			roleName: "abc_0-7",
    39  			hashPrefixes: []string{
    40  				"0", "1", "2", "3", "4", "5", "6", "7",
    41  			},
    42  		},
    43  		{
    44  			hb: &HashBin{
    45  				rolePrefix:  "abc_",
    46  				hexDigitLen: 2,
    47  				first:       0x0,
    48  				last:        0xf,
    49  			},
    50  			roleName: "abc_00-0f",
    51  			hashPrefixes: []string{
    52  				"00", "01", "02", "03", "04", "05", "06", "07",
    53  				"08", "09", "0a", "0b", "0c", "0d", "0e", "0f",
    54  			},
    55  		},
    56  		{
    57  			hb: &HashBin{
    58  				rolePrefix:  "cba_",
    59  				hexDigitLen: 4,
    60  				first:       0xcd,
    61  				last:        0xcf,
    62  			},
    63  			roleName:     "cba_00cd-00cf",
    64  			hashPrefixes: []string{"00cd", "00ce", "00cf"},
    65  		},
    66  		{
    67  			hb: &HashBin{
    68  				rolePrefix:  "cba_",
    69  				hexDigitLen: 3,
    70  				first:       0xc1,
    71  				last:        0xc1,
    72  			},
    73  			roleName:     "cba_0c1",
    74  			hashPrefixes: []string{"0c1"},
    75  		},
    76  	}
    77  
    78  	for i, tc := range tcs {
    79  		assert.Equalf(t, tc.roleName, tc.hb.RoleName(), "test case %v: RoleName()", i)
    80  		assert.Equalf(t, tc.hashPrefixes, tc.hb.HashPrefixes(), "test case %v: HashPrefixes()", i)
    81  	}
    82  }
    83  
    84  func TestHashBins(t *testing.T) {
    85  	tcs := []struct {
    86  		bitLen    int
    87  		roleNames []string
    88  	}{
    89  		{1, []string{"0-7", "8-f"}},
    90  		{2, []string{"0-3", "4-7", "8-b", "c-f"}},
    91  		{3, []string{"0-1", "2-3", "4-5", "6-7", "8-9", "a-b", "c-d", "e-f"}},
    92  		{4, []string{
    93  			"0", "1", "2", "3", "4", "5", "6", "7",
    94  			"8", "9", "a", "b", "c", "d", "e", "f",
    95  		}},
    96  		{5, []string{
    97  			"00-07", "08-0f", "10-17", "18-1f", "20-27", "28-2f", "30-37", "38-3f",
    98  			"40-47", "48-4f", "50-57", "58-5f", "60-67", "68-6f", "70-77", "78-7f",
    99  			"80-87", "88-8f", "90-97", "98-9f", "a0-a7", "a8-af", "b0-b7", "b8-bf",
   100  			"c0-c7", "c8-cf", "d0-d7", "d8-df", "e0-e7", "e8-ef", "f0-f7", "f8-ff",
   101  		}},
   102  	}
   103  	for i, tc := range tcs {
   104  		got := []string{}
   105  		hbs, err := NewHashBins("", tc.bitLen)
   106  		assert.NoError(t, err)
   107  		n := hbs.NumBins()
   108  		for i := uint64(0); i < n; i += 1 {
   109  			hb := hbs.GetBin(i)
   110  			got = append(got, hb.RoleName())
   111  		}
   112  		assert.Equalf(t, tc.roleNames, got, "test case %v", i)
   113  	}
   114  
   115  	_, err := NewHashBins("", 0)
   116  	assert.Error(t, err)
   117  	_, err = NewHashBins("", 33)
   118  	assert.Error(t, err)
   119  }
   120  

View as plain text