~samwhited/xmpp

ref: 467e8c4a0b977d765eadf64f02ebb5edee0ce805 xmpp/ping/ping_test.go -rw-r--r-- 3.8 KiB
467e8c4aSam Whited component: add integration tests 1 year, 4 months ago
                                                                                
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
// Copyright 2019 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package ping_test

import (
	"context"
	"encoding/xml"
	"strings"
	"testing"

	"mellium.im/xmlstream"
	"mellium.im/xmpp/internal/xmpptest"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/mux"
	"mellium.im/xmpp/ping"
	"mellium.im/xmpp/stanza"
)

var (
	_ xmlstream.WriterTo  = ping.IQ{}
	_ xmlstream.Marshaler = ping.IQ{}
	_ mux.IQHandler       = ping.Handler{}
)

func TestEncode(t *testing.T) {
	j := jid.MustParse("feste@example.net")

	ping := ping.IQ{
		IQ: stanza.IQ{To: j},
	}

	t.Run("marshal", func(t *testing.T) {
		out, err := xml.Marshal(ping)
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		const expected = `<iq id="" to="feste@example.net" from="" type="get"><ping xmlns="urn:xmpp:ping"></ping></iq>`
		if string(out) != expected {
			t.Errorf("wrong encoding: want=%s, got=%s", expected, out)
		}
	})

	t.Run("write", func(t *testing.T) {
		var b strings.Builder
		e := xml.NewEncoder(&b)
		_, err := ping.WriteXML(e)
		if err != nil {
			t.Fatalf("error writing XML token stream: %v", err)
		}
		err = e.Flush()
		if err != nil {
			t.Fatalf("error flushing token stream: %v", err)
		}

		const expected = `<iq type="" to="feste@example.net"><ping xmlns="urn:xmpp:ping"></ping></iq>`
		if streamOut := b.String(); streamOut != expected {
			t.Errorf("wrong stream encoding: want=%s, got=%s", expected, streamOut)
		}
	})
}

type tokenReadEncoder struct {
	xml.TokenReader
	xmlstream.Encoder
}

func TestRoundTrip(t *testing.T) {
	m := mux.New(ping.Handle())
	cs := xmpptest.NewClientServer(
		xmpptest.ServerHandler(m),
	)

	err := ping.Send(context.Background(), cs.Client, cs.Server.LocalAddr())
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
}

func TestWrongIQType(t *testing.T) {
	var b strings.Builder
	e := xml.NewEncoder(&b)
	d := xml.NewDecoder(strings.NewReader(`<iq type="set"><ping xmlns="urn:xmpp:ping"/></iq>`))
	tok, _ := d.Token()
	start := tok.(xml.StartElement)

	m := mux.New(mux.IQ(stanza.SetIQ, xml.Name{Local: "ping", Space: ping.NS}, ping.Handler{}))
	err := m.HandleXMPP(tokenReadEncoder{
		TokenReader: d,
		Encoder:     e,
	}, &start)
	if err != nil {
		t.Errorf("unexpected error handling ping: %v", err)
	}
	err = e.Flush()
	if err != nil {
		t.Errorf("unexpected error flushing encoder: %v", err)
	}

	out := b.String()
	if out != "" {
		t.Errorf("unexpected output: %s", out)
	}
}

func TestBadPayloadLocalname(t *testing.T) {
	var b strings.Builder
	e := xml.NewEncoder(&b)
	d := xml.NewDecoder(strings.NewReader(`<iq type="get"><badlocal xmlns="urn:xmpp:ping"/></iq>`))
	tok, _ := d.Token()
	start := tok.(xml.StartElement)

	m := mux.New(mux.IQ(stanza.GetIQ, xml.Name{Local: "badlocal", Space: ping.NS}, ping.Handler{}))
	err := m.HandleXMPP(tokenReadEncoder{
		TokenReader: d,
		Encoder:     e,
	}, &start)
	if err != nil {
		t.Errorf("unexpected error handling ping: %v", err)
	}
	err = e.Flush()
	if err != nil {
		t.Errorf("unexpected error flushing encoder: %v", err)
	}

	out := b.String()
	if out != "" {
		t.Errorf("unexpected output: %s", out)
	}
}

func TestBadPayloadNamespace(t *testing.T) {
	var b strings.Builder
	e := xml.NewEncoder(&b)
	d := xml.NewDecoder(strings.NewReader(`<iq type="get"><ping xmlns="badnamespace"/></iq>`))
	tok, _ := d.Token()
	start := tok.(xml.StartElement)

	m := mux.New(mux.IQ(stanza.GetIQ, xml.Name{Local: "ping", Space: "badnamespace"}, ping.Handler{}))
	err := m.HandleXMPP(tokenReadEncoder{
		TokenReader: d,
		Encoder:     e,
	}, &start)
	if err != nil {
		t.Errorf("unexpected error handling ping: %v", err)
	}
	err = e.Flush()
	if err != nil {
		t.Errorf("unexpected error flushing encoder: %v", err)
	}

	out := b.String()
	if out != "" {
		t.Errorf("unexpected output: %s", out)
	}
}