-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreader_test.go
139 lines (133 loc) · 3.62 KB
/
reader_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2014 The zephyr-go authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zephyr
import (
"errors"
"io/ioutil"
"log"
"net"
"os"
"reflect"
"testing"
"github.com/zephyr-im/krb5-go"
"github.com/zephyr-im/zephyr-go/zephyrtest"
)
func TestReadNoticesFromServer(t *testing.T) {
// This test is noisy.
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stderr)
addr1 := &net.UDPAddr{IP: net.IPv4(1, 1, 1, 1), Port: 1111}
addr2 := &net.UDPAddr{IP: net.IPv4(2, 2, 2, 2), Port: 2222}
clientAddr := &net.UDPAddr{IP: net.IPv4(3, 3, 3, 3), Port: 3333}
fatalErr := errors.New("polarity insufficiently reversed")
type test struct {
keyblock *krb5.KeyBlock
reads []zephyrtest.PacketRead
expected []*NoticeReaderResult
}
tests := []test{
// Basic case
{
sampleKeyBlock(),
[]zephyrtest.PacketRead{
{samplePacket(), addr1, nil},
}, []*NoticeReaderResult{
{sampleNotice(), AuthYes, addr1},
},
},
// Various non-fatal errors.
{
sampleKeyBlock(),
[]zephyrtest.PacketRead{
{nil, nil, zephyrtest.TemporaryError},
{samplePacket(), addr1, nil},
{nil, nil, zephyrtest.TemporaryError},
{nil, nil, zephyrtest.TemporaryError},
{samplePacket(), addr2, nil},
{sampleFailPacket(), addr1, nil},
{sampleMalformedChecksumPacket(), addr2, nil},
{sampleMalformedPortPacket(), addr1, nil},
{[]byte("bogus"), addr2, nil},
{samplePacket(), addr1, nil},
},
[]*NoticeReaderResult{
// samplePacket
{sampleNotice(), AuthYes, addr1},
// samplePacket
{sampleNotice(), AuthYes, addr2},
// sampleFailPacket
{sampleNotice(), AuthFailed, addr1},
// sampleMalformedChecksumPacket
{sampleNotice(), AuthFailed, addr2},
// sampleMalformedPortPacket and bogus are dropped.
// samplePacket
{sampleNotice(), AuthYes, addr1},
},
},
// Stop after fatal error.
{
sampleKeyBlock(),
[]zephyrtest.PacketRead{
{nil, nil, fatalErr},
{samplePacket(), addr1, nil},
},
[]*NoticeReaderResult{},
},
// nil key.
{
nil,
[]zephyrtest.PacketRead{
{samplePacket(), addr1, nil},
}, []*NoticeReaderResult{
{sampleNotice(), AuthFailed, addr1},
},
},
}
for ti, test := range tests {
// Buffer of 1 because one of the tests intentionally
// has an extra read.
readChan := make(chan zephyrtest.PacketRead, 1)
go func() {
for _, read := range test.reads {
readChan <- read
}
close(readChan)
}()
mock := zephyrtest.NewMockPacketConn(clientAddr, readChan)
out := ReadNoticesFromServer(mock, test.keyblock)
for ei, expect := range test.expected {
if r, ok := <-out; !ok {
t.Errorf("%d.%d. Expected notice: %v",
ti, ei, expect)
} else {
expectNoticesEqual(t, r.Notice, expect.Notice)
if r.AuthStatus != expect.AuthStatus {
t.Errorf("%d.%d. AuthStatus = %v; want %v",
ti, ei,
r.AuthStatus,
expect.AuthStatus)
}
if !reflect.DeepEqual(r.Addr, expect.Addr) {
t.Errorf("%d.%d. Addr = %v; want %v",
ti, ei,
r.Addr,
expect.Addr)
}
}
}
for r := range out {
t.Errorf("%d. unexpected notice: %v", ti, r)
}
}
}