本文整理汇总了Golang中github.com/keybase/client/go/libkb.NormalizedUsername.String方法的典型用法代码示例。如果您正苦于以下问题:Golang NormalizedUsername.String方法的具体用法?Golang NormalizedUsername.String怎么用?Golang NormalizedUsername.String使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/keybase/client/go/libkb.NormalizedUsername
的用法示例。
在下文中一共展示了NormalizedUsername.String方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CurrentSession
// CurrentSession uses the global session to find the session. If
// the user isn't logged in, it returns ErrNoSession.
func (h *SessionHandler) CurrentSession(_ context.Context, sessionID int) (keybase1.Session, error) {
var s keybase1.Session
var token string
var username libkb.NormalizedUsername
var uid keybase1.UID
var deviceSubkey libkb.GenericKey
var err error
aerr := h.G().LoginState().Account(func(a *libkb.Account) {
uid, username, token, deviceSubkey, err = a.UserInfo()
}, "Service - SessionHandler - UserInfo")
if aerr != nil {
return s, aerr
}
if err != nil {
if _, ok := err.(libkb.LoginRequiredError); ok {
return s, ErrNoSession
}
return s, err
}
s.Uid = uid
s.Username = username.String()
s.Token = token
s.DeviceSubkeyKid = deviceSubkey.GetKID()
return s, nil
}
示例2: TestBasicMDUpdate
func TestBasicMDUpdate(t *testing.T) {
// simulate two users
var userName1, userName2 libkb.NormalizedUsername = "u1", "u2"
config1, _, ctx := kbfsOpsConcurInit(t, userName1, userName2)
defer CheckConfigAndShutdown(t, config1)
config2 := ConfigAsUser(config1.(*ConfigLocal), userName2)
defer CheckConfigAndShutdown(t, config2)
name := userName1.String() + "," + userName2.String()
kbfsOps1 := config1.KBFSOps()
rootNode1, _, err :=
kbfsOps1.GetOrCreateRootNode(ctx, name, false, MasterBranch)
if err != nil {
t.Fatalf("Couldn't create folder: %v", err)
}
kbfsOps2 := config2.KBFSOps()
rootNode2, _, err :=
kbfsOps2.GetOrCreateRootNode(ctx, name, false, MasterBranch)
if err != nil {
t.Fatalf("Couldn't get root: %v", err)
}
_, statusChan, err := kbfsOps2.Status(ctx, rootNode2.GetFolderBranch())
if err != nil {
t.Fatalf("Couldn't get status")
}
// user 1 creates a file
_, _, err = kbfsOps1.CreateFile(ctx, rootNode1, "a", false)
if err != nil {
t.Fatalf("Couldn't create file: %v", err)
}
err = kbfsOps2.SyncFromServer(ctx, rootNode2.GetFolderBranch())
if err != nil {
t.Fatalf("Couldn't sync from server: %v", err)
}
entries, err := kbfsOps2.GetDirChildren(ctx, rootNode2)
if err != nil {
t.Fatalf("User 2 couldn't see the root dir: %v", err)
}
if len(entries) != 1 {
t.Fatalf("User 2 sees wrong number of entries in root dir: %d vs 1",
len(entries))
}
if _, ok := entries["a"]; !ok {
t.Fatalf("User 2 doesn't see file a")
}
// The status should have fired as well (though in this case the
// writer is the same as before)
<-statusChan
checkStatus(t, ctx, kbfsOps1, false, userName1, nil,
rootNode1.GetFolderBranch(), "Node 1")
checkStatus(t, ctx, kbfsOps2, false, userName1, nil,
rootNode2.GetFolderBranch(), "Node 2")
}
示例3: TestCRMergedChainsDifferentDirectories
// Same as TestCRMergedChainsSimple, but the two users make changes in
// different, unrelated subdirectories, forcing the resolver to use
// mostly original block pointers when constructing the merged path.
func TestCRMergedChainsDifferentDirectories(t *testing.T) {
var userName1, userName2 libkb.NormalizedUsername = "u1", "u2"
config1, uid1, ctx := kbfsOpsConcurInit(t, userName1, userName2)
defer CheckConfigAndShutdown(t, config1)
config2 := ConfigAsUser(config1.(*ConfigLocal), userName2)
defer CheckConfigAndShutdown(t, config2)
_, uid2, err := config2.KBPKI().GetCurrentUserInfo(ctx)
if err != nil {
t.Fatal(err)
}
name := userName1.String() + "," + userName2.String()
configs := make(map[keybase1.UID]Config)
configs[uid1] = config1
configs[uid2] = config2
nodesA := testCRSharedFolderForUsers(t, name, uid1, configs, []string{"dirA"})
dirA1 := nodesA[uid1]
nodesB := testCRSharedFolderForUsers(t, name, uid1, configs, []string{"dirB"})
dirB1 := nodesB[uid1]
dirB2 := nodesB[uid2]
fb := dirA1.GetFolderBranch()
// pause user 2
_, err = DisableUpdatesForTesting(config2, fb)
if err != nil {
t.Fatalf("Can't disable updates for user 2: %v", err)
}
// user1 makes a file in dir A
_, _, err = config1.KBFSOps().CreateFile(ctx, dirA1, "file1", false)
if err != nil {
t.Fatalf("Couldn't create file: %v", err)
}
cr1 := testCRGetCROrBust(t, config1, fb)
cr2 := testCRGetCROrBust(t, config2, fb)
cr2.Shutdown()
// user2 makes a file in dir B
_, _, err = config2.KBFSOps().CreateFile(ctx, dirB2, "file2", false)
if err != nil {
t.Fatalf("Couldn't create file: %v", err)
}
// Now step through conflict resolution manually for user 2
mergedPaths := make(map[BlockPointer]path)
expectedUnmergedPath := cr2.fbo.nodeCache.PathFromNode(dirB2)
mergedPath := cr1.fbo.nodeCache.PathFromNode(dirB1)
mergedPaths[expectedUnmergedPath.tailPointer()] = mergedPath
expectedActions := map[BlockPointer]crActionList{
mergedPath.tailPointer(): {©UnmergedEntryAction{
"file2", "file2", "", false, false, DirEntry{}, nil}},
}
testCRCheckPathsAndActions(t, cr2, []path{expectedUnmergedPath},
mergedPaths, nil, expectedActions)
}
示例4: TestCRMergedChainsRenameCycleSimple
// Tests that conflict resolution detects and can fix rename cycles.
func TestCRMergedChainsRenameCycleSimple(t *testing.T) {
var userName1, userName2 libkb.NormalizedUsername = "u1", "u2"
config1, uid1, ctx := kbfsOpsConcurInit(t, userName1, userName2)
defer CheckConfigAndShutdown(t, config1)
config2 := ConfigAsUser(config1.(*ConfigLocal), userName2)
defer CheckConfigAndShutdown(t, config2)
_, uid2, err := config2.KBPKI().GetCurrentUserInfo(ctx)
if err != nil {
t.Fatal(err)
}
name := userName1.String() + "," + userName2.String()
configs := make(map[keybase1.UID]Config)
configs[uid1] = config1
configs[uid2] = config2
nodesRoot := testCRSharedFolderForUsers(t, name, uid1, configs, []string{"root"})
dirRoot1 := nodesRoot[uid1]
dirRoot2 := nodesRoot[uid2]
fb := dirRoot1.GetFolderBranch()
nodesA := testCRSharedFolderForUsers(t, name, uid1, configs,
[]string{"root", "dirA"})
dirA1 := nodesA[uid1]
nodesB := testCRSharedFolderForUsers(t, name, uid1, configs,
[]string{"root", "dirB"})
dirB1 := nodesB[uid1]
dirB2 := nodesB[uid2]
cr1 := testCRGetCROrBust(t, config1, fb)
cr2 := testCRGetCROrBust(t, config2, fb)
cr2.Shutdown()
dirRootPtr := cr2.fbo.nodeCache.PathFromNode(dirRoot2).tailPointer()
// pause user 2
_, err = DisableUpdatesForTesting(config2, fb)
if err != nil {
t.Fatalf("Can't disable updates for user 2: %v", err)
}
// user1 moves dirB into dirA
err = config1.KBFSOps().Rename(ctx, dirRoot1, "dirB", dirA1, "dirB")
if err != nil {
t.Fatalf("Couldn't make dir: %v", err)
}
// user2 moves dirA into dirB
err = config2.KBFSOps().Rename(ctx, dirRoot2, "dirA", dirB2, "dirA")
if err != nil {
t.Fatalf("Couldn't make dir: %v", err)
}
// Now step through conflict resolution manually for user 2
mergedPaths := make(map[BlockPointer]path)
// root
unmergedPathRoot := cr2.fbo.nodeCache.PathFromNode(dirRoot2)
mergedPathRoot := cr1.fbo.nodeCache.PathFromNode(dirRoot1)
mergedPaths[unmergedPathRoot.tailPointer()] = mergedPathRoot
unmergedPathB := cr2.fbo.nodeCache.PathFromNode(dirB2)
mergedPathB := cr1.fbo.nodeCache.PathFromNode(dirB1)
mergedPaths[unmergedPathB.tailPointer()] = mergedPathB
ro := newRmOp("dirA", dirRootPtr)
ro.Dir.Ref = unmergedPathRoot.tailPointer()
ro.dropThis = true
ro.setWriterInfo(writerInfo{name: "u2"})
ro.setFinalPath(unmergedPathRoot)
expectedActions := map[BlockPointer]crActionList{
mergedPathRoot.tailPointer(): {&dropUnmergedAction{ro}},
mergedPathB.tailPointer(): {©UnmergedEntryAction{
"dirA", "dirA", "./../", false, false, DirEntry{}, nil}},
}
testCRCheckPathsAndActions(t, cr2, []path{unmergedPathRoot, unmergedPathB},
mergedPaths, nil, expectedActions)
}
示例5: TestRekeyQueueBasic
func TestRekeyQueueBasic(t *testing.T) {
var u1, u2, u3, u4 libkb.NormalizedUsername = "u1", "u2", "u3", "u4"
config1, _, ctx := kbfsOpsConcurInit(t, u1, u2, u3, u4)
defer config1.Shutdown()
config2 := ConfigAsUser(config1.(*ConfigLocal), u2)
defer config2.Shutdown()
_, uid2, err := config2.KBPKI().GetCurrentUserInfo(context.Background())
if err != nil {
t.Fatal(err)
}
config3 := ConfigAsUser(config1.(*ConfigLocal), u3)
defer config3.Shutdown()
_, _, err = config3.KBPKI().GetCurrentUserInfo(context.Background())
if err != nil {
t.Fatal(err)
}
config4 := ConfigAsUser(config1.(*ConfigLocal), u4)
defer config4.Shutdown()
_, _, err = config4.KBPKI().GetCurrentUserInfo(context.Background())
if err != nil {
t.Fatal(err)
}
kbfsOps1 := config1.KBFSOps()
var names []string
// Create a few shared folders
for i := 0; i < 3; i++ {
writers := []string{u1.String(), u2.String()}
if i > 0 {
writers = append(writers, u3.String())
}
if i > 1 {
writers = append(writers, u4.String())
}
name := strings.Join(writers, ",")
names = append(names, name)
// user 1 creates the directory
rootNode1 := GetRootNodeOrBust(t, config1, name, false)
// user 1 creates a file
_, _, err = kbfsOps1.CreateFile(ctx, rootNode1, "a", false)
if err != nil {
t.Fatalf("Couldn't create file: %v", err)
}
}
// Create a new device for user 2
config2Dev2 := ConfigAsUser(config1.(*ConfigLocal), u2)
defer config2Dev2.Shutdown()
AddDeviceForLocalUserOrBust(t, config1, uid2)
AddDeviceForLocalUserOrBust(t, config2, uid2)
devIndex := AddDeviceForLocalUserOrBust(t, config2Dev2, uid2)
SwitchDeviceForLocalUserOrBust(t, config2Dev2, devIndex)
// user 2 should be unable to read the data now since its device
// wasn't registered when the folder was originally created.
for _, name := range names {
_, err := GetRootNodeForTest(config2Dev2, name, false)
if _, ok := err.(NeedSelfRekeyError); !ok {
t.Fatalf("Got unexpected error when reading with new key: %v", err)
}
}
var rekeyChannels []<-chan error
// now user 1 should rekey via its rekey worker
for _, name := range names {
rootNode1 := GetRootNodeOrBust(t, config1, name, false)
// queue it for rekey
c := config1.RekeyQueue().Enqueue(rootNode1.GetFolderBranch().Tlf)
rekeyChannels = append(rekeyChannels, c)
}
// listen for all of the rekey results
for _, c := range rekeyChannels {
if err := <-c; err != nil {
t.Fatal(err)
}
}
// user 2's new device should be able to read now
for _, name := range names {
_ = GetRootNodeOrBust(t, config2Dev2, name, false)
}
}
示例6: TestKeyManagerRekeyAddAndRevokeDeviceWithConflict
// Two devices conflict when revoking a 3rd device.
// Test that after this both can still read the latest version of the folder.
func TestKeyManagerRekeyAddAndRevokeDeviceWithConflict(t *testing.T) {
var u1, u2 libkb.NormalizedUsername = "u1", "u2"
config1, _, ctx := kbfsOpsConcurInit(t, u1, u2)
defer CheckConfigAndShutdown(t, config1)
config2 := ConfigAsUser(config1.(*ConfigLocal), u2)
defer CheckConfigAndShutdown(t, config2)
uid2, err := config2.KBPKI().GetCurrentUID(context.Background())
if err != nil {
t.Fatal(err)
}
// create a shared folder
name := u1.String() + "," + u2.String()
kbfsOps1 := config1.KBFSOps()
rootNode1, _, err :=
kbfsOps1.GetOrCreateRootNode(ctx, name, false, MasterBranch)
if err != nil {
t.Fatalf("Couldn't create folder: %v", err)
}
// user 1 creates a file
_, _, err = kbfsOps1.CreateFile(ctx, rootNode1, "a", false)
if err != nil {
t.Fatalf("Couldn't create file: %v", err)
}
config2Dev2 := ConfigAsUser(config1.(*ConfigLocal), u2)
defer CheckConfigAndShutdown(t, config2Dev2)
// give user 2 a new device
AddDeviceForLocalUserOrBust(t, config1, uid2)
AddDeviceForLocalUserOrBust(t, config2, uid2)
devIndex := AddDeviceForLocalUserOrBust(t, config2Dev2, uid2)
SwitchDeviceForLocalUserOrBust(t, config2Dev2, devIndex)
// user 2 should be unable to read the data now since its device
// wasn't registered when the folder was originally created.
kbfsOps2Dev2 := config2Dev2.KBFSOps()
root2Dev2, _, err :=
kbfsOps2Dev2.GetOrCreateRootNode(ctx, name, false, MasterBranch)
if _, ok := err.(ReadAccessError); !ok {
t.Fatalf("Got unexpected error when reading with new key: %v", err)
}
// now user 1 should rekey
err = kbfsOps1.Rekey(ctx, rootNode1.GetFolderBranch().Tlf)
if err != nil {
t.Fatalf("Couldn't rekey: %v", err)
}
// this device should be able to read now
root2Dev2, _, err =
kbfsOps2Dev2.GetOrCreateRootNode(ctx, name, false, MasterBranch)
if err != nil {
t.Fatalf("Got unexpected error after rekey: %v", err)
}
// Now revoke the original user 2 device
RevokeDeviceForLocalUserOrBust(t, config1, uid2, 0)
RevokeDeviceForLocalUserOrBust(t, config2Dev2, uid2, 0)
// disable updates on user 1
c, err := DisableUpdatesForTesting(config1, rootNode1.GetFolderBranch())
if err != nil {
t.Fatalf("Couldn't disable updates: %v", err)
}
// rekey again but with user 2 device 2
err = kbfsOps2Dev2.Rekey(ctx, root2Dev2.GetFolderBranch().Tlf)
if err != nil {
t.Fatalf("Couldn't rekey: %v", err)
}
// have user 1 also try to rekey but fail due to conflict
err = kbfsOps1.Rekey(ctx, rootNode1.GetFolderBranch().Tlf)
if _, isConflict := err.(MDServerErrorConflictRevision); !isConflict {
t.Fatalf("Expected failure due to conflict")
}
// device 1 re-enables updates
c <- struct{}{}
err = kbfsOps1.SyncFromServer(ctx, rootNode1.GetFolderBranch())
if err != nil {
t.Fatalf("Couldn't sync from server: %v", err)
}
err = kbfsOps2Dev2.SyncFromServer(ctx, root2Dev2.GetFolderBranch())
if err != nil {
t.Fatalf("Couldn't sync from server: %v", err)
}
// force re-encryption of the root dir
_, _, err = kbfsOps2Dev2.CreateFile(ctx, root2Dev2, "b", false)
if err != nil {
t.Fatalf("Couldn't create file: %v", err)
}
//.........这里部分代码省略.........
示例7: TestMultiUserWrite
// Tests that multiple users can write to the same file sequentially
// without any problems.
func TestMultiUserWrite(t *testing.T) {
// simulate two users
var userName1, userName2 libkb.NormalizedUsername = "u1", "u2"
config1, _, ctx := kbfsOpsConcurInit(t, userName1, userName2)
defer CheckConfigAndShutdown(t, config1)
config2 := ConfigAsUser(config1.(*ConfigLocal), userName2)
defer CheckConfigAndShutdown(t, config2)
name := userName1.String() + "," + userName2.String()
// user1 creates a file in a shared dir
rootNode1 := GetRootNodeOrBust(t, config1, name, false)
kbfsOps1 := config1.KBFSOps()
_, _, err := kbfsOps1.CreateFile(ctx, rootNode1, "a", false)
if err != nil {
t.Fatalf("Couldn't create file: %v", err)
}
// then user2 write to the file
rootNode2 := GetRootNodeOrBust(t, config2, name, false)
kbfsOps2 := config2.KBFSOps()
fileNode2, _, err := kbfsOps2.Lookup(ctx, rootNode2, "a")
if err != nil {
t.Fatalf("Couldn't lookup file: %v", err)
}
data2 := []byte{2}
err = kbfsOps2.Write(ctx, fileNode2, data2, 0)
if err != nil {
t.Fatalf("Couldn't write file: %v", err)
}
// Write twice to make sure that multiple write operations within
// a sync work when the writer is changing.
err = kbfsOps2.Write(ctx, fileNode2, data2, 0)
if err != nil {
t.Fatalf("Couldn't write file: %v", err)
}
err = kbfsOps2.Sync(ctx, fileNode2)
if err != nil {
t.Fatalf("Couldn't sync file: %v", err)
}
readAndCompareData(t, config2, ctx, name, data2, userName2)
// A second write by the same user
data3 := []byte{3}
err = kbfsOps2.Write(ctx, fileNode2, data3, 0)
if err != nil {
t.Fatalf("Couldn't write file: %v", err)
}
err = kbfsOps2.Sync(ctx, fileNode2)
if err != nil {
t.Fatalf("Couldn't sync file: %v", err)
}
readAndCompareData(t, config2, ctx, name, data3, userName2)
err = kbfsOps1.SyncFromServerForTesting(ctx, rootNode1.GetFolderBranch())
if err != nil {
t.Fatalf("Couldn't sync from server: %v", err)
}
readAndCompareData(t, config1, ctx, name, data3, userName2)
}
示例8: TestQuotaReclamationSimple
// Test that quota reclamation works for a simple case where the user
// does a few updates, then lets quota reclamation run, and we make
// sure that all historical blocks have been deleted.
func TestQuotaReclamationSimple(t *testing.T) {
var userName libkb.NormalizedUsername = "test_user"
config, _, ctx := kbfsOpsInitNoMocks(t, userName)
defer CheckConfigAndShutdown(t, config)
clock, now := newTestClockAndTimeNow()
config.SetClock(clock)
rootNode := GetRootNodeOrBust(t, config, userName.String(), false)
kbfsOps := config.KBFSOps()
_, _, err := kbfsOps.CreateDir(ctx, rootNode, "a")
if err != nil {
t.Fatalf("Couldn't create dir: %v", err)
}
err = kbfsOps.RemoveDir(ctx, rootNode, "a")
if err != nil {
t.Fatalf("Couldn't remove dir: %v", err)
}
// Wait for outstanding archives
err = kbfsOps.SyncFromServerForTesting(ctx, rootNode.GetFolderBranch())
if err != nil {
t.Fatalf("Couldn't sync from server: %v", err)
}
// Make sure no blocks are deleted before there's a new-enough update.
bserverLocal, ok := config.BlockServer().(*BlockServerLocal)
if !ok {
t.Fatalf("Bad block server")
}
preQR1Blocks, err := bserverLocal.getAll(rootNode.GetFolderBranch().Tlf)
if err != nil {
t.Fatalf("Couldn't get blocks: %v", err)
}
ops := kbfsOps.(*KBFSOpsStandard).getOpsByNode(ctx, rootNode)
ops.fbm.forceQuotaReclamation()
err = ops.fbm.waitForQuotaReclamations(ctx)
if err != nil {
t.Fatalf("Couldn't wait for QR: %v", err)
}
postQR1Blocks, err := bserverLocal.getAll(rootNode.GetFolderBranch().Tlf)
if err != nil {
t.Fatalf("Couldn't get blocks: %v", err)
}
if !reflect.DeepEqual(preQR1Blocks, postQR1Blocks) {
t.Fatalf("Blocks deleted too early (%v vs %v)!",
preQR1Blocks, postQR1Blocks)
}
// Increase the time and make a new revision, but don't run quota
// reclamation yet.
clock.Set(now.Add(2 * config.QuotaReclamationMinUnrefAge()))
_, _, err = kbfsOps.CreateDir(ctx, rootNode, "b")
if err != nil {
t.Fatalf("Couldn't create dir: %v", err)
}
preQR2Blocks, err := bserverLocal.getAll(rootNode.GetFolderBranch().Tlf)
if err != nil {
t.Fatalf("Couldn't get blocks: %v", err)
}
ops.fbm.forceQuotaReclamation()
err = ops.fbm.waitForQuotaReclamations(ctx)
if err != nil {
t.Fatalf("Couldn't wait for QR: %v", err)
}
postQR2Blocks, err := bserverLocal.getAll(rootNode.GetFolderBranch().Tlf)
if err != nil {
t.Fatalf("Couldn't get blocks: %v", err)
}
if pre, post := totalBlockRefs(preQR2Blocks),
totalBlockRefs(postQR2Blocks); post >= pre {
t.Errorf("Blocks didn't shrink after reclamation: pre: %d, post %d",
pre, post)
}
}
示例9: TestQuotaReclamationIncrementalReclamation
// Test that a single quota reclamation run doesn't try to reclaim too
// much quota at once.
func TestQuotaReclamationIncrementalReclamation(t *testing.T) {
var userName libkb.NormalizedUsername = "test_user"
config, _, ctx := kbfsOpsInitNoMocks(t, userName)
defer CheckConfigAndShutdown(t, config)
now := time.Now()
var clock TestClock
clock.Set(now)
config.SetClock(&clock)
rootNode := GetRootNodeOrBust(t, config, userName.String(), false)
// Do a bunch of operations.
kbfsOps := config.KBFSOps()
for i := 0; i < numPointersPerGCThreshold; i++ {
_, _, err := kbfsOps.CreateDir(ctx, rootNode, "a")
if err != nil {
t.Fatalf("Couldn't create dir: %v", err)
}
err = kbfsOps.RemoveDir(ctx, rootNode, "a")
if err != nil {
t.Fatalf("Couldn't remove dir: %v", err)
}
}
// Increase the time, and make sure that there is still more than
// one block in the history
clock.Set(now.Add(2 * config.QuotaReclamationMinUnrefAge()))
// Run it.
ops := kbfsOps.(*KBFSOpsStandard).getOpsByNode(ctx, rootNode)
ops.fbm.forceQuotaReclamation()
err := ops.fbm.waitForQuotaReclamations(ctx)
if err != nil {
t.Fatalf("Couldn't wait for QR: %v", err)
}
bserverLocal, ok := config.BlockServer().(*BlockServerLocal)
if !ok {
t.Fatalf("Bad block server")
}
blocks, err := bserverLocal.getAll(rootNode.GetFolderBranch().Tlf)
if err != nil {
t.Fatalf("Couldn't get blocks: %v", err)
}
b := totalBlockRefs(blocks)
if b <= 1 {
t.Errorf("Too many blocks left after first QR: %d", b)
}
// Now let it run to completion
for b > 1 {
ops.fbm.forceQuotaReclamation()
err = ops.fbm.waitForQuotaReclamations(ctx)
if err != nil {
t.Fatalf("Couldn't wait for QR: %v", err)
}
blocks, err := bserverLocal.getAll(rootNode.GetFolderBranch().Tlf)
if err != nil {
t.Fatalf("Couldn't get blocks: %v", err)
}
oldB := b
b = totalBlockRefs(blocks)
if b >= oldB {
t.Fatalf("Blocks didn't shrink after reclamation: %d vs. %d",
b, oldB)
}
}
}
示例10: TestBasicCRFileConflictWithRekey
// Tests that two users can make independent writes while forked, and
// conflict resolution will merge them correctly and the rekey bit is
// preserved until rekey.
func TestBasicCRFileConflictWithRekey(t *testing.T) {
// simulate two users
var userName1, userName2 libkb.NormalizedUsername = "u1", "u2"
config1, _, ctx := kbfsOpsConcurInit(t, userName1, userName2)
defer CheckConfigAndShutdown(t, config1)
config1.MDServer().DisableRekeyUpdatesForTesting()
config2 := ConfigAsUser(config1.(*ConfigLocal), userName2)
defer CheckConfigAndShutdown(t, config2)
uid2, err := config2.KBPKI().GetCurrentUID(context.Background())
if err != nil {
t.Fatal(err)
}
config2.MDServer().DisableRekeyUpdatesForTesting()
now := time.Now()
config2.SetClock(&TestClock{now})
name := userName1.String() + "," + userName2.String()
// user1 creates a file in a shared dir
kbfsOps1 := config1.KBFSOps()
rootNode1, _, err :=
kbfsOps1.GetOrCreateRootNode(ctx, name, false, MasterBranch)
if err != nil {
t.Fatalf("Couldn't create folder: %v", err)
}
dirA1, _, err := kbfsOps1.CreateDir(ctx, rootNode1, "a")
if err != nil {
t.Fatalf("Couldn't create dir: %v", err)
}
fileB1, _, err := kbfsOps1.CreateFile(ctx, dirA1, "b", false)
if err != nil {
t.Fatalf("Couldn't create file: %v", err)
}
// look it up on user2
kbfsOps2 := config2.KBFSOps()
rootNode2, _, err :=
kbfsOps2.GetOrCreateRootNode(ctx, name, false, MasterBranch)
if err != nil {
t.Fatalf("Couldn't create folder: %v", err)
}
dirA2, _, err := kbfsOps2.Lookup(ctx, rootNode2, "a")
if err != nil {
t.Fatalf("Couldn't lookup dir: %v", err)
}
fileB2, _, err := kbfsOps2.Lookup(ctx, dirA2, "b")
if err != nil {
t.Fatalf("Couldn't lookup file: %v", err)
}
config2Dev2 := ConfigAsUser(config1.(*ConfigLocal), userName2)
// we don't check the config because this device can't read all of the md blocks.
defer config2Dev2.Shutdown()
config2Dev2.MDServer().DisableRekeyUpdatesForTesting()
// Now give u2 a new device. The configs don't share a Keybase
// Daemon so we have to do it in all places.
AddDeviceForLocalUserOrBust(t, config1, uid2)
AddDeviceForLocalUserOrBust(t, config2, uid2)
devIndex := AddDeviceForLocalUserOrBust(t, config2Dev2, uid2)
SwitchDeviceForLocalUserOrBust(t, config2Dev2, devIndex)
// user2 device 2 should be unable to read the data now since its device
// wasn't registered when the folder was originally created.
kbfsOps2Dev2 := config2Dev2.KBFSOps()
_, _, err =
kbfsOps2Dev2.GetOrCreateRootNode(ctx, name, false, MasterBranch)
if _, ok := err.(ReadAccessError); !ok {
t.Fatalf("Got unexpected error when reading with new key: %v", err)
}
// User 2 syncs
err = kbfsOps2.SyncFromServer(ctx, rootNode2.GetFolderBranch())
if err != nil {
t.Fatalf("Couldn't sync from server: %v", err)
}
// disable updates on user2
c, err := DisableUpdatesForTesting(config2, rootNode2.GetFolderBranch())
if err != nil {
t.Fatalf("Couldn't disable updates: %v", err)
}
// User 1 writes the file
data1 := []byte{1, 2, 3, 4, 5}
err = kbfsOps1.Write(ctx, fileB1, data1, 0)
if err != nil {
t.Fatalf("Couldn't write file: %v", err)
}
err = kbfsOps1.Sync(ctx, fileB1)
if err != nil {
t.Fatalf("Couldn't sync file: %v", err)
}
// User 2 dev 2 should set the rekey bit
err = kbfsOps2Dev2.Rekey(ctx, rootNode2.GetFolderBranch().Tlf)
//.........这里部分代码省略.........
示例11: TestMultiUserWrite
// Tests that multiple users can write to the same file sequentially
// without any problems.
func TestMultiUserWrite(t *testing.T) {
// simulate two users
var userName1, userName2 libkb.NormalizedUsername = "u1", "u2"
config1, _, ctx := kbfsOpsConcurInit(t, userName1, userName2)
defer CheckConfigAndShutdown(t, config1)
config2 := ConfigAsUser(config1.(*ConfigLocal), userName2)
defer CheckConfigAndShutdown(t, config2)
name := userName1.String() + "," + userName2.String()
// user1 creates a file in a shared dir
kbfsOps1 := config1.KBFSOps()
rootNode1, _, err :=
kbfsOps1.GetOrCreateRootNode(ctx, name, false, MasterBranch)
if err != nil {
t.Errorf("Couldn't create folder: %v", err)
}
_, _, err = kbfsOps1.CreateFile(ctx, rootNode1, "a", false)
if err != nil {
t.Fatalf("Couldn't create file: %v", err)
}
// then user2 write to the file
kbfsOps2 := config2.KBFSOps()
rootNode2, _, err :=
kbfsOps2.GetOrCreateRootNode(ctx, name, false, MasterBranch)
if err != nil {
t.Errorf("Couldn't create folder: %v", err)
}
fileNode2, _, err := kbfsOps2.Lookup(ctx, rootNode2, "a")
if err != nil {
t.Fatalf("Couldn't lookup file: %v", err)
}
data2 := []byte{2}
err = kbfsOps2.Write(ctx, fileNode2, data2, 0)
if err != nil {
t.Fatalf("Couldn't write file: %v", err)
}
// The writer should be user 2, even before the Sync
ops := kbfsOps2.(*KBFSOpsStandard).getOpsByNode(fileNode2)
de, err := ops.statEntry(ctx, fileNode2)
if err != nil {
t.Fatalf("Couldn't lookup file: %v", err)
}
uid2, err := config2.KBPKI().GetCurrentUID(context.Background())
if err != nil {
t.Fatal(err)
}
if de.GetWriter() != uid2 {
t.Errorf("After user 2's first write, Writer is wrong: %v",
de.GetWriter())
}
err = kbfsOps2.Sync(ctx, fileNode2)
if err != nil {
t.Fatalf("Couldn't sync file: %v", err)
}
data3 := []byte{3}
err = kbfsOps2.Write(ctx, fileNode2, data3, 0)
if err != nil {
t.Fatalf("Couldn't write file: %v", err)
}
err = kbfsOps2.Sync(ctx, fileNode2)
if err != nil {
t.Fatalf("Couldn't sync file: %v", err)
}
readAndCompareData(t, config2, ctx, name, data3, userName2)
}
示例12: testMultipleMDUpdates
func testMultipleMDUpdates(t *testing.T, unembedChanges bool) {
// simulate two users
var userName1, userName2 libkb.NormalizedUsername = "u1", "u2"
config1, _, ctx := kbfsOpsConcurInit(t, userName1, userName2)
defer CheckConfigAndShutdown(t, config1)
config2 := ConfigAsUser(config1.(*ConfigLocal), userName2)
defer CheckConfigAndShutdown(t, config2)
if unembedChanges {
bss1, ok1 := config1.BlockSplitter().(*BlockSplitterSimple)
bss2, ok2 := config2.BlockSplitter().(*BlockSplitterSimple)
if !ok1 || !ok2 {
t.Fatalf("Couldn't convert BlockSplitters!")
}
bss1.blockChangeEmbedMaxSize = 3
bss2.blockChangeEmbedMaxSize = 3
}
name := userName1.String() + "," + userName2.String()
kbfsOps1 := config1.KBFSOps()
rootNode1, _, err :=
kbfsOps1.GetOrCreateRootNode(ctx, name, false, MasterBranch)
if err != nil {
t.Fatalf("Couldn't create folder: %v", err)
}
// user 1 creates a file
_, _, err = kbfsOps1.CreateFile(ctx, rootNode1, "a", false)
if err != nil {
t.Fatalf("Couldn't create file: %v", err)
}
// user 2 looks up the directory (and sees the file)
kbfsOps2 := config2.KBFSOps()
rootNode2, _, err :=
kbfsOps2.GetOrCreateRootNode(ctx, name, false, MasterBranch)
if err != nil {
t.Errorf("Couldn't get root: %v", err)
}
// now user 1 renames the old file, and creates a new one
err = kbfsOps1.Rename(ctx, rootNode1, "a", rootNode1, "b")
if err != nil {
t.Fatalf("Couldn't rename file: %v", err)
}
_, _, err = kbfsOps1.CreateFile(ctx, rootNode1, "c", false)
if err != nil {
t.Fatalf("Couldn't create file: %v", err)
}
err = kbfsOps2.SyncFromServer(ctx, rootNode2.GetFolderBranch())
if err != nil {
t.Fatalf("Couldn't sync from server: %v", err)
}
entries, err := kbfsOps2.GetDirChildren(ctx, rootNode2)
if err != nil {
t.Fatalf("User 2 couldn't see the root dir: %v", err)
}
if len(entries) != 2 {
t.Fatalf("User 2 sees wrong number of entries in root dir: %d vs 2",
len(entries))
}
if _, ok := entries["b"]; !ok {
t.Fatalf("User 2 doesn't see file b")
}
if _, ok := entries["c"]; !ok {
t.Fatalf("User 2 doesn't see file c")
}
}
示例13: TestCRMergedChainsComplex
// A mix of the above TestCRMergedChains* tests, with various other
// types of operations thrown in the mix (like u2 deleting unrelated
// directories, etc).
func TestCRMergedChainsComplex(t *testing.T) {
var userName1, userName2 libkb.NormalizedUsername = "u1", "u2"
config1, uid1, ctx := kbfsOpsConcurInit(t, userName1, userName2)
defer CheckConfigAndShutdown(t, config1)
config2 := ConfigAsUser(config1.(*ConfigLocal), userName2)
defer CheckConfigAndShutdown(t, config2)
_, uid2, err := config2.KBPKI().GetCurrentUserInfo(ctx)
if err != nil {
t.Fatal(err)
}
// Setup:
// /dirA/dirB/dirC
// /dirA/dirB/dirD/file5
// /dirE/dirF
// /dirG/dirH
name := userName1.String() + "," + userName2.String()
configs := make(map[keybase1.UID]Config)
configs[uid1] = config1
configs[uid2] = config2
nodesA := testCRSharedFolderForUsers(t, name, uid1, configs, []string{"dirA"})
dirA1 := nodesA[uid1]
dirA2 := nodesA[uid2]
fb := dirA1.GetFolderBranch()
cr1 := testCRGetCROrBust(t, config1, fb)
cr2 := testCRGetCROrBust(t, config2, fb)
cr2.Shutdown()
nodesB := testCRSharedFolderForUsers(t, name, uid1, configs,
[]string{"dirA", "dirB"})
dirB1 := nodesB[uid1]
dirB2 := nodesB[uid2]
nodesC := testCRSharedFolderForUsers(t, name, uid1, configs,
[]string{"dirA", "dirB", "dirC"})
dirC2 := nodesC[uid2]
nodesD := testCRSharedFolderForUsers(t, name, uid1, configs,
[]string{"dirA", "dirB", "dirD"})
dirD1 := nodesD[uid1]
dirD2 := nodesD[uid2]
nodesE := testCRSharedFolderForUsers(t, name, uid1, configs, []string{"dirE"})
dirE1 := nodesE[uid1]
nodesF := testCRSharedFolderForUsers(t, name, uid1, configs,
[]string{"dirE", "dirF"})
dirF2 := nodesF[uid2]
dirEPtr := cr2.fbo.nodeCache.PathFromNode(dirE1).tailPointer()
dirFPtr := cr2.fbo.nodeCache.PathFromNode(dirF2).tailPointer()
nodesG := testCRSharedFolderForUsers(t, name, uid1, configs, []string{"dirG"})
dirG1 := nodesG[uid1]
nodesH := testCRSharedFolderForUsers(t, name, uid1, configs,
[]string{"dirG", "dirH"})
dirH1 := nodesH[uid1]
dirH2 := nodesH[uid2]
dirHPtr := cr1.fbo.nodeCache.PathFromNode(dirH1).tailPointer()
_, _, err = config1.KBFSOps().CreateFile(ctx, dirD1, "file5", false)
if err != nil {
t.Fatalf("Couldn't create file: %v", err)
}
config2.KBFSOps().SyncFromServerForTesting(ctx, fb)
// pause user 2
_, err = DisableUpdatesForTesting(config2, fb)
if err != nil {
t.Fatalf("Can't disable updates for user 2: %v", err)
}
// user 1:
// touch /dirA/file1
// rm -rf /dirE/dirF
// mv /dirG/dirH /dirA/dirI
//
// user 2:
// mkdir /dirA/dirJ
// touch /dirA/dirJ/file2
// touch /dirE/dirF/file3
// touch /dirA/dirB/dirC/file4
// mv /dirA/dirB/dirC/file4 /dirG/dirH/file4
// rm /dirA/dirB/dirD/file5
// rm -rf /dirA/dirB/dirD
// user 1:
_, _, err = config1.KBFSOps().CreateFile(ctx, dirA1, "file1", false)
if err != nil {
t.Fatalf("Couldn't create file: %v", err)
}
err = config1.KBFSOps().RemoveDir(ctx, dirE1, "dirF")
if err != nil {
t.Fatalf("Couldn't remove dir: %v", err)
}
err = config1.KBFSOps().Rename(ctx, dirG1, "dirH", dirA1, "dirI")
if err != nil {
t.Fatalf("Couldn't remove dir: %v", err)
}
//.........这里部分代码省略.........
示例14: TestCRMergedChainsDeletedDirectories
// Same as TestCRMergedChainsSimple, but u1 actually deletes some of
// the subdirectories used by u2, forcing the resolver to generate
// some recreateOps.
func TestCRMergedChainsDeletedDirectories(t *testing.T) {
var userName1, userName2 libkb.NormalizedUsername = "u1", "u2"
config1, uid1, ctx := kbfsOpsConcurInit(t, userName1, userName2)
defer CheckConfigAndShutdown(t, config1)
config2 := ConfigAsUser(config1.(*ConfigLocal), userName2)
defer CheckConfigAndShutdown(t, config2)
_, uid2, err := config2.KBPKI().GetCurrentUserInfo(ctx)
if err != nil {
t.Fatal(err)
}
name := userName1.String() + "," + userName2.String()
configs := make(map[keybase1.UID]Config)
configs[uid1] = config1
configs[uid2] = config2
nodesA := testCRSharedFolderForUsers(t, name, uid1, configs, []string{"dirA"})
dirA1 := nodesA[uid1]
fb := dirA1.GetFolderBranch()
cr1 := testCRGetCROrBust(t, config1, fb)
cr2 := testCRGetCROrBust(t, config2, fb)
cr2.Shutdown()
nodesB := testCRSharedFolderForUsers(t, name, uid1, configs,
[]string{"dirA", "dirB"})
dirB1 := nodesB[uid1]
nodesC := testCRSharedFolderForUsers(t, name, uid1, configs,
[]string{"dirA", "dirB", "dirC"})
dirC2 := nodesC[uid2]
dirAPtr := cr1.fbo.nodeCache.PathFromNode(dirA1).tailPointer()
dirBPtr := cr1.fbo.nodeCache.PathFromNode(dirB1).tailPointer()
dirCPtr := cr2.fbo.nodeCache.PathFromNode(dirC2).tailPointer()
// pause user 2
_, err = DisableUpdatesForTesting(config2, fb)
if err != nil {
t.Fatalf("Can't disable updates for user 2: %v", err)
}
// user1 deletes dirB and dirC
err = config1.KBFSOps().RemoveDir(ctx, dirB1, "dirC")
if err != nil {
t.Fatalf("Couldn't remove dir: %v", err)
}
err = config1.KBFSOps().RemoveDir(ctx, dirA1, "dirB")
if err != nil {
t.Fatalf("Couldn't remove dir: %v", err)
}
// user2 makes a file in dir C
_, _, err = config2.KBFSOps().CreateFile(ctx, dirC2, "file2", false)
if err != nil {
t.Fatalf("Couldn't create file: %v", err)
}
// Now step through conflict resolution manually for user 2
expectedUnmergedPath := cr2.fbo.nodeCache.PathFromNode(dirC2)
// The merged path will consist of the latest root and dirA
// components, plus the original blockpointers of the deleted
// nodes.
mergedPaths := make(map[BlockPointer]path)
mergedPath := cr1.fbo.nodeCache.PathFromNode(dirA1)
mergedPath.path = append(mergedPath.path, pathNode{
BlockPointer: dirBPtr,
Name: "dirB",
})
mergedPath.path = append(mergedPath.path, pathNode{
BlockPointer: dirCPtr,
Name: "dirC",
})
mergedPaths[expectedUnmergedPath.tailPointer()] = mergedPath
coB := newCreateOp("dirB", dirAPtr, Dir)
coC := newCreateOp("dirC", dirBPtr, Dir)
dirAPtr1 := cr1.fbo.nodeCache.PathFromNode(dirA1).tailPointer()
expectedActions := map[BlockPointer]crActionList{
dirCPtr: {©UnmergedEntryAction{"file2", "file2", "",
false, false, DirEntry{}, nil}},
dirBPtr: {©UnmergedEntryAction{"dirC", "dirC", "", false, false,
DirEntry{}, nil}},
dirAPtr1: {©UnmergedEntryAction{"dirB", "dirB", "", false, false,
DirEntry{}, nil}},
}
testCRCheckPathsAndActions(t, cr2, []path{expectedUnmergedPath},
mergedPaths, []*createOp{coB, coC}, expectedActions)
}
示例15: TestCRDoActionsWriteConflict
// Test that actions get executed properly in the case of two
// simultaneous writes to the same file.
func TestCRDoActionsWriteConflict(t *testing.T) {
var userName1, userName2 libkb.NormalizedUsername = "u1", "u2"
config1, uid1, ctx := kbfsOpsConcurInit(t, userName1, userName2)
defer CheckConfigAndShutdown(t, config1)
config2 := ConfigAsUser(config1.(*ConfigLocal), userName2)
defer CheckConfigAndShutdown(t, config2)
_, uid2, err := config2.KBPKI().GetCurrentUserInfo(ctx)
if err != nil {
t.Fatal(err)
}
clock, now := newTestClockAndTimeNow()
config2.SetClock(clock)
name := userName1.String() + "," + userName2.String()
configs := make(map[keybase1.UID]Config)
configs[uid1] = config1
configs[uid2] = config2
nodes := testCRSharedFolderForUsers(t, name, uid1, configs, []string{"dir"})
dir1 := nodes[uid1]
dir2 := nodes[uid2]
fb := dir1.GetFolderBranch()
// user1 makes a file
file1, _, err := config1.KBFSOps().CreateFile(ctx, dir1, "file", false)
if err != nil {
t.Fatalf("Couldn't create file: %v", err)
}
// user2 lookup
err = config2.KBFSOps().SyncFromServerForTesting(ctx, fb)
if err != nil {
t.Fatalf("Couldn't sync user 2")
}
file2, _, err := config2.KBFSOps().Lookup(ctx, dir2, "file")
if err != nil {
t.Fatalf("Couldn't lookup file: %v", err)
}
// pause user 2
_, err = DisableUpdatesForTesting(config2, fb)
if err != nil {
t.Fatalf("Can't disable updates for user 2: %v", err)
}
cr1 := testCRGetCROrBust(t, config1, fb)
cr2 := testCRGetCROrBust(t, config2, fb)
cr2.Shutdown()
// user1 writes the file
err = config1.KBFSOps().Write(ctx, file1, []byte{1, 2, 3}, 0)
if err != nil {
t.Fatalf("Couldn't write file: %v", err)
}
err = config1.KBFSOps().Sync(ctx, file1)
if err != nil {
t.Fatalf("Couldn't sync file: %v", err)
}
// user2 writes the file
unmergedData := []byte{4, 5, 6}
err = config2.KBFSOps().Write(ctx, file2, unmergedData, 0)
if err != nil {
t.Fatalf("Couldn't write file: %v", err)
}
err = config2.KBFSOps().Sync(ctx, file2)
if err != nil {
t.Fatalf("Couldn't sync file: %v", err)
}
lState := makeFBOLockState()
// Now run through conflict resolution manually for user2.
unmergedChains, mergedChains, unmergedPaths, mergedPaths,
recreateOps, _, _, err := cr2.buildChainsAndPaths(ctx, lState)
if err != nil {
t.Fatalf("Couldn't build chains and paths: %v", err)
}
actionMap, _, err := cr2.computeActions(ctx, unmergedChains, mergedChains,
mergedPaths, recreateOps)
if err != nil {
t.Fatalf("Couldn't compute actions: %v", err)
}
lbc := make(localBcache)
newFileBlocks := make(fileBlockMap)
err = cr2.doActions(ctx, lState, unmergedChains, mergedChains,
unmergedPaths, mergedPaths, actionMap, lbc, newFileBlocks)
if err != nil {
t.Fatalf("Couldn't do actions: %v", err)
}
// Does the merged block contain the two files?
mergedRootPath := cr1.fbo.nodeCache.PathFromNode(dir1)
cre := WriterDeviceDateConflictRenamer{}
//.........这里部分代码省略.........