~samwhited/xmpp

ref: 7fa903e220ac86501595262dd3d89c3b51f9be7d xmpp/ping/ping_test.go -rw-r--r-- 3.4 KiB
7fa903e2Sam Whited all: be more strict about stanza namespaces a month 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
// 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/disco/info"
	"mellium.im/xmpp/internal/xmpptest"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/mux"
	"mellium.im/xmpp/ping"
	"mellium.im/xmpp/stanza"
)

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

var marshalTestCases = []xmpptest.EncodingTestCase{
	0: {
		NoUnmarshal: true,
		Value: &ping.IQ{
			IQ: stanza.IQ{To: jid.MustParse("feste@example.net")},
		},
		XML: `<iq type="" to="feste@example.net"><ping xmlns="urn:xmpp:ping"></ping></iq>`,
	},
	1: {
		Value: &ping.IQ{
			IQ: stanza.IQ{
				Type: stanza.GetIQ,
				To:   jid.MustParse("feste@example.net"),
			},
		},
		XML: `<iq type="get" to="feste@example.net"><ping xmlns="urn:xmpp:ping"></ping></iq>`,
	},
}

func TestEncode(t *testing.T) {
	xmpptest.RunEncodingTests(t, marshalTestCases)
}

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)
	}
}