github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/externals/implicit_team_test.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  package externals
     5  
     6  import (
     7  	"bytes"
     8  	"context"
     9  	"encoding/hex"
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/require"
    14  
    15  	libkb "github.com/keybase/client/go/libkb"
    16  	"github.com/keybase/client/go/msgpack"
    17  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    18  )
    19  
    20  func containsString(xs []string, target string) bool {
    21  	for _, x := range xs {
    22  		if x == target {
    23  			return true
    24  		}
    25  	}
    26  	return false
    27  }
    28  
    29  func TestParseImplicitTeamTLFName(t *testing.T) {
    30  	tc := setupTest(t, "ParseImplicitTeamTLFName", 1)
    31  	defer tc.Cleanup()
    32  	badNames := []string{
    33  		"foobar",
    34  		"/keybas/public/foo,bar",
    35  		"/keybase/publi/foo,bar",
    36  		"/keybase/public/foobar,foo:@bar",
    37  		"/keybase/public/foobar,foobar::",
    38  		"/keybase/public/foobar,alice@fakemedia",
    39  		"/keybase/public/foobar__underscore",
    40  	}
    41  	for _, badName := range badNames {
    42  		_, err := libkb.ParseImplicitTeamTLFName(MakeAssertionContext(libkb.NewMetaContext(context.Background(), tc.G)), badName)
    43  		require.Error(t, err)
    44  	}
    45  	goodName := "/keybase/public/dave,twitter:alice,bob@facebook,carol@keybase,echo"
    46  	name, err := libkb.ParseImplicitTeamTLFName(MakeAssertionContext(libkb.NewMetaContext(context.Background(), tc.G)), goodName)
    47  	require.NoError(t, err)
    48  	require.Equal(t, name.IsPublic, true)
    49  	require.Equal(t, len(name.Writers.KeybaseUsers), 3)
    50  	require.Equal(t, len(name.Writers.UnresolvedUsers), 2)
    51  	require.True(t, containsString(name.Writers.KeybaseUsers, "dave"))
    52  	require.True(t, containsString(name.Writers.KeybaseUsers, "carol"))
    53  	require.True(t, containsString(name.Writers.KeybaseUsers, "echo"))
    54  
    55  	firstSocial := name.Writers.UnresolvedUsers[0]
    56  	secondSocial := name.Writers.UnresolvedUsers[1]
    57  	aliceExpected := keybase1.SocialAssertion{User: "alice", Service: keybase1.SocialAssertionService("twitter")}
    58  	bobExpected := keybase1.SocialAssertion{User: "bob", Service: keybase1.SocialAssertionService("facebook")}
    59  	require.True(t, firstSocial != secondSocial)
    60  	require.True(t, firstSocial == aliceExpected || firstSocial == bobExpected)
    61  	require.True(t, secondSocial == aliceExpected || secondSocial == bobExpected)
    62  
    63  	goodName = "/keybase/public/dave,bob@facebook#alice (conflicted copy 2017-03-04)"
    64  	name, err = libkb.ParseImplicitTeamTLFName(MakeAssertionContext(libkb.NewMetaContext(context.Background(), tc.G)), goodName)
    65  	require.NoError(t, err)
    66  	require.Equal(t, name.IsPublic, true)
    67  	require.Equal(t, len(name.Writers.KeybaseUsers), 1)
    68  	require.Equal(t, len(name.Writers.UnresolvedUsers), 1)
    69  	require.True(t, containsString(name.Writers.KeybaseUsers, "dave"))
    70  	require.Equal(t, name.ConflictInfo.Generation, keybase1.ConflictGeneration(1), "right conflict info")
    71  
    72  	goodName = "/keybase/public/dave,bob@facebook#alice (conflicted copy 2017-03-04 #2)"
    73  	name, err = libkb.ParseImplicitTeamTLFName(MakeAssertionContext(libkb.NewMetaContext(context.Background(), tc.G)), goodName)
    74  	require.NoError(t, err)
    75  	require.Equal(t, name.IsPublic, true)
    76  	require.Equal(t, len(name.Writers.KeybaseUsers), 1)
    77  	require.Equal(t, len(name.Writers.UnresolvedUsers), 1)
    78  	require.True(t, containsString(name.Writers.KeybaseUsers, "dave"))
    79  	require.Equal(t, name.ConflictInfo.Generation, keybase1.ConflictGeneration(2), "right conflict info")
    80  }
    81  
    82  func TestParseImplicitTeamTLFNameEvenMore(t *testing.T) {
    83  	tc := setupTest(t, "ParseImplicitTeamTLFNameEvenMore", 1)
    84  	defer tc.Cleanup()
    85  	tests := []struct {
    86  		input  string
    87  		output *keybase1.ImplicitTeamDisplayName
    88  	}{
    89  		{
    90  			"/keybase/private/bob,alice#bob,alice",
    91  			&keybase1.ImplicitTeamDisplayName{
    92  				IsPublic: false,
    93  				Writers: keybase1.ImplicitTeamUserSet{
    94  					KeybaseUsers: []string{"alice", "bob"},
    95  				},
    96  			},
    97  		},
    98  		{
    99  			"/keybase/private/bob,alice#bob,alice,doug,charlie",
   100  			&keybase1.ImplicitTeamDisplayName{
   101  				IsPublic: false,
   102  				Writers: keybase1.ImplicitTeamUserSet{
   103  					KeybaseUsers: []string{"alice", "bob"},
   104  				},
   105  				Readers: keybase1.ImplicitTeamUserSet{
   106  					KeybaseUsers: []string{"charlie", "doug"},
   107  				},
   108  			},
   109  		},
   110  		{
   111  			"/keybase/private/bob,alice,jason@github#bob,alice,doug,charlie,github:jason,keith@twitter,twitter:keith,beth@reddit,keith@github",
   112  			&keybase1.ImplicitTeamDisplayName{
   113  				IsPublic: false,
   114  				Writers: keybase1.ImplicitTeamUserSet{
   115  					KeybaseUsers: []string{"alice", "bob"},
   116  					UnresolvedUsers: []keybase1.SocialAssertion{
   117  						{
   118  							User:    "jason",
   119  							Service: keybase1.SocialAssertionService("github"),
   120  						},
   121  					},
   122  				},
   123  				Readers: keybase1.ImplicitTeamUserSet{
   124  					KeybaseUsers: []string{"charlie", "doug"},
   125  					UnresolvedUsers: []keybase1.SocialAssertion{
   126  						{
   127  							User:    "beth",
   128  							Service: keybase1.SocialAssertionService("reddit"),
   129  						},
   130  						{
   131  							User:    "keith",
   132  							Service: keybase1.SocialAssertionService("github"),
   133  						},
   134  						{
   135  							User:    "keith",
   136  							Service: keybase1.SocialAssertionService("twitter"),
   137  						},
   138  					},
   139  				},
   140  			},
   141  		},
   142  		{
   143  			"/keybase/private/keybase:alice,bob@keybase#bob,alice",
   144  			&keybase1.ImplicitTeamDisplayName{
   145  				IsPublic: false,
   146  				Writers: keybase1.ImplicitTeamUserSet{
   147  					KeybaseUsers: []string{"alice", "bob"},
   148  				},
   149  			},
   150  		},
   151  		{"/keybase/private/alice#alice#alice", nil},
   152  		{"/keybase/private/#alice", nil},
   153  	}
   154  
   155  	deepEq := func(a, b keybase1.ImplicitTeamDisplayName) bool {
   156  		x, _ := msgpack.Encode(a)
   157  		y, _ := msgpack.Encode(b)
   158  		fmt.Printf("%s\n", hex.EncodeToString(x))
   159  		fmt.Printf("%s\n", hex.EncodeToString(y))
   160  		return bytes.Equal(x, y)
   161  	}
   162  
   163  	for _, test := range tests {
   164  		itn, err := libkb.ParseImplicitTeamTLFName(MakeAssertionContext(libkb.NewMetaContext(context.Background(), tc.G)), test.input)
   165  		if test.output == nil {
   166  			require.Error(t, err)
   167  		} else {
   168  			require.True(t, deepEq(itn, *test.output))
   169  		}
   170  	}
   171  }
   172  
   173  // TestParseImplicitTeamDisplayName is just a quick sanity check.
   174  // quick sanity test -- mostly redundant with TLFName test above
   175  func TestParseImplicitTeamDisplayName(t *testing.T) {
   176  	tc := setupTest(t, "ParseImplicitTeamDisplayName", 1)
   177  	defer tc.Cleanup()
   178  	goodName := "twitter:alice,bob@facebook,carol@keybase,dave"
   179  	_, err := libkb.ParseImplicitTeamDisplayName(MakeAssertionContext(libkb.NewMetaContext(context.Background(), tc.G)), "", false)
   180  	require.Error(t, err)
   181  	namePrivate, err := libkb.ParseImplicitTeamDisplayName(MakeAssertionContext(libkb.NewMetaContext(context.Background(), tc.G)), goodName, false)
   182  	require.NoError(t, err)
   183  	namePublic, err := libkb.ParseImplicitTeamDisplayName(MakeAssertionContext(libkb.NewMetaContext(context.Background(), tc.G)), goodName, true)
   184  	require.NoError(t, err)
   185  	require.False(t, namePrivate.IsPublic)
   186  	require.True(t, namePublic.IsPublic)
   187  }