~samwhited/xmpp

xmpp/delay/delay_test.go -rw-r--r-- 5.5 KiB
c9743d9fSam Whited docs: update changelog to include deprecations 3 days 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
158
159
160
161
162
163
164
165
// Copyright 2021 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 delay_test

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

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

var (
	_ xml.Marshaler       = delay.Delay{}
	_ xmlstream.Marshaler = delay.Delay{}
	_ xmlstream.WriterTo  = delay.Delay{}
	_ xml.Unmarshaler     = (*delay.Delay)(nil)
)

var insertTestCases = [...]struct {
	in     string
	out    string
	stanza bool
	ns     string
}{
	0: {},
	1: {
		stanza: true,
		in:     `<message xmlns="jabber:client"/>`,
		out:    `<message xmlns="jabber:client"><delay xmlns="urn:xmpp:delay" stamp="0001-01-01T00:00:00Z" from="me@example.net">foo</delay></message>`,
		ns:     ns.Client,
	},
	2: {
		stanza: true,
		in:     `<message xmlns="jabber:server"/><message xmlns="jabber:server"><body>test</body></message><message xmlns="jabber:client"/>`,
		out:    `<message xmlns="jabber:server"><delay xmlns="urn:xmpp:delay" stamp="0001-01-01T00:00:00Z" from="me@example.net">foo</delay></message><message xmlns="jabber:server"><delay xmlns="urn:xmpp:delay" stamp="0001-01-01T00:00:00Z" from="me@example.net">foo</delay><body xmlns="jabber:server">test</body></message><message xmlns="jabber:client"></message>`,
		ns:     ns.Server,
	},
	3: {
		stanza: true,
		in:     `<message xmlns="jabber:badns"/>`,
		out:    `<message xmlns="jabber:badns"></message>`,
		ns:     ns.Client,
	},
	4: {
		in:  `<message xmlns="jabber:client"/>`,
		out: `<message xmlns="jabber:client"><delay xmlns="urn:xmpp:delay" stamp="0001-01-01T00:00:00Z" from="me@example.net">foo</delay></message>`,
		ns:  ns.Client,
	},
	5: {
		in:  `<message xmlns="jabber:server"/><message xmlns="jabber:client"><body>test</body></message>`,
		out: `<message xmlns="jabber:server"><delay xmlns="urn:xmpp:delay" stamp="0001-01-01T00:00:00Z" from="me@example.net">foo</delay></message><message xmlns="jabber:client"><delay xmlns="urn:xmpp:delay" stamp="0001-01-01T00:00:00Z" from="me@example.net">foo</delay><body xmlns="jabber:client">test</body></message>`,
		ns:  ns.Server,
	},
	6: {
		in:  `<message xmlns="jabber:badns"/>`,
		out: `<message xmlns="jabber:badns"><delay xmlns="urn:xmpp:delay" stamp="0001-01-01T00:00:00Z" from="me@example.net">foo</delay></message>`,
		ns:  ns.Client,
	},
	7: {
		in:  `<message xmlns="jabber:anyns"/>`,
		out: `<message xmlns="jabber:anyns"><delay xmlns="urn:xmpp:delay" stamp="0001-01-01T00:00:00Z" from="me@example.net">foo</delay></message>`,
	},
}

func TestInsert(t *testing.T) {
	for i, tc := range insertTestCases {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			d := delay.Delay{From: jid.MustParse("me@example.net"), Time: time.Time{}, Reason: "foo"}
			var r xml.TokenReader
			if tc.stanza {
				stanzaDelayer := delay.Stanza(d, tc.ns)
				r = stanzaDelayer(xml.NewDecoder(strings.NewReader(tc.in)))
			} else {
				r = delay.Insert(d)(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 (start.Name.Local == "message" || start.Name.Local == "iq") && attr.Name.Local == "xmlns"
			})(r)
			var buf strings.Builder
			e := xml.NewEncoder(&buf)
			_, err := xmlstream.Copy(e, r)
			if err != nil {
				t.Fatalf("error encoding: %v", err)
			}
			if err = e.Flush(); err != nil {
				t.Fatalf("error flushing: %v", err)
			}

			if out := buf.String(); tc.out != out {
				t.Errorf("wrong output:\nwant=%s,\n got=%s", tc.out, out)
			}
		})
	}
}

var marshalTests = [...]struct {
	unmarshal bool // true if we should only unmarshal for this test.
	in        delay.Delay
	out       string
}{
	0: {
		out: `<delay xmlns="urn:xmpp:delay" stamp="0001-01-01T00:00:00Z"></delay>`,
	},
	1: {
		in:  delay.Delay{From: jid.MustParse("me@example.net")},
		out: `<delay xmlns="urn:xmpp:delay" stamp="0001-01-01T00:00:00Z" from="me@example.net"></delay>`,
	},
	2: {
		in:  delay.Delay{Time: time.Time{}.Add(24 * time.Hour)},
		out: `<delay xmlns="urn:xmpp:delay" stamp="0001-01-02T00:00:00Z"></delay>`,
	},
	3: {
		in:  delay.Delay{Reason: "foo"},
		out: `<delay xmlns="urn:xmpp:delay" stamp="0001-01-01T00:00:00Z">foo</delay>`,
	},
	4: {
		in:  delay.Delay{From: jid.MustParse("me@example.net"), Time: time.Time{}.Add(24 * time.Hour), Reason: "foo"},
		out: `<delay xmlns="urn:xmpp:delay" stamp="0001-01-02T00:00:00Z" from="me@example.net">foo</delay>`,
	},
	5: {
		unmarshal: true,
		in:        delay.Delay{Time: time.Time{}.Add(24 * time.Hour), Reason: "foo"},
		out:       `<delay xmlns="urn:xmpp:delay" stamp="0001-01-02T00:00:00Z" foo:from="me@example.net" xmlns:foo="test">foo</delay>`,
	},
}

func TestMarshal(t *testing.T) {
	for i, tc := range marshalTests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			if !tc.unmarshal {
				b, err := xml.Marshal(tc.in)
				if err != nil {
					t.Fatalf("unexpected error marshaling: %v", err)
				}
				if out := string(b); out != tc.out {
					t.Fatalf("wrong value:\nwant=%v,\n got=%v", tc.out, out)
				}
			}

			d := delay.Delay{}
			err := xml.Unmarshal([]byte(tc.out), &d)
			if err != nil {
				t.Fatalf("error unmarshaling: %v", err)
			}
			if !d.From.Equal(tc.in.From) {
				t.Errorf("wrong from JID: want=%v, got=%v", tc.in.From, d.From)
			}
			if !d.Time.Equal(tc.in.Time) {
				t.Errorf("wrong timestamp: want=%v, got=%v", tc.in.Time, d.Time)
			}
			if d.Reason != tc.in.Reason {
				t.Errorf("wrong chardata: want=%q, got=%q", tc.in.Reason, d.Reason)
			}
		})
	}
}