github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libpages/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 libpages
     6  
     7  import (
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"os"
    11  	"testing"
    12  
    13  	lru "github.com/hashicorp/golang-lru"
    14  	"github.com/keybase/client/go/kbfs/data"
    15  	"github.com/keybase/client/go/kbfs/ioutil"
    16  	"github.com/keybase/client/go/kbfs/libcontext"
    17  	"github.com/keybase/client/go/kbfs/libfs"
    18  	"github.com/keybase/client/go/kbfs/libkbfs"
    19  	"github.com/keybase/client/go/kbfs/tlf"
    20  	"github.com/keybase/client/go/kbfs/tlfhandle"
    21  	"github.com/keybase/client/go/protocol/keybase1"
    22  	"github.com/stretchr/testify/require"
    23  	"go.uber.org/zap"
    24  )
    25  
    26  func populateContent(t *testing.T, config libkbfs.Config) {
    27  	ctx := libcontext.BackgroundContextWithCancellationDelayer()
    28  	h, err := tlfhandle.ParseHandle(
    29  		ctx, config.KBPKI(), config.MDOps(), nil, "bot,user", tlf.Private)
    30  	require.NoError(t, err)
    31  	fs, err := libfs.NewFS(
    32  		ctx, config, h, data.MasterBranch, "", "", keybase1.MDPriorityNormal)
    33  	require.NoError(t, err)
    34  	err = fs.MkdirAll("/dir", 0600)
    35  	require.NoError(t, err)
    36  	err = fs.Symlink("dir", "dir-link")
    37  	require.NoError(t, err)
    38  	f, err := fs.Create("/dir/file")
    39  	require.NoError(t, err)
    40  	_, err = f.Write([]byte("test"))
    41  	require.NoError(t, err)
    42  	err = f.Close()
    43  	require.NoError(t, err)
    44  	err = fs.SyncAll()
    45  	require.NoError(t, err)
    46  }
    47  
    48  func makeTestKBFSConfig(t *testing.T) (
    49  	kbfsConfig libkbfs.Config, shutdown func()) {
    50  	// This env is needed for the regression test for HOTPOT-2207.
    51  	oldEnv := os.Getenv(libkbfs.EnvKeybaseTestObfuscateLogsForTest)
    52  	os.Setenv(libkbfs.EnvKeybaseTestObfuscateLogsForTest, "1")
    53  
    54  	ctx := libcontext.BackgroundContextWithCancellationDelayer()
    55  	cfg := libkbfs.MakeTestConfigOrBustLoggedInWithMode(
    56  		t, 0, libkbfs.InitSingleOp, "bot", "user")
    57  
    58  	populateContent(t, cfg)
    59  
    60  	tempdir, err := ioutil.TempDir(os.TempDir(), "journal_server")
    61  	require.NoError(t, err)
    62  	err = cfg.EnableDiskLimiter(tempdir)
    63  	require.NoError(t, err)
    64  	err = cfg.EnableJournaling(
    65  		ctx, tempdir, libkbfs.TLFJournalSingleOpBackgroundWorkEnabled)
    66  	require.NoError(t, err)
    67  	shutdown = func() {
    68  		libkbfs.CheckConfigAndShutdown(ctx, t, cfg)
    69  		os.Setenv(libkbfs.EnvKeybaseTestObfuscateLogsForTest, oldEnv)
    70  		err := ioutil.RemoveAll(tempdir)
    71  		require.NoError(t, err)
    72  	}
    73  
    74  	return cfg, shutdown
    75  }
    76  
    77  type TestRootLoader map[string]string
    78  
    79  func (l TestRootLoader) LoadRoot(domain string) (root Root, err error) {
    80  	str, ok := l[domain]
    81  	if !ok {
    82  		return Root{}, ErrKeybasePagesRecordNotFound{}
    83  	}
    84  	r, err := ParseRoot(str)
    85  	if err != nil {
    86  		return Root{}, err
    87  	}
    88  	return *r, nil
    89  }
    90  
    91  func TestServerDefault(t *testing.T) {
    92  	kbfsConfig, shutdown := makeTestKBFSConfig(t)
    93  	defer shutdown()
    94  
    95  	logger, err := zap.NewDevelopment()
    96  	require.NoError(t, err)
    97  	server := Server{
    98  		kbfsConfig: kbfsConfig,
    99  		config: &ServerConfig{
   100  			Logger: logger,
   101  		},
   102  		rootLoader: TestRootLoader{
   103  			"example.com": "/keybase/private/user,bot",
   104  		},
   105  	}
   106  	server.siteCache, err = lru.NewWithEvict(fsCacheSize, server.siteCacheEvict)
   107  	require.NoError(t, err)
   108  
   109  	w := httptest.NewRecorder()
   110  	server.ServeHTTP(w, httptest.NewRequest("GET", "/", nil))
   111  	require.Equal(t, http.StatusForbidden, w.Code)
   112  
   113  	w = httptest.NewRecorder()
   114  	server.ServeHTTP(w, httptest.NewRequest("GET", "/non-existent", nil))
   115  	require.Equal(t, http.StatusNotFound, w.Code)
   116  
   117  	// Regression test HOTPOT-2207.
   118  	w = httptest.NewRecorder()
   119  	server.ServeHTTP(w, httptest.NewRequest("GET", "/dir-link/file", nil))
   120  	require.Equal(t, http.StatusOK, w.Code)
   121  
   122  	// TODO: if we ever add a test that involves bcrypt, remember to swap
   123  	// DefaultCost out and use MinCost.
   124  }