~samwhited/xmpp

05044243b8222698f6f54376e6a90751cbc76e98 — Sam Whited 5 years ago 09ca904
Add initial resource binding feature w/ tests
2 files changed, 108 insertions(+), 0 deletions(-)

A bind.go
A bind_test.go
A bind.go => bind.go +38 -0
@@ 0,0 1,38 @@
// Copyright 2016 Sam Whited.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp

import (
	"context"
	"encoding/xml"
	"fmt"
	"io"
)

// BindResource returns a stream feature that can be used for binding a
// resource. The provided resource may (and probably "should") be empty, and is
// merely a suggestion; the server may return a completely different resource to
// prevent conflicts. If BindResource is used on a server connection, the
// resource argument is ignored.
func BindResource(resource string) *StreamFeature {
	return &StreamFeature{
		Name:       xml.Name{Space: NSBind, Local: "bind"},
		Necessary:  Secure,
		Prohibited: Authn,
		List: func(ctx context.Context, w io.Writer) (bool, error) {
			_, err := fmt.Fprintf(w, `<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>`)
			return true, err
		},
		Parse: func(ctx context.Context, d *xml.Decoder, start *xml.StartElement) (bool, interface{}, error) {
			parsed := struct {
				XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"`
			}{}
			return true, nil, d.DecodeElement(&parsed, start)
		},
		Negotiate: func(ctx context.Context, conn *Conn, data interface{}) (mask SessionState, err error) {
			panic("Not yet implemented")
		},
	}
}

A bind_test.go => bind_test.go +70 -0
@@ 0,0 1,70 @@
// Copyright 2016 Sam Whited.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp

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

func TestBindList(t *testing.T) {
	buf := &bytes.Buffer{}
	bind := BindResource("")
	req, err := bind.List(context.Background(), buf)
	if 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'/>` {
		t.Errorf("Got unexpected value for bind listing: `%s`", buf.String())
	}
}

func TestBindParse(t *testing.T) {
	for _, test := range []struct {
		XML string
		err bool
	}{
		{`<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>`, false},
		{`<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'></bind>`, false},
		{`<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>STUFF</bind>`, false},
		{`<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><test/></bind>`, false},
		{`<notbind xmlns='urn:ietf:params:xml:ns:xmpp-bind'></notbind>`, true},
		{`<bind xmlns='notbindns'></bind>`, true},
	} {
		// Run each test twice, once without a requested resource and once for a
		// requested resource (which should be ignored, making the results
		// identical).
		for _, bind := range []*StreamFeature{BindResource(""), BindResource("ignored")} {
			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")
				continue
			case !test.err && err != nil:
				t.Error(err)
				continue
			}
			if !req {
				t.Error("Expected parsed bind feature to be required")
			}
			if data != nil {
				t.Error("Expected bind data to be nil")
			}
		}
	}
}