...

Source file src/github.com/ProtonMail/go-crypto/openpgp/packet/userid_test.go

Documentation: github.com/ProtonMail/go-crypto/openpgp/packet

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package packet
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  var userIdTests = []struct {
    12  	id                   string
    13  	name, comment, email string
    14  }{
    15  	{"", "", "", ""},
    16  	{"John Smith", "John Smith", "", ""},
    17  	{"John Smith ()", "John Smith", "", ""},
    18  	{"John Smith () <>", "John Smith", "", ""},
    19  	{"(comment", "", "comment", ""},
    20  	{"(comment)", "", "comment", ""},
    21  	{"<email", "", "", "email"},
    22  	{"<email>   sdfk", "", "", "email"},
    23  	{"  John Smith  (  Comment ) asdkflj < email > lksdfj", "John Smith", "Comment", "email"},
    24  	{"  John Smith  < email > lksdfj", "John Smith", "", "email"},
    25  	{"(<foo", "", "<foo", ""},
    26  	{"René Descartes (العربي)", "René Descartes", "العربي", ""},
    27  	{"John@Smith", "", "", "John@Smith"},
    28  }
    29  
    30  func TestParseUserId(t *testing.T) {
    31  	for i, test := range userIdTests {
    32  		name, comment, email := parseUserId(test.id)
    33  		if name != test.name {
    34  			t.Errorf("%d: name mismatch got:%s want:%s", i, name, test.name)
    35  		}
    36  		if comment != test.comment {
    37  			t.Errorf("%d: comment mismatch got:%s want:%s", i, comment, test.comment)
    38  		}
    39  		if email != test.email {
    40  			t.Errorf("%d: email mismatch got:%s want:%s", i, email, test.email)
    41  		}
    42  	}
    43  }
    44  
    45  var newUserIdTests = []struct {
    46  	name, comment, email, id string
    47  }{
    48  	{"foo", "", "", "foo"},
    49  	{"", "bar", "", "(bar)"},
    50  	{"", "", "baz", "<baz>"},
    51  	{"foo", "bar", "", "foo (bar)"},
    52  	{"foo", "", "baz", "foo <baz>"},
    53  	{"", "bar", "baz", "(bar) <baz>"},
    54  	{"foo", "bar", "baz", "foo (bar) <baz>"},
    55  }
    56  
    57  func TestNewUserId(t *testing.T) {
    58  	for i, test := range newUserIdTests {
    59  		uid := NewUserId(test.name, test.comment, test.email)
    60  		if uid == nil {
    61  			t.Errorf("#%d: returned nil", i)
    62  			continue
    63  		}
    64  		if uid.Id != test.id {
    65  			t.Errorf("#%d: got '%s', want '%s'", i, uid.Id, test.id)
    66  		}
    67  	}
    68  }
    69  
    70  var invalidNewUserIdTests = []struct {
    71  	name, comment, email string
    72  }{
    73  	{"foo(", "", ""},
    74  	{"foo<", "", ""},
    75  	{"", "bar)", ""},
    76  	{"", "bar<", ""},
    77  	{"", "", "baz>"},
    78  	{"", "", "baz)"},
    79  	{"", "", "baz\x00"},
    80  }
    81  
    82  func TestNewUserIdWithInvalidInput(t *testing.T) {
    83  	for i, test := range invalidNewUserIdTests {
    84  		if uid := NewUserId(test.name, test.comment, test.email); uid != nil {
    85  			t.Errorf("#%d: returned non-nil value: %#v", i, uid)
    86  		}
    87  	}
    88  }
    89  

View as plain text