github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/helpers/crypto/box_test.go (about)

     1  package crypto
     2  
     3  import (
     4  	"crypto/rand"
     5  
     6  	. "gopkg.in/check.v1"
     7  )
     8  
     9  type TestBox struct {
    10  	encKey [EncryptionKeySize]byte
    11  }
    12  
    13  var _ = Suite(&TestBox{})
    14  
    15  func (t *TestBox) SetUpTest(c *C) {
    16  	t.encKey = [EncryptionKeySize]byte{}
    17  	_, err := rand.Read(t.encKey[:])
    18  	c.Assert(err, IsNil)
    19  }
    20  
    21  func (t *TestBox) getBox() *Box {
    22  	return NewBox(t.encKey)
    23  }
    24  
    25  func (t *TestBox) TestEncryptionDecryption(c *C) {
    26  	testData := []byte("This is testdata")
    27  	encrypted, err := t.getBox().EncryptWithRandomKey(testData)
    28  	c.Assert(err, IsNil)
    29  
    30  	decrypted, err := t.getBox().DecryptContent(encrypted)
    31  	c.Assert(err, IsNil)
    32  
    33  	c.Assert(testData, DeepEquals, decrypted)
    34  }
    35  
    36  func (t *TestBox) TestDecryptionUnderMinimalLength(c *C) {
    37  	testData := []byte{}
    38  	_, err := t.getBox().DecryptContent(testData)
    39  
    40  	c.Assert(err, NotNil)
    41  }
    42  
    43  func (t *TestBox) TestDecryptionOtherKey(c *C) {
    44  	testData := []byte("This is testdata")
    45  	encrypted, err := t.getBox().EncryptWithRandomKey(testData)
    46  	c.Assert(err, IsNil)
    47  
    48  	t.encKey = [EncryptionKeySize]byte{}
    49  
    50  	_, err = t.getBox().DecryptContent(encrypted)
    51  
    52  	c.Assert(err, NotNil)
    53  }