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
77c2aa21
Commit
77c2aa21
authored
3 years ago
by
Josh Brooks
Browse files
Options
Downloads
Patches
Plain Diff
Add tests for mnemonic
parent
acda7de7
No related branches found
No related tags found
2 merge requests
!23
Release
,
!19
Implement mnemonic in client
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
api/mnemonic_test.go
+100
-0
100 additions, 0 deletions
api/mnemonic_test.go
with
100 additions
and
0 deletions
api/mnemonic_test.go
0 → 100644
+
100
−
0
View file @
77c2aa21
///////////////////////////////////////////////////////////////////////////////
// Copyright © 2020 xx network SEZC //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
///////////////////////////////////////////////////////////////////////////////
package
api
import
(
"bytes"
"gitlab.com/xx_network/crypto/csprng"
xxMnemonic
"gitlab.com/xx_network/crypto/mnemonic"
"gitlab.com/xx_network/primitives/utils"
"io"
"math/rand"
"testing"
)
func
TestStoreSecretWithMnemonic
(
t
*
testing
.
T
)
{
secret
:=
[]
byte
(
"test123"
)
storageDir
:=
"ignore.1"
mnemonic
,
err
:=
StoreSecretWithMnemonic
(
secret
,
storageDir
)
if
err
!=
nil
{
t
.
Errorf
(
"StoreSecretWithMnemonic error; %v"
,
err
)
}
// Tests the mnemonic returned is valid
_
,
err
=
xxMnemonic
.
DecodeMnemonic
(
mnemonic
)
if
err
!=
nil
{
t
.
Errorf
(
"StoreSecretWithMnemonic did not return a decodable mnemonic: %v"
,
err
)
}
// Test that the file was written to
if
!
utils
.
Exists
(
storageDir
+
mnemonicFile
)
{
t
.
Errorf
(
"Mnemonic file does not exist in storage: %v"
,
err
)
}
}
func
TestEncryptDecryptMnemonic
(
t
*
testing
.
T
)
{
prng
:=
NewPrng
(
32
)
// Generate a test mnemonic
testMnemonic
,
err
:=
xxMnemonic
.
GenerateMnemonic
(
prng
,
32
)
if
err
!=
nil
{
t
.
Fatalf
(
"GenerateMnemonic error: %v"
,
err
)
}
decodedMnemonic
,
err
:=
xxMnemonic
.
DecodeMnemonic
(
testMnemonic
)
if
err
!=
nil
{
t
.
Fatalf
(
"DecodeMnemonic error: %v"
,
err
)
}
secret
:=
[]
byte
(
"test123"
)
// Encrypt the secret
ciphertext
,
err
:=
encryptWithMnemonic
(
secret
,
decodedMnemonic
,
prng
)
if
err
!=
nil
{
t
.
Fatalf
(
"encryptWithMnemonic error: %v"
,
err
)
}
// Decrypt the secret
received
,
err
:=
decryptWithMnemonic
(
ciphertext
,
decodedMnemonic
)
if
err
!=
nil
{
t
.
Fatalf
(
"decryptWithMnemonic error: %v"
,
err
)
}
// Test if secret matches decrypted data
if
!
bytes
.
Equal
(
received
,
secret
)
{
t
.
Fatalf
(
"Decrypted data does not match original plaintext."
+
"
\n\t
Expected: %v
\n\t
Received: %v"
,
secret
,
received
)
}
}
func
TestLoadSecretWithMnemonic
(
t
*
testing
.
T
)
{
secret
:=
[]
byte
(
"test123"
)
storageDir
:=
"ignore.1"
mnemonic
,
err
:=
StoreSecretWithMnemonic
(
secret
,
storageDir
)
if
err
!=
nil
{
t
.
Errorf
(
"StoreSecretWithMnemonic error; %v"
,
err
)
}
received
,
err
:=
LoadSecretWithMnemonic
(
mnemonic
,
storageDir
)
if
err
!=
nil
{
t
.
Errorf
(
"LoadSecretWithMnemonic error: %v"
,
err
)
}
if
!
bytes
.
Equal
(
received
,
secret
)
{
t
.
Fatalf
(
"Loaded secret does not match original data."
+
"
\n\t
Expected: %v
\n\t
Received: %v"
,
secret
,
received
)
}
}
// Prng is a PRNG that satisfies the csprng.Source interface.
type
Prng
struct
{
prng
io
.
Reader
}
func
NewPrng
(
seed
int64
)
csprng
.
Source
{
return
&
Prng
{
rand
.
New
(
rand
.
NewSource
(
seed
))}
}
func
(
s
*
Prng
)
Read
(
b
[]
byte
)
(
int
,
error
)
{
return
s
.
prng
.
Read
(
b
)
}
func
(
s
*
Prng
)
SetSeed
([]
byte
)
error
{
return
nil
}
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