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

     1  package repository
     2  
     3  import (
     4  	"crypto/rand"
     5  	"encoding/hex"
     6  	"os"
     7  
     8  	. "gopkg.in/check.v1"
     9  
    10  	edhelpers "github.com/hoffie/larasync/helpers/ed25519"
    11  	"github.com/hoffie/larasync/repository/content"
    12  )
    13  
    14  type AuthorizationManagerTests struct {
    15  	dir                 string
    16  	am                  *AuthorizationManager
    17  	signaturePrivateKey [PrivateKeySize]byte
    18  	encryptionKey       [EncryptionKeySize]byte
    19  }
    20  
    21  var _ = Suite(&AuthorizationManagerTests{})
    22  
    23  func (t *AuthorizationManagerTests) SetUpTest(c *C) {
    24  	t.dir = c.MkDir()
    25  	fileStorage := content.NewFileStorage(t.dir)
    26  	t.am = newAuthorizationManager(fileStorage)
    27  	rand.Read(t.encryptionKey[:])
    28  	rand.Read(t.signaturePrivateKey[:])
    29  }
    30  
    31  func (t *AuthorizationManagerTests) signaturePublicKey() [PublicKeySize]byte {
    32  	return edhelpers.GetPublicKeyFromPrivate(t.signaturePrivateKey)
    33  }
    34  
    35  func (t *AuthorizationManagerTests) signaturePublicKeyString() string {
    36  	pubKey := t.signaturePublicKey()
    37  	return hex.EncodeToString(pubKey[:])
    38  }
    39  
    40  func (t *AuthorizationManagerTests) testAuthorization() *Authorization {
    41  	auth := &Authorization{
    42  		SigningKey:    [PrivateKeySize]byte{},
    43  		EncryptionKey: [EncryptionKeySize]byte{},
    44  		HashingKey:    [HashingKeySize]byte{},
    45  	}
    46  
    47  	rand.Read(auth.EncryptionKey[:])
    48  	rand.Read(auth.SigningKey[:])
    49  	rand.Read(auth.HashingKey[:])
    50  
    51  	return auth
    52  }
    53  
    54  func (t *AuthorizationManagerTests) addAuthorization(c *C, auth *Authorization) {
    55  	err := t.am.Set(
    56  		t.signaturePublicKey(),
    57  		t.encryptionKey,
    58  		auth,
    59  	)
    60  	c.Assert(err, IsNil)
    61  }
    62  
    63  func (t *AuthorizationManagerTests) TestGet(c *C) {
    64  	auth := t.testAuthorization()
    65  	t.addAuthorization(c, auth)
    66  
    67  	retrievedAuth, err := t.am.Get(t.signaturePublicKey(), t.encryptionKey)
    68  	c.Assert(err, IsNil)
    69  
    70  	c.Assert(auth.EncryptionKey, DeepEquals, retrievedAuth.EncryptionKey)
    71  	c.Assert(auth.HashingKey, DeepEquals, retrievedAuth.HashingKey)
    72  	c.Assert(auth.SigningKey, DeepEquals, retrievedAuth.SigningKey)
    73  }
    74  
    75  func (t *AuthorizationManagerTests) TestGetDecryptionError(c *C) {
    76  	auth := t.testAuthorization()
    77  	t.addAuthorization(c, auth)
    78  
    79  	_, err := t.am.Get(t.signaturePublicKey(), [EncryptionKeySize]byte{})
    80  	c.Assert(err, NotNil)
    81  }
    82  
    83  func (t *AuthorizationManagerTests) TestGetNotFound(c *C) {
    84  	_, err := t.am.Get(t.signaturePublicKey(), t.encryptionKey)
    85  	c.Assert(os.IsNotExist(err), Equals, true)
    86  }
    87  
    88  func (t *AuthorizationManagerTests) TestGetReaderString(c *C) {
    89  	auth := t.testAuthorization()
    90  	t.addAuthorization(c, auth)
    91  
    92  	reader, err := t.am.GetReaderString(t.signaturePublicKeyString())
    93  	defer reader.Close()
    94  	c.Assert(err, IsNil)
    95  }
    96  
    97  func (t *AuthorizationManagerTests) TestGetReaderDecodeError(c *C) {
    98  	auth := t.testAuthorization()
    99  	t.addAuthorization(c, auth)
   100  
   101  	_, err := t.am.GetReaderString("äöü")
   102  	c.Assert(err, NotNil)
   103  }
   104  
   105  func (t *AuthorizationManagerTests) TestGetReaderLengthError(c *C) {
   106  	auth := t.testAuthorization()
   107  	t.addAuthorization(c, auth)
   108  
   109  	pubKey := t.signaturePublicKeyString()
   110  
   111  	_, err := t.am.GetReaderString(pubKey[:len(pubKey)-2])
   112  	c.Assert(err, NotNil)
   113  }
   114  
   115  func (t *AuthorizationManagerTests) TestExistsNegative(c *C) {
   116  	c.Assert(t.am.Exists(t.signaturePublicKey()), Equals, false)
   117  }
   118  
   119  func (t *AuthorizationManagerTests) TestExistsPositive(c *C) {
   120  	auth := t.testAuthorization()
   121  	t.addAuthorization(c, auth)
   122  
   123  	c.Assert(t.am.Exists(t.signaturePublicKey()), Equals, true)
   124  }
   125  
   126  func (t *AuthorizationManagerTests) TestExistsForStringNegative(c *C) {
   127  	c.Assert(t.am.ExistsForString(t.signaturePublicKeyString()), Equals, false)
   128  }
   129  
   130  func (t *AuthorizationManagerTests) TestExistsForStringPositive(c *C) {
   131  	auth := t.testAuthorization()
   132  	t.addAuthorization(c, auth)
   133  
   134  	c.Assert(t.am.ExistsForString(t.signaturePublicKeyString()), Equals, true)
   135  }
   136  
   137  func (t *AuthorizationManagerTests) TestDelete(c *C) {
   138  	auth := t.testAuthorization()
   139  	t.addAuthorization(c, auth)
   140  	err := t.am.Delete(t.signaturePublicKey())
   141  
   142  	c.Assert(err, IsNil)
   143  	c.Assert(t.am.Exists(t.signaturePublicKey()), Equals, false)
   144  }
   145  
   146  func (t *AuthorizationManagerTests) TestDeleteError(c *C) {
   147  	err := t.am.Delete(t.signaturePublicKey())
   148  	c.Assert(os.IsNotExist(err), Equals, true)
   149  }
   150  
   151  func (t *AuthorizationManagerTests) TestDeleteString(c *C) {
   152  	auth := t.testAuthorization()
   153  	t.addAuthorization(c, auth)
   154  	err := t.am.DeleteForString(t.signaturePublicKeyString())
   155  
   156  	c.Assert(err, IsNil)
   157  	c.Assert(t.am.Exists(t.signaturePublicKey()), Equals, false)
   158  }
   159  
   160  func (t *AuthorizationManagerTests) TestDeleteForStringError(c *C) {
   161  	err := t.am.DeleteForString(t.signaturePublicKeyString())
   162  	c.Assert(os.IsNotExist(err), Equals, true)
   163  }
   164  
   165  func (t *AuthorizationManagerTests) TestSet(c *C) {
   166  	auth := t.testAuthorization()
   167  	err := t.am.Set(
   168  		t.signaturePublicKey(),
   169  		t.encryptionKey,
   170  		auth,
   171  	)
   172  	c.Assert(err, IsNil)
   173  }