diff --git a/auth/fmt_test.go b/auth/fmt_test.go new file mode 100644 index 0000000000000000000000000000000000000000..c28ce0a91a4690341f0b8d72ae2b7a0d137e473c --- /dev/null +++ b/auth/fmt_test.go @@ -0,0 +1,348 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package auth + +import ( + "bytes" + "reflect" + "testing" +) + +// Tests newBaseFormat +func TestNewBaseFormat(t *testing.T) { + // Construct message + pubKeySize := 256 + payloadSize := saltSize + pubKeySize + baseMsg := newBaseFormat(payloadSize, pubKeySize) + + // Check that the base format was constructed properly + if !bytes.Equal(baseMsg.pubkey, make([]byte, pubKeySize)) { + t.Errorf("NewBaseFormat error: "+ + "Unexpected pubkey field in base format."+ + "\n\tExpected: %v"+ + "\n\tReceived: %v", make([]byte, pubKeySize), baseMsg.pubkey) + } + + if !bytes.Equal(baseMsg.salt, make([]byte, saltSize)) { + t.Errorf("NewBaseFormat error: "+ + "Unexpected salt field in base format."+ + "\n\tExpected: %v"+ + "\n\tReceived: %v", make([]byte, saltSize), baseMsg.salt) + } + + expectedEcrPayloadSize := payloadSize - (pubKeySize + saltSize) + if !bytes.Equal(baseMsg.ecrPayload, make([]byte, expectedEcrPayloadSize)) { + t.Errorf("NewBaseFormat error: "+ + "Unexpected payload field in base format."+ + "\n\tExpected: %v"+ + "\n\tReceived: %v", make([]byte, expectedEcrPayloadSize), baseMsg.ecrPayload) + } + + // Error case, where payload size is less than the public key plus salt + defer func() { + if r := recover(); r == nil { + t.Error("newBaseFormat() did not panic when the size of " + + "the payload is smaller than the size of the public key.") + } + }() + + newBaseFormat(0, pubKeySize) +} + +/* Tests the setter/getter methods for baseFormat */ + +// Set/Get PubKey tests +func TestBaseFormat_SetGetPubKey(t *testing.T) { + // Construct message + pubKeySize := 256 + payloadSize := saltSize + pubKeySize + baseMsg := newBaseFormat(payloadSize, pubKeySize) + + // Test setter + grp := getGroup() + pubKey := grp.NewInt(25) + baseMsg.SetPubKey(pubKey) + expectedBytes := pubKey.LeftpadBytes(uint64(len(baseMsg.pubkey))) + if !bytes.Equal(baseMsg.pubkey, expectedBytes) { + t.Errorf("SetPubKey() error: "+ + "Public key field does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", expectedBytes, baseMsg.pubkey) + } + + // Test getter + receivedKey := baseMsg.GetPubKey(grp) + if !bytes.Equal(pubKey.Bytes(), receivedKey.Bytes()) { + t.Errorf("GetPubKey() error: "+ + "Public key retrieved does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", pubKey, receivedKey) + } + +} + +// Set/Get salt tests +func TestBaseFormat_SetGetSalt(t *testing.T) { + // Construct message + pubKeySize := 256 + payloadSize := saltSize + pubKeySize + baseMsg := newBaseFormat(payloadSize, pubKeySize) + + // Test setter + salt := newSalt("salt") + baseMsg.SetSalt(salt) + if !bytes.Equal(salt, baseMsg.salt) { + t.Errorf("SetSalt() error: "+ + "Salt field does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", salt, baseMsg.salt) + } + + // Test getter + receivedSalt := baseMsg.GetSalt() + if !bytes.Equal(salt, receivedSalt) { + t.Errorf("GetSalt() error: "+ + "Salt retrieved does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", salt, receivedSalt) + } + + // Test setter error path: Setting salt of incorrect size + defer func() { + if r := recover(); r == nil { + t.Error("SetSalt() did not panic when the size of " + + "the salt is smaller than the required salt size.") + } + }() + + baseMsg.SetSalt([]byte("salt")) +} + +// Set/Get EcrPayload tests +func TestBaseFormat_SetGetEcrPayload(t *testing.T) { + // Construct message + pubKeySize := 256 + payloadSize := (saltSize + pubKeySize) * 2 + baseMsg := newBaseFormat(payloadSize, pubKeySize) + + // Test setter + ecrPayloadSize := payloadSize - (pubKeySize + saltSize) + ecrPayload := newEcrPayload(ecrPayloadSize, "ecrPayload") + baseMsg.SetEcrPayload(ecrPayload) + if !bytes.Equal(ecrPayload, baseMsg.ecrPayload) { + t.Errorf("SetEcrPayload() error: "+ + "EcrPayload field does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", ecrPayload, baseMsg.ecrPayload) + + } + + // Test Getter + receivedEcrPayload := baseMsg.GetEcrPayload() + if !bytes.Equal(receivedEcrPayload, ecrPayload) { + t.Errorf("GetEcrPayload() error: "+ + "EcrPayload retrieved does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", ecrPayload, receivedEcrPayload) + } + + // Setter error path: Setting ecrPayload that + // does not completely fill field + defer func() { + if r := recover(); r == nil { + t.Error("SetEcrPayload() did not panic when the size of " + + "the ecrPayload is smaller than the pre-constructed field.") + } + }() + baseMsg.SetEcrPayload([]byte("ecrPayload")) +} + +// Marshal/ unmarshal tests +func TestBaseFormat_MarshalUnmarshal(t *testing.T) { + // Construct a fully populated message + pubKeySize := 256 + payloadSize := (saltSize + pubKeySize) * 2 + baseMsg := newBaseFormat(payloadSize, pubKeySize) + ecrPayloadSize := payloadSize - (pubKeySize + saltSize) + ecrPayload := newEcrPayload(ecrPayloadSize, "ecrPayload") + baseMsg.SetEcrPayload(ecrPayload) + salt := newSalt("salt") + baseMsg.SetSalt(salt) + grp := getGroup() + pubKey := grp.NewInt(25) + baseMsg.SetPubKey(pubKey) + + // Test marshal + data := baseMsg.Marshal() + if !bytes.Equal(data, baseMsg.data) { + t.Errorf("baseFormat.Marshal() error: "+ + "Marshalled data is not expected."+ + "\n\tExpected: %v\n\tReceived: %v", baseMsg.data, data) + } + + // Test unmarshal + newMsg, err := unmarshalBaseFormat(data, pubKeySize) + if err != nil { + t.Errorf("unmarshalBaseFormat() error: "+ + "Could not unmarshal into baseFormat: %v", err) + } + + if !reflect.DeepEqual(newMsg, baseMsg) { + t.Errorf("unmarshalBaseFormat() error: "+ + "Unmarshalled message does not match originally marshalled message."+ + "\n\tExpected: %v\n\tRecieved: %v", baseMsg, newMsg) + } + + // Unmarshal error test: Invalid size parameter + _, err = unmarshalBaseFormat(make([]byte, 0), pubKeySize) + if err == nil { + t.Errorf("unmarshalBaseFormat() error: " + + "Should not be able to unmarshal when baseFormat is too small") + } + +} + +// Tests newEcrFormat +func TestNewEcrFormat(t *testing.T) { + // Construct message + payloadSize := ownershipSize * 2 + ecrMsg := newEcrFormat(payloadSize) + + // Check that the ecrFormat was constructed properly + if !bytes.Equal(ecrMsg.ownership, make([]byte, ownershipSize)) { + t.Errorf("newEcrFormat error: "+ + "Unexpected ownership field in ecrFormat."+ + "\n\tExpected: %v"+ + "\n\tReceived: %v", make([]byte, payloadSize), ecrMsg.ownership) + } + + if !bytes.Equal(ecrMsg.payload, make([]byte, payloadSize-ownershipSize)) { + t.Errorf("newEcrFormat error: "+ + "Unexpected ownership field in ecrFormat."+ + "\n\tExpected: %v"+ + "\n\tReceived: %v", make([]byte, payloadSize-ownershipSize), ecrMsg.payload) + } + + // Error case, where payload size is less than the public key plus salt + defer func() { + if r := recover(); r == nil { + t.Error("newEcrFormat() did not panic when the size of " + + "the payload is smaller than the size of the ownership") + } + }() + + newEcrFormat(0) +} + +/* Tests the setter/getter methods for ecrFormat */ + +// Set/Get ownership tests +func TestEcrFormat_SetGetOwnership(t *testing.T) { + // Construct message + payloadSize := ownershipSize * 2 + ecrMsg := newEcrFormat(payloadSize) + + // Test setter + ownership := newOwnership("owner") + ecrMsg.SetOwnership(ownership) + if !bytes.Equal(ownership, ecrMsg.ownership) { + t.Errorf("SetOwnership() error: "+ + "Ownership field does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", ownership, ecrMsg.ownership) + + } + + // Test getter + receivedOwnership := ecrMsg.GetOwnership() + if !bytes.Equal(receivedOwnership, ecrMsg.ownership) { + t.Errorf("GetOwnership() error: "+ + "Ownership retrieved does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", ownership, receivedOwnership) + + } + + // Test setter error path: Setting ownership of incorrect size + defer func() { + if r := recover(); r == nil { + t.Error("SetOwnership() did not panic when the size of " + + "the ownership is smaller than the required ownership size.") + } + }() + + ecrMsg.SetOwnership([]byte("ownership")) +} + +// Set/Get payload tests +func TestEcrFormat_SetGetPayload(t *testing.T) { + // Construct message + payloadSize := ownershipSize * 2 + ecrMsg := newEcrFormat(payloadSize) + + // Test set + expectedPayload := newEcrPayload(payloadSize-ownershipSize, "ownership") + ecrMsg.SetPayload(expectedPayload) + + if !bytes.Equal(expectedPayload, ecrMsg.payload) { + t.Errorf("SetPayload() error: "+ + "Payload field does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", expectedPayload, ecrMsg.payload) + } + + // Test get + receivedPayload := ecrMsg.GetPayload() + if !bytes.Equal(receivedPayload, expectedPayload) { + t.Errorf("GetPayload() error: "+ + "Payload retrieved does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", expectedPayload, receivedPayload) + + } + + // Test setter error path: Setting payload of incorrect size + defer func() { + if r := recover(); r == nil { + t.Error("SetPayload() did not panic when the size of " + + "the payload is smaller than the required payload size.") + } + }() + + ecrMsg.SetPayload([]byte("payload")) +} + +// Marshal/ unmarshal tests +func TestEcrFormat_MarshalUnmarshal(t *testing.T) { + // Construct message + payloadSize := ownershipSize * 2 + ecrMsg := newEcrFormat(payloadSize) + expectedPayload := newEcrPayload(payloadSize-ownershipSize, "ownership") + ecrMsg.SetPayload(expectedPayload) + ownership := newOwnership("owner") + ecrMsg.SetOwnership(ownership) + + // Test marshal + data := ecrMsg.Marshal() + if !bytes.Equal(data, ecrMsg.data) { + t.Errorf("ecrFormat.Marshal() error: "+ + "Marshalled data is not expected."+ + "\n\tExpected: %v\n\tReceived: %v", ecrMsg.data, data) + } + + // Test unmarshal + newMsg, err := unmarshalEcrFormat(data) + if err != nil { + t.Errorf("unmarshalEcrFormat() error: "+ + "Could not unmarshal into ecrFormat: %v", err) + } + + if !reflect.DeepEqual(newMsg, ecrMsg) { + t.Errorf("unmarshalBaseFormat() error: "+ + "Unmarshalled message does not match originally marshalled message."+ + "\n\tExpected: %v\n\tRecieved: %v", ecrMsg, newMsg) + } + + // Unmarshal error test: Invalid size parameter + _, err = unmarshalEcrFormat(make([]byte, 0)) + if err == nil { + t.Errorf("unmarshalEcrFormat() error: " + + "Should not be able to unmarshal when ecrFormat is too small") + } + +} diff --git a/auth/utils_test.go b/auth/utils_test.go new file mode 100644 index 0000000000000000000000000000000000000000..b2535747e9cb1eaa20f19df792bab630a701168f --- /dev/null +++ b/auth/utils_test.go @@ -0,0 +1,42 @@ +package auth + +import ( + "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/xx_network/crypto/large" +) + +func getGroup() *cyclic.Group { + return cyclic.NewGroup( + large.NewIntFromString("E2EE983D031DC1DB6F1A7A67DF0E9A8E5561DB8E8D4941"+ + "3394C049B7A8ACCEDC298708F121951D9CF920EC5D146727AA4AE535B0922C688"+ + "B55B3DD2AEDF6C01C94764DAB937935AA83BE36E67760713AB44A6337C20E7861"+ + "575E745D31F8B9E9AD8412118C62A3E2E29DF46B0864D0C951C394A5CBBDC6ADC"+ + "718DD2A3E041023DBB5AB23EBB4742DE9C1687B5B34FA48C3521632C4A530E8FF"+ + "B1BC51DADDF453B0B2717C2BC6669ED76B4BDD5C9FF558E88F26E5785302BEDBC"+ + "A23EAC5ACE92096EE8A60642FB61E8F3D24990B8CB12EE448EEF78E184C7242DD"+ + "161C7738F32BF29A841698978825B4111B4BC3E1E198455095958333D776D8B2B"+ + "EEED3A1A1A221A6E37E664A64B83981C46FFDDC1A45E3D5211AAF8BFBC072768C"+ + "4F50D7D7803D2D4F278DE8014A47323631D7E064DE81C0C6BFA43EF0E6998860F"+ + "1390B5D3FEACAF1696015CB79C3F9C2D93D961120CD0E5F12CBB687EAB045241F"+ + "96789C38E89D796138E6319BE62E35D87B1048CA28BE389B575E994DCA7554715"+ + "84A09EC723742DC35873847AEF49F66E43873", 16), + large.NewIntFromString("2", 16)) +} + +func newSalt(s string) []byte { + salt := make([]byte, saltSize) + copy(salt[:], s) + return salt +} + +func newEcrPayload(size int, s string) []byte { + b := make([]byte, size) + copy(b[:], s) + return b +} + +func newOwnership(s string) []byte { + ownership := make([]byte, ownershipSize) + copy(ownership[:], s) + return ownership +}