-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stress tests #43
Stress tests #43
Changes from 7 commits
0819d89
79a534a
803075a
8525fba
57effe9
c5683a7
4da5cb4
22d7236
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package crypto | ||
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"errors" | ||
"testing" | ||
) | ||
|
||
func TestDigest(t *testing.T) { | ||
msg := []byte("test message") | ||
d := Digest(msg) | ||
if len(d) != HashSizeByte { | ||
t.Fatal("Computation of Hash failed.") | ||
} | ||
if bytes.Equal(d, make([]byte, HashSizeByte)) { | ||
t.Fatal("Hash is all zeros.") | ||
} | ||
} | ||
|
||
type testErrorRandReader struct{} | ||
|
||
func (er testErrorRandReader) Read([]byte) (int, error) { | ||
return 0, errors.New("Not enough entropy!") | ||
} | ||
|
||
func TestMakeRand(t *testing.T) { | ||
r, err := MakeRand() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// check if hashed the random output: | ||
if len(r) != HashSizeByte { | ||
t.Fatal("Looks like Digest wasn't called correctly.") | ||
} | ||
orig := rand.Reader | ||
rand.Reader = testErrorRandReader{} | ||
r, err = MakeRand() | ||
if err == nil { | ||
t.Fatal("No error returned") | ||
} | ||
rand.Reader = orig | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package util | ||
|
||
import ( | ||
"encoding/binary" | ||
"math/rand" | ||
"testing" | ||
"time" | ||
|
@@ -27,3 +28,35 @@ func TestBitsBytesConvert(t *testing.T) { | |
} | ||
} | ||
} | ||
|
||
func TestIntToBytes(t *testing.T) { | ||
numInt := 42 | ||
b := IntToBytes(numInt) | ||
if int(binary.LittleEndian.Uint32(b)) != numInt { | ||
t.Fatal("Conversion to bytes looks wrong!") | ||
} | ||
// TODO It is possible to call `IntToBytes` with a signed int < 0 | ||
// isn't that problematic? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. I don't expect the server will use signed ints. Do you think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think to let it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactoring There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW (unsigned) inters can overflow in golang without panic/error or something similar: https://golang.org/ref/spec#Integer_overflow There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
OK, should this go into another issue/PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think yes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, thanks! Opened #53 for that. |
||
// for instance this will fail: | ||
// b := IntToBytes(-42) | ||
// if int(binary.LittleEndian.Uint32(b)) != numInt { | ||
// t.Fatal("Conversion to bytes looks wrong!") | ||
// } | ||
} | ||
|
||
func TestULongToBytes(t *testing.T) { | ||
numInt := uint64(42) | ||
b := ULongToBytes(numInt) | ||
if binary.LittleEndian.Uint64(b) != numInt { | ||
t.Fatal("Conversion to bytes looks wrong!") | ||
} | ||
} | ||
|
||
func TestLongToBytes(t *testing.T) { | ||
numInt := int64(42) | ||
b := LongToBytes(numInt) | ||
if int64(binary.LittleEndian.Uint64(b)) != numInt { | ||
t.Fatal("Conversion to bytes looks wrong!") | ||
} | ||
// TODO similar problem as in TestIntToBytes | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use this function? https://github.com/coniks-sys/coniks-go/blob/master/merkletree/tb.go#L21-L27
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense! Thanks for the pointer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now there is even a public
Serialize
method (https://github.com/coniks-sys/coniks-go/blob/master/merkletree/tb.go#L29-L31). I would prefer to use it instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense to me!