Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
Ginko-ify signature package tests
Browse files Browse the repository at this point in the history
Signed-off-by: Warren Fernandes <wfernandes@pivotal.io>
  • Loading branch information
John Tuley authored and Warren Fernandes committed Dec 4, 2014
1 parent 1c010a6 commit 4f97b67
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 60 deletions.
13 changes: 13 additions & 0 deletions signature/signature_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package signature

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"
)

func TestSignature(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Signature Suite")
}
118 changes: 58 additions & 60 deletions signature/symmetric_test.go
Original file line number Diff line number Diff line change
@@ -1,65 +1,63 @@
package signature

import (
"github.com/stretchr/testify/assert"
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestSimpleEncryption(t *testing.T) {
key := "aaaaaaaaaaaaaaaa"
message := []byte("Super secret message that no one should read")
encrypted, err := Encrypt(key, message)
assert.NoError(t, err)

decrypted, err := Decrypt(key, encrypted)
assert.NoError(t, err)

assert.Equal(t, decrypted, message)
assert.NotEqual(t, encrypted, message)
}

func TestEncryptionWithAShortKey(t *testing.T) {
key := "short key"
message := []byte("Super secret message that no one should read")
encrypted, err := Encrypt(key, message)
assert.NoError(t, err)

decrypted, err := Decrypt(key, encrypted)
assert.NoError(t, err)

assert.Equal(t, decrypted, message)
assert.NotEqual(t, encrypted, message)
}

func TestDecryptionWithWrongKey(t *testing.T) {
key := "short key"
message := []byte("Super secret message that no one should read")
encrypted, err := Encrypt(key, message)
assert.NoError(t, err)

_, err = Decrypt("wrong key", encrypted)
assert.Error(t, err)
}

func TestThatEncryptionIsNonDeterministic(t *testing.T) {
key := "aaaaaaaaaaaaaaaa"
message := []byte("Super secret message that no one should read")
encrypted1, err := Encrypt(key, message)
assert.NoError(t, err)

encrypted2, err := Encrypt(key, message)
assert.NoError(t, err)

assert.NotEqual(t, encrypted1, encrypted2)
}

//Test so that we are able to test that Ruby encrypts/signs the same way
func TestDigest(t *testing.T) {
assert.Equal(t, DigestBytes([]byte("some-key")), []byte{0x68, 0x2f, 0x66, 0x97, 0xfa, 0x93, 0xec, 0xa6, 0xc8, 0x1, 0xa2, 0x32, 0x51, 0x9a, 0x9, 0xe3, 0xfe, 0xc, 0x5c, 0x33, 0x94, 0x65, 0xee, 0x53, 0xc3, 0xf9, 0xed, 0xf9, 0x2f, 0xd0, 0x1f, 0x35})
}

//Test so that we are able to test that Ruby encrypts/signs the same way
func TestForKeyCreation(t *testing.T) {
key := "12345"
assert.Equal(t, getEncryptionKey(key), []byte{0x59, 0x94, 0x47, 0x1a, 0xbb, 0x1, 0x11, 0x2a, 0xfc, 0xc1, 0x81, 0x59, 0xf6, 0xcc, 0x74, 0xb4})
}
var _ = Describe("Encryption", func() {
var message = []byte("Super secret message that no one should read")

It("encrypts and decrypts with a 'long' key", func() {
key := "aaaaaaaaaaaaaaaa"
encrypted, err := Encrypt(key, message)
Expect(err).NotTo(HaveOccurred())

decrypted, err := Decrypt(key, encrypted)
Expect(err).NotTo(HaveOccurred())

Expect(decrypted).To(Equal(message))
Expect(encrypted).NotTo(Equal(message))
})

It("encrypts and decrypts with a 'short' key", func() {
key := "short key"
encrypted, err := Encrypt(key, message)
Expect(err).NotTo(HaveOccurred())

decrypted, err := Decrypt(key, encrypted)
Expect(err).NotTo(HaveOccurred())

Expect(decrypted).To(Equal(message))
Expect(encrypted).NotTo(Equal(message))
})

It("fails to decrypt with the wrong key", func() {
key := "short key"
encrypted, err := Encrypt(key, message)
Expect(err).NotTo(HaveOccurred())

_, err = Decrypt("wrong key", encrypted)
Expect(err).To(HaveOccurred())
})

It("is non-deterministic", func() {
key := "aaaaaaaaaaaaaaaa"
encrypted1, err := Encrypt(key, message)
Expect(err).NotTo(HaveOccurred())

encrypted2, err := Encrypt(key, message)
Expect(err).NotTo(HaveOccurred())

Expect(encrypted1).NotTo(Equal(encrypted2))
})

It("correctly computes a digest", func() {
Expect(DigestBytes([]byte("some-key"))).To(Equal([]byte{0x68, 0x2f, 0x66, 0x97, 0xfa, 0x93, 0xec, 0xa6, 0xc8, 0x1, 0xa2, 0x32, 0x51, 0x9a, 0x9, 0xe3, 0xfe, 0xc, 0x5c, 0x33, 0x94, 0x65, 0xee, 0x53, 0xc3, 0xf9, 0xed, 0xf9, 0x2f, 0xd0, 0x1f, 0x35}))
})

It("deterministically computes the actual encryption key", func() {
key := "12345"
Expect(getEncryptionKey(key)).To(Equal([]byte{0x59, 0x94, 0x47, 0x1a, 0xbb, 0x1, 0x11, 0x2a, 0xfc, 0xc1, 0x81, 0x59, 0xf6, 0xcc, 0x74, 0xb4}))
})
})

0 comments on commit 4f97b67

Please sign in to comment.