otrv3_test.go 4.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
package otr3

import "testing"

func Test_verifyInstanceTags_ignoresOurInstaceTagIfItIsZero(t *testing.T) {
	v := otrV3{}
	c := &Conversation{version: v}

	err := v.verifyInstanceTags(c, 0x100, 0)

	assertNil(t, err)
}

func Test_verifyInstanceTags_returnsErrorWhenOurInstanceTagIsLesserThan0x100(t *testing.T) {
	v := otrV3{}
	c := &Conversation{version: v}

	err := v.verifyInstanceTags(c, 0x100, 0x99)

	assertEquals(t, err, errInvalidOTRMessage)
}

func Test_verifyInstanceTags_signalsMalformedMessageWhenOurInstanceTagIsLesserThan0x100(t *testing.T) {
	v := otrV3{}
	c := &Conversation{version: v}

	c.expectMessageEvent(t, func() {
		v.verifyInstanceTags(c, 0x100, 0x99)
	}, MessageEventReceivedMessageMalformed, nil, nil)
}

func Test_verifyInstanceTags_returnsErrorWhenOurInstanceDoesNotMatch(t *testing.T) {
	v := otrV3{}
	c := &Conversation{version: v}
	c.theirInstanceTag = 0x100
	c.ourInstanceTag = 0x122

	err := v.verifyInstanceTags(c, c.theirInstanceTag, 0x121)

	assertEquals(t, err, errReceivedMessageForOtherInstance)
}

func Test_verifyInstanceTags_signalsAMessageEventWhenOurInstanceTagDoesNotMatch(t *testing.T) {
	v := otrV3{}
	c := &Conversation{version: v}
	c.theirInstanceTag = 0x100
	c.ourInstanceTag = 0x122

	c.expectMessageEvent(t, func() {
		v.verifyInstanceTags(c, c.theirInstanceTag, 0x121)
	}, MessageEventReceivedMessageForOtherInstance, nil, nil)
}

func Test_verifyInstanceTags_savesTheirInstanceTag(t *testing.T) {
	v := otrV3{}
	c := &Conversation{}

	err := v.verifyInstanceTags(c, 0x101, 0)
	assertNil(t, err)
	assertEquals(t, c.theirInstanceTag, uint32(0x101))
}

func Test_verifyInstanceTags_returnsErrorWhenTheirInstanceTagIsLesserThan0x100(t *testing.T) {
	v := otrV3{}
	c := &Conversation{version: v}

	err := v.verifyInstanceTags(c, 0x99, 0x100)

	assertEquals(t, err, errInvalidOTRMessage)
}

func Test_verifyInstanceTags_returnsErrorWhenTheirInstanceTagIsZero(t *testing.T) {
	v := otrV3{}
	c := &Conversation{version: v}

	err := v.verifyInstanceTags(c, 0, 0x100)

	assertEquals(t, err, errInvalidOTRMessage)
}

func Test_verifyInstanceTags_signalsMalformedMessageWhenTheirInstanceTagIsTooLow(t *testing.T) {
	v := otrV3{}
	c := &Conversation{version: v}

	c.expectMessageEvent(t, func() {
		v.verifyInstanceTags(c, 0, 0x100)
	}, MessageEventReceivedMessageMalformed, nil, nil)
}

func Test_verifyInstanceTags_returnsErrorWhenTheirInstanceTagDoesNotMatch(t *testing.T) {
	v := otrV3{}
	c := &Conversation{version: v}
	c.theirInstanceTag = 0x122

	err := v.verifyInstanceTags(c, 0x121, c.ourInstanceTag)
	assertEquals(t, err, errReceivedMessageForOtherInstance)
}

func Test_verifyInstanceTags_signalsAMessageEventWhenTheirInstanceTagDoesNotMatch(t *testing.T) {
	v := otrV3{}
	c := &Conversation{version: v}
	c.theirInstanceTag = 0x122

	c.expectMessageEvent(t, func() {
		v.verifyInstanceTags(c, 0x121, c.ourInstanceTag)
	}, MessageEventReceivedMessageForOtherInstance, nil, nil)
}

func Test_otrv3_parseMessageHeader_signalsMalformedMessageWhenWeCantParseInstanceTags(t *testing.T) {
	v := otrV3{}
	c := &Conversation{version: v}

	c.expectMessageEvent(t, func() {
		v.parseMessageHeader(c, []byte{0x00, 0x03, 0x02, 0x00, 0x00, 0x01, 0x22, 0x00, 0x00, 0x01})
	}, MessageEventReceivedMessageMalformed, nil, nil)
}

func Test_generateInstanceTag_generatesOurInstanceTag(t *testing.T) {
	rand := fixedRand([]string{"00000099", "00001234"})
	c := &Conversation{Rand: rand}

	err := c.generateInstanceTag()

	assertEquals(t, err, nil)
	assertEquals(t, c.ourInstanceTag, uint32(0x1234))
}

func Test_generateInstanceTag_returnsAnErrorIfFailsToReadFromRand(t *testing.T) {
	rand := fixedRand([]string{"00000099", "00000080"})
	c := &Conversation{Rand: rand}

	err := c.generateInstanceTag()

	assertEquals(t, err, errShortRandomRead)
	assertEquals(t, c.ourInstanceTag, uint32(0))
}

func Test_messageHeader_generatesOurInstanceTagLazily(t *testing.T) {
	c := &Conversation{}

	_, err := otrV3{}.messageHeader(c, msgTypeDHCommit)

	assertEquals(t, err, nil)
	assertEquals(t, c.ourInstanceTag < minValidInstanceTag, false)

	previousInstanceTag := c.ourInstanceTag

	_, err = otrV3{}.messageHeader(c, msgTypeDHCommit)
	assertEquals(t, err, nil)
	assertEquals(t, c.ourInstanceTag, previousInstanceTag)
}