github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/test/engine_common.go (about)

     1  // Copyright 2016 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 test
     6  
     7  import (
     8  	"sort"
     9  	"testing"
    10  
    11  	"github.com/keybase/client/go/kbfs/data"
    12  	"github.com/keybase/client/go/kbfs/libkbfs"
    13  	"github.com/keybase/client/go/kbfs/tlf"
    14  	kbname "github.com/keybase/client/go/kbun"
    15  )
    16  
    17  func setBlockSizes(t testing.TB, config libkbfs.Config, blockSize, blockChangeSize int64) {
    18  	// Set the block sizes, if any
    19  	if blockSize > 0 || blockChangeSize > 0 {
    20  		if blockSize == 0 {
    21  			blockSize = 512 * 1024
    22  		}
    23  		if blockChangeSize < 0 {
    24  			t.Fatal("Can't handle negative blockChangeSize")
    25  		}
    26  		if blockChangeSize == 0 {
    27  			blockChangeSize = 8 * 1024
    28  		}
    29  		bsplit, err := data.NewBlockSplitterSimple(blockSize,
    30  			uint64(blockChangeSize), config.Codec())
    31  		if err != nil {
    32  			t.Fatalf("Couldn't make block splitter for block size %d,"+
    33  				" blockChangeSize %d: %v", blockSize, blockChangeSize, err)
    34  		}
    35  		err = bsplit.SetMaxDirEntriesByBlockSize(config.Codec())
    36  		if err != nil {
    37  			t.Fatalf("Couldn't set max dir entries: %v", err)
    38  		}
    39  		config.SetBlockSplitter(bsplit)
    40  	}
    41  }
    42  
    43  func maybeSetBw(t testing.TB, config libkbfs.Config, bwKBps int) {
    44  	if bwKBps > 0 {
    45  		// TODO: reinstate constrained bandwidth tests.
    46  
    47  		// Looks like we're testing big transfers, so let's do
    48  		// background flushes.
    49  		config.SetDoBackgroundFlushes(true)
    50  	}
    51  }
    52  
    53  func makeTeams(t testing.TB, config libkbfs.Config, e Engine, teams teamMap,
    54  	users map[kbname.NormalizedUsername]User) {
    55  	teamNames := make([]kbname.NormalizedUsername, 0, len(teams))
    56  	for name := range teams {
    57  		teamNames = append(teamNames, name)
    58  	}
    59  	sort.Slice(teamNames, func(i, j int) bool {
    60  		return string(teamNames[i]) < string(teamNames[j])
    61  	}) // make sure root names go first.
    62  	infos := libkbfs.AddEmptyTeamsForTestOrBust(t, config, teamNames...)
    63  	for i, name := range teamNames {
    64  		members := teams[name]
    65  		tid := infos[i].TID
    66  		for _, w := range members.writers {
    67  			libkbfs.AddTeamWriterForTestOrBust(t, config, tid,
    68  				e.GetUID(users[w]))
    69  		}
    70  		for _, r := range members.readers {
    71  			libkbfs.AddTeamReaderForTestOrBust(t, config, tid,
    72  				e.GetUID(users[r]))
    73  		}
    74  	}
    75  }
    76  
    77  func makeImplicitTeams(t testing.TB, config libkbfs.Config, e Engine,
    78  	implicitTeams teamMap, users map[kbname.NormalizedUsername]User) {
    79  	if len(implicitTeams) > 0 {
    80  		err := libkbfs.EnableImplicitTeamsForTest(config)
    81  		if err != nil {
    82  			t.Fatal(err)
    83  		}
    84  	}
    85  
    86  	counter := byte(1)
    87  	for name, members := range implicitTeams {
    88  		ty := tlf.Private
    89  		if len(members.readers) == 1 &&
    90  			members.readers[0] == kbname.NormalizedUsername("public") {
    91  			ty = tlf.Public
    92  		}
    93  
    94  		teamID := libkbfs.AddImplicitTeamForTestOrBust(
    95  			t, config, name.String(), "", counter, ty)
    96  		counter++
    97  
    98  		for _, w := range members.writers {
    99  			libkbfs.AddTeamWriterForTestOrBust(t, config, teamID,
   100  				e.GetUID(users[w]))
   101  		}
   102  		if ty == tlf.Private {
   103  			for _, r := range members.readers {
   104  				libkbfs.AddTeamReaderForTestOrBust(t, config, teamID,
   105  					e.GetUID(users[r]))
   106  			}
   107  		}
   108  	}
   109  }