github.com/sunriselayer/sunrise-da@v0.13.1-sr3/cmd/util_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/sunriselayer/sunrise-da/share"
     9  )
    10  
    11  func Test_parseNamespaceID(t *testing.T) {
    12  	type testCase struct {
    13  		name    string
    14  		param   string
    15  		want    share.Namespace
    16  		wantErr bool
    17  	}
    18  	testCases := []testCase{
    19  		{
    20  			param: "0x0c204d39600fddd3",
    21  			name:  "8 byte hex encoded namespace ID gets left padded",
    22  			want: share.Namespace{
    23  				0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
    24  				0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x20, 0x4d, 0x39, 0x60, 0xf, 0xdd, 0xd3,
    25  			},
    26  			wantErr: false,
    27  		},
    28  		{
    29  			name:  "10 byte hex encoded namespace ID",
    30  			param: "0x42690c204d39600fddd3",
    31  			want: share.Namespace{
    32  				0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
    33  				0x0, 0x0, 0x0, 0x0, 0x42, 0x69, 0xc, 0x20, 0x4d, 0x39, 0x60, 0xf, 0xdd, 0xd3,
    34  			},
    35  			wantErr: false,
    36  		},
    37  		{
    38  			name:  "29 byte hex encoded namespace ID",
    39  			param: "0x0000000000000000000000000000000000000001010101010101010101",
    40  			want: share.Namespace{
    41  				0x0,                                                                                      // namespace version
    42  				0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // v0 ID prefix
    43  				0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // namespace ID
    44  			},
    45  			wantErr: true,
    46  		},
    47  		{
    48  			name:    "11 byte hex encoded namespace ID returns error",
    49  			param:   "0x42690c204d39600fddd3a3",
    50  			want:    share.Namespace{},
    51  			wantErr: true,
    52  		},
    53  		{
    54  			name:  "10 byte base64 encoded namespace ID",
    55  			param: "QmkMIE05YA/d0w==",
    56  			want: share.Namespace{
    57  				0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
    58  				0x0, 0x0, 0x0, 0x0, 0x42, 0x69, 0xc, 0x20, 0x4d, 0x39, 0x60, 0xf, 0xdd, 0xd3,
    59  			},
    60  			wantErr: false,
    61  		},
    62  		{
    63  			name:    "not base64 or hex encoded namespace ID returns error",
    64  			param:   "5748493939429",
    65  			want:    share.Namespace{},
    66  			wantErr: true,
    67  		},
    68  	}
    69  
    70  	for _, tc := range testCases {
    71  		t.Run(tc.name, func(t *testing.T) {
    72  			got, err := ParseV0Namespace(tc.param)
    73  			if tc.wantErr {
    74  				assert.Error(t, err)
    75  				return
    76  			}
    77  			assert.NoError(t, err)
    78  			assert.Equal(t, tc.want, got)
    79  		})
    80  	}
    81  }