github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/saltpackkeystest/saltpack_mock_pseudonym_resolver.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  //
     4  //go:build !production
     5  // +build !production
     6  
     7  package saltpackkeystest
     8  
     9  import (
    10  	"encoding/hex"
    11  	"testing"
    12  
    13  	"github.com/keybase/saltpack"
    14  )
    15  
    16  // MockPseudonymResolver resolver returns nil for all the pseudonyms requested unless otherwise instructed
    17  // by the AddPseudonym method. It does not make any network requests to the server.
    18  type MockPseudonymResolver struct {
    19  	T       *testing.T
    20  	pnymMap map[[32]byte](*saltpack.SymmetricKey)
    21  }
    22  
    23  var _ saltpack.SymmetricKeyResolver = (*MockPseudonymResolver)(nil)
    24  
    25  func (r *MockPseudonymResolver) ResolveKeys(identifiers [][]byte) ([]*saltpack.SymmetricKey, error) {
    26  	var pnyms []string
    27  	var keys []*saltpack.SymmetricKey
    28  
    29  	for _, psBytes := range identifiers {
    30  		pnyms = append(pnyms, hex.EncodeToString(psBytes))
    31  		var pnym [32]byte
    32  		copy(pnym[:], psBytes)
    33  		if key, found := r.pnymMap[pnym]; found {
    34  			keys = append(keys, key)
    35  		} else {
    36  			keys = append(keys, nil)
    37  		}
    38  	}
    39  	r.T.Logf("MockPseudonymResolver called on input: %+v", pnyms)
    40  	return keys, nil
    41  }
    42  
    43  func (r *MockPseudonymResolver) AddPseudonym(pnym [32]byte, key *saltpack.SymmetricKey) {
    44  	r.T.Logf("MockPseudonymResolver: adding pnym %s, key %s", hex.EncodeToString(pnym[:]), hex.EncodeToString(key[:]))
    45  	r.pnymMap[pnym] = key
    46  }
    47  
    48  func NewMockPseudonymResolver(t *testing.T) saltpack.SymmetricKeyResolver {
    49  	return &MockPseudonymResolver{
    50  		T:       t,
    51  		pnymMap: make(map[[32]byte](*saltpack.SymmetricKey)),
    52  	}
    53  }