payload almost done

This commit is contained in:
didier.amyot committed 2014-10-17 15:46:21 -04:00
1 parent 77bfcba5b1
commit 09d0f237d5
16 files changed
+690 -118

No files matched your search

+20 -2
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"crypto/rsa"
"encoding/binary"
"encoding/json"
"io"
"math/big"
"math/rand"
@@ -14,11 +15,12 @@ func B(x int64) *big.Int {
}
type MockRandomReader struct {
random *rand.Rand
readLength int
random *rand.Rand
}
func NewMockRandomReader() *MockRandomReader {
return &MockRandomReader{rand.New(rand.NewSource(99))}
return &MockRandomReader{0, rand.New(rand.NewSource(99))}
}
func (this *MockRandomReader) getNextFourRandomBytes() []byte {
@@ -31,9 +33,14 @@ func (this *MockRandomReader) Read(p []byte) (n int, err error) {
for i := 0; i < len(p); i += 4 {
copy(p[i:], this.getNextFourRandomBytes())
}
this.readLength += len(p)
return len(p), nil
}
func (this *MockRandomReader) GetReadLength() int {
return this.readLength
}
func (this *MockRandomReader) GetRandomBytes(size int) []byte {
ret := make([]byte, size)
this.Read(ret)
@@ -74,6 +81,17 @@ func (this *MockIoWriter) ReaderAt() io.ReaderAt {
return bytes.NewReader(this.state)
}
func (this *MockIoWriter) IsValidJson() bool {
m := make(map[string]interface{})
l := make([]interface{}, 0, 100)
if json.Unmarshal(this.Bytes(), &m) == nil {
return true
} else if json.Unmarshal(this.Bytes(), &l) == nil {
return true
}
return false
}
func Range(length int) []byte {
ret := make([]byte, length)
for i, _ := range ret {
+33 -1
View File
@@ -43,6 +43,26 @@ func TestMockIoWriter(t *testing.T) {
}
}
func TestIsValidJson(t *testing.T) {
m := make(map[string]bool)
m[`{}`] = true
m[`[]`] = true
m[`[1, "abc", {"aa": true, "2": [1,2,3]}]`] = true
m[`{"aa": true, "2": [1,2,3]}`] = true
m[`regular string`] = false
m[`{"aa": true "2": [1,2,3]}`] = false // malformed json
for json_, isJSON := range m {
w := &MockIoWriter{[]byte(json_)}
ok := w.IsValidJson()
if ok && !isJSON {
t.Error(json_, "is not json but has been detected as json")
} else if !ok && isJSON {
t.Error(json_, "is json but has not been detected as json")
}
}
}
func TestGetRSAPublicKeyAndGetRSAPrivateKey(t *testing.T) {
privateKey := GetRSAPrivateKey(50)
if err := privateKey.Validate(); err != nil {
@@ -60,7 +80,7 @@ func TestGetRSAPublicKeyAndGetRSAPrivateKey(t *testing.T) {
}
func TestGetNextFourRandomBytes(t *testing.T) {
m := &MockRandomReader{rand.New(rand.NewSource(22))}
m := &MockRandomReader{0, rand.New(rand.NewSource(22))}
rb := m.getNextFourRandomBytes()
var expt []byte = []byte{233, 237, 107, 32}
if len(rb) != 4 {
@@ -103,3 +123,15 @@ func TestGetRandomBytes(t *testing.T) {
t.Fail()
}
}
func TestGetReadLength(t *testing.T) {
m := NewMockRandomReader()
dump := make([]byte, 4*10+3)
if _, err := m.Read(dump); err != nil {
t.Error(err)
return
}
if m.GetReadLength() != 4*10+3 {
t.Error("bad read length")
}
}