Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 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 | } |
| 28 | |
| 29 | func TestParseUserId(t *testing.T) { |
| 30 | for i, test := range userIdTests { |
| 31 | name, comment, email := parseUserId(test.id) |
| 32 | if name != test.name { |
| 33 | t.Errorf("%d: name mismatch got:%s want:%s", i, name, test.name) |
| 34 | } |
| 35 | if comment != test.comment { |
| 36 | t.Errorf("%d: comment mismatch got:%s want:%s", i, comment, test.comment) |
| 37 | } |
| 38 | if email != test.email { |
| 39 | t.Errorf("%d: email mismatch got:%s want:%s", i, email, test.email) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | var newUserIdTests = []struct { |
| 45 | name, comment, email, id string |
| 46 | }{ |
| 47 | {"foo", "", "", "foo"}, |
| 48 | {"", "bar", "", "(bar)"}, |
| 49 | {"", "", "baz", "<baz>"}, |
| 50 | {"foo", "bar", "", "foo (bar)"}, |
| 51 | {"foo", "", "baz", "foo <baz>"}, |
| 52 | {"", "bar", "baz", "(bar) <baz>"}, |
| 53 | {"foo", "bar", "baz", "foo (bar) <baz>"}, |
| 54 | } |
| 55 | |
| 56 | func TestNewUserId(t *testing.T) { |
| 57 | for i, test := range newUserIdTests { |
| 58 | uid := NewUserId(test.name, test.comment, test.email) |
| 59 | if uid == nil { |
| 60 | t.Errorf("#%d: returned nil", i) |
| 61 | continue |
| 62 | } |
| 63 | if uid.Id != test.id { |
| 64 | t.Errorf("#%d: got '%s', want '%s'", i, uid.Id, test.id) |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | var invalidNewUserIdTests = []struct { |
| 70 | name, comment, email string |
| 71 | }{ |
| 72 | {"foo(", "", ""}, |
| 73 | {"foo<", "", ""}, |
| 74 | {"", "bar)", ""}, |
| 75 | {"", "bar<", ""}, |
| 76 | {"", "", "baz>"}, |
| 77 | {"", "", "baz)"}, |
| 78 | {"", "", "baz\x00"}, |
| 79 | } |
| 80 | |
| 81 | func TestNewUserIdWithInvalidInput(t *testing.T) { |
| 82 | for i, test := range invalidNewUserIdTests { |
| 83 | if uid := NewUserId(test.name, test.comment, test.email); uid != nil { |
| 84 | t.Errorf("#%d: returned non-nil value: %#v", i, uid) |
| 85 | } |
| 86 | } |
| 87 | } |