File: helpers_for_test.go

package info (click to toggle)
coyim 0.3.7-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,064 kB
  • ctags: 4,528
  • sloc: xml: 5,120; sh: 328; python: 286; makefile: 235; ruby: 51
file content (183 lines) | stat: -rw-r--r-- 4,131 bytes parent folder | download
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
package session

import (
	"io"
	"reflect"
	"time"

	gocheck "gopkg.in/check.v1"

	"github.com/twstrike/otr3"
	"github.com/twstrike/coyim/session/access"
	"github.com/twstrike/coyim/session/events"
)

type mockSessionEventHandler struct {
	info                func(string)
	warn                func(string)
	alert               func(string)
	rosterReceived      func(access.Session)
	iqReceived          func(uid string)
	newOTRKeys          func(from string, conversation *otr3.Conversation)
	otrEnded            func(uid string)
	messageReceived     func(s access.Session, from string, timestamp time.Time, encrypted bool, message []byte)
	processPresence     func(from, to, show, status string, gone bool)
	subscriptionRequest func(s access.Session, uid string)
	subscribed          func(account, peer string)
	unsubscribe         func(account, peer string)
	disconnected        func()
	registerCallback    func(title, instructions string, fields []interface{}) error
}

func (m *mockSessionEventHandler) Info(v string) {
	if m.info != nil {
		m.info(v)
	}
}

func (m *mockSessionEventHandler) Warn(v string) {
	if m.warn != nil {
		m.warn(v)
	}
}

func (m *mockSessionEventHandler) Alert(v string) {
	if m.alert != nil {
		m.alert(v)
	}
}

func (m *mockSessionEventHandler) RosterReceived(s access.Session) {
	if m.rosterReceived != nil {
		m.rosterReceived(s)
	}
}

func (m *mockSessionEventHandler) IQReceived(uid string) {
	if m.iqReceived != nil {
		m.iqReceived(uid)
	}
}

func (m *mockSessionEventHandler) NewOTRKeys(from string, conversation *otr3.Conversation) {
	if m.newOTRKeys != nil {
		m.newOTRKeys(from, conversation)
	}
}

func (m *mockSessionEventHandler) OTREnded(uid string) {
	if m.otrEnded != nil {
		m.otrEnded(uid)
	}
}

func (m *mockSessionEventHandler) MessageReceived(s access.Session, from string, timestamp time.Time, encrypted bool, message []byte) {
	if m.messageReceived != nil {
		m.messageReceived(s, from, timestamp, encrypted, message)
	}
}

func (m *mockSessionEventHandler) ProcessPresence(from, to, show, status string, gone bool) {
	if m.processPresence != nil {
		m.processPresence(from, to, show, status, gone)
	}
}

func (m *mockSessionEventHandler) SubscriptionRequest(s access.Session, uid string) {
	if m.subscriptionRequest != nil {
		m.subscriptionRequest(s, uid)
	}
}

func (m *mockSessionEventHandler) Subscribed(account, peer string) {
	if m.subscribed != nil {
		m.subscribed(account, peer)
	}
}

func (m *mockSessionEventHandler) Unsubscribe(account, peer string) {
	if m.unsubscribe != nil {
		m.unsubscribe(account, peer)
	}
}

func (m *mockSessionEventHandler) Disconnected() {
	if m.disconnected != nil {
		m.disconnected()
	}
}

func (m *mockSessionEventHandler) RegisterCallback(title, instructions string, fields []interface{}) error {
	if m.registerCallback != nil {
		return m.registerCallback(title, instructions, fields)
	}
	return nil
}

type mockConnIOReaderWriter struct {
	read        []byte
	readIndex   int
	write       []byte
	errCount    int
	calledClose int
	err         error
}

func (iom *mockConnIOReaderWriter) Read(p []byte) (n int, err error) {
	if iom.readIndex >= len(iom.read) {
		return 0, io.EOF
	}
	i := copy(p, iom.read[iom.readIndex:])
	iom.readIndex += i
	var e error
	if iom.errCount == 0 {
		e = iom.err
	}
	iom.errCount--
	return i, e
}

func (iom *mockConnIOReaderWriter) Write(p []byte) (n int, err error) {
	iom.write = append(iom.write, p...)
	var e error
	if iom.errCount == 0 {
		e = iom.err
	}
	iom.errCount--
	return len(p), e
}

func (iom *mockConnIOReaderWriter) Close() error {
	iom.calledClose++
	return nil
}

func captureLogsEvents(c <-chan interface{}) (ret []events.Log) {
	for {
		select {
		case ev := <-c:
			switch t := ev.(type) {
			case events.Log:
				ret = append(ret, t)
			default:
				//ignore
			}
		case <-time.After(1 * time.Millisecond):
			return
		}
	}

	return
}

func assertLogContains(c *gocheck.C, ch <-chan interface{}, exp events.Log) {
	logs := captureLogsEvents(ch)

	for _, l := range logs {
		if reflect.DeepEqual(l, exp) {
			return
		}
	}

	c.Errorf("Could not finr %#v in %#v", exp, logs)
}