Skip to content

Commit

Permalink
Add unit tests for config / fetcher / repo (theupdateframework#44)
Browse files Browse the repository at this point in the history
* Add unit test for metadata config

This change adds tests for initializing metadata config

Signed-off-by: Ivana Atanasova <iyovcheva@vmware.com>

* Add unit tests for Fetcher

This change tests the DownloadFile functionality of Fetcher covering
both use-cases of failed download and a successful root role
download of a real test repo

Signed-off-by: Ivana Atanasova <iyovcheva@vmware.com>

* Add unit tests for repository type

This change adds test coverage for repository checking that all
roles once set are as expected

Signed-off-by: Ivana Atanasova <iyovcheva@vmware.com>

---------

Signed-off-by: Ivana Atanasova <iyovcheva@vmware.com>
  • Loading branch information
ivanayov authored and rdimitrov committed Jan 25, 2024
1 parent 6ea63a0 commit 439d902
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 0 deletions.
90 changes: 90 additions & 0 deletions metadata/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2023 VMware, Inc.
//
// This product is licensed to you under the BSD-2 license (the "License").
// You may not use this product except in compliance with the BSD-2 License.
// This product may include a number of subcomponents with separate copyright
// notices and license terms. Your use of these subcomponents is subject to
// the terms and conditions of the subcomponent's license, as noted in the
// LICENSE file.
//
// SPDX-License-Identifier: BSD-2-Clause

package config

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewUpdaterConfig(t *testing.T) {

remoteURL := "somepath"
rootBytes := []byte("somerootbytes")

updaterConfig, err := New(remoteURL, rootBytes)

assert.NoError(t, err)
assert.NotNil(t, updaterConfig)
assert.NotNil(t, updaterConfig.Fetcher)

assert.Equal(t, 32, updaterConfig.MaxDelegations)
assert.Equal(t, int64(32), updaterConfig.MaxRootRotations)
assert.Equal(t, int64(512000), updaterConfig.RootMaxLength)
assert.Equal(t, int64(16384), updaterConfig.TimestampMaxLength)
assert.Equal(t, int64(2000000), updaterConfig.SnapshotMaxLength)
assert.Equal(t, int64(5000000), updaterConfig.TargetsMaxLength)
assert.Equal(t, false, updaterConfig.DisableLocalCache)
assert.Equal(t, true, updaterConfig.PrefixTargetsWithHash)
assert.Equal(t, updaterConfig.RemoteMetadataURL, remoteURL)
assert.Equal(t, updaterConfig.LocalTrustedRoot, rootBytes)
assert.Equal(t, updaterConfig.RemoteTargetsURL, remoteURL+"/targets")
assert.Empty(t, updaterConfig.LocalMetadataDir)
assert.Empty(t, updaterConfig.LocalTargetsDir)
}

func TestEnsurePathsExist(t *testing.T) {

remoteURL := "somepath"
rootBytes := []byte("somerootbytes")

updaterConfig, err := New(remoteURL, rootBytes)
assert.NoError(t, err)
assert.NotNil(t, updaterConfig)

err = updaterConfig.EnsurePathsExist()
assert.Error(t, err, "mkdir : no such file or directory")

tmp := os.TempDir()
metadataPath := fmt.Sprintf("%s/metadata", tmp)
targetsPath := fmt.Sprintf("%s/targets", tmp)

assert.NoDirExists(t, metadataPath)
assert.NoDirExists(t, targetsPath)

updaterConfig.LocalMetadataDir = metadataPath
updaterConfig.LocalTargetsDir = targetsPath

updaterConfig.DisableLocalCache = true
err = updaterConfig.EnsurePathsExist()
assert.NoError(t, err)
assert.NoDirExists(t, metadataPath)
assert.NoDirExists(t, targetsPath)

updaterConfig.DisableLocalCache = false
err = updaterConfig.EnsurePathsExist()
assert.NoError(t, err)

assert.DirExists(t, metadataPath)
assert.DirExists(t, targetsPath)

err = os.RemoveAll(metadataPath)
assert.NoError(t, err)
assert.NoDirExists(t, metadataPath)

err = os.RemoveAll(targetsPath)
assert.NoError(t, err)
assert.NoDirExists(t, targetsPath)
}
62 changes: 62 additions & 0 deletions metadata/fetcher/fetcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2023 VMware, Inc.
//
// This product is licensed to you under the BSD-2 license (the "License").
// You may not use this product except in compliance with the BSD-2 License.
// This product may include a number of subcomponents with separate copyright
// notices and license terms. Your use of these subcomponents is subject to
// the terms and conditions of the subcomponent's license, as noted in the
// LICENSE file.
//
// SPDX-License-Identifier: BSD-2-Clause

package fetcher

import (
"errors"
"testing"
"time"

"github.com/rdimitrov/go-tuf-metadata/metadata"
"github.com/stretchr/testify/assert"
)

const (
exampleURL = "https://example.com/metadata/"
realUrl = "https://jku.github.io/tuf-demo/metadata/1.root.json"
)

func TestDownloadFileWithExampleUrl(t *testing.T) {
fetcher := DefaultFetcher{httpUserAgent: ""}

fetcher.httpUserAgent = "someUserAgent"

data, err := fetcher.DownloadFile(exampleURL, 34)
if assert.NotNil(t, err) {
if assert.IsType(t, metadata.ErrDownloadHTTP{}, err) {
var checkErr metadata.ErrDownloadHTTP
if errors.As(err, &checkErr) {
assert.NotEqual(t, 200, checkErr.StatusCode)
assert.Equal(t, 404, checkErr.StatusCode)
}
}
}
assert.Empty(t, data)
}

func TestDownloadFileWithRealURL(t *testing.T) {
fetcher := DefaultFetcher{httpUserAgent: ""}

data, err := fetcher.DownloadFile(realUrl, 3000)
assert.Nil(t, err)
assert.NotEmpty(t, data)

now := time.Now().UTC()
safeExpiry := now.Truncate(time.Second).AddDate(0, 0, 30)
mdRoot := metadata.Root(safeExpiry)
err = mdRoot.UnmarshalJSON(data)

assert.Nil(t, err)
assert.Equal(t, mdRoot.Signed.Type, metadata.ROOT)
assert.Equal(t, mdRoot.Signed.Version, int64(1))
assert.LessOrEqual(t, mdRoot.Signed.SpecVersion, metadata.SPECIFICATION_VERSION)
}
52 changes: 52 additions & 0 deletions metadata/repository/repository_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2023 VMware, Inc.
//
// This product is licensed to you under the BSD-2 license (the "License").
// You may not use this product except in compliance with the BSD-2 License.
// This product may include a number of subcomponents with separate copyright
// notices and license terms. Your use of these subcomponents is subject to
// the terms and conditions of the subcomponent's license, as noted in the
// LICENSE file.
//
// SPDX-License-Identifier: BSD-2-Clause

package repository

import (
"testing"
"time"

"github.com/rdimitrov/go-tuf-metadata/metadata"
"github.com/stretchr/testify/assert"
)

func TestNewRepository(t *testing.T) {
repo := New()

now := time.Now().UTC()
safeExpiry := now.Truncate(time.Second).AddDate(0, 0, 30)

root := metadata.Root(safeExpiry)
repo.SetRoot(root)
assert.Equal(t, "root", repo.Root().Signed.Type)
assert.Equal(t, int64(1), repo.Root().Signed.Version)
assert.Equal(t, metadata.SPECIFICATION_VERSION, repo.Root().Signed.SpecVersion)

targets := metadata.Targets(safeExpiry)
repo.SetTargets("targets", targets)
assert.Equal(t, "targets", repo.Targets("targets").Signed.Type)
assert.Equal(t, int64(1), repo.Targets("targets").Signed.Version)
assert.Equal(t, metadata.SPECIFICATION_VERSION, repo.Targets("targets").Signed.SpecVersion)

timestamp := metadata.Timestamp(safeExpiry)
repo.SetTimestamp(timestamp)
// repo.SetRoot(root)
assert.Equal(t, "timestamp", repo.Timestamp().Signed.Type)
assert.Equal(t, int64(1), repo.Timestamp().Signed.Version)
assert.Equal(t, metadata.SPECIFICATION_VERSION, repo.Timestamp().Signed.SpecVersion)

snapshot := metadata.Snapshot(safeExpiry)
repo.SetSnapshot(snapshot)
assert.Equal(t, "snapshot", repo.Snapshot().Signed.Type)
assert.Equal(t, int64(1), repo.Snapshot().Signed.Version)
assert.Equal(t, metadata.SPECIFICATION_VERSION, repo.Snapshot().Signed.SpecVersion)
}

0 comments on commit 439d902

Please sign in to comment.