github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/repository/manager_test.go (about) 1 package repository 2 3 import ( 4 "os" 5 "path/filepath" 6 7 . "gopkg.in/check.v1" 8 ) 9 10 type CreationTests struct { 11 dir string 12 } 13 14 type Tests struct { 15 dir string 16 m *Manager 17 } 18 19 var _ = Suite(&CreationTests{}) 20 var _ = Suite(&Tests{}) 21 22 func (t *CreationTests) SetUpTest(c *C) { 23 t.dir = c.MkDir() 24 } 25 26 func (t *CreationTests) TestNew(c *C) { 27 m, err := NewManager(t.dir) 28 c.Assert(m, NotNil) 29 c.Assert(err, IsNil) 30 } 31 32 func (t *CreationTests) TestNewBadTarget(c *C) { 33 m, err := NewManager(filepath.Join(t.dir, "foo/bar")) 34 c.Assert(m, IsNil) 35 c.Assert(err, NotNil) 36 } 37 38 func (t *CreationTests) TestNewOnFile(c *C) { 39 const name = "test" 40 f, err := os.Create(filepath.Join(t.dir, name)) 41 c.Assert(err, IsNil) 42 f.Close() 43 m, err := NewManager(filepath.Join(t.dir, name)) 44 c.Assert(m, IsNil) 45 c.Assert(err, NotNil) 46 } 47 48 func (t *Tests) SetUpTest(c *C) { 49 t.dir = c.MkDir() 50 m, err := NewManager(t.dir) 51 c.Assert(err, IsNil) 52 t.m = m 53 } 54 55 func (t *Tests) TestList(c *C) { 56 e, err := t.m.ListNames() 57 c.Assert(err, IsNil) 58 c.Assert(e, DeepEquals, []string{}) 59 } 60 61 func (t *Tests) TestListExcludeFile(c *C) { 62 const name = "test" 63 f, err := os.Create(filepath.Join(t.dir, name)) 64 c.Assert(err, IsNil) 65 f.Close() 66 e, err := t.m.ListNames() 67 c.Assert(err, IsNil) 68 c.Assert(e, DeepEquals, []string{}) 69 } 70 71 func (t *Tests) TestListBadBasePath(c *C) { 72 // fake error condition for testing 73 t.m.basePath = "/dev/null/asdf" 74 e, err := t.m.ListNames() 75 c.Assert(err, NotNil) 76 c.Assert(e, IsNil) 77 } 78 79 func (t *Tests) TestCreate(c *C) { 80 err := t.m.Create("test", []byte("pubkey")) 81 c.Assert(err, IsNil) 82 e, err := t.m.ListNames() 83 c.Assert(err, IsNil) 84 c.Assert(e, DeepEquals, []string{"test"}) 85 } 86 87 func (t *Tests) TestOpen(c *C) { 88 t.m.Create("test", []byte("pubkey")) 89 r, err := t.m.Open("test") 90 c.Assert(err, IsNil) 91 c.Assert(r, FitsTypeOf, &Repository{}) 92 c.Assert(r.Path, Equals, filepath.Join(t.dir, "test")) 93 } 94 95 func (t *Tests) TestPubkey(c *C) { 96 expKey := []byte("01234567890123456789012345678901") 97 var arrExpKey [PublicKeySize]byte 98 copy(arrExpKey[:], expKey[:PublicKeySize]) 99 100 t.m.Create("test", expKey) 101 r, err := t.m.Open("test") 102 c.Assert(err, IsNil) 103 c.Assert(r, FitsTypeOf, &Repository{}) 104 key, err := r.keys.SigningPublicKey() 105 c.Assert(err, IsNil) 106 c.Assert(key, DeepEquals, arrExpKey) 107 } 108 109 func (t *Tests) TestOpenNonExisting(c *C) { 110 r, err := t.m.Open("test") 111 c.Assert(err, NotNil) 112 c.Assert(r, IsNil) 113 } 114 115 func (t *Tests) TestOpenNonDir(c *C) { 116 const name = "test" 117 f, err := os.Create(filepath.Join(t.dir, name)) 118 c.Assert(err, IsNil) 119 f.Close() 120 r, err := t.m.Open(name) 121 c.Assert(err, NotNil) 122 c.Assert(r, IsNil) 123 } 124 125 func (t *Tests) TestExists(c *C) { 126 const name = "test" 127 err := t.m.Create("test", []byte("pubkey")) 128 c.Assert(err, IsNil) 129 c.Assert(t.m.Exists(name), Equals, true) 130 } 131 132 func (t *Tests) TestDoesNotExist(c *C) { 133 const name = "test" 134 c.Assert(t.m.Exists(name), Equals, false) 135 }