Ejemplo n.º 1
0
func (s *Startup) FillMessage(m *femebe.Message) {
	buf := bytes.NewBuffer(make([]byte, 0, 1024))

	// Startup-message type word
	buf.Write([]byte{0x00, 0x03, 0x00, 0x00})

	for name, value := range s.Params {
		femebe.WriteCString(buf, name)
		femebe.WriteCString(buf, value)
	}

	buf.Write([]byte{'\000'})

	m.InitFromBytes(femebe.MSG_TYPE_FIRST, buf.Bytes())
}
func TestVersionBadValue(t *testing.T) {
	msgInit := func(dst *femebe.Message, exit exitFn) {
		buf := bytes.Buffer{}
		femebe.WriteCString(&buf, "PG7.4.15/1")

		dst.InitFromBytes('V', buf.Bytes())
	}

	sentinel := &msgInit

	exit := func(args ...interface{}) {
		// Because this kind of error simply reports a string (with no
		// type taxonomy to go with it), and that seems good enough,
		// elide confirming precisely what the error is.
		panic(sentinel)
	}

	defer func() {
		if r := recover(); r != nil && r != sentinel {
			t.Fatal("Paniced, but not with the sentinel value")
		}

		// Success
	}()

	processVerMsg(msgInit, exit)
	t.Fatal("Message should call exit, aborting execution before this")
}
func TestVersionOK(t *testing.T) {
	msgInit := func(dst *femebe.Message, exit exitFn) {
		buf := bytes.Buffer{}
		femebe.WriteCString(&buf, "PG-9.2.2/logfebe-1")

		dst.InitFromBytes('V', buf.Bytes())
	}

	exit := func(args ...interface{}) {
		t.Fatal("Ver Message is thought to be well formed. " +
			"Invariant broken.")
	}

	processVerMsg(msgInit, exit)
}