Beispiel #1
0
func (sh SessionHandler) HandleFeature(props stream.Properties) stream.Properties {
	if props.Status&stream.Bind != 0 || props.Status&stream.Auth == 0 {
		return props
	}

	props.Features = append(props.Features, element.Session)
	return props
}
Beispiel #2
0
func (h Handler) GenerateFeature(props stream.Properties) stream.Properties {
	if props.Status&stream.Bind != 0 || props.Status&stream.Auth == 0 {
		return props
	}

	props.Features = append(props.Features, element.Bind)
	return props
}
Beispiel #3
0
func (h *Handler) GenerateFeature(props stream.Properties) stream.Properties {
	if props.Status&stream.Auth != 0 {
		return props
	}
	mechs := element.SASLMechanisms
	for name := range h.mechs {
		mechs = mechs.AddChild(element.New("mechanism").SetText(name))
	}
	props.Features = append(props.Features, mechs)
	return props
}
Beispiel #4
0
func TestStartReceiving(t *testing.T) {
	t.Parallel()

	var want, got []byte
	var props, gotProps stream.Properties
	var err, wantErr error

	pipe1, pipe2 := net.Pipe()
	tcpTsp := NewTCP(pipe1, stream.Receiving, &tls.Config{}, true)
	props.Header = stream.Header{}

	// Should return Domain Not Set error if the domain isnot set on the
	// stream properties.
	_, err = tcpTsp.Start(props)
	if err != stream.ErrDomainNotSet {
		t.Error("Should return ErrDomainNotSet error if the domain is no set on the properties.")
		t.Errorf("\nWant:%s\nGot :%s", stream.ErrDomainNotSet, err)
	}

	// Should return error from Next
	props.Domain = "localhost"
	err = pipe2.Close()
	if err != nil {
		t.Errorf("Unexpected error from pipe2.Close: %s", err)
	}
	_, err = tcpTsp.Start(props)
	if err != io.EOF {
		t.Error("Should return error from Next")
		t.Errorf("\nWant:%s\nGot :%s", io.EOF, err)
	}

	// Should return error from NewHeader
	pipe1, pipe2 = net.Pipe()
	tcpTsp = NewTCP(pipe1, stream.Receiving, &tls.Config{}, true)
	go func() {
		_, err := pipe2.Write([]byte("<baz xmlns='foo:bar'/>"))
		if err != nil {
			t.Errorf("Unexpected error while writing to pipe2: %s", err)
		}
	}()
	_, err = tcpTsp.Start(props)
	wantErr = fmt.Errorf("Element is not <stream:stream> it is a <foo:bar:baz>")
	if err.Error() != wantErr.Error() {
		t.Error("Should return error from NewHeader")
		t.Errorf("\nWant:%s\nGot :%s", wantErr, err)
	}

	// Should send HostUnknown if the to field of the header does not match the
	// Domain field on properties
	pipe1, pipe2 = net.Pipe()
	tcpTsp = NewTCP(pipe1, stream.Receiving, &tls.Config{}, true)
	go func() {
		hdr := stream.Header{To: "not-localhost", From: "foo@bar"}
		_, err := pipe2.Write(hdr.WriteBytes())
		if err != nil {
			t.Errorf("Unexpected error while writing to pipe2: %s", err)
		}
		hdr.To, hdr.From = hdr.From, "localhost"
		want = hdr.WriteBytes()
		// We need to add an extra 36 bytes for the id length
		hdrLen := len(want) + 36
		want = append(want, element.StreamError.HostUnknown.WriteBytes()...)
		// We need to add an extra 36 bytes for the id length
		got = make([]byte, len(want)+36)
		_, err = pipe2.Read(got)
		if err != nil {
			t.Errorf("Unexpected error while reading from pipe2: %s", err)
		}
		_, err = pipe2.Read(got[hdrLen:])
		if err != nil {
			t.Errorf("Unexpected error while reading from pipe2: %s", err)
		}
	}()
	gotProps, err = tcpTsp.Start(props)
	if err != nil {
		t.Errorf("Unexpected error from Start: %s", err)
	}
	if gotProps.Status != stream.Closed {
		t.Error("Expected stream to be marked as closed after host unknown error")
	}
	// Need to remove stream ID before comparing
	idx := bytes.Index(got, []byte("id='"))
	if idx == -1 {
		t.Error("Received stream is missing id attribute")
	}
	// We slice the id out of the received stream header.
	got = append(got[:idx+4], got[idx+40:]...)
	if !bytes.Equal(want, got) {
		t.Error("Should send HostUnknown if the to field of the header does not match the domain field on properties.")
		t.Errorf("\nWant:%s\nGot :%s", want, got)
	}

	// Should return error from writing header to the underlying connection
	pipe1, pipe2 = net.Pipe()
	tcpTsp = NewTCP(pipe1, stream.Receiving, &tls.Config{}, true)
	go func() {
		hdr := stream.Header{To: "localhost", From: "foo@bar"}
		_, err := pipe2.Write(hdr.WriteBytes())
		if err != nil {
			t.Errorf("Unexpected error while writing to pipe2: %s", err)
		}
		err = pipe2.Close()
		if err != nil {
			t.Errorf("Unexpected error while closing pipe2: %s", err)
		}
	}()
	gotProps, err = tcpTsp.Start(props)
	if err != io.ErrClosedPipe {
		t.Error("Should return error from writing header to the underlying connection.")
		t.Errorf("\nWant:%s\nGot :%s", io.ErrClosedPipe, err)
	}

	// Should set the To field to the properties To field if it is set
	props.To = "authenticatedFoo@bar"

	// Should overwrite stream features if there is a tls config and the stream
	// is not yet secure
	pipe1, pipe2 = net.Pipe()
	tcpTsp = NewTCP(pipe1, stream.Receiving, &tls.Config{}, true)
	go func() {
		hdr := stream.Header{To: "localhost", From: "foo@bar"}
		_, err := pipe2.Write(hdr.WriteBytes())
		if err != nil {
			t.Errorf("Unexpected error while writing to pipe2: %s", err)
		}
		// Doing two tests at the same time, because they are orthogonal
		hdr.To, hdr.From = "authenticatedFoo@bar", "localhost"
		want = hdr.WriteBytes()
		// We need to add an extra 36 bytes for the id length
		hdrLen := len(want) + 36
		ftrs := element.StreamFeatures.AddChild(
			element.StartTLS.AddChild(
				element.Required),
		)
		want = append(want, ftrs.WriteBytes()...)
		// We need to add an extra 36 bytes for the id length
		got = make([]byte, len(want)+36)
		_, err = pipe2.Read(got)
		if err != nil {
			t.Errorf("Unexpected error while reading from pipe2: %s", err)
		}
		_, err = pipe2.Read(got[hdrLen:])
		if err != nil {
			t.Errorf("Unexpected error while reading from pipe2: %s", err)
		}
	}()
	gotProps, err = tcpTsp.Start(props)
	if err != nil {
		t.Errorf("Unexpected error from Start: %s", err)
	}
	// Need to remove stream ID before comparing
	idx = bytes.Index(got, []byte("id='"))
	if idx == -1 {
		t.Error("Received stream is missing id attribute")
	}
	// We slice the id out of the received stream header.
	got = append(got[:idx+4], got[idx+40:]...)
	if !bytes.Equal(want, got) {
		t.Error("Should overwrite stream features if there is a tls config and the stream is not yet secure.")
		t.Errorf("\nWant:%s\nGot :%s", want, got)
	}

	// Should be able to start stream
	props.Features = append(props.Features, element.Bind)
	pipe1, pipe2 = net.Pipe()
	tcpTsp = NewTCP(pipe1, stream.Receiving, &tls.Config{}, true)
	tcpTsp.(*TCP).secure = true
	go func() {
		hdr := stream.Header{To: "localhost", From: "foo@bar"}
		_, err := pipe2.Write(hdr.WriteBytes())
		if err != nil {
			t.Errorf("Unexpected error while writing to pipe2: %s", err)
		}
		// Doing two tests at the same time, because they are orthogonal
		hdr.To, hdr.From = "authenticatedFoo@bar", "localhost"
		want = hdr.WriteBytes()
		// We need to add an extra 36 bytes for the id length
		hdrLen := len(want) + 36
		ftrs := element.StreamFeatures.AddChild(element.Bind)
		want = append(want, ftrs.WriteBytes()...)
		// We need to add an extra 36 bytes for the id length
		got = make([]byte, len(want)+36)
		_, err = pipe2.Read(got)
		if err != nil {
			t.Errorf("Unexpected error while reading from pipe2: %s", err)
		}
		_, err = pipe2.Read(got[hdrLen:])
		if err != nil {
			t.Errorf("Unexpected error while reading from pipe2: %s", err)
		}
	}()
	gotProps, err = tcpTsp.Start(props)
	if err != nil {
		t.Errorf("Unexpected error from Start: %s", err)
	}
	// Need to remove stream ID before comparing
	idx = bytes.Index(got, []byte("id='"))
	if idx == -1 {
		t.Error("Received stream is missing id attribute")
	}
	// We slice the id out of the received stream header.
	got = append(got[:idx+4], got[idx+40:]...)
	if !bytes.Equal(want, got) {
		t.Error("Should be able to start stream")
		t.Errorf("\nWant:%s\nGot :%s", want, got)
	}
}