github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/externals/assertion_parser_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  	"context"
     8  	"testing"
     9  
    10  	libkb "github.com/keybase/client/go/libkb"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestNormalization(t *testing.T) {
    15  	tc := setupTest(t, "Normalization", 1)
    16  	defer tc.Cleanup()
    17  	inp := "Web://A.AA || HttP://B.bb && dnS://C.cc || MaxFactor@reddit || zQueal@keyBASE || XanxA@hackernews || foO@TWITTER || 0123456789ABCDEF0123456789abcd19@uid || josh@gubble.SoCiAl"
    18  	outp := "a.aa@web,b.bb@http+c.cc@dns,maxfactor@reddit,zqueal,XanxA@hackernews,foo@twitter,0123456789abcdef0123456789abcd19@uid,josh@gubble.social"
    19  	expr, err := AssertionParse(libkb.NewMetaContext(context.Background(), tc.G), inp)
    20  	require.NoError(t, err)
    21  	require.Equal(t, expr.String(), outp)
    22  }
    23  
    24  type Pair struct {
    25  	k, v string
    26  }
    27  
    28  func TestParserFail1(t *testing.T) {
    29  	tc := setupTest(t, "ParserFail1", 1)
    30  	defer tc.Cleanup()
    31  	bads := []Pair{
    32  		{"", "Unexpected EOF parsing assertion"},
    33  		{"aa ||", "Unexpected EOF parsing assertion"},
    34  		{"aa &&", "Unexpected EOF parsing assertion"},
    35  		{"(aa", "Unbalanced parentheses"},
    36  		{"aa && dns:", "Bad assertion, no value given (key=dns)"},
    37  		{"bb && foo:a", "Unknown social network: foo"},
    38  		{"&& aa", "Unexpected token: &&"},
    39  		{"|| aa", "Unexpected token: ||"},
    40  		{"aa)", "Found junk at end of input: )"},
    41  		{"()", "Illegal parenthetical expression"},
    42  
    43  		// Invalid usernames
    44  		{"dns://a", "Invalid hostname: a"},
    45  		{"a@pgp", "bad hex string: 'a'"},
    46  		{"aBCP@pgp", "bad hex string: 'abcp'"},
    47  		{"jj@pgp", "bad hex string: 'jj'"},
    48  		{"[al@ice@keybase.io]@email", "Invalid email address: al@ice@keybase.io"},
    49  		{"email:[al@ice@keybase.io]", "Invalid email address: al@ice@keybase.io"},
    50  		{"phone:onetwothree", "Invalid phone number: onetwothree"},
    51  		{"onetwothree@phone", "Invalid phone number: onetwothree"},
    52  		{"1-555-222@phone", "Invalid phone number: 1-555-222"},
    53  
    54  		// Username missing
    55  		{"f@reddit", "Bad username: 'f'"},
    56  		{"http://", "Bad assertion, no value given (key=http)"},
    57  		{"reddit:", "Bad username: ''"},
    58  		{"reddit://", "Bad username: ''"},
    59  		{"gubble.social:", "username must be at least 2 characters, was 0"},
    60  		{"@gubble.social", "username must be at least 2 characters, was 0"},
    61  		{"@rooter", "Bad username: ''"},
    62  		{"@zzzzz", "Unknown social network: zzzzz"},
    63  
    64  		// Service name missing
    65  		{"hello@", "Invalid key-value identity: hello@"},
    66  		{":illegal", "Invalid key-value identity: :illegal"},
    67  		{"://what", "Invalid key-value identity: ://what"},
    68  
    69  		{"@", "Invalid key-value identity: @"},
    70  		{":", "Invalid key-value identity: :"},
    71  		{"://", "Invalid key-value identity: ://"},
    72  
    73  		{"alice@rooter@email", "Invalid key-value identity: alice@rooter@email"},
    74  		{"alice@keybase.io@email", "Invalid key-value identity: alice@keybase.io@email"},
    75  
    76  		{"(alice@keybasers.de)@email", "Illegal parenthetical expression"},
    77  		{"twitter://alice&&(alice@keybasers.de)@email", "Found junk at end of input: )"},
    78  		{"bob,[al#ice@kb.io]@email", "Syntax error when parsing: [al#ice@kb.io]@email"}, // `bob,` parsed successfully, but lexer did not match anything after
    79  		{"[al#ice@keybase.io]@email", "Syntax error when parsing: [al#ice@keybase.io]@email"},
    80  		{"[b,b@keybase.io]@email", "Invalid key-value identity: [b,b@keybase.io]@email"},
    81  
    82  		// Always require [] syntax for emails, even though this is theoretically { service: "email", name : "spam" }.
    83  		{"spam@email", "expected bracket syntax for email assertion"},
    84  
    85  		// entire email:alice@keybase.io is sweeped as URL and passet to
    86  		// assertion parser, which does not recognize syntax with both : and @
    87  		{"email:alice@keybase.io", "Invalid key-value identity: email:alice@keybase.io"},
    88  		// similar to above
    89  		{"email://alice@keybase.io", "Invalid key-value identity: email://alice@keybase.io"},
    90  		// non-email example
    91  		{"rooter:alice@twitter", "Invalid key-value identity: rooter:alice@twitter"},
    92  
    93  		// [] is parser-level illegal
    94  		{"[]@email", "Invalid key-value identity: []@email"},
    95  		{"[]@rooter", "Invalid key-value identity: []@rooter"},
    96  		{"rooter:[]", "Invalid key-value identity: rooter:[]"},
    97  		{"email:[]", "Invalid key-value identity: email:[]"},
    98  		{"email://[]", "Invalid key-value identity: email://[]"},
    99  
   100  		{"[alice]@rooter", "unexpected bracket syntax for assertion: rooter"},
   101  		{"rooter:[alice]", "unexpected bracket syntax for assertion: rooter"},
   102  
   103  		{"[michal]", "Syntax error when parsing: [michal]"},
   104  		// This is lexed as URL, ERROR, EOF, but it fails prematurely
   105  		// with assertion parser error on "[michal]@" (first URL), so we
   106  		// don't get to see "Syntax error".
   107  		{"[michal]@[keybase]", "Invalid key-value identity: [michal]@"},
   108  
   109  		{"[+1-555-222]@phone", "unexpected bracket syntax for assertion: phone"}, // nice try but no
   110  	}
   111  
   112  	for _, bad := range bads {
   113  		ret, err := AssertionParse(libkb.NewMetaContext(context.Background(), tc.G), bad.k)
   114  		require.Error(t, err, "for %q: ret is: %+v", bad.k, ret)
   115  		require.Equal(t, bad.v, err.Error(), "when testing %q", bad.k)
   116  	}
   117  }