Skip to content
Snippets Groups Projects
Select Git revision
  • 40b80af39d764de7ada42d8d77f4492b8a957c57
  • release default protected
  • 11-22-implement-kv-interface-defined-in-collectiveversionedkvgo
  • hotfix/TestHostPool_UpdateNdf_AddFilter
  • XX-4719/announcementChannels
  • xx-4717/logLevel
  • jonah/noob-channel
  • master protected
  • XX-4707/tagDiskJson
  • xx-4698/notification-retry
  • hotfix/notifylockup
  • syncNodes
  • hotfix/localCB
  • XX-4677/NewChanManagerMobile
  • XX-4689/DmSync
  • duplicatePrefix
  • XX-4601/HavenInvites
  • finalizedUICallbacks
  • XX-4673/AdminKeySync
  • debugNotifID
  • anne/test
  • v4.7.5
  • v4.7.4
  • v4.7.3
  • v4.7.2
  • v4.7.1
  • v4.6.3
  • v4.6.1
  • v4.5.0
  • v4.4.4
  • v4.3.11
  • v4.3.8
  • v4.3.7
  • v4.3.6
  • v4.3.5
  • v4.2.0
  • v4.3.0
  • v4.3.4
  • v4.3.3
  • v4.3.2
  • v4.3.1
41 results

session.go

Blame
  • firstMessagePart_test.go 3.16 KiB
    ///////////////////////////////////////////////////////////////////////////////
    // 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"
    )
    
    // 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},
    }
    
    // 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)
    	}
    }
    
    // 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")
    	}
    }
    
    // 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)
    	}
    }
    
    // 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)
    	}
    }
    
    // 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)
    	}
    }