OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [net/] [mail/] [message_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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 mail
6
 
7
import (
8
        "bytes"
9
        "io/ioutil"
10
        "reflect"
11
        "testing"
12
        "time"
13
)
14
 
15
var parseTests = []struct {
16
        in     string
17
        header Header
18
        body   string
19
}{
20
        {
21
                // RFC 5322, Appendix A.1.1
22
                in: `From: John Doe 
23
To: Mary Smith 
24
Subject: Saying Hello
25
Date: Fri, 21 Nov 1997 09:55:06 -0600
26
Message-ID: <1234@local.machine.example>
27
 
28
This is a message just to say hello.
29
So, "Hello".
30
`,
31
                header: Header{
32
                        "From":       []string{"John Doe "},
33
                        "To":         []string{"Mary Smith "},
34
                        "Subject":    []string{"Saying Hello"},
35
                        "Date":       []string{"Fri, 21 Nov 1997 09:55:06 -0600"},
36
                        "Message-Id": []string{"<1234@local.machine.example>"},
37
                },
38
                body: "This is a message just to say hello.\nSo, \"Hello\".\n",
39
        },
40
}
41
 
42
func TestParsing(t *testing.T) {
43
        for i, test := range parseTests {
44
                msg, err := ReadMessage(bytes.NewBuffer([]byte(test.in)))
45
                if err != nil {
46
                        t.Errorf("test #%d: Failed parsing message: %v", i, err)
47
                        continue
48
                }
49
                if !headerEq(msg.Header, test.header) {
50
                        t.Errorf("test #%d: Incorrectly parsed message header.\nGot:\n%+v\nWant:\n%+v",
51
                                i, msg.Header, test.header)
52
                }
53
                body, err := ioutil.ReadAll(msg.Body)
54
                if err != nil {
55
                        t.Errorf("test #%d: Failed reading body: %v", i, err)
56
                        continue
57
                }
58
                bodyStr := string(body)
59
                if bodyStr != test.body {
60
                        t.Errorf("test #%d: Incorrectly parsed message body.\nGot:\n%+v\nWant:\n%+v",
61
                                i, bodyStr, test.body)
62
                }
63
        }
64
}
65
 
66
func headerEq(a, b Header) bool {
67
        if len(a) != len(b) {
68
                return false
69
        }
70
        for k, as := range a {
71
                bs, ok := b[k]
72
                if !ok {
73
                        return false
74
                }
75
                if !reflect.DeepEqual(as, bs) {
76
                        return false
77
                }
78
        }
79
        return true
80
}
81
 
82
func TestDateParsing(t *testing.T) {
83
        tests := []struct {
84
                dateStr string
85
                exp     time.Time
86
        }{
87
                // RFC 5322, Appendix A.1.1
88
                {
89
                        "Fri, 21 Nov 1997 09:55:06 -0600",
90
                        time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
91
                },
92
                // RFC5322, Appendix A.6.2
93
                // Obsolete date.
94
                {
95
                        "21 Nov 97 09:55:06 GMT",
96
                        time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("GMT", 0)),
97
                },
98
        }
99
        for _, test := range tests {
100
                hdr := Header{
101
                        "Date": []string{test.dateStr},
102
                }
103
                date, err := hdr.Date()
104
                if err != nil {
105
                        t.Errorf("Failed parsing %q: %v", test.dateStr, err)
106
                        continue
107
                }
108
                if !date.Equal(test.exp) {
109
                        t.Errorf("Parse of %q: got %+v, want %+v", test.dateStr, date, test.exp)
110
                }
111
        }
112
}
113
 
114
func TestAddressParsing(t *testing.T) {
115
        tests := []struct {
116
                addrsStr string
117
                exp      []*Address
118
        }{
119
                // Bare address
120
                {
121
                        `jdoe@machine.example`,
122
                        []*Address{{
123
                                Address: "jdoe@machine.example",
124
                        }},
125
                },
126
                // RFC 5322, Appendix A.1.1
127
                {
128
                        `John Doe `,
129
                        []*Address{{
130
                                Name:    "John Doe",
131
                                Address: "jdoe@machine.example",
132
                        }},
133
                },
134
                // RFC 5322, Appendix A.1.2
135
                {
136
                        `"Joe Q. Public" `,
137
                        []*Address{{
138
                                Name:    "Joe Q. Public",
139
                                Address: "john.q.public@example.com",
140
                        }},
141
                },
142
                {
143
                        `Mary Smith , jdoe@example.org, Who? `,
144
                        []*Address{
145
                                {
146
                                        Name:    "Mary Smith",
147
                                        Address: "mary@x.test",
148
                                },
149
                                {
150
                                        Address: "jdoe@example.org",
151
                                },
152
                                {
153
                                        Name:    "Who?",
154
                                        Address: "one@y.test",
155
                                },
156
                        },
157
                },
158
                {
159
                        `, "Giant; \"Big\" Box" `,
160
                        []*Address{
161
                                {
162
                                        Address: "boss@nil.test",
163
                                },
164
                                {
165
                                        Name:    `Giant; "Big" Box`,
166
                                        Address: "sysservices@example.net",
167
                                },
168
                        },
169
                },
170
                // RFC 5322, Appendix A.1.3
171
                // TODO(dsymonds): Group addresses.
172
 
173
                // RFC 2047 "Q"-encoded ISO-8859-1 address.
174
                {
175
                        `=?iso-8859-1?q?J=F6rg_Doe?= `,
176
                        []*Address{
177
                                {
178
                                        Name:    `Jörg Doe`,
179
                                        Address: "joerg@example.com",
180
                                },
181
                        },
182
                },
183
                // RFC 2047 "Q"-encoded UTF-8 address.
184
                {
185
                        `=?utf-8?q?J=C3=B6rg_Doe?= `,
186
                        []*Address{
187
                                {
188
                                        Name:    `Jörg Doe`,
189
                                        Address: "joerg@example.com",
190
                                },
191
                        },
192
                },
193
                // RFC 2047, Section 8.
194
                {
195
                        `=?ISO-8859-1?Q?Andr=E9?= Pirard `,
196
                        []*Address{
197
                                {
198
                                        Name:    `André Pirard`,
199
                                        Address: "PIRARD@vm1.ulg.ac.be",
200
                                },
201
                        },
202
                },
203
                // Custom example of RFC 2047 "B"-encoded ISO-8859-1 address.
204
                {
205
                        `=?ISO-8859-1?B?SvZyZw==?= `,
206
                        []*Address{
207
                                {
208
                                        Name:    `Jörg`,
209
                                        Address: "joerg@example.com",
210
                                },
211
                        },
212
                },
213
                // Custom example of RFC 2047 "B"-encoded UTF-8 address.
214
                {
215
                        `=?UTF-8?B?SsO2cmc=?= `,
216
                        []*Address{
217
                                {
218
                                        Name:    `Jörg`,
219
                                        Address: "joerg@example.com",
220
                                },
221
                        },
222
                },
223
        }
224
        for _, test := range tests {
225
                addrs, err := newAddrParser(test.addrsStr).parseAddressList()
226
                if err != nil {
227
                        t.Errorf("Failed parsing %q: %v", test.addrsStr, err)
228
                        continue
229
                }
230
                if !reflect.DeepEqual(addrs, test.exp) {
231
                        t.Errorf("Parse of %q: got %+v, want %+v", test.addrsStr, addrs, test.exp)
232
                }
233
        }
234
}
235
 
236
func TestAddressFormatting(t *testing.T) {
237
        tests := []struct {
238
                addr *Address
239
                exp  string
240
        }{
241
                {
242
                        &Address{Address: "bob@example.com"},
243
                        "",
244
                },
245
                {
246
                        &Address{Name: "Bob", Address: "bob@example.com"},
247
                        `"Bob" `,
248
                },
249
                {
250
                        // note the ö (o with an umlaut)
251
                        &Address{Name: "Böb", Address: "bob@example.com"},
252
                        `=?utf-8?q?B=C3=B6b?= `,
253
                },
254
        }
255
        for _, test := range tests {
256
                s := test.addr.String()
257
                if s != test.exp {
258
                        t.Errorf("Address%+v.String() = %v, want %v", *test.addr, s, test.exp)
259
                }
260
        }
261
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.