summaryrefslogtreecommitdiff
path: root/pkg/vpn/demux/demuxer_test.go
blob: 096e35a4b7f3bf583777d9925a6015b0c92b4666 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright (c) 2016 Martin Atkins

// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package demux

import (
	"bytes"
	"fmt"
	"io"
	"reflect"
	"testing"
)

func TestDemultiplex(t *testing.T) {
	type TestCase struct {
		Input           []string
		ExpectedReplies []string
		ExpectedEvents  []string
	}

	testCases := []TestCase{
		{
			Input:           []string{},
			ExpectedReplies: []string{},
			ExpectedEvents:  []string{},
		},
		{
			Input: []string{
				"SUCCESS: foo bar baz",
			},
			ExpectedReplies: []string{
				"SUCCESS: foo bar baz",
			},
			ExpectedEvents: []string{},
		},
		{
			Input: []string{
				">STATE:1234,ASSIGN_IP,,10.0.0.1,",
			},
			ExpectedReplies: []string{},
			ExpectedEvents: []string{
				"STATE:1234,ASSIGN_IP,,10.0.0.1,",
			},
		},
		{
			Input: []string{
				">STATE:1234,ASSIGN_IP,,10.0.0.1,",
				">STATE:5678,ASSIGN_IP,,10.0.0.1,",
				">STATE:9012,ASSIGN_IP,,10.0.0.1,",
			},
			ExpectedReplies: []string{},
			ExpectedEvents: []string{
				"STATE:1234,ASSIGN_IP,,10.0.0.1,",
				"STATE:5678,ASSIGN_IP,,10.0.0.1,",
				"STATE:9012,ASSIGN_IP,,10.0.0.1,",
			},
		},
		{
			Input: []string{
				">STATE:1234,ASSIGN_IP,,10.0.0.1,",
				"SUCCESS: foo bar baz",
				">STATE:5678,ASSIGN_IP,,10.0.0.1,",
			},
			ExpectedReplies: []string{
				"SUCCESS: foo bar baz",
			},
			ExpectedEvents: []string{
				"STATE:1234,ASSIGN_IP,,10.0.0.1,",
				"STATE:5678,ASSIGN_IP,,10.0.0.1,",
			},
		},
		{
			Input: []string{
				"SUCCESS: foo bar baz",
				">STATE:1234,ASSIGN_IP,,10.0.0.1,",
				"SUCCESS: baz bar foo",
			},
			ExpectedReplies: []string{
				"SUCCESS: foo bar baz",
				"SUCCESS: baz bar foo",
			},
			ExpectedEvents: []string{
				"STATE:1234,ASSIGN_IP,,10.0.0.1,",
			},
		},
	}

	for i, testCase := range testCases {
		r := mockReader(testCase.Input)
		gotReplies, gotEvents := captureMsgs(r)

		if !reflect.DeepEqual(gotReplies, testCase.ExpectedReplies) {
			t.Errorf(
				"test %d returned incorrect replies\ngot  %#v\nwant %#v",
				i, gotReplies, testCase.ExpectedReplies,
			)
		}

		if !reflect.DeepEqual(gotEvents, testCase.ExpectedEvents) {
			t.Errorf(
				"test %d returned incorrect events\ngot  %#v\nwant %#v",
				i, gotEvents, testCase.ExpectedEvents,
			)
		}
	}
}

func TestDemultiplex_error(t *testing.T) {
	r := &alwaysErroringReader{}

	gotReplies, gotEvents := captureMsgs(r)

	expectedReplies := []string{}
	expectedEvents := []string{
		"FATAL:Error reading from OpenVPN",
	}

	if !reflect.DeepEqual(gotReplies, expectedReplies) {
		t.Errorf(
			"incorrect replies\ngot  %#v\nwant %#v",
			gotReplies, expectedReplies,
		)
	}

	if !reflect.DeepEqual(gotEvents, expectedEvents) {
		t.Errorf(
			"incorrect events\ngot  %#v\nwant %#v",
			gotEvents, expectedEvents,
		)
	}
}

func mockReader(msgs []string) io.Reader {
	var buf []byte
	for _, msg := range msgs {
		buf = append(buf, []byte(msg)...)
		buf = append(buf, '\n')
	}
	return bytes.NewReader(buf)
}

func captureMsgs(r io.Reader) (replies, events []string) {
	replyCh := make(chan []byte)
	eventCh := make(chan []byte)

	replies = make([]string, 0)
	events = make([]string, 0)

	go Demultiplex(r, replyCh, eventCh)

	for replyCh != nil || eventCh != nil {
		select {

		case msg, ok := <-replyCh:
			if ok {
				replies = append(replies, string(msg))
			} else {
				replyCh = nil
			}

		case msg, ok := <-eventCh:
			if ok {
				events = append(events, string(msg))
			} else {
				eventCh = nil
			}

		}

	}

	return replies, events
}

type alwaysErroringReader struct{}

func (r *alwaysErroringReader) Read(buf []byte) (int, error) {
	return 0, fmt.Errorf("mock error")
}

// Somewhat-contrived example of blocking for a reply while concurrently
// processing asynchronous events.
func ExampleDemultiplex() {
	// In a real caller we would have a net.IPConn as our reader,
	// but we'll use a bytes reader here as a test.
	r := bytes.NewReader([]byte(
		// A reply to a hypothetical command interspersed between
		// two asynchronous events.
		">HOLD:Waiting for hold release\nSUCCESS: foo\n>FATAL:baz\n",
	))

	// No strong need for buffering on this channel because usually
	// a message sender will immediately block waiting for the
	// associated response message.
	replyCh := make(chan []byte)

	// Make sure the event channel buffer is deep enough that slow event
	// processing won't significantly delay synchronous replies. If you
	// process events quickly, or if you aren't sending any commands
	// concurrently with acting on events, then this is not so important.
	eventCh := make(chan []byte, 10)

	// Start demultiplexing the message stream in the background.
	// This goroutine will exit once the reader signals EOF.
	go Demultiplex(r, replyCh, eventCh)

	// Some coroutine has sent a hypothetical message to OpenVPN,
	// and it can directly block until the associated reply arrives.
	// The events will be concurrently handled by our event loop
	// below while we wait for the reply to show up.
	go func() {
		replyMsgBuf := <-replyCh
		fmt.Printf("Command reply: %s\n", string(replyMsgBuf))
	}()

	// Main event loop deals with the async events as they arrive,
	// independently of any commands that are pending.
	for msgBuf := range eventCh {
		fmt.Printf("Event: %s\n", string(msgBuf))
	}
}