~samwhited/xmpp

d5437ef6a08d7fdd75ea9667b7d9e7e62d8c8c24 — Sam Whited 7 years ago a8abf50
Make error values expected type (error)
2 files changed, 30 insertions(+), 30 deletions(-)

M jid.go
M jid_test.go
M jid.go => jid.go +25 -25
@@ 16,16 16,16 @@ import (
)

// Define some reusable error messages.
const (
	ErrorInvalidString   = "String is not valid UTF-8"
	ErrorEmptyPart       = "JID parts must be > 0 bytes"
	ErrorLongPart        = "JID parts must be < 1023 bytes"
	ErrorLongDomainLabel = "Domain names must be ≤ 63 chars per label"
	ErrorLongDomainName  = "Domain names must be ≤ 253 chars"
	ErrorLongDomainBytes = "Domain names must be ≤ 255 octets"
	ErrorInvalidJid      = "String is not a valid JID"
	ErrorIllegalRune     = "String contains an illegal chartacter"
	ErrorIllegalSpace    = "String contains illegal whitespace"
var (
	ErrorInvalidString   = errors.New("String is not valid UTF-8")
	ErrorEmptyPart       = errors.New("JID parts must be > 0 bytes")
	ErrorLongPart        = errors.New("JID parts must be < 1023 bytes")
	ErrorLongDomainLabel = errors.New("Domain names must be ≤ 63 chars per label")
	ErrorLongDomainName  = errors.New("Domain names must be ≤ 253 chars")
	ErrorLongDomainBytes = errors.New("Domain names must be ≤ 255 octets")
	ErrorInvalidJid      = errors.New("String is not a valid JID")
	ErrorIllegalRune     = errors.New("String contains an illegal chartacter")
	ErrorIllegalSpace    = errors.New("String contains illegal whitespace")
)

// NF is the Unicode normalization form to use. According to RFC 6122:


@@ 88,21 88,21 @@ func NormalizeJIDPart(part string) (string, error) {
	switch normalized := NF.String(part); {
	case len(normalized) == 0:
		// The normalized length should be > 0 bytes
		return "", errors.New(ErrorEmptyPart)
		return "", ErrorEmptyPart
	case len(normalized) > 1023:
		// The normalized length should be ≤ 1023 bytes
		return "", errors.New(ErrorLongPart)
		return "", ErrorLongPart
	case !utf8.ValidString(part):
		// The original string should be valid UTF-8
		return "", errors.New(ErrorInvalidString)
		return "", ErrorInvalidString
	case strings.ContainsAny(part, "\"&'/:<>@"):
		// The original string should not contain any illegal characters. After
		// normalization some of these characters maybe present.
		return "", errors.New(ErrorIllegalRune)
		return "", ErrorIllegalRune
	// TODO: Is there no function or method to just do this?
	case len(strings.Fields("'"+normalized+"'")) != 1:
		// There should be no whitespace in the normalized part.
		return "", errors.New(ErrorIllegalSpace)
		return "", ErrorIllegalSpace
		// TODO: Use a proper stringprep library to make sure this is all correct.
	default:
		return normalized, nil


@@ 117,17 117,17 @@ func NormalizeResourcePart(part string) (string, error) {
	switch normalized := NF.String(part); {
	case len(normalized) == 0:
		// The normalized length should be > 0 bytes
		return "", errors.New(ErrorEmptyPart)
		return "", ErrorEmptyPart
	case len(normalized) > 1023:
		// The normalized length should be ≤ 1023 bytes
		return "", errors.New(ErrorLongPart)
		return "", ErrorLongPart
	case !utf8.ValidString(part):
		// The original string should be valid UTF-8
		return "", errors.New(ErrorInvalidString)
		return "", ErrorInvalidString
	// TODO: Is there no function or method to just do this?
	case len(strings.Fields("'"+normalized+"'")) != 1:
		// There should be no whitespace in the normalized part.
		return "", errors.New(ErrorIllegalSpace)
		return "", ErrorIllegalSpace
		// TODO: Use a proper stringprep library to make sure this is all correct.
	default:
		return normalized, nil


@@ 220,7 220,7 @@ func (j *JID) FromString(s string) error {

	// Make sure the string is valid UTF-8
	if !utf8.ValidString(s) {
		return errors.New(ErrorInvalidString)
		return ErrorInvalidString
	}

	// According to RFC 6122:


@@ 238,7 238,7 @@ func (j *JID) FromString(s string) error {

	// Do not allow whitespace elsewhere in the string…
	if len(strings.Fields(s)) != 1 {
		return errors.New(ErrorIllegalSpace)
		return ErrorIllegalSpace
	}

	atCount := strings.Count(s, "@")


@@ 257,7 257,7 @@ func (j *JID) FromString(s string) error {
	case atCount == 1 && slashCount == 0:
		// Bare JID ("test@example.net" or "test@example")
		if atLoc == 0 || atLoc == len(s)-1 {
			return errors.New(ErrorEmptyPart)
			return ErrorEmptyPart
		}
		err := j.SetLocalPart(s[0:atLoc])
		if err != nil {


@@ 272,7 272,7 @@ func (j *JID) FromString(s string) error {
		// domainpart + resourcepart (eg. "example/rp" or "example/@/")
		if slashLoc == 0 || slashLoc == len(s)-1 {
			// Error if JID is of the form "/jid" or "jid/" ("jid//" is okay)
			return errors.New(ErrorEmptyPart)
			return ErrorEmptyPart
		}
		err := j.SetDomainPart(s[0:slashLoc])
		if err != nil {


@@ 287,7 287,7 @@ func (j *JID) FromString(s string) error {
		// Full JID (eg. "test@example.net/resourcepart" or "test@example.net/@/")
		last := len(s) - 1
		if atLoc == 0 || slashLoc == 0 || atLoc == last || slashLoc == last || slashLoc == atLoc+1 {
			return errors.New(ErrorEmptyPart)
			return ErrorEmptyPart
		}
		err := j.SetLocalPart(s[0:atLoc])
		if err != nil {


@@ 303,7 303,7 @@ func (j *JID) FromString(s string) error {
		}

	default: // Too many '@' or '/' symbols
		return errors.New(ErrorIllegalRune)
		return ErrorIllegalRune
	}

	return nil

M jid_test.go => jid_test.go +5 -5
@@ 14,7 14,7 @@ import (
func TestNewInvalidUtf8Jid(t *testing.T) {
	invalid := string([]byte{0xff, 0xfe, 0xfd})
	_, err := NewJID(invalid + "@example.com/resourcepart")
	if err == nil || err.Error() != ErrorInvalidString {
	if err == nil || err != ErrorInvalidString {
		t.FailNow()
	}
}


@@ 22,7 22,7 @@ func TestNewInvalidUtf8Jid(t *testing.T) {
// Trying to create a JID with an empty localpart should error.
func TestNewEmptyLocalPart(t *testing.T) {
	_, err := NewJID("@example.com/resourcepart")
	if err == nil || err.Error() != ErrorEmptyPart {
	if err == nil || err != ErrorEmptyPart {
		t.FailNow()
	}
}


@@ 63,7 63,7 @@ func TestNewJidInResourcePart(t *testing.T) {
// Trying to create a JID with an empty resource part should error.
func TestNewEmptyResourcePart(t *testing.T) {
	_, err := NewJID("text@example.com/")
	if err == nil || err.Error() != ErrorEmptyPart {
	if err == nil || err != ErrorEmptyPart {
		t.FailNow()
	}
}


@@ 91,7 91,7 @@ func TestNewSurroundingWhitespace(t *testing.T) {
// New JIDs should not allow `\t`.
func TestNewHasTab(t *testing.T) {
	_, err := NewJID("localpart	@example.com/resourcepart")
	if err == nil || err.Error() != ErrorIllegalSpace {
	if err == nil || err != ErrorIllegalSpace {
		t.FailNow()
	}
}


@@ 99,7 99,7 @@ func TestNewHasTab(t *testing.T) {
// New JIDs should not allow spaces.
func TestNewHasSpace(t *testing.T) {
	_, err := NewJID("localpart@exampl e.com/resourcepart")
	if err == nil || err.Error() != ErrorIllegalSpace {
	if err == nil || err != ErrorIllegalSpace {
		t.FailNow()
	}
}