~samwhited/xmpp

ref: c98da397314a57419fb1041f927b83246ed7366d xmpp/bind_test.go -rw-r--r-- 4.3 KiB
c98da397Sam Whited carbons: new package 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
// Copyright 2016 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 xmpp_test

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

	"mellium.im/xmpp"
	"mellium.im/xmpp/internal/ns"
	"mellium.im/xmpp/internal/xmpptest"
	"mellium.im/xmpp/jid"
)

func TestBindList(t *testing.T) {
	buf := &bytes.Buffer{}
	bind := xmpp.BindResource()
	e := xml.NewEncoder(buf)
	start := xml.StartElement{Name: xml.Name{Space: ns.Bind, Local: "bind"}}
	req, err := bind.List(context.Background(), e, start)
	if err != nil {
		t.Fatal(err)
	}
	if err = e.Flush(); err != nil {
		t.Fatal(err)
	}
	if !req {
		t.Error("Bind must always be required")
	}
	if buf.String() != `<bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"></bind>` {
		t.Errorf("Got unexpected value for bind listing: `%s`", buf.String())
	}
}

func TestBindParse(t *testing.T) {
	bind := xmpp.BindResource()
	for i, test := range []struct {
		XML string
		err bool
	}{
		0: {`<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>`, false},
		1: {`<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'></bind>`, false},
		2: {`<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>STUFF</bind>`, false},
		3: {`<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><test/></bind>`, false},
		4: {`<notbind xmlns='urn:ietf:params:xml:ns:xmpp-bind'></notbind>`, true},
		5: {`<bind xmlns='notbindns'></bind>`, true},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {

			d := xml.NewDecoder(strings.NewReader(test.XML))
			tok, err := d.Token()
			if err != nil {
				// We screwed up the test string…
				panic(err)
			}
			start := tok.(xml.StartElement)
			req, data, err := bind.Parse(context.Background(), d, &start)
			switch {
			case test.err && err == nil:
				t.Error("Expected error from parse")
				return
			case !test.err && err != nil:
				t.Error(err)
				return
			}
			if !req {
				t.Error("Expected parsed bind feature to be required")
			}
			if data != nil {
				t.Error("Expected bind data to be nil")
			}
		})
	}
}
func bindFunc(j jid.JID, s string) (jid.JID, error) {
	if s == "" {
		s = "empty"
	}
	return j.WithResource(s)
}

var bindTestCases = [...]xmpptest.FeatureTestCase{
	// BindCustom server tests
	0: {
		State:      xmpp.Received,
		Feature:    xmpp.BindCustom(bindFunc),
		In:         `<iq type="set" id="123" to="example.net" from="test@example.net"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>test</resource></bind></iq>`,
		Out:        `<iq xmlns="jabber:server" type="result" to="test@example.net" from="example.net" id="123"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>test@example.net/test</jid></bind></iq>`,
		FinalState: xmpp.Ready,
	},
	1: {
		State:      xmpp.Received,
		Feature:    xmpp.BindCustom(bindFunc),
		In:         `<iq type="set" id="1" to="example.net"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>test</resource></bind></iq>`,
		Out:        `<iq xmlns="jabber:server" type="result" from="example.net" id="1"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>test@example.net/test</jid></bind></iq>`,
		FinalState: xmpp.Ready,
	},
	2: {
		State:      xmpp.Received,
		Feature:    xmpp.BindCustom(bindFunc),
		In:         `<iq type="set" id="2" from="me@example.net"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>test</resource></bind></iq>`,
		Out:        `<iq xmlns="jabber:server" type="result" to="me@example.net" id="2"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>test@example.net/test</jid></bind></iq>`,
		FinalState: xmpp.Ready,
	},
	3: {
		State:      xmpp.Received,
		Feature:    xmpp.BindCustom(bindFunc),
		In:         `<iq type="set" id="123"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource></resource></bind></iq>`,
		Out:        `<iq xmlns="jabber:server" type="result" id="123"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>test@example.net/empty</jid></bind></iq>`,
		FinalState: xmpp.Ready,
	},
	4: {
		State:      xmpp.Received,
		Feature:    xmpp.BindCustom(bindFunc),
		In:         `<iq type="set" id="123"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/></iq>`,
		Out:        `<iq xmlns="jabber:server" type="result" id="123"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>test@example.net/empty</jid></bind></iq>`,
		FinalState: xmpp.Ready,
	},
}

func TestBind(t *testing.T) {
	xmpptest.RunFeatureTests(t, bindTestCases[:])
}