github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/simplefs/downloads_test.go (about) 1 // Copyright 2019 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package simplefs 6 7 import ( 8 "fmt" 9 10 "os" 11 "path/filepath" 12 "strings" 13 "testing" 14 "time" 15 16 "github.com/keybase/client/go/kbfs/env" 17 "github.com/keybase/client/go/kbfs/libkbfs" 18 "github.com/keybase/client/go/protocol/keybase1" 19 "github.com/stretchr/testify/require" 20 "golang.org/x/net/context" 21 ) 22 23 func TestDownloadManager(t *testing.T) { 24 ctx := context.Background() 25 config := libkbfs.MakeTestConfigOrBust(t, "jdoe") 26 sfs := newSimpleFS(env.EmptyAppStateUpdater{}, config) 27 defer closeSimpleFS(ctx, t, sfs) 28 29 t.Log("Write a file in the shared directory") 30 pathPriv := keybase1.NewPathWithKbfsPath(`/private/jdoe`) 31 writeRemoteFile( 32 ctx, t, sfs, pathAppend(pathPriv, `test.txt`), []byte(`foo`)) 33 syncFS(ctx, t, sfs, "/private/jdoe") 34 35 cacheDir, err := os.MkdirTemp(TempDirBase, "simplefs-downloadtest-cache") 36 require.NoError(t, err) 37 defer os.RemoveAll(cacheDir) 38 downloadDir, err := os.MkdirTemp(TempDirBase, "simplefs-downloadtest-download") 39 require.NoError(t, err) 40 defer os.RemoveAll(downloadDir) 41 42 err = sfs.SimpleFSConfigureDownload(ctx, keybase1.SimpleFSConfigureDownloadArg{ 43 CacheDirOverride: cacheDir, 44 DownloadDirOverride: downloadDir, 45 }) 46 require.NoError(t, err) 47 48 testDownload := func(isRegularDownload bool, regularDownloadIndex int) { 49 downloadID, err := sfs.SimpleFSStartDownload(ctx, keybase1.SimpleFSStartDownloadArg{ 50 Path: keybase1.KBFSPath{Path: "/private/jdoe/test.txt"}, 51 IsRegularDownload: isRegularDownload, 52 }) 53 require.NoError(t, err) 54 status, err := sfs.SimpleFSGetDownloadStatus(ctx) 55 require.NoError(t, err) 56 require.Len(t, status.States, 1) 57 require.Equal(t, downloadID, status.States[0].DownloadID) 58 if isRegularDownload { 59 require.Len(t, status.RegularDownloadIDs, 1) 60 require.Equal(t, downloadID, status.RegularDownloadIDs[0]) 61 } else { 62 require.Empty(t, status.RegularDownloadIDs) 63 } 64 info, err := sfs.SimpleFSGetDownloadInfo(ctx, downloadID) 65 require.NoError(t, err) 66 require.Equal(t, isRegularDownload, info.IsRegularDownload) 67 require.Equal(t, "/private/jdoe/test.txt", info.Path.Path) 68 require.Equal(t, "test.txt", info.Filename) 69 for i := 0; !status.States[0].Done; i++ { 70 if i > 10 { 71 t.Fatalf("waiting on download to finish timeout") 72 } 73 status, err = sfs.SimpleFSGetDownloadStatus(ctx) 74 require.NoError(t, err) 75 time.Sleep(time.Second / 2) 76 } 77 if isRegularDownload { 78 if regularDownloadIndex == 0 { 79 require.Equal(t, filepath.Join(downloadDir, "test.txt"), status.States[0].LocalPath) 80 } else { 81 require.Equal(t, filepath.Join(downloadDir, fmt.Sprintf("test (%d).txt", regularDownloadIndex)), status.States[0].LocalPath) 82 } 83 } else { 84 lpath := filepath.Clean(status.States[0].LocalPath) 85 require.True(t, strings.HasPrefix(lpath, filepath.Clean(cacheDir))) 86 require.True(t, strings.HasSuffix(lpath, ".txt")) 87 } 88 err = sfs.SimpleFSDismissDownload(ctx, downloadID) 89 require.NoError(t, err) 90 } 91 testDownload(true, 0) 92 testDownload(true, 1) 93 testDownload(false, 0) 94 }