github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/chat/utils/kbfs_path_utils_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/keybase/client/go/libkb"
     7  	"github.com/keybase/client/go/protocol/chat1"
     8  	"github.com/keybase/client/go/protocol/keybase1"
     9  	"github.com/stretchr/testify/require"
    10  	"golang.org/x/net/context"
    11  )
    12  
    13  func strPointer(str string) *string { return &str }
    14  func makeKBFSPathForTest(rawPath string, standardPath *string) chat1.KBFSPath {
    15  	if standardPath == nil {
    16  		return chat1.KBFSPath{RawPath: rawPath, StandardPath: rawPath}
    17  	}
    18  	return chat1.KBFSPath{RawPath: rawPath, StandardPath: *standardPath}
    19  }
    20  
    21  var kbfsPathTests = map[string]chat1.KBFSPath{
    22  	"/keybase ha":                            makeKBFSPathForTest("/keybase", nil),
    23  	"/keybase/哟":                             {},
    24  	"before/keybase":                         {},
    25  	"之前/keybase":                             makeKBFSPathForTest("/keybase", nil),
    26  	"/keybase/public":                        makeKBFSPathForTest("/keybase/public", nil),
    27  	"/keybase/team":                          makeKBFSPathForTest("/keybase/team", nil),
    28  	"/keybase/private/":                      makeKBFSPathForTest("/keybase/private/", strPointer("/keybase/private")),
    29  	"/keybase/team/keybase":                  makeKBFSPathForTest("/keybase/team/keybase", nil),
    30  	"/keybase/team/keybase/blahblah":         makeKBFSPathForTest("/keybase/team/keybase/blahblah", nil),
    31  	`/keybase/team/keybase/blah\ blah\ blah`: makeKBFSPathForTest(`/keybase/team/keybase/blah\ blah\ blah`, strPointer("/keybase/team/keybase/blah blah blah")),
    32  	`/keybase/team/keybase/blah\\blah\\blah`: makeKBFSPathForTest(`/keybase/team/keybase/blah\\blah\\blah`, strPointer(`/keybase/team/keybase/blah\blah\blah`)),
    33  	"/keybase/team/keybase/blahblah/":        makeKBFSPathForTest("/keybase/team/keybase/blahblah/", strPointer("/keybase/team/keybase/blahblah")),
    34  	"/keybase/private/songgao/🍻":             makeKBFSPathForTest("/keybase/private/songgao/🍻", nil),
    35  	"/keybase/private/songgao/🍻/🍹.png/":      makeKBFSPathForTest("/keybase/private/songgao/🍻/🍹.png/", strPointer("/keybase/private/songgao/🍻/🍹.png")),
    36  	"/keybase/private/songgao/囧/yo":          makeKBFSPathForTest("/keybase/private/songgao/囧/yo", nil),
    37  	"/keybase/team/keybase,blah":             {},
    38  	"/keybase/team/keybase.blah":             makeKBFSPathForTest("/keybase/team/keybase.blah", nil),
    39  	"/keybaseprivate":                        {},
    40  	"/keybaseprivate/team":                   {},
    41  	"/keybase/teamaa/keybase":                {},
    42  	"/keybase/.kbfs_status":                  makeKBFSPathForTest("/keybase/.kbfs_status", nil),
    43  	"/foo":                                   {},
    44  
    45  	"/keybase/private/songgao,strib#jzila/file":                                                                   makeKBFSPathForTest("/keybase/private/songgao,strib#jzila/file", nil),
    46  	"/keybase/private/song-gao,strib#jzila/file":                                                                  {},
    47  	"/keybase/private/songgao,strib#jzila,jakob223/file":                                                          makeKBFSPathForTest("/keybase/private/songgao,strib#jzila,jakob223/file", nil),
    48  	"/keybase/private/__songgao__@twitter,strib@github,jzila@reddit,jakob.weisbl.at@dns/file":                     makeKBFSPathForTest("/keybase/private/__songgao__@twitter,strib@github,jzila@reddit,jakob.weisbl.at@dns/file", nil),
    49  	`"/keybase/private/songgao,kbpbot_staging,songgao_test (files before songgao_test account reset 2019-05-10)"`: makeKBFSPathForTest(`"/keybase/private/songgao,kbpbot_staging,songgao_test (files before songgao_test account reset 2019-05-10)"`, strPointer("/keybase/private/songgao,kbpbot_staging,songgao_test (files before songgao_test account reset 2019-05-10)")),
    50  	"/keybase/private/songgao,[meatball+keybase@gao.io]@email":                                                    makeKBFSPathForTest("/keybase/private/songgao,[meatball+keybase@gao.io]@email", strPointer("/keybase/private/songgao,[meatball+keybase@gao.io]@email")),
    51  
    52  	"keybase://team/keybase/blahblah":                               makeKBFSPathForTest("keybase://team/keybase/blahblah", strPointer("/keybase/team/keybase/blahblah")),
    53  	"keybase://private/foo/blahblah":                                makeKBFSPathForTest("keybase://private/foo/blahblah", strPointer("/keybase/private/foo/blahblah")),
    54  	"keybase://public/foo/blahblah":                                 makeKBFSPathForTest("keybase://public/foo/blahblah", strPointer("/keybase/public/foo/blahblah")),
    55  	"keybase://public/foo/blah%20blah":                              makeKBFSPathForTest("keybase://public/foo/blah%20blah", strPointer("/keybase/public/foo/blah blah")),
    56  	"keybase://chat/blah":                                           {},
    57  	"keybase://private/songgao,[meatball+keybase@gao.io]@email/abc": makeKBFSPathForTest("keybase://private/songgao,[meatball+keybase@gao.io]@email/abc", strPointer("/keybase/private/songgao,[meatball+keybase@gao.io]@email/abc")),
    58  
    59  	"/Volumes/Keybase/team/keybase/blahblah":             makeKBFSPathForTest("/Volumes/Keybase/team/keybase/blahblah", strPointer("/keybase/team/keybase/blahblah")),
    60  	"/Volumes/Keybase/private/foo/blahblah":              makeKBFSPathForTest("/Volumes/Keybase/private/foo/blahblah", strPointer("/keybase/private/foo/blahblah")),
    61  	"/Volumes/Keybase/public/foo/blahblah":               makeKBFSPathForTest("/Volumes/Keybase/public/foo/blahblah", strPointer("/keybase/public/foo/blahblah")),
    62  	`/Volumes/Keybase\ (meatball)/team/keybase/blahblah`: makeKBFSPathForTest(`/Volumes/Keybase\ (meatball)/team/keybase/blahblah`, strPointer("/keybase/team/keybase/blahblah")),
    63  	`/Volumes/Keybase\ (meatball)/private/foo/blahblah`:  makeKBFSPathForTest(`/Volumes/Keybase\ (meatball)/private/foo/blahblah`, strPointer("/keybase/private/foo/blahblah")),
    64  	`/Volumes/Keybase\ (meatball)/public/foo/blahblah`:   makeKBFSPathForTest(`/Volumes/Keybase\ (meatball)/public/foo/blahblah`, strPointer("/keybase/public/foo/blahblah")),
    65  	`"/Volumes/Keybase (meatball)/public/foo/blahblah"`:  makeKBFSPathForTest(`"/Volumes/Keybase (meatball)/public/foo/blahblah"`, strPointer("/keybase/public/foo/blahblah")),
    66  
    67  	`K:\team\keybase\blahblah`:        makeKBFSPathForTest(`K:\team\keybase\blahblah`, strPointer("/keybase/team/keybase/blahblah")),
    68  	`K:\private\foo\blahblah`:         makeKBFSPathForTest(`K:\private\foo\blahblah`, strPointer("/keybase/private/foo/blahblah")),
    69  	`k:\public\foo\blahblah`:          makeKBFSPathForTest(`k:\public\foo\blahblah`, strPointer("/keybase/public/foo/blahblah")),
    70  	`K:\public\foo\blahblah lalala`:   makeKBFSPathForTest(`K:\public\foo\blahblah`, strPointer("/keybase/public/foo/blahblah")),
    71  	`"K:\public\foo\blahblah lalala"`: makeKBFSPathForTest(`"K:\public\foo\blahblah lalala"`, strPointer("/keybase/public/foo/blahblah lalala")),
    72  
    73  	"/keybase.":                        makeKBFSPathForTest("/keybase", nil),
    74  	"/keybase/team.":                   makeKBFSPathForTest("/keybase/team", nil),
    75  	"/keybase/team/keybase/blahblah.":  makeKBFSPathForTest("/keybase/team/keybase/blahblah", nil),
    76  	`K:\team\keybase\blahblah.`:        makeKBFSPathForTest(`K:\team\keybase\blahblah`, strPointer("/keybase/team/keybase/blahblah")),
    77  	"keybase://team/keybase/blahblah.": makeKBFSPathForTest("keybase://team/keybase/blahblah", strPointer("/keybase/team/keybase/blahblah")),
    78  
    79  	"/keybase?":                        makeKBFSPathForTest("/keybase", nil),
    80  	"/keybase/team?":                   makeKBFSPathForTest("/keybase/team", nil),
    81  	"/keybase/team/keybase/blahblah?":  makeKBFSPathForTest("/keybase/team/keybase/blahblah", nil),
    82  	`K:\team\keybase\blahblah?`:        makeKBFSPathForTest(`K:\team\keybase\blahblah`, strPointer("/keybase/team/keybase/blahblah")),
    83  	"keybase://team/keybase/blahblah?": makeKBFSPathForTest("keybase://team/keybase/blahblah", strPointer("/keybase/team/keybase/blahblah")),
    84  
    85  	`"/keybase/team/keybase/blahblah."`: makeKBFSPathForTest(`"/keybase/team/keybase/blahblah."`, strPointer("/keybase/team/keybase/blahblah.")),
    86  	`"K:\team\keybase\blahblah."`:       makeKBFSPathForTest(`"K:\team\keybase\blahblah."`, strPointer("/keybase/team/keybase/blahblah.")),
    87  
    88  	`"/keybase/team/keybase (local conflicted copy 2019-10-24 #2)"`: makeKBFSPathForTest(`"/keybase/team/keybase (local conflicted copy 2019-10-24 #2)"`, strPointer("/keybase/team/keybase (local conflicted copy 2019-10-24 #2)")),
    89  }
    90  
    91  func TestParseKBFSPathMatches(t *testing.T) {
    92  	for input, expected := range kbfsPathTests {
    93  		paths := ParseKBFSPaths(context.Background(), input)
    94  		if len(expected.RawPath) > 0 {
    95  			require.Len(t, paths, 1, "error matching: %s", input)
    96  			require.Equal(t, expected.RawPath, paths[0].RawPath, "wrong RawPath %q", input)
    97  			require.Equal(t, expected.StandardPath, paths[0].StandardPath, "wrong RebasePath %q", input)
    98  		} else {
    99  			require.Len(t, paths, 0, "unexpected match: %s", input)
   100  		}
   101  	}
   102  }
   103  
   104  func TestParseKBFSPathDetailed(t *testing.T) {
   105  	for _, input := range []string{
   106  		`this is a kbfs path /keybase/team/keybase/blah\ blah\ blah`,
   107  		`this is a kbfs path "K:\team\keybase\blah blah blah"`,
   108  	} {
   109  		paths := ParseKBFSPaths(context.Background(), input)
   110  		require.Len(t, paths, 1, "input: %s", input)
   111  		require.Equal(t, 20, paths[0].StartIndex, "input: %s", input)
   112  		require.Equal(t, "/keybase/team/keybase/blah blah blah", paths[0].StandardPath, "input: %s", input)
   113  		require.Equal(t, "keybase://team/keybase/blah%20blah%20blah", paths[0].PathInfo.DeeplinkPath, "input: %s", input)
   114  		if libkb.RuntimeGroup() == keybase1.RuntimeGroup_WINDOWSLIKE {
   115  			require.Equal(t, `\team\keybase\blah blah blah`, paths[0].PathInfo.PlatformAfterMountPath, "input: %s", input)
   116  		} else {
   117  			require.Equal(t, "/team/keybase/blah blah blah", paths[0].PathInfo.PlatformAfterMountPath, "input: %s", input)
   118  		}
   119  	}
   120  
   121  	withSuffix := `"/keybase/private/songgao,kbpbot_staging,songgao_test (files before songgao_test account reset 2019-05-10)"`
   122  	paths := ParseKBFSPaths(context.Background(), withSuffix)
   123  	require.Len(t, paths, 1, "input: %s", withSuffix)
   124  	require.Equal(t, "keybase://private/songgao%2Ckbpbot_staging%2Csonggao_test%20%28files%20before%20songgao_test%20account%20reset%202019-05-10%29", paths[0].PathInfo.DeeplinkPath)
   125  }