~samwhited/xmpp

76de4018a3b6ba10c63d36c1e7d2845e5c6e6c5e — Sam Whited 5 years ago 9de4741
Add tests for JID splitting

Spit out coverage when running tests with make
2 files changed, 47 insertions(+), 2 deletions(-)

M Makefile
M jid_test.go
M Makefile => Makefile +2 -2
@@ 2,11 2,11 @@

.PHONY: test
test:
	go test
	go test -cover

.PHONY: benchmark
benchmark:
	go test -bench . -benchmem -run 'Benchmark.*'
	go test -cover -bench . -benchmem -run 'Benchmark.*'

.PHONY: build
build:

M jid_test.go => jid_test.go +45 -0
@@ 4,10 4,55 @@
package jid

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

// Ensure that JID parts are split properly.
func TestValidPartsFromString(t *testing.T) {
	valid_decompositions := [][]string{
		{"lp@dp/rp", "lp", "dp", "rp"},
		{"dp/rp", "", "dp", "rp"},
		{"dp", "", "dp", ""},
		{"lp@dp//rp", "lp", "dp", "/rp"},
		{"lp@dp/rp/", "lp", "dp", "rp/"},
		{"lp@dp/@rp/", "lp", "dp", "@rp/"},
		{"lp@dp/lp@dp/rp", "lp", "dp", "lp@dp/rp"},
		{"dp//rp", "", "dp", "/rp"},
		{"dp/rp/", "", "dp", "rp/"},
		{"dp/@rp/", "", "dp", "@rp/"},
		{"dp/lp@dp/rp", "", "dp", "lp@dp/rp"},
	}
	for _, d := range valid_decompositions {
		lp, dp, rp, err := partsFromString(d[0])
		if err != nil || lp != d[1] || dp != d[2] || rp != d[3] {
			t.FailNow()
		}
	}
}

// Ensure that JIDs that are too long return an error.
func TestLongParts(t *testing.T) {
	// Generate a part that is too long.
	pb := bytes.NewBuffer(make([]byte, 0, 1024))
	for i := 0; i < 64; i++ {
		pb.WriteString("aaaaaaaaaaaaaaaa")
	}
	ps := pb.String()
	invalid_decompositions := []string{
		ps + "@example.com/test",
		"lp@" + ps + "/test",
		"lp@example.com/" + ps,
		ps + "@" + ps + "/" + ps,
	}
	for _, d := range invalid_decompositions {
		if _, _, _, err := partsFromString(d); err != nil {
			t.FailNow()
		}
	}
}

// JIDS cannot contain invalid UTF8 in the localpart.
func TestNewInvalidUtf8Localpart(t *testing.T) {
	invalid := string([]byte{0xff, 0xfe, 0xfd})