added some files

This commit is contained in:
didier.amyot committed 2014-09-13 13:42:37 -07:00
1 parent 112b062571
commit 4897e8d307
7 files changed
+519

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
package helper
import (
"crypto/rsa"
"encoding/binary"
"math/big"
"math/rand"
)
func B(x int64) *big.Int {
return big.NewInt(x)
}
type MockRandomReader struct {
random *rand.Rand
}
func NewMockRandomReader() *MockRandomReader {
return &MockRandomReader{rand.New(rand.NewSource(99))}
}
func (this *MockRandomReader) getNextFourRandomBytes() []byte {
ret := make([]byte, 4)
binary.LittleEndian.PutUint32(ret, uint32(this.random.Int31()))
return ret
}
func (this *MockRandomReader) Read(p []byte) (n int, err error) {
for i := 0; i < len(p); i += 4 {
copy(p[i:], this.getNextFourRandomBytes())
}
return len(p), nil
}
func (this *MockRandomReader) GetRandomBytes(size int) []byte {
ret := make([]byte, size)
this.Read(ret)
return ret
}
type MockIoWriter struct {
state []byte
}
func NewMockIoWriter() *MockIoWriter {
return &MockIoWriter{make([]byte, 0, 1024)}
}
func (this *MockIoWriter) Write(p []byte) (n int, err error) {
this.state = append(this.state, p...)
return len(p), nil
}
func (this *MockIoWriter) String() string {
return string(this.state)
}
func (this *MockIoWriter) Length() int {
return len(this.state)
}
func (this *MockIoWriter) Bytes() []byte {
return this.state
}
func Range(length int) []byte {
ret := make([]byte, length)
for i, _ := range ret {
ret[i] = byte(i)
}
return ret
}
func FromRange(from, to int) []byte {
ret := make([]byte, to-from)
for i, _ := range ret {
ret[i] = byte(i + from)
}
return ret
}
func GetRSAPublicKey(size int) *rsa.PublicKey {
return &GetRSAPrivateKey(size).PublicKey
}
func GetRSAPrivateKey(size int) *rsa.PrivateKey {
key, err := rsa.GenerateKey(NewMockRandomReader(), size)
if err != nil {
panic(err)
}
return key
}
+100
View File
@@ -0,0 +1,100 @@
package helper
import (
"fmt"
"math/big"
"math/rand"
"reflect"
"testing"
)
func TestRange(t *testing.T) {
if !reflect.DeepEqual(Range(3), []byte{0, 1, 2}) {
t.Fail()
}
}
func TestFromRange(t *testing.T) {
if !reflect.DeepEqual(FromRange(3, 5), []byte{3, 4}) {
t.Fail()
}
}
func TestB(t *testing.T) {
if !reflect.DeepEqual(B(3), big.NewInt(3)) {
t.Fail()
}
}
func TestMockIoWriter(t *testing.T) {
w := NewMockIoWriter()
fmt.Fprint(w, "something to write")
if !reflect.DeepEqual(w.Bytes(), []byte("something to write")) {
t.Fail()
} else if "something to write" != w.String() {
t.Fail()
} else if w.Length() != len("something to write") {
t.Fail()
}
}
func TestGetRSAPublicKeyAndGetRSAPrivateKey(t *testing.T) {
privateKey := GetRSAPrivateKey(50)
if err := privateKey.Validate(); err != nil {
t.Error(err)
return
}
publicKey := GetRSAPublicKey(50)
if publicKey.E != privateKey.E || publicKey.N.Cmp(privateKey.N) != 0 {
t.Error("private and public keys do not match", publicKey.N, privateKey.N)
}
if publicKey.N.BitLen() != 50 {
t.Error("modulo has bad bit length")
}
}
func TestGetNextFourRandomBytes(t *testing.T) {
m := &MockRandomReader{rand.New(rand.NewSource(22))}
rb := m.getNextFourRandomBytes()
var expt []byte = []byte{233, 237, 107, 32}
if len(rb) != 4 {
t.Error("bad random length")
return
} else if !reflect.DeepEqual(expt, rb) {
t.Error("unexpect randome data")
}
rb = m.getNextFourRandomBytes()
expt = []byte{27, 131, 191, 20}
if len(rb) != 4 {
t.Error("bad random length")
return
} else if !reflect.DeepEqual(expt, rb) {
t.Error("unexpect randome data")
}
}
func TestMockRandomReader(t *testing.T) {
m := NewMockRandomReader()
dump := make([]byte, 4*10+3)
if l, err := m.Read(dump); l != len(dump) || err != nil {
t.Error("that is very bad :(")
return
}
var expt []byte = []byte{73, 233, 188, 33, 31, 118, 98, 81, 66, 120, 254, 85, 138,
36, 118, 80, 38, 83, 169, 57, 27, 61, 103, 122, 37, 164, 185, 40, 112, 66, 1,
50, 185, 128, 156, 20, 8, 164, 37, 13, 182, 50, 175}
if !reflect.DeepEqual(dump, expt) {
t.Error("unexpected returned data\n", dump)
}
}
func TestGetRandomBytes(t *testing.T) {
rdBytes := NewMockRandomReader().GetRandomBytes(10)
expt := make([]byte, 10)
NewMockRandomReader().Read(expt)
if !reflect.DeepEqual(expt, rdBytes) {
t.Fail()
}
}