github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/remote/registry/profiles_test.go (about)

     1  package registry
     2  
     3  import (
     4  	"encoding/base64"
     5  	"math/rand"
     6  	"testing"
     7  
     8  	crypto "github.com/libp2p/go-libp2p-core/crypto"
     9  )
    10  
    11  func TestRegisterProfile(t *testing.T) {
    12  	ps := NewMemProfiles()
    13  
    14  	src := rand.New(rand.NewSource(0))
    15  	key0, _, err := crypto.GenerateSecp256k1Key(src)
    16  	if err != nil {
    17  		t.Error(err.Error())
    18  		return
    19  	}
    20  	p, err := ProfileFromPrivateKey(&Profile{Username: "key0"}, key0)
    21  	if err != nil {
    22  		t.Error(err.Error())
    23  		return
    24  	}
    25  
    26  	src = rand.New(rand.NewSource(10000))
    27  	// TODO (b5) - this makes tests slow. use a cache
    28  	key1, _, err := crypto.GenerateEd25519Key(src)
    29  	if err != nil {
    30  		t.Error(err.Error())
    31  		return
    32  	}
    33  	p2, err := ProfileFromPrivateKey(&Profile{Username: "key0"}, key1)
    34  	if err != nil {
    35  		t.Error(err.Error())
    36  		return
    37  	}
    38  
    39  	mismatchSig, err := key0.Sign([]byte("bad_data"))
    40  	if err != nil {
    41  		t.Error(err.Error())
    42  		return
    43  	}
    44  
    45  	p3, err := ProfileFromPrivateKey(&Profile{Username: "renamed"}, key0)
    46  	if err != nil {
    47  		t.Error(err.Error())
    48  		return
    49  	}
    50  
    51  	cases := []struct {
    52  		p   *Profile
    53  		err string
    54  	}{
    55  		{&Profile{Username: "a"}, "profileID is required"},
    56  		{&Profile{ProfileID: p.ProfileID, Username: p.Username, Signature: p.Signature, PublicKey: "bad_data"}, "invalid publickey: decoding base64-encoded public key: illegal base64 data at input byte 3"},
    57  		{&Profile{ProfileID: p.ProfileID, Username: p.Username, Signature: p.Signature, PublicKey: base64.StdEncoding.EncodeToString([]byte("bad_data"))}, `invalid publickey: public key "YmFkX2RhdGE=" is invalid: unexpected EOF`},
    58  		{&Profile{ProfileID: p.ProfileID, Username: p.Username, PublicKey: p.PublicKey, Signature: "bad_data"}, "signature base64 encoding: illegal base64 data at input byte 3"},
    59  		{&Profile{ProfileID: p.ProfileID, Username: p.Username, PublicKey: p.PublicKey, Signature: base64.StdEncoding.EncodeToString([]byte("bad_data"))}, "invalid signature: malformed signature: no header magic"},
    60  		{&Profile{ProfileID: p.ProfileID, Username: p.Username, PublicKey: p.PublicKey, Signature: base64.StdEncoding.EncodeToString(mismatchSig)}, "mismatched signature"},
    61  		{p, ""},
    62  		{p, ""}, // check that peer can double-register their own username without err
    63  		{p2, "username 'key0' is taken"},
    64  		{p3, ""},
    65  	}
    66  
    67  	for i, c := range cases {
    68  		err := RegisterProfile(ps, c.p)
    69  		if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) {
    70  			t.Errorf("case %d error mismatch. expected: '%s', got: '%s'", i, c.err, err)
    71  		}
    72  	}
    73  
    74  	if err := DeregisterProfile(ps, &Profile{}); err == nil {
    75  		t.Error("invalid profile should error")
    76  	}
    77  	if err := DeregisterProfile(ps, &Profile{ProfileID: p.ProfileID, Username: p.Username, PublicKey: p.PublicKey, Signature: base64.StdEncoding.EncodeToString(mismatchSig)}); err == nil {
    78  		t.Error("unverifiable profile should error")
    79  	}
    80  	if err := DeregisterProfile(ps, p2); err != nil {
    81  		t.Errorf("error deregistering: %s", err.Error())
    82  	}
    83  }
    84  
    85  func TestProfilesSortedRange(t *testing.T) {
    86  	ps := NewMemProfiles()
    87  
    88  	src := rand.New(rand.NewSource(0))
    89  	usernames := []string{"a", "b", "c"}
    90  	for _, username := range usernames {
    91  		pkey, _, err := crypto.GenerateSecp256k1Key(src)
    92  		if err != nil {
    93  			t.Error(err.Error())
    94  			return
    95  		}
    96  		p, err := ProfileFromPrivateKey(&Profile{Username: username}, pkey)
    97  		if err != nil {
    98  			t.Error(err.Error())
    99  			return
   100  		}
   101  
   102  		if err := RegisterProfile(ps, p); err != nil {
   103  			t.Error(err.Error())
   104  			return
   105  		}
   106  	}
   107  
   108  	l, err := ps.Len()
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  
   113  	if l != len(usernames) {
   114  		t.Errorf("expected len to equal username length")
   115  		return
   116  	}
   117  
   118  	for iter := 0; iter < 100; iter++ {
   119  		i := 0
   120  		ps.SortedRange(func(key string, p *Profile) (bool, error) {
   121  			if usernames[i] != p.Username {
   122  				t.Errorf("iter: %d sorted index %d mismatch. expected: %s, got: %s", iter, i, usernames[i], p.Username)
   123  				return true, nil
   124  			}
   125  			i++
   126  			return false, nil
   127  		})
   128  		break
   129  	}
   130  }