func TestSession_CheckTargetTooLow(t *testing.T) {
	store, _ := NewMemoryStoreFactory().Create(SessionID{})
	session := Session{store: store}

	builder := getBuilder()
	msgBytes, _ := builder.Build()
	msg, _ := parseMessage(msgBytes)

	store.SetNextTargetMsgSeqNum(45)

	//missing seq number
	err := session.checkTargetTooLow(*msg)
	if err == nil {
		t.Error("Expected error")
	}

	if err.RejectReason() != rejectReasonRequiredTagMissing {
		t.Error("Unexpected reject reason", err.RejectReason())
	}

	//too low
	builder.Header().Set(fix.NewIntField(tag.MsgSeqNum, 43))
	msgBytes, _ = builder.Build()
	msg, _ = parseMessage(msgBytes)

	err = session.checkTargetTooLow(*msg)
	if err == nil {
		t.Error("Expected error")
	}
	_ = err.(targetTooLow)

	//spot on
	builder.Header().Set(fix.NewIntField(tag.MsgSeqNum, 45))
	msgBytes, _ = builder.Build()
	msg, _ = parseMessage(msgBytes)

	err = session.checkTargetTooLow(*msg)
	if err != nil {
		t.Error("Unexpected error ", err)
	}
}
Exemple #2
0
func (s *Session) send(builder MessageBuilder) {
	s.fillDefaultHeader(builder)

	seqNum := s.store.NextSenderMsgSeqNum()
	builder.Header().Set(fix.NewIntField(tag.MsgSeqNum, seqNum))

	msgType := new(fix.StringValue)
	if builder.Header().GetField(tag.MsgType, msgType); fix.IsAdminMessageType(msgType.Value) {
		s.application.ToAdmin(builder, s.sessionID)
	} else {
		s.application.ToApp(builder, s.sessionID)
	}
	if msgBytes, err := builder.Build(); err != nil {
		panic(err)
	} else {
		s.store.SaveMessage(seqNum, msgBytes)
		s.sendBytes(msgBytes)
		s.store.IncrNextSenderMsgSeqNum()
	}
}
func TestFieldMap_Total(t *testing.T) {
	f1 := fix.NewStringField(1, "hello")
	f2 := fix.NewStringField(2, "world")

	beginString := fix.NewStringField(8, "FIX.4.4")
	bodyLength := fix.NewIntField(9, 100)
	checkSum := fix.NewStringField(10, "100")

	fMap := fieldMap{}
	fMap.init(normalFieldOrder)
	fMap.Set(f1)
	fMap.Set(f2)
	fMap.Set(beginString)
	fMap.Set(bodyLength)
	fMap.Set(checkSum)

	if fMap.total() != 2116 {
		t.Error("Total should includes all fields but checkSum- got ", fMap.total())
	}
}
func (s *ValidationTests) createFIX40NewOrderSingle() MessageBuilder {
	msg := NewMessageBuilder()
	msg.Header().Set(fix.NewStringField(tag.MsgType, "D"))
	msg.Header().Set(fix.NewStringField(tag.BeginString, "FIX.4.0"))
	msg.Header().Set(fix.NewStringField(tag.BodyLength, "0"))
	msg.Header().Set(fix.NewStringField(tag.SenderCompID, "0"))
	msg.Header().Set(fix.NewStringField(tag.TargetCompID, "0"))
	msg.Header().Set(fix.NewStringField(tag.MsgSeqNum, "0"))
	msg.Header().Set(fix.NewUTCTimestampField(tag.SendingTime, time.Now()))

	msg.Body().Set(fix.NewStringField(tag.ClOrdID, "A"))
	msg.Body().Set(fix.NewStringField(tag.HandlInst, "1"))
	msg.Body().Set(fix.NewStringField(tag.Symbol, "A"))
	msg.Body().Set(fix.NewStringField(tag.Side, "1"))
	msg.Body().Set(fix.NewStringField(tag.OrdType, "1"))
	msg.Body().Set(fix.NewIntField(tag.OrderQty, 5))

	msg.Trailer().Set(fix.NewStringField(tag.CheckSum, "000"))

	return msg
}
Exemple #5
0
func (m *Message) rebuild() {
	header := m.Header.(fieldMap)
	trailer := m.Trailer.(fieldMap)

	bodyLength := header.length() + len(m.bodyBytes) + trailer.length()
	header.Set(fix.NewIntField(tag.BodyLength, bodyLength))
	checkSum := header.total() + trailer.total()
	for _, b := range m.bodyBytes {
		checkSum += int(b)
	}
	checkSum %= 256

	trailer.Set(newCheckSum(checkSum))

	var b bytes.Buffer
	header.write(&b)
	b.Write(m.bodyBytes)
	trailer.write(&b)

	m.rawMessage = b.Bytes()
}
func (m messageBuilder) cook() {
	bodyLength := m.header.length() + m.body.length() + m.trailer.length()
	m.header.Set(fix.NewIntField(tag.BodyLength, bodyLength))
	checkSum := (m.header.total() + m.body.total() + m.trailer.total()) % 256
	m.trailer.Set(newCheckSum(checkSum))
}