Skip to content

Commit 91c85a0

Browse files
authored
test: add tests for rollback protection on snapshot, targets, delegations (#450)
* test: add tests for rollback protection Signed-off-by: Asra Ali <[email protected]> * golangci-lint Signed-off-by: Asra Ali <[email protected]> Signed-off-by: Asra Ali <[email protected]>
1 parent 2b21357 commit 91c85a0

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

client/client_test.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,125 @@ func (s *ClientSuite) TestUpdateHTTP(c *C) {
12231223
}
12241224
}
12251225

1226+
// TestRollbackSnapshot tests a rollback version of snapshot.
1227+
func (s *ClientSuite) TestRollbackSnapshot(c *C) {
1228+
client := s.updatedClient(c)
1229+
1230+
// generate a new snapshot & timestamp v2 and sync with the client
1231+
version := client.snapshotVer
1232+
c.Assert(version > 0, Equals, true)
1233+
c.Assert(s.repo.Snapshot(), IsNil)
1234+
c.Assert(s.repo.Timestamp(), IsNil)
1235+
c.Assert(s.repo.Commit(), IsNil)
1236+
s.syncRemote(c)
1237+
_, err := client.Update()
1238+
c.Assert(err, IsNil)
1239+
c.Assert(client.snapshotVer > version, Equals, true)
1240+
1241+
// replace remote snapshot.json with old version and timestamp again.
1242+
s.repo.SetSnapshotVersion(version)
1243+
c.Assert(s.repo.Snapshot(), IsNil)
1244+
c.Assert(s.repo.Timestamp(), IsNil)
1245+
c.Assert(s.repo.Commit(), IsNil)
1246+
s.syncRemote(c)
1247+
1248+
// check update returns ErrLowVersion
1249+
_, err = client.Update()
1250+
1251+
c.Assert(err, DeepEquals, verify.ErrLowVersion{
1252+
Actual: version,
1253+
Current: client.snapshotVer,
1254+
})
1255+
}
1256+
1257+
func (s *ClientSuite) TestRollbackTopLevelTargets(c *C) {
1258+
client := s.updatedClient(c)
1259+
1260+
// generate a new targets and sync with the client
1261+
version := client.targetsVer
1262+
c.Assert(version > 0, Equals, true)
1263+
s.addRemoteTarget(c, "bar.txt")
1264+
_, err := client.Update()
1265+
c.Assert(err, IsNil)
1266+
c.Assert(client.targetsVer > version, Equals, true)
1267+
1268+
// replace remote snapshot.json with old version and timestamp again.
1269+
s.repo.SetTargetsVersion(version)
1270+
c.Assert(s.repo.Snapshot(), IsNil)
1271+
c.Assert(s.repo.Timestamp(), IsNil)
1272+
c.Assert(s.repo.Commit(), IsNil)
1273+
s.syncRemote(c)
1274+
1275+
// check update returns ErrLowVersion
1276+
_, err = client.Update()
1277+
c.Assert(err, DeepEquals, verify.ErrLowVersion{
1278+
Actual: version,
1279+
Current: client.targetsVer,
1280+
})
1281+
}
1282+
1283+
func (s *ClientSuite) TestRollbackDelegatedTargets(c *C) {
1284+
client := s.updatedClient(c)
1285+
// add a delegation
1286+
signer, err := keys.GenerateEd25519Key()
1287+
c.Assert(err, IsNil)
1288+
role := data.DelegatedRole{
1289+
Name: "role",
1290+
KeyIDs: signer.PublicData().IDs(),
1291+
Paths: []string{"bar.txt", "baz.txt"},
1292+
Threshold: 1,
1293+
}
1294+
s.store.SaveSigner("role", signer)
1295+
s.repo.AddDelegatedRole("targets", role, []*data.PublicKey{signer.PublicData()})
1296+
s.repo.AddTargetToPreferredRole("bar.txt", nil, "role")
1297+
c.Assert(s.repo.Snapshot(), IsNil)
1298+
c.Assert(s.repo.Timestamp(), IsNil)
1299+
c.Assert(s.repo.Commit(), IsNil)
1300+
s.syncRemote(c)
1301+
1302+
// save v1 delegation
1303+
meta, err := s.store.GetMeta()
1304+
c.Assert(err, IsNil)
1305+
oldRole, ok := meta["role.json"]
1306+
if !ok {
1307+
c.Fatal("missing role.json")
1308+
}
1309+
// update client and verify download delegated target
1310+
_, err = client.Update()
1311+
c.Assert(err, IsNil)
1312+
var dest testDestination
1313+
c.Assert(client.Download("bar.txt", &dest), IsNil)
1314+
1315+
// update delegation to v2
1316+
s.repo.AddTargetToPreferredRole("baz.txt", nil, "role")
1317+
c.Assert(s.repo.Snapshot(), IsNil)
1318+
c.Assert(s.repo.Timestamp(), IsNil)
1319+
c.Assert(s.repo.Commit(), IsNil)
1320+
s.syncRemote(c)
1321+
1322+
// update client and verify download v2 delegated target
1323+
_, err = client.Update()
1324+
c.Assert(err, IsNil)
1325+
c.Assert(dest.Delete(), IsNil)
1326+
c.Assert(client.Download("baz.txt", &dest), IsNil)
1327+
1328+
// rollback role.json version.
1329+
c.Assert(s.store.SetMeta("role.json", oldRole), IsNil)
1330+
repo, err := tuf.NewRepo(s.store)
1331+
c.Assert(err, IsNil)
1332+
c.Assert(repo.Snapshot(), IsNil)
1333+
c.Assert(repo.Timestamp(), IsNil)
1334+
c.Assert(repo.Commit(), IsNil)
1335+
s.syncRemote(c)
1336+
1337+
// check update returns ErrLowVersion
1338+
_, err = client.Update()
1339+
c.Assert(err, DeepEquals, verify.ErrLowVersion{
1340+
Actual: 1,
1341+
Current: 2,
1342+
})
1343+
}
1344+
12261345
type testDestination struct {
12271346
bytes.Buffer
12281347
deleted bool

0 commit comments

Comments
 (0)