Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
client
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
client
Commits
7eab6bd8
Commit
7eab6bd8
authored
4 years ago
by
Josh Brooks
Browse files
Options
Downloads
Patches
Plain Diff
Test requestFormat
parent
2c31e9b6
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!23
Release
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
auth/fmt_test.go
+128
-4
128 additions, 4 deletions
auth/fmt_test.go
auth/utils_test.go
+9
-1
9 additions, 1 deletion
auth/utils_test.go
with
137 additions
and
5 deletions
auth/fmt_test.go
+
128
−
4
View file @
7eab6bd8
...
@@ -9,6 +9,8 @@ package auth
...
@@ -9,6 +9,8 @@ package auth
import
(
import
(
"bytes"
"bytes"
"gitlab.com/xx_network/primitives/id"
"math/rand"
"reflect"
"reflect"
"testing"
"testing"
)
)
...
@@ -128,7 +130,7 @@ func TestBaseFormat_SetGetEcrPayload(t *testing.T) {
...
@@ -128,7 +130,7 @@ func TestBaseFormat_SetGetEcrPayload(t *testing.T) {
// Test setter
// Test setter
ecrPayloadSize
:=
payloadSize
-
(
pubKeySize
+
saltSize
)
ecrPayloadSize
:=
payloadSize
-
(
pubKeySize
+
saltSize
)
ecrPayload
:=
new
Ecr
Payload
(
ecrPayloadSize
,
"ecrPayload"
)
ecrPayload
:=
newPayload
(
ecrPayloadSize
,
"ecrPayload"
)
baseMsg
.
SetEcrPayload
(
ecrPayload
)
baseMsg
.
SetEcrPayload
(
ecrPayload
)
if
!
bytes
.
Equal
(
ecrPayload
,
baseMsg
.
ecrPayload
)
{
if
!
bytes
.
Equal
(
ecrPayload
,
baseMsg
.
ecrPayload
)
{
t
.
Errorf
(
"SetEcrPayload() error: "
+
t
.
Errorf
(
"SetEcrPayload() error: "
+
...
@@ -163,7 +165,7 @@ func TestBaseFormat_MarshalUnmarshal(t *testing.T) {
...
@@ -163,7 +165,7 @@ func TestBaseFormat_MarshalUnmarshal(t *testing.T) {
payloadSize
:=
(
saltSize
+
pubKeySize
)
*
2
payloadSize
:=
(
saltSize
+
pubKeySize
)
*
2
baseMsg
:=
newBaseFormat
(
payloadSize
,
pubKeySize
)
baseMsg
:=
newBaseFormat
(
payloadSize
,
pubKeySize
)
ecrPayloadSize
:=
payloadSize
-
(
pubKeySize
+
saltSize
)
ecrPayloadSize
:=
payloadSize
-
(
pubKeySize
+
saltSize
)
ecrPayload
:=
new
Ecr
Payload
(
ecrPayloadSize
,
"ecrPayload"
)
ecrPayload
:=
newPayload
(
ecrPayloadSize
,
"ecrPayload"
)
baseMsg
.
SetEcrPayload
(
ecrPayload
)
baseMsg
.
SetEcrPayload
(
ecrPayload
)
salt
:=
newSalt
(
"salt"
)
salt
:=
newSalt
(
"salt"
)
baseMsg
.
SetSalt
(
salt
)
baseMsg
.
SetSalt
(
salt
)
...
@@ -278,7 +280,7 @@ func TestEcrFormat_SetGetPayload(t *testing.T) {
...
@@ -278,7 +280,7 @@ func TestEcrFormat_SetGetPayload(t *testing.T) {
ecrMsg
:=
newEcrFormat
(
payloadSize
)
ecrMsg
:=
newEcrFormat
(
payloadSize
)
// Test set
// Test set
expectedPayload
:=
new
Ecr
Payload
(
payloadSize
-
ownershipSize
,
"ownership"
)
expectedPayload
:=
newPayload
(
payloadSize
-
ownershipSize
,
"ownership"
)
ecrMsg
.
SetPayload
(
expectedPayload
)
ecrMsg
.
SetPayload
(
expectedPayload
)
if
!
bytes
.
Equal
(
expectedPayload
,
ecrMsg
.
payload
)
{
if
!
bytes
.
Equal
(
expectedPayload
,
ecrMsg
.
payload
)
{
...
@@ -312,7 +314,7 @@ func TestEcrFormat_MarshalUnmarshal(t *testing.T) {
...
@@ -312,7 +314,7 @@ func TestEcrFormat_MarshalUnmarshal(t *testing.T) {
// Construct message
// Construct message
payloadSize
:=
ownershipSize
*
2
payloadSize
:=
ownershipSize
*
2
ecrMsg
:=
newEcrFormat
(
payloadSize
)
ecrMsg
:=
newEcrFormat
(
payloadSize
)
expectedPayload
:=
new
Ecr
Payload
(
payloadSize
-
ownershipSize
,
"ownership"
)
expectedPayload
:=
newPayload
(
payloadSize
-
ownershipSize
,
"ownership"
)
ecrMsg
.
SetPayload
(
expectedPayload
)
ecrMsg
.
SetPayload
(
expectedPayload
)
ownership
:=
newOwnership
(
"owner"
)
ownership
:=
newOwnership
(
"owner"
)
ecrMsg
.
SetOwnership
(
ownership
)
ecrMsg
.
SetOwnership
(
ownership
)
...
@@ -346,3 +348,125 @@ func TestEcrFormat_MarshalUnmarshal(t *testing.T) {
...
@@ -346,3 +348,125 @@ func TestEcrFormat_MarshalUnmarshal(t *testing.T) {
}
}
}
}
// Tests newRequestFormat
func
TestNewRequestFormat
(
t
*
testing
.
T
)
{
// Construct message
payloadSize
:=
id
.
ArrIDLen
*
2
-
1
ecrMsg
:=
newEcrFormat
(
payloadSize
)
expectedPayload
:=
newPayload
(
id
.
ArrIDLen
,
"ownership"
)
ecrMsg
.
SetPayload
(
expectedPayload
)
reqMsg
,
err
:=
newRequestFormat
(
ecrMsg
)
if
err
!=
nil
{
t
.
Fatalf
(
"newRequestFormat() error: "
+
"Failed to construct message: %v"
,
err
)
}
// Check that the requestFormat was constructed properly
if
!
bytes
.
Equal
(
reqMsg
.
id
,
expectedPayload
)
{
t
.
Errorf
(
"newRequestFormat() error: "
+
"Unexpected id field in requestFormat."
+
"
\n\t
Expected: %v"
+
"
\n\t
Received: %v"
,
make
([]
byte
,
id
.
ArrIDLen
),
reqMsg
.
id
)
}
if
!
bytes
.
Equal
(
reqMsg
.
msgPayload
,
make
([]
byte
,
0
))
{
t
.
Errorf
(
"newRequestFormat() error: "
+
"Unexpected msgPayload field in requestFormat."
+
"
\n\t
Expected: %v"
+
"
\n\t
Received: %v"
,
make
([]
byte
,
0
),
reqMsg
.
msgPayload
)
}
payloadSize
=
ownershipSize
*
2
ecrMsg
=
newEcrFormat
(
payloadSize
)
reqMsg
,
err
=
newRequestFormat
(
ecrMsg
)
if
err
==
nil
{
t
.
Errorf
(
"Expecter error: Should be invalid size when calling newRequestFormat"
)
}
}
/* Setter/Getter tests for RequestFormat */
// Unit test for Get/SetID
func
TestRequestFormat_SetGetID
(
t
*
testing
.
T
)
{
// Construct message
payloadSize
:=
id
.
ArrIDLen
*
2
-
1
ecrMsg
:=
newEcrFormat
(
payloadSize
)
expectedPayload
:=
newPayload
(
id
.
ArrIDLen
,
"ownership"
)
ecrMsg
.
SetPayload
(
expectedPayload
)
reqMsg
,
err
:=
newRequestFormat
(
ecrMsg
)
if
err
!=
nil
{
t
.
Fatalf
(
"newRequestFormat() error: "
+
"Failed to construct message: %v"
,
err
)
}
// Test SetID
prng
:=
rand
.
New
(
rand
.
NewSource
(
42
))
expectedId
:=
randID
(
prng
,
id
.
User
)
reqMsg
.
SetID
(
expectedId
)
if
!
bytes
.
Equal
(
reqMsg
.
id
,
expectedId
.
Bytes
())
{
t
.
Errorf
(
"SetID() error: "
+
"Id field does not have expected value."
+
"
\n\t
Expected: %v
\n\t
Received: %v"
,
expectedId
,
reqMsg
.
msgPayload
)
}
// Test GetID
receivedId
,
err
:=
reqMsg
.
GetID
()
if
err
!=
nil
{
t
.
Fatalf
(
"GetID() error: "
+
"Retrieved id does not match expected value:"
+
"
\n\t
Expected: %v
\n\t
Received: %v"
,
expectedId
,
receivedId
)
}
// Test GetID error: unmarshal-able ID in requestFormat
reqMsg
.
id
=
[]
byte
(
"badId"
)
receivedId
,
err
=
reqMsg
.
GetID
()
if
err
==
nil
{
t
.
Errorf
(
"GetID() error: "
+
"Should not be able get ID from request message "
)
}
}
// Unit test for Get/SetMsgPayload
func
TestRequestFormat_SetGetMsgPayload
(
t
*
testing
.
T
)
{
// Construct message
payloadSize
:=
id
.
ArrIDLen
*
3
-
1
ecrMsg
:=
newEcrFormat
(
payloadSize
)
expectedPayload
:=
newPayload
(
id
.
ArrIDLen
*
2
,
"ownership"
)
ecrMsg
.
SetPayload
(
expectedPayload
)
reqMsg
,
err
:=
newRequestFormat
(
ecrMsg
)
if
err
!=
nil
{
t
.
Fatalf
(
"newRequestFormat() error: "
+
"Failed to construct message: %v"
,
err
)
}
// Test SetMsgPayload
msgPayload
:=
newPayload
(
id
.
ArrIDLen
,
"msgPayload"
)
reqMsg
.
SetMsgPayload
(
msgPayload
)
if
!
bytes
.
Equal
(
reqMsg
.
msgPayload
,
msgPayload
)
{
t
.
Errorf
(
"SetMsgPayload() error: "
+
"MsgPayload has unexpected value: "
+
"
\n\t
Expected: %v
\n\t
Received: %v"
,
msgPayload
,
reqMsg
.
msgPayload
)
}
// Test GetMsgPayload
retrievedMsgPayload
:=
reqMsg
.
GetMsgPayload
()
if
!
bytes
.
Equal
(
retrievedMsgPayload
,
msgPayload
)
{
t
.
Errorf
(
"GetMsgPayload() error: "
+
"MsgPayload has unexpected value: "
+
"
\n\t
Expected: %v
\n\t
Received: %v"
,
msgPayload
,
retrievedMsgPayload
)
}
// Test SetMsgPayload error: Invalid message payload size
defer
func
()
{
if
r
:=
recover
();
r
==
nil
{
t
.
Error
(
"SetMsgPayload() did not panic when the size of "
+
"the payload is the incorrect size."
)
}
}()
expectedPayload
=
append
(
expectedPayload
,
expectedPayload
...
)
reqMsg
.
SetMsgPayload
(
expectedPayload
)
}
This diff is collapsed.
Click to expand it.
auth/utils_test.go
+
9
−
1
View file @
7eab6bd8
...
@@ -3,6 +3,8 @@ package auth
...
@@ -3,6 +3,8 @@ package auth
import
(
import
(
"gitlab.com/elixxir/crypto/cyclic"
"gitlab.com/elixxir/crypto/cyclic"
"gitlab.com/xx_network/crypto/large"
"gitlab.com/xx_network/crypto/large"
"gitlab.com/xx_network/primitives/id"
"math/rand"
)
)
func
getGroup
()
*
cyclic
.
Group
{
func
getGroup
()
*
cyclic
.
Group
{
...
@@ -23,13 +25,19 @@ func getGroup() *cyclic.Group {
...
@@ -23,13 +25,19 @@ func getGroup() *cyclic.Group {
large
.
NewIntFromString
(
"2"
,
16
))
large
.
NewIntFromString
(
"2"
,
16
))
}
}
// randID returns a new random ID of the specified type.
func
randID
(
rng
*
rand
.
Rand
,
t
id
.
Type
)
*
id
.
ID
{
newID
,
_
:=
id
.
NewRandomID
(
rng
,
t
)
return
newID
}
func
newSalt
(
s
string
)
[]
byte
{
func
newSalt
(
s
string
)
[]
byte
{
salt
:=
make
([]
byte
,
saltSize
)
salt
:=
make
([]
byte
,
saltSize
)
copy
(
salt
[
:
],
s
)
copy
(
salt
[
:
],
s
)
return
salt
return
salt
}
}
func
new
Ecr
Payload
(
size
int
,
s
string
)
[]
byte
{
func
newPayload
(
size
int
,
s
string
)
[]
byte
{
b
:=
make
([]
byte
,
size
)
b
:=
make
([]
byte
,
size
)
copy
(
b
[
:
],
s
)
copy
(
b
[
:
],
s
)
return
b
return
b
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment