github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libkbfs/kbpki_util_test.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 libkbfs
     6  
     7  import (
     8  	"sync"
     9  
    10  	"github.com/keybase/client/go/kbfs/idutil"
    11  	idutiltest "github.com/keybase/client/go/kbfs/idutil/test"
    12  	"github.com/keybase/client/go/kbfs/kbfscodec"
    13  	"github.com/keybase/client/go/kbfs/tlf"
    14  	kbname "github.com/keybase/client/go/kbun"
    15  	"github.com/keybase/client/go/protocol/keybase1"
    16  	"golang.org/x/net/context"
    17  )
    18  
    19  type daemonKBPKI struct {
    20  	KBPKI
    21  	daemon *idutiltest.DaemonKBPKI
    22  }
    23  
    24  func (d daemonKBPKI) GetCurrentSession(ctx context.Context) (
    25  	idutil.SessionInfo, error) {
    26  	return d.daemon.GetCurrentSession(ctx)
    27  }
    28  
    29  func (d daemonKBPKI) Resolve(
    30  	ctx context.Context, assertion string,
    31  	offline keybase1.OfflineAvailability) (
    32  	kbname.NormalizedUsername, keybase1.UserOrTeamID, error) {
    33  	return d.daemon.Resolve(ctx, assertion, offline)
    34  }
    35  
    36  func (d daemonKBPKI) NormalizeSocialAssertion(
    37  	ctx context.Context, assertion string) (
    38  	keybase1.SocialAssertion, error) {
    39  	return d.daemon.NormalizeSocialAssertion(ctx, assertion)
    40  }
    41  
    42  func (d daemonKBPKI) Identify(
    43  	ctx context.Context, assertion, reason string,
    44  	offline keybase1.OfflineAvailability) (
    45  	kbname.NormalizedUsername, keybase1.UserOrTeamID, error) {
    46  	return d.daemon.Identify(ctx, assertion, reason, offline)
    47  }
    48  
    49  func (d daemonKBPKI) ResolveImplicitTeam(
    50  	ctx context.Context, assertions, suffix string, tlfType tlf.Type,
    51  	offline keybase1.OfflineAvailability) (
    52  	idutil.ImplicitTeamInfo, error) {
    53  	return d.daemon.ResolveImplicitTeam(
    54  		ctx, assertions, suffix, tlfType, offline)
    55  }
    56  
    57  func (d daemonKBPKI) GetNormalizedUsername(
    58  	ctx context.Context, id keybase1.UserOrTeamID,
    59  	offline keybase1.OfflineAvailability) (kbname.NormalizedUsername, error) {
    60  	return d.daemon.GetNormalizedUsername(ctx, id, offline)
    61  }
    62  
    63  func (d daemonKBPKI) ResolveTeamTLFID(
    64  	ctx context.Context, teamID keybase1.TeamID,
    65  	offline keybase1.OfflineAvailability) (tlf.ID, error) {
    66  	return d.daemon.ResolveTeamTLFID(ctx, teamID, offline)
    67  }
    68  
    69  // interposeDaemonKBPKI replaces the existing (mock) KBPKI with a
    70  // daemonKBPKI that handles all the username-related calls.
    71  //
    72  // TODO: Make tests that use this just use KBPKIClient; need to figure
    73  // out what to do with other mocked methods of KBPKI.
    74  func interposeDaemonKBPKI(
    75  	config *ConfigMock, users ...kbname.NormalizedUsername) {
    76  	localUsers := idutil.MakeLocalUsers(users)
    77  	loggedInUser := localUsers[0]
    78  
    79  	daemon := NewKeybaseDaemonMemory(
    80  		loggedInUser.UID, localUsers, nil, kbfscodec.NewMsgpack())
    81  	config.SetKeybaseService(daemon)
    82  
    83  	idutilDaemonKBPKI := &idutiltest.DaemonKBPKI{
    84  		KBPKI:  config.mockKbpki,
    85  		Daemon: daemon.DaemonLocal,
    86  	}
    87  	config.SetKBPKI(daemonKBPKI{config.KBPKI(), idutilDaemonKBPKI})
    88  }
    89  
    90  // identifyCountingKBPKI is a KBPKI instance that counts calls to
    91  // Identify.
    92  type identifyCountingKBPKI struct {
    93  	KBPKI
    94  	identifyLock  sync.RWMutex
    95  	identifyCalls int
    96  }
    97  
    98  func (ik *identifyCountingKBPKI) addIdentifyCall() {
    99  	ik.identifyLock.Lock()
   100  	defer ik.identifyLock.Unlock()
   101  	ik.identifyCalls++
   102  }
   103  
   104  func (ik *identifyCountingKBPKI) getIdentifyCalls() int {
   105  	ik.identifyLock.RLock()
   106  	defer ik.identifyLock.RUnlock()
   107  	return ik.identifyCalls
   108  }
   109  
   110  func (ik *identifyCountingKBPKI) Identify(
   111  	ctx context.Context, assertion, reason string,
   112  	offline keybase1.OfflineAvailability) (
   113  	kbname.NormalizedUsername, keybase1.UserOrTeamID, error) {
   114  	ik.addIdentifyCall()
   115  	return ik.KBPKI.Identify(ctx, assertion, reason, offline)
   116  }