-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathfsbackstore_test.go
105 lines (86 loc) · 2.98 KB
/
fsbackstore_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package asserts_test
import (
"os"
"path/filepath"
"syscall"
. "gopkg.in/check.v1"
"github.com/ubuntu-core/snappy/asserts"
)
type fsBackstoreSuite struct{}
var _ = Suite(&fsBackstoreSuite{})
func (fsbss *fsBackstoreSuite) TestOpenOK(c *C) {
// ensure umask is clean when creating the DB dir
oldUmask := syscall.Umask(0)
defer syscall.Umask(oldUmask)
topDir := filepath.Join(c.MkDir(), "asserts-db")
bs, err := asserts.OpenFSBackstore(topDir)
c.Check(err, IsNil)
c.Check(bs, NotNil)
info, err := os.Stat(filepath.Join(topDir, "asserts-v0"))
c.Assert(err, IsNil)
c.Assert(info.IsDir(), Equals, true)
c.Check(info.Mode().Perm(), Equals, os.FileMode(0775))
}
func (fsbss *fsBackstoreSuite) TestOpenCreateFail(c *C) {
parent := filepath.Join(c.MkDir(), "var")
topDir := filepath.Join(parent, "asserts-db")
// make it not writable
err := os.Mkdir(parent, 0555)
c.Assert(err, IsNil)
bs, err := asserts.OpenFSBackstore(topDir)
c.Assert(err, ErrorMatches, "failed to create assert storage root: .*")
c.Check(bs, IsNil)
}
func (fsbss *fsBackstoreSuite) TestOpenWorldWritableFail(c *C) {
topDir := filepath.Join(c.MkDir(), "asserts-db")
// make it world-writable
oldUmask := syscall.Umask(0)
os.MkdirAll(filepath.Join(topDir, "asserts-v0"), 0777)
syscall.Umask(oldUmask)
bs, err := asserts.OpenFSBackstore(topDir)
c.Assert(err, ErrorMatches, "assert storage root unexpectedly world-writable: .*")
c.Check(bs, IsNil)
}
func (fsbss *fsBackstoreSuite) TestPutOldRevision(c *C) {
topDir := filepath.Join(c.MkDir(), "asserts-db")
bs, err := asserts.OpenFSBackstore(topDir)
c.Assert(err, IsNil)
// Create two revisions of assertion.
a0, err := asserts.Decode([]byte("type: test-only\n" +
"authority-id: auth-id1\n" +
"primary-key: foo\n" +
"\n" +
"openpgp c2ln"))
c.Assert(err, IsNil)
a1, err := asserts.Decode([]byte("type: test-only\n" +
"authority-id: auth-id1\n" +
"primary-key: foo\n" +
"revision: 1\n" +
"\n" +
"openpgp c2ln"))
c.Assert(err, IsNil)
// Put newer revision, follwed by old revision.
err = bs.Put(asserts.TestOnlyType, a1)
c.Assert(err, IsNil)
err = bs.Put(asserts.TestOnlyType, a0)
c.Check(err, ErrorMatches,
`assertion added must have more recent revision than current one \(adding 0, currently 1\)`)
c.Check(err, DeepEquals, &asserts.SupersededRevisionError{Current: 1, Revision: 0})
}