~samwhited/xmpp

xmpp/stanza/id_test.go -rw-r--r-- 4.9 KiB
e9b0a2deSam Whited docs: do a quick editing pass over the docs 2 hours 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
// Copyright 2020 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 stanza_test

import (
	"encoding/xml"
	"regexp"
	"strconv"
	"strings"
	"testing"

	"mellium.im/xmlstream"
	"mellium.im/xmpp/internal/ns"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/stanza"
)

const (
	testOrigin = `<origin-id xmlns="urn:xmpp:sid:0" id="abc"></origin-id>`
	testStanza = `<stanza-id xmlns="urn:xmpp:sid:0" id="abc" by="test@example.net"></stanza-id>`
)

var idTestCases = [...]struct {
	in     string
	origin string
	id     string
	ns     string
}{
	0: {
		in:     `<message xmlns="jabber:client"></message>`,
		origin: `<message xmlns="jabber:client">` + testOrigin + `</message>`,
		id:     `<message xmlns="jabber:client">` + testStanza + `</message>`,
		ns:     ns.Client,
	},
	1: {
		in:     `<iq xmlns="jabber:client"></iq>`,
		origin: `<iq xmlns="jabber:client">` + testOrigin + `</iq>`,
		id:     `<iq xmlns="jabber:client">` + testStanza + `</iq>`,
		ns:     ns.Client,
	},
	2: {
		in:     `<presence xmlns="jabber:client"></presence>`,
		origin: `<presence xmlns="jabber:client">` + testOrigin + `</presence>`,
		id:     `<presence xmlns="jabber:client">` + testStanza + `</presence>`,
		ns:     ns.Client,
	},
	3: {
		in:     `<message xmlns="jabber:server"></message>`,
		origin: `<message xmlns="jabber:server">` + testOrigin + `</message>`,
		id:     `<message xmlns="jabber:server">` + testStanza + `</message>`,
		ns:     ns.Server,
	},
	4: {
		in:     `<iq xmlns="jabber:server"></iq>`,
		origin: `<iq xmlns="jabber:server">` + testOrigin + `</iq>`,
		id:     `<iq xmlns="jabber:server">` + testStanza + `</iq>`,
		ns:     ns.Server,
	},
	5: {
		in:     `<presence xmlns="jabber:server"></presence>`,
		origin: `<presence xmlns="jabber:server">` + testOrigin + `</presence>`,
		id:     `<presence xmlns="jabber:server">` + testStanza + `</presence>`,
		ns:     ns.Server,
	},
	6: {
		in:     `<not-stanza><message xmlns="jabber:client"></message></not-stanza>`,
		origin: `<not-stanza><message xmlns="jabber:client"></message></not-stanza>`,
		id:     `<not-stanza><message xmlns="jabber:client"></message></not-stanza>`,
		ns:     ns.Client,
	},
	7: {
		in:     `<not-stanza><iq xmlns="jabber:client"></iq></not-stanza>`,
		origin: `<not-stanza><iq xmlns="jabber:client"></iq></not-stanza>`,
		id:     `<not-stanza><iq xmlns="jabber:client"></iq></not-stanza>`,
		ns:     ns.Client,
	},
	8: {
		in:     `<not-stanza><presence xmlns="jabber:client"></presence></not-stanza>`,
		origin: `<not-stanza><presence xmlns="jabber:client"></presence></not-stanza>`,
		id:     `<not-stanza><presence xmlns="jabber:client"></presence></not-stanza>`,
		ns:     ns.Client,
	},
	9: {
		in:     `<presence xmlns="jabber:badns"></presence>`,
		origin: `<presence xmlns="jabber:badns"></presence>`,
		id:     `<presence xmlns="jabber:badns"></presence>`,
		ns:     ns.Client,
	},
	10: {
		in:     `<presence xmlns="jabber:badns"></presence>`,
		origin: `<presence xmlns="jabber:badns">` + testOrigin + `</presence>`,
		id:     `<presence xmlns="jabber:badns">` + testStanza + `</presence>`,
		ns:     "jabber:badns",
	},
}

func TestAddID(t *testing.T) {
	idReplacer := regexp.MustCompile(`id="(.*?)"`)

	by := jid.MustParse("test@example.net")

	for i, tc := range idTestCases {
		addID := stanza.AddID(by, tc.ns)
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			t.Run("origin", func(t *testing.T) {
				r := stanza.AddOriginID(xml.NewDecoder(strings.NewReader(tc.in)), tc.ns)
				// Prevent duplicate xmlns attributes. See https://mellium.im/issue/75
				r = xmlstream.RemoveAttr(func(start xml.StartElement, attr xml.Attr) bool {
					return attr.Name.Local == "xmlns"
				})(r)
				var buf strings.Builder
				e := xml.NewEncoder(&buf)
				_, err := xmlstream.Copy(e, r)
				if err != nil {
					t.Fatalf("error copying xml stream: %v", err)
				}
				if err = e.Flush(); err != nil {
					t.Fatalf("error flushing stream: %v", err)
				}
				out := buf.String()
				// We need this to be testable, not random.
				out = idReplacer.ReplaceAllString(out, `id="abc"`)
				if out != tc.origin {
					t.Errorf("wrong output:\nwant=%v,\n got=%v", tc.origin, out)
				}
			})
			t.Run("stanza", func(t *testing.T) {
				r := addID(xml.NewDecoder(strings.NewReader(tc.in)))
				// Prevent duplicate xmlns attributes. See https://mellium.im/issue/75
				r = xmlstream.RemoveAttr(func(start xml.StartElement, attr xml.Attr) bool {
					return attr.Name.Local == "xmlns"
				})(r)
				var buf strings.Builder
				e := xml.NewEncoder(&buf)
				_, err := xmlstream.Copy(e, r)
				if err != nil {
					t.Fatalf("error copying xml stream: %v", err)
				}
				if err = e.Flush(); err != nil {
					t.Fatalf("error flushing stream: %v", err)
				}
				out := buf.String()
				// We need this to be testable, not random.
				out = idReplacer.ReplaceAllString(out, `id="abc"`)
				if out != tc.id {
					t.Errorf("wrong output:\nwant=%v,\n got=%v", tc.id, out)
				}
			})
		})
	}
}