diff --git a/network/message/parse/firstMessagePart_test.go b/network/message/parse/firstMessagePart_test.go index e1e7e692621e7251703695ac7c37861dfc18c830..676723247fec562607d43f9a29dec827890c76a8 100644 --- a/network/message/parse/firstMessagePart_test.go +++ b/network/message/parse/firstMessagePart_test.go @@ -1,34 +1,100 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + package parse import ( "bytes" "gitlab.com/elixxir/client/interfaces/message" + "reflect" "testing" "time" ) -func TestnewFirstMessagePart(t *testing.T) { - now := time.Now() - fmp := newFirstMessagePart(message.Text, uint32(6), uint8(2), now, - []byte{'t', 'e', 's', 't', 'i', 'n', 'g'}) +// Expected firstMessagePart for checking against, generated by fmp in TestNewFirstMessagePart +var efmp = firstMessagePart{ + messagePart: messagePart{ + Data: []byte{0, 0, 4, 53, 0, 0, 13, 2, 0, 0, 0, 2, 1, 0, 0, 0, 14, 215, 133, 90, 117, 0, 0, 0, 0, 255, 255, + 116, 101, 115, 116, 105, 110, 103, 115, 116, 114, 105, 110, 103}, + Id: []byte{0, 0, 4, 53}, + Part: []byte{0}, + Len: []byte{0, 13}, + Contents: []byte{116, 101, 115, 116, 105, 110, 103, 115, 116, 114, 105, 110, 103}, + }, + NumParts: []byte{2}, + Type: []byte{0, 0, 0, 2}, + Timestamp: []byte{1, 0, 0, 0, 14, 215, 133, 90, 117, 0, 0, 0, 0, 255, 255}, +} - if fmp.GetType() != message.Text { +// Test that newFirstMessagePart returns a correctly made firstMessagePart +func TestNewFirstMessagePart(t *testing.T) { + fmp := newFirstMessagePart( + message.Text, + 1077, + 2, + time.Unix(1609786229, 0).UTC(), + []byte{'t', 'e', 's', 't', 'i', 'n', 'g', + 's', 't', 'r', 'i', 'n', 'g'}, + ) + gotTime, err := fmp.GetTimestamp() + if err != nil { + t.Error(err) + } + expectedTime, err := fmp.GetTimestamp() + if err != nil { + t.Error(err) } + if !gotTime.Equal(expectedTime) { + t.Errorf("Got time: %v, expected time: %v", gotTime, expectedTime) + } + + if !reflect.DeepEqual(fmp, efmp) { + t.Errorf("Expected and got firstMessagePart did not match.\n\tGot: %#v\n\tExpected: %#v", fmp, efmp) + } +} - if fmp.GetNumParts() != uint8(2) { +// Test that FirstMessagePartFromBytes returns a correctly made firstMessagePart from the bytes of one +func TestFirstMessagePartFromBytes(t *testing.T) { + fmp := FirstMessagePartFromBytes(efmp.Data) + if !reflect.DeepEqual(fmp, efmp) { + t.Error("Expected and got firstMessagePart did not match") } +} - recorded_now, err := fmp.GetTimestamp() - if err != nil { - t.Fatal(err) +// Test that GetType returns the correct type for a firstMessagePart +func TestFirstMessagePart_GetType(t *testing.T) { + if efmp.GetType() != message.Text { + t.Errorf("Got %v, expected %v", efmp.GetType(), message.Text) } - if recorded_now != now { +} +// Test that GetNumParts returns the correct number of parts for a firstMessagePart +func TestFirstMessagePart_GetNumParts(t *testing.T) { + if efmp.GetNumParts() != 2 { + t.Errorf("Got %v, expected %v", efmp.GetNumParts(), 2) } +} - if !bytes.Equal(fmp.Bytes(), []byte{'t', 'e', 's', 't', 'i', 'n', 'g'}) { +// Test that GetTimestamp returns the correct timestamp for a firstMessagePart +func TestFirstMessagePart_GetTimestamp(t *testing.T) { + et, err := efmp.GetTimestamp() + if err != nil { + t.Error(err) + } + if !time.Unix(1609786229, 0).Equal(et) { + t.Errorf("Got %v, expected %v", et, time.Unix(1609786229, 0)) + } +} +// Test that GetTimestamp returns the correct bytes for a firstMessagePart +func TestFirstMessagePart_Bytes(t *testing.T) { + if bytes.Compare(efmp.Bytes(), efmp.Data) != 0 { + t.Errorf("Got %v, expected %v", efmp.Bytes(), efmp.Data) } } diff --git a/network/message/parse/messagePart_test.go b/network/message/parse/messagePart_test.go new file mode 100644 index 0000000000000000000000000000000000000000..5ab1db0213c05a18f7615a836e627350a5879616 --- /dev/null +++ b/network/message/parse/messagePart_test.go @@ -0,0 +1,70 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package parse + +import ( + "bytes" + "reflect" + "testing" +) + +// Expected messagePart for checking against, generated by gotmp in Test_newMessagePart +var emp = messagePart{ + Data: []uint8{0x0, 0x0, 0x0, 0x20, 0x6, 0x0, 0x7, 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67}, + Id: []uint8{0x0, 0x0, 0x0, 0x20}, Part: []uint8{0x6}, + Len: []uint8{0x0, 0x7}, + Contents: []uint8{0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67}, +} + +// This tests that a new function part is successfully created +func Test_newMessagePart(t *testing.T) { + gotmp := newMessagePart(32, 6, []byte{'t', 'e', 's', 't', 'i', 'n', 'g'}) + if !reflect.DeepEqual(gotmp, emp) { + t.Errorf("MessagePart received and MessagePart expected do not match.\n\tGot: %#v\n\tExpected: %#v", gotmp, emp) + } +} + +// Test that GetID returns the correct ID +func TestMessagePart_GetID(t *testing.T) { + if emp.GetID() != 32 { + t.Errorf("received and expected do not match."+ + "\n\tGot: %#v\n\tExpected: %#v", emp.GetID(), 32) + } +} + +// Test that GetPart returns the correct part number +func TestMessagePart_GetPart(t *testing.T) { + if emp.GetPart() != 6 { + t.Errorf("received and expected do not match."+ + "\n\tGot: %#v\n\tExpected: %#v", emp.GetPart(), 6) + } +} + +// Test that GetContents returns the message contests +func TestMessagePart_GetContents(t *testing.T) { + if bytes.Compare(emp.GetContents(), []byte{'t', 'e', 's', 't', 'i', 'n', 'g'}) != 0 { + t.Errorf("received and expected do not match."+ + "\n\tGot: %#v\n\tExpected: %#v", emp.GetContents(), 6) + } +} + +// Test that GetSizedContents returns the message contests +func TestMessagePart_GetSizedContents(t *testing.T) { + if bytes.Compare(emp.GetSizedContents(), []byte{'t', 'e', 's', 't', 'i', 'n', 'g'}) != 0 { + t.Errorf("received and expected do not match."+ + "\n\tGot: %#v\n\tExpected: %#v", emp.GetSizedContents(), 6) + } +} + +// Test that GetContentsLength returns the message length +func TestMessagePart_GetContentsLength(t *testing.T) { + if emp.GetContentsLength() != 7 { + t.Errorf("received and expected do not match."+ + "\n\tGot: %#v\n\tExpected: %#v", emp.GetContentsLength(), 7) + } +} diff --git a/network/message/parse/partition_test.go b/network/message/parse/partition_test.go new file mode 100644 index 0000000000000000000000000000000000000000..59963cfefc57ab70d8d76e07fd384d8aae293a40 --- /dev/null +++ b/network/message/parse/partition_test.go @@ -0,0 +1,106 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package parse + +import ( + "gitlab.com/elixxir/client/interfaces/message" + "gitlab.com/elixxir/client/storage" + "gitlab.com/xx_network/primitives/id" + "testing" + "time" +) + +var ipsumTestStr = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sit amet euismod est. Donec dolor " + + "neque, efficitur et interdum eu, lacinia quis mi. Duis bibendum elit ac lacus finibus pharetra. Suspendisse " + + "blandit erat in odio faucibus consectetur. Suspendisse sed consequat purus. Curabitur fringilla mi sit amet odio " + + "interdum suscipit. Etiam vitae dui posuere, congue mi a, convallis odio. In commodo risus at lorem volutpat " + + "placerat. In cursus magna purus, suscipit dictum lorem aliquam non. Praesent efficitur." + +// Test that NewPartitioner outputs a correctly made Partitioner +func TestNewPartitioner(t *testing.T) { + storeSession := storage.InitTestingSession(t) + p := NewPartitioner(4096, storeSession) + + if p.baseMessageSize != 4096 { + t.Errorf("baseMessageSize content mismatch"+ + "\n\texpected: %v\n\treceived: %v", + 4096, p.baseMessageSize) + } + + if p.deltaFirstPart != 20 { + t.Errorf("deltaFirstPart content mismatch"+ + "\n\texpected: %v\n\treceived: %v", + 20, p.deltaFirstPart) + } + + if p.firstContentsSize != 4069 { + t.Errorf("firstContentsSize content mismatch"+ + "\n\texpected: %v\n\treceived: %v", + 4069, p.firstContentsSize) + } + + if p.maxSize != 1042675 { + t.Errorf("maxSize content mismatch"+ + "\n\texpected: %v\n\treceived: %v", + 1042675, p.maxSize) + } + + if p.partContentsSize != 4089 { + t.Errorf("partContentsSize content mismatch"+ + "\n\texpected: %v\n\treceived: %v", + 4089, p.partContentsSize) + } + + if p.session != storeSession { + t.Errorf("session content mismatch") + } +} + +// Test that no error is returned running Partition +func TestPartitioner_Partition(t *testing.T) { + storeSession := storage.InitTestingSession(t) + p := NewPartitioner(len(ipsumTestStr), storeSession) + + _, _, err := p.Partition(&id.DummyUser, message.Text, + time.Now(), []byte(ipsumTestStr)) + if err != nil { + t.Error(err) + } +} + +// Test that HandlePartition can handle a message part +func TestPartitioner_HandlePartition(t *testing.T) { + storeSession := storage.InitTestingSession(t) + p := NewPartitioner(len(ipsumTestStr), storeSession) + + m := newMessagePart(1107, 1, []byte(ipsumTestStr)) + + _, _ = p.HandlePartition( + &id.DummyUser, + message.None, + m.Bytes(), + []byte{'t', 'e', 's', 't', 'i', 'n', 'g', + 's', 't', 'r', 'i', 'n', 'g'}, + ) +} + +// Test that HandlePartition can handle a first message part +func TestPartitioner_HandleFirstPartition(t *testing.T) { + storeSession := storage.InitTestingSession(t) + p := NewPartitioner(len(ipsumTestStr), storeSession) + + m := newFirstMessagePart(message.Text, 1107, 1, time.Now(), []byte(ipsumTestStr)) + + _, _ = p.HandlePartition( + &id.DummyUser, + message.None, + m.Bytes(), + []byte{'t', 'e', 's', 't', 'i', 'n', 'g', + 's', 't', 'r', 'i', 'n', 'g'}, + ) +}