func TestSession_CheckBeginString(t *testing.T) {
	session := Session{
		sessionID: SessionID{BeginString: "FIX.4.2"},
	}

	builder := getBuilder()

	//wrong value
	builder.Header().Set(fix.NewStringField(tag.BeginString, "FIX.4.4"))
	msgBytes, _ := builder.Build()
	msg, _ := parseMessage(msgBytes)

	err := session.checkBeginString(*msg)
	if err == nil {
		t.Error("Expected Error")
	}
	_ = err.(incorrectBeginString)

	builder.Header().Set(fix.NewStringField(tag.BeginString, session.sessionID.BeginString))
	msgBytes, _ = builder.Build()
	msg, _ = parseMessage(msgBytes)

	err = session.checkBeginString(*msg)

	if err != nil {
		t.Error("Unexpected error", err)
	}
}
func (s *ValidationTests) TestValidateInvalidTagNumber(c *C) {
	dict, _ := datadictionary.Parse("spec/FIX40.xml")

	builder := s.createFIX40NewOrderSingle()
	builder.Header().Set(fix.NewStringField(9999, "hello"))
	msgBytes, _ := builder.Build()
	msg, _ := parseMessage(msgBytes)
	reject := validate(dict, *msg)
	c.Check(reject, NotNil)
	c.Check(reject.RejectReason(), Equals, rejectReasonInvalidTagNumber)
	c.Check(*reject.RefTagID(), Equals, fix.Tag(9999))

	builder = s.createFIX40NewOrderSingle()
	builder.Trailer().Set(fix.NewStringField(9999, "hello"))
	msgBytes, _ = builder.Build()
	msg, _ = parseMessage(msgBytes)

	reject = validate(dict, *msg)
	c.Check(reject, NotNil)
	c.Check(reject.RejectReason(), Equals, rejectReasonInvalidTagNumber)
	c.Check(*reject.RefTagID(), Equals, fix.Tag(9999))

	builder = s.createFIX40NewOrderSingle()
	builder.Body().Set(fix.NewStringField(9999, "hello"))
	msgBytes, _ = builder.Build()
	msg, _ = parseMessage(msgBytes)

	reject = validate(dict, *msg)
	c.Check(reject, NotNil)
	c.Check(reject.RejectReason(), Equals, rejectReasonInvalidTagNumber)
	c.Check(*reject.RefTagID(), Equals, fix.Tag(9999))
}
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) TestValidateValueIsIncorrect(c *C) {
	dict, _ := datadictionary.Parse("spec/FIX40.xml")
	builder := s.createFIX40NewOrderSingle()
	builder.Body().Set(fix.NewStringField(tag.HandlInst, "4"))
	msgBytes, _ := builder.Build()
	msg, _ := parseMessage(msgBytes)

	reject := validate(dict, *msg)
	c.Check(reject, NotNil)
	c.Check(reject.RejectReason(), Equals, rejectReasonValueIsIncorrect)
	c.Check(*reject.RefTagID(), Equals, tag.HandlInst)
}
func TestMessageBuilder_checkBuild(t *testing.T) {
	builder = NewMessageBuilder()

	builder.Header().Set(field.NewBeginString(fix.BeginString_FIX44))
	builder.Header().Set(fix.NewStringField(tag.MsgType, "A"))
	builder.Header().Set(fix.NewStringField(tag.SendingTime, "20140615-19:49:56"))

	builder.Body().Set(field.NewUsername("my_user"))
	builder.Body().Set(field.NewPassword("secret"))

	expectedBytes := []byte("8=FIX.4.49=4935=A52=20140615-19:49:56553=my_user554=secret10=072")
	result, err := builder.Build()
	if err != nil {
		t.Error("Unexpected error", err)
	}

	if !bytes.Equal(expectedBytes, result) {
		t.Error("Unexpected bytes, got ", string(result))
	}

}
func (s *ValidationTests) TestValidateIncorrectDataFormatForValue(c *C) {
	dict, _ := datadictionary.Parse("spec/FIX40.xml")
	builder := s.createFIX40NewOrderSingle()
	builder.Body().Set(fix.NewStringField(tag.OrderQty, "+200.00"))
	msgBytes, _ := builder.Build()
	msg, _ := parseMessage(msgBytes)

	reject := validate(dict, *msg)
	c.Check(reject, NotNil)
	c.Check(reject.RejectReason(), Equals, rejectReasonIncorrectDataFormatForValue)
	c.Check(*reject.RefTagID(), Equals, tag.OrderQty)
}
func (s *ValidationTests) TestValidateInvalidMsgType(c *C) {
	dict, _ := datadictionary.Parse("spec/FIX40.xml")

	builder := s.createFIX40NewOrderSingle()
	builder.Header().Set(fix.NewStringField(tag.MsgType, "z"))
	msgBytes, _ := builder.Build()
	msg, _ := parseMessage(msgBytes)

	reject := validate(dict, *msg)
	c.Check(reject, NotNil)
	c.Check(reject.RejectReason(), Equals, rejectReasonInvalidMsgType)
}
func (s *ValidationTests) TestValidateTagSpecifiedWithoutAValue(c *C) {
	dict, _ := datadictionary.Parse("spec/FIX40.xml")
	builder := s.createFIX40NewOrderSingle()
	builder.Body().Set(fix.NewStringField(tag.ClientID, ""))
	msgBytes, _ := builder.Build()
	msg, _ := parseMessage(msgBytes)

	reject := validate(dict, *msg)
	c.Check(reject, NotNil)
	c.Check(reject.RejectReason(), Equals, rejectReasonTagSpecifiedWithoutAValue)
	c.Check(*reject.RefTagID(), Equals, tag.ClientID)
}
func (s *ValidationTests) TestValidateTagNotDefinedForMessage(c *C) {
	dict, _ := datadictionary.Parse("spec/FIX40.xml")

	builder := s.createFIX40NewOrderSingle()
	builder.Body().Set(fix.NewStringField(41, "hello"))
	msgBytes, _ := builder.Build()
	msg, _ := parseMessage(msgBytes)

	reject := validate(dict, *msg)
	c.Check(reject, NotNil)
	c.Check(reject.RejectReason(), Equals, rejectReasonTagNotDefinedForThisMessageType)
	c.Check(*reject.RefTagID(), Equals, fix.Tag(41))
}
func (s *ValidationTests) TestValidateTagSpecifiedOutOfRequiredOrder(c *C) {
	dict, _ := datadictionary.Parse("spec/FIX40.xml")
	builder := s.createFIX40NewOrderSingle()
	//should be in header
	builder.Body().Set(fix.NewStringField(tag.OnBehalfOfCompID, "CWB"))
	msgBytes, _ := builder.Build()
	msg, _ := parseMessage(msgBytes)

	reject := validate(dict, *msg)
	c.Check(reject, NotNil)
	c.Check(reject.RejectReason(), Equals, rejectReasonTagSpecifiedOutOfRequiredOrder)
	c.Check(*reject.RefTagID(), Equals, tag.OnBehalfOfCompID)
}
Beispiel #11
0
func (s *Session) resend(msg *Message) {
	header := msg.Header.(fieldMap)
	header.Set(field.NewPossDupFlag(true))

	origSendingTime := new(fix.StringValue)
	if err := header.GetField(tag.SendingTime, origSendingTime); err == nil {
		header.Set(fix.NewStringField(tag.OrigSendingTime, origSendingTime.Value))
	}

	s.insertSendingTime(header)

	msg.rebuild()
	s.sendBytes(msg.rawMessage)
}
Beispiel #12
0
func TestMessage_rebuild(t *testing.T) {
	rawMsg := []byte("8=FIX.4.29=10435=D34=249=TW52=20140515-19:49:56.65956=ISLD11=10021=140=154=155=TSLA60=00010101-00:00:00.00010=039")

	msg, _ := parseMessage(rawMsg)
	header := msg.Header.(fieldMap)

	header.Set(fix.NewStringField(tag.OrigSendingTime, "20140515-19:49:56.659"))
	header.Set(fix.NewStringField(tag.SendingTime, "20140615-19:49:56"))

	msg.rebuild()

	expectedBytes := []byte("8=FIX.4.29=12635=D34=249=TW52=20140615-19:49:5656=ISLD122=20140515-19:49:56.65911=10021=140=154=155=TSLA60=00010101-00:00:00.00010=128")

	if !bytes.Equal(expectedBytes, msg.rawMessage) {
		t.Error("Unexpected bytes, got ", string(msg.rawMessage))
	}

	expectedBodyBytes := []byte("11=10021=140=154=155=TSLA60=00010101-00:00:00.000")

	if !bytes.Equal(msg.bodyBytes, expectedBodyBytes) {
		t.Error("Incorrect body bytes, got ", string(msg.bodyBytes))
	}
}
func TestFieldMap_SetAndGet(t *testing.T) {
	fMap := fieldMap{}
	fMap.init(normalFieldOrder)

	fMap.Set(fix.NewStringField(1, "hello"))
	fMap.Set(fix.NewStringField(2, "world"))

	var testCases = []struct {
		tag         fix.Tag
		expectErr   bool
		expectValue string
	}{
		{tag: 1, expectValue: "hello"},
		{tag: 2, expectValue: "world"},
		{tag: 44, expectErr: true},
	}

	for _, tc := range testCases {
		testField := fix.NewStringField(tc.tag, "")
		err := fMap.Get(testField)

		if tc.expectErr {
			if err == nil {
				t.Error("Expected Error")
			}
			continue
		}

		if err != nil {
			t.Error("Unexpected Error", err)
		}

		if testField.Value != tc.expectValue {
			t.Errorf("Expected %v got %v", tc.expectValue, testField.Value)
		}
	}
}
Beispiel #14
0
func (state *inSession) generateSequenceReset(session *Session, beginSeqNo int, endSeqNo int) {
	sequenceReset := NewMessageBuilder()
	session.fillDefaultHeader(sequenceReset)

	sequenceReset.Header().Set(field.NewMsgType("4"))
	sequenceReset.Header().Set(field.NewMsgSeqNum(beginSeqNo))
	sequenceReset.Header().Set(field.NewPossDupFlag(true))
	sequenceReset.Body().Set(field.NewNewSeqNo(endSeqNo))
	sequenceReset.Body().Set(field.NewGapFillFlag(true))

	origSendingTime := new(fix.StringValue)
	if err := sequenceReset.Header().GetField(tag.SendingTime, origSendingTime); err == nil {
		sequenceReset.Header().Set(fix.NewStringField(tag.OrigSendingTime, origSendingTime.Value))
	}

	//FIXME error check?
	msgBytes, _ := sequenceReset.Build()
	session.sendBytes(msgBytes)
}
Beispiel #15
0
func getBuilder() MessageBuilder {
	builder := NewMessageBuilder()
	builder.Header().Set(fix.NewStringField(tag.BeginString, fix.BeginString_FIX40))
	builder.Header().Set(fix.NewStringField(tag.MsgType, "D"))
	return builder
}
func (s *ValidationTests) createFIX43NewOrderSingle() MessageBuilder {
	msg := NewMessageBuilder()
	msg.Header().Set(fix.NewStringField(tag.MsgType, "D"))
	msg.Header().Set(fix.NewStringField(tag.BeginString, "FIX.4.3"))
	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.NewIntField(tag.OrderQty, 5))
	msg.Body().Set(fix.NewStringField(tag.OrdType, "1"))
	msg.Body().Set(fix.NewUTCTimestampField(tag.TransactTime, time.Now()))

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

	return msg
}
Beispiel #17
0
func newCheckSum(value int) *fix.StringField {
	return fix.NewStringField(tag.CheckSum, fmt.Sprintf("%03d", value))
}
func (s *ValidationTests) TestValidateFieldNotFound(c *C) {
	dict, _ := datadictionary.Parse("spec/FIX40.xml")

	builder := NewMessageBuilder()
	builder.Header().Set(fix.NewStringField(tag.MsgType, "D"))
	builder.Header().Set(fix.NewStringField(tag.BeginString, "FIX.4.0"))
	builder.Header().Set(fix.NewStringField(tag.BodyLength, "0"))
	builder.Header().Set(fix.NewStringField(tag.SenderCompID, "0"))
	builder.Header().Set(fix.NewStringField(tag.TargetCompID, "0"))
	builder.Header().Set(fix.NewStringField(tag.MsgSeqNum, "0"))
	builder.Header().Set(fix.NewUTCTimestampField(tag.SendingTime, time.Now()))
	builder.Trailer().Set(fix.NewStringField(tag.CheckSum, "000"))

	builder.Body().Set(fix.NewStringField(tag.ClOrdID, "A"))
	builder.Body().Set(fix.NewStringField(tag.HandlInst, "A"))
	builder.Body().Set(fix.NewStringField(tag.Symbol, "A"))
	builder.Body().Set(fix.NewStringField(tag.Side, "A"))
	builder.Body().Set(fix.NewStringField(tag.OrderQty, "A"))

	//ord type is required
	//builder.Body().Set(fix.NewStringField(tag.OrdType, "A"))
	msgBytes, _ := builder.Build()
	msg, _ := parseMessage(msgBytes)

	reject := validate(dict, *msg)
	c.Check(reject, NotNil)
	c.Check(reject.RejectReason(), Equals, rejectReasonRequiredTagMissing)
	c.Check(*reject.RefTagID(), Equals, tag.OrdType)

	builder = NewMessageBuilder()
	builder.Trailer().Set(fix.NewStringField(tag.CheckSum, "000"))
	builder.Body().Set(fix.NewStringField(tag.ClOrdID, "A"))
	builder.Body().Set(fix.NewStringField(tag.HandlInst, "A"))
	builder.Body().Set(fix.NewStringField(tag.Symbol, "A"))
	builder.Body().Set(fix.NewStringField(tag.Side, "A"))
	builder.Body().Set(fix.NewStringField(tag.OrderQty, "A"))

	builder.Header().Set(fix.NewStringField(tag.MsgType, "D"))
	builder.Header().Set(fix.NewStringField(tag.BeginString, "FIX.4.0"))
	builder.Header().Set(fix.NewStringField(tag.BodyLength, "0"))
	builder.Header().Set(fix.NewStringField(tag.SenderCompID, "0"))
	builder.Header().Set(fix.NewStringField(tag.TargetCompID, "0"))
	builder.Header().Set(fix.NewStringField(tag.MsgSeqNum, "0"))
	//sending time is required
	//msg.Header.FieldMap.Set(fix.NewStringField(tag.SendingTime, "0"))

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

	reject = validate(dict, *msg)
	c.Check(reject, NotNil)
	c.Check(reject.RejectReason(), Equals, rejectReasonRequiredTagMissing)
	c.Check(*reject.RefTagID(), Equals, tag.SendingTime)
}