github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/repository/authorization_test.go (about)

     1  package repository
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/rand"
     6  
     7  	. "gopkg.in/check.v1"
     8  
     9  	"github.com/hoffie/larasync/repository/odf"
    10  )
    11  
    12  type AuthorizationTest struct {
    13  	SigningKey    [PrivateKeySize]byte
    14  	EncryptionKey [EncryptionKeySize]byte
    15  	HashingKey    [HashingKeySize]byte
    16  }
    17  
    18  var _ = Suite(&AuthorizationTest{})
    19  
    20  func (t *AuthorizationTest) SetUpTest(c *C) {
    21  	t.SigningKey = [PrivateKeySize]byte{}
    22  	t.EncryptionKey = [EncryptionKeySize]byte{}
    23  
    24  	privateKeyBytes := make([]byte, PrivateKeySize)
    25  	_, err := rand.Read(privateKeyBytes)
    26  	c.Assert(err, IsNil)
    27  
    28  	encryptionKeyBytes := make([]byte, EncryptionKeySize)
    29  	_, err = rand.Read(encryptionKeyBytes)
    30  	c.Assert(err, IsNil)
    31  
    32  	hashingKeyBytes := make([]byte, HashingKeySize)
    33  	_, err = rand.Read(hashingKeyBytes)
    34  	c.Assert(err, IsNil)
    35  
    36  	copy(t.SigningKey[:], privateKeyBytes[0:PrivateKeySize])
    37  	copy(t.EncryptionKey[:], encryptionKeyBytes[0:EncryptionKeySize])
    38  	copy(t.HashingKey[:], hashingKeyBytes[0:HashingKeySize])
    39  }
    40  
    41  func (t *AuthorizationTest) getAuthorization() *Authorization {
    42  	return &Authorization{
    43  		SigningKey:    t.SigningKey,
    44  		EncryptionKey: t.EncryptionKey,
    45  		HashingKey:    t.HashingKey,
    46  	}
    47  }
    48  
    49  func (t *AuthorizationTest) getPbAuthorization() *odf.Authorization {
    50  	return &odf.Authorization{
    51  		SigningKey:    t.SigningKey[:],
    52  		EncryptionKey: t.EncryptionKey[:],
    53  		HashingKey:    t.HashingKey[:],
    54  	}
    55  }
    56  
    57  func (t *AuthorizationTest) TestConversionToPb(c *C) {
    58  	authorization := t.getAuthorization()
    59  	pbAuthorization, err := authorization.toPb()
    60  	c.Assert(err, IsNil)
    61  
    62  	assertEqualAuthorizationRepresentation(c, authorization, pbAuthorization)
    63  }
    64  
    65  func (t *AuthorizationTest) TestConversionFromPb(c *C) {
    66  	pbAuthorization := t.getPbAuthorization()
    67  	authorization := newAuthorizationFromPb(pbAuthorization)
    68  	assertEqualAuthorizationRepresentation(c, authorization, pbAuthorization)
    69  }
    70  
    71  func assertEqualAuthorizationRepresentation(
    72  	c *C,
    73  	authorization *Authorization,
    74  	pbAuthorization *odf.Authorization,
    75  ) {
    76  	c.Assert(
    77  		authorization.SigningKey[:],
    78  		DeepEquals,
    79  		pbAuthorization.GetSigningKey(),
    80  	)
    81  	c.Assert(
    82  		authorization.EncryptionKey[:],
    83  		DeepEquals,
    84  		pbAuthorization.GetEncryptionKey(),
    85  	)
    86  	c.Assert(
    87  		authorization.HashingKey[:],
    88  		DeepEquals,
    89  		pbAuthorization.GetHashingKey(),
    90  	)
    91  }
    92  
    93  func assertEqualAuthorizations(
    94  	c *C,
    95  	authorization *Authorization,
    96  	otherAuthorization *Authorization,
    97  ) {
    98  	c.Assert(
    99  		authorization.SigningKey,
   100  		DeepEquals,
   101  		otherAuthorization.SigningKey,
   102  	)
   103  
   104  	c.Assert(
   105  		authorization.EncryptionKey,
   106  		DeepEquals,
   107  		otherAuthorization.EncryptionKey,
   108  	)
   109  
   110  	c.Assert(
   111  		authorization.HashingKey,
   112  		DeepEquals,
   113  		otherAuthorization.HashingKey,
   114  	)
   115  }
   116  
   117  func (t *AuthorizationTest) TestReadFrom(c *C) {
   118  	authorization := t.getAuthorization()
   119  	buffer := &bytes.Buffer{}
   120  	_, err := authorization.WriteTo(buffer)
   121  	c.Assert(err, IsNil)
   122  
   123  	otherAuth := &Authorization{}
   124  	_, err = otherAuth.ReadFrom(buffer)
   125  	c.Assert(err, IsNil)
   126  
   127  	assertEqualAuthorizations(c, authorization, otherAuth)
   128  }
   129  
   130  func (t *AuthorizationTest) TestReadFromError(c *C) {
   131  	authorization := t.getAuthorization()
   132  	buffer := &bytes.Buffer{}
   133  	_, err := authorization.WriteTo(buffer)
   134  	c.Assert(err, IsNil)
   135  
   136  	data := buffer.Bytes()
   137  	copy(data, make([]byte, len(data)))
   138  
   139  	otherAuth := &Authorization{}
   140  	_, err = otherAuth.ReadFrom(buffer)
   141  	c.Assert(err, NotNil)
   142  }