add tests for utils

This commit is contained in:
Marten Seemann
2016-04-14 11:30:22 +07:00
parent 98cd4aa513
commit 4f39e0f14d
3 changed files with 99 additions and 0 deletions

View File

@@ -6,6 +6,7 @@ import (
)
// ReadUintN reads N bytes
// ToDo: add tests
func ReadUintN(b io.ByteReader, length uint8) (uint64, error) {
var res uint64
for i := uint8(0); i < length; i++ {

13
utils/utils_suite_test.go Normal file
View File

@@ -0,0 +1,13 @@
package utils
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)
func TestCrypto(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Utils Suite")
}

85
utils/utils_test.go Normal file
View File

@@ -0,0 +1,85 @@
package utils
import (
"bytes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Utils", func() {
Context("WriteUint64", func() {
It("outputs 8 bytes", func() {
b := &bytes.Buffer{}
WriteUint64(b, uint64(1))
Expect(b.Len()).To(Equal(8))
})
It("outputs a little endian", func() {
num := uint64(0xFFEEDDCCBBAA9988)
b := &bytes.Buffer{}
WriteUint64(b, num)
Expect(b.Bytes()).To(Equal([]byte{0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}))
})
})
Context("WriteUint32", func() {
It("outputs 4 bytes", func() {
b := &bytes.Buffer{}
WriteUint32(b, uint32(1))
Expect(b.Len()).To(Equal(4))
})
It("outputs a little endian", func() {
num := uint32(0xEFAC3512)
b := &bytes.Buffer{}
WriteUint32(b, num)
Expect(b.Bytes()).To(Equal([]byte{0x12, 0x35, 0xAC, 0xEF}))
})
})
Context("ReadUint16", func() {
It("reads a little endian", func() {
b := []byte{0x13, 0xEF}
val, err := ReadUint16(bytes.NewReader(b))
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal(uint16(0xEF13)))
})
It("throws an error if less than 2 bytes are passed", func() {
b := []byte{0x13}
_, err := ReadUint16(bytes.NewReader(b))
Expect(err).To(HaveOccurred())
})
})
Context("ReadUint32", func() {
It("reads a little endian", func() {
b := []byte{0x12, 0x35, 0xAB, 0xFF}
val, err := ReadUint32(bytes.NewReader(b))
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal(uint32(0xFFAB3512)))
})
It("throws an error if less than 4 bytes are passed", func() {
b := []byte{0x13, 0x34, 0xEA}
_, err := ReadUint32(bytes.NewReader(b))
Expect(err).To(HaveOccurred())
})
})
Context("WriteUint16", func() {
It("outputs 2 bytes", func() {
b := &bytes.Buffer{}
WriteUint16(b, uint16(1))
Expect(b.Len()).To(Equal(2))
})
It("outputs a little endian", func() {
num := uint16(0xFF11)
b := &bytes.Buffer{}
WriteUint16(b, num)
Expect(b.Bytes()).To(Equal([]byte{0x11, 0xFF}))
})
})
})