github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libhttpserver/server_test.go (about) 1 // Copyright 2018 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 libhttpserver 6 7 import ( 8 "fmt" 9 "net/http" 10 "os" 11 "testing" 12 13 "github.com/keybase/client/go/kbfs/data" 14 "github.com/keybase/client/go/kbfs/env" 15 "github.com/keybase/client/go/kbfs/ioutil" 16 "github.com/keybase/client/go/kbfs/libcontext" 17 "github.com/keybase/client/go/kbfs/libkbfs" 18 "github.com/keybase/client/go/kbfs/tlf" 19 "github.com/keybase/client/go/kbfs/tlfhandle" 20 "github.com/stretchr/testify/require" 21 ) 22 23 func makeTestKBFSConfig(t *testing.T) ( 24 kbfsConfig libkbfs.Config, shutdown func()) { 25 ctx := libcontext.BackgroundContextWithCancellationDelayer() 26 cfg := libkbfs.MakeTestConfigOrBustLoggedInWithMode( 27 t, 0, libkbfs.InitSingleOp, "alice", "bob") 28 29 tempdir, err := ioutil.TempDir(os.TempDir(), "journal_server") 30 require.NoError(t, err) 31 defer func() { 32 if err != nil { 33 _ = ioutil.RemoveAll(tempdir) 34 } 35 }() 36 err = cfg.EnableDiskLimiter(tempdir) 37 require.NoError(t, err) 38 err = cfg.EnableJournaling( 39 ctx, tempdir, libkbfs.TLFJournalSingleOpBackgroundWorkEnabled) 40 require.NoError(t, err) 41 shutdown = func() { 42 libkbfs.CheckConfigAndShutdown(ctx, t, cfg) 43 err := ioutil.RemoveAll(tempdir) 44 require.NoError(t, err) 45 } 46 47 h, err := tlfhandle.ParseHandle( 48 ctx, cfg.KBPKI(), cfg.MDOps(), cfg, "alice,bob", tlf.Private) 49 require.NoError(t, err) 50 51 root, _, err := cfg.KBFSOps().GetOrCreateRootNode(ctx, h, data.MasterBranch) 52 require.NoError(t, err) 53 _, _, err = cfg.KBFSOps().CreateFile( 54 ctx, root, root.ChildName("test.txt"), false, false) 55 require.NoError(t, err) 56 57 return cfg, shutdown 58 } 59 60 func TestServerDefault(t *testing.T) { 61 kbfsConfig, shutdown := makeTestKBFSConfig(t) 62 defer shutdown() 63 64 s, err := New(env.EmptyAppStateUpdater{}, kbfsConfig) 65 require.NoError(t, err) 66 67 addr, err := s.Address() 68 require.NoError(t, err) 69 70 token, err := s.CurrentToken() 71 require.NoError(t, err) 72 73 resp, err := http.Get(fmt.Sprintf( 74 "http://%s/files/private/alice,bob/non-existent", addr)) 75 require.NoError(t, err) 76 require.Equal(t, http.StatusForbidden, resp.StatusCode) 77 78 resp, err = http.Get(fmt.Sprintf( 79 "http://%s/files/private/alice,bob/non-existent?token=deadbeaf", addr)) 80 require.NoError(t, err) 81 require.Equal(t, http.StatusForbidden, resp.StatusCode) 82 83 resp, err = http.Get(fmt.Sprintf( 84 "http://%s/files/private/alice,bob/non-existent?token=%s", addr, token)) 85 require.NoError(t, err) 86 require.Equal(t, http.StatusBadRequest, resp.StatusCode) 87 88 resp, err = http.Get(fmt.Sprintf( 89 "http://%s/files/private/alice,bob/non-existent?token=%s&viewTypeInvariance=0", addr, token)) 90 require.NoError(t, err) 91 require.Equal(t, http.StatusNotFound, resp.StatusCode) 92 93 resp, err = http.Get(fmt.Sprintf( 94 "http://%s/files/private/alice,bob/test.txt?token=%s&viewTypeInvariance=0", addr, token)) 95 require.NoError(t, err) 96 require.Equal(t, http.StatusPreconditionFailed, resp.StatusCode) 97 98 resp, err = http.Get(fmt.Sprintf( 99 "http://%s/files/private/alice,bob/test.txt?token=%s&viewTypeInvariance=1", addr, token)) 100 require.NoError(t, err) 101 require.Equal(t, http.StatusOK, resp.StatusCode) 102 require.Contains(t, resp.Header.Get("Content-Type"), "charset=") 103 104 resp, err = http.Get(fmt.Sprintf( 105 "http://%s/files/blah/alice,bob/non-existent?token=%s&viewTypeInvariance=1", addr, token)) 106 require.NoError(t, err) 107 require.Equal(t, http.StatusBadRequest, resp.StatusCode) 108 }