github.com/decred/dcrlnd@v0.7.6/lnrpc/walletrpc/walletkit_util_test.go (about) 1 package walletrpc 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 func TestParseDerivationPath(t *testing.T) { 10 testCases := []struct { 11 name string 12 path string 13 expectedErr string 14 expectedResult []uint32 15 }{{ 16 name: "empty path", 17 path: "", 18 expectedErr: "path cannot be empty", 19 }, { 20 name: "just whitespace", 21 path: " \n\t\r", 22 expectedErr: "path cannot be empty", 23 }, { 24 name: "incorrect prefix", 25 path: "0/0", 26 expectedErr: "path must start with m/", 27 }, { 28 name: "invalid number", 29 path: "m/a'/0'", 30 expectedErr: "could not parse part \"a\": strconv.ParseInt", 31 }, { 32 name: "double slash", 33 path: "m/0'//", 34 expectedErr: "could not parse part \"\": strconv.ParseInt", 35 }, { 36 name: "number too large", 37 path: "m/99999999999999", 38 expectedErr: "could not parse part \"99999999999999\": strconv", 39 }, { 40 name: "empty path", 41 path: "m/", 42 expectedResult: []uint32{}, 43 }, { 44 name: "mixed path", 45 path: "m/0'/1'/2'/3/4/5/6'/7'", 46 expectedResult: []uint32{0, 1, 2, 3, 4, 5, 6, 7}, 47 }, { 48 name: "short path", 49 path: "m/0'", 50 expectedResult: []uint32{0}, 51 }, { 52 name: "plain path", 53 path: "m/0/1/2", 54 expectedResult: []uint32{0, 1, 2}, 55 }} 56 57 for _, tc := range testCases { 58 tc := tc 59 60 t.Run(tc.name, func(tt *testing.T) { 61 result, err := parseDerivationPath(tc.path) 62 63 if tc.expectedErr != "" { 64 require.Error(tt, err) 65 require.Contains( 66 tt, err.Error(), tc.expectedErr, 67 ) 68 } else { 69 require.NoError(tt, err) 70 require.Equal(tt, tc.expectedResult, result) 71 } 72 }) 73 } 74 }