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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
package sources
import (
"context"
"log"
"net/url"
"os"
"path"
"path/filepath"
"testing"
incus "github.com/lxc/incus/v6/shared/util"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"github.com/lxc/distrobuilder/v3/shared"
)
func TestVerifyFile(t *testing.T) {
t.Skip("Skipping test that accesses the Internet")
wd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to retrieve working directory: %v", err)
}
testdataDir := filepath.Join(wd, "..", "testdata")
keys := []string{"0x5DE8949A899C8D99"}
keyserver := "keyserver.ubuntu.com"
tests := []struct {
name string
signedFile string
signatureFile string
keys []string
keyserver string
shouldFail bool
}{
{
"testfile with detached signature",
filepath.Join(testdataDir, "testfile"),
filepath.Join(testdataDir, "testfile.sig"),
keys,
keyserver,
false,
},
{
"testfile with cleartext signature",
filepath.Join(testdataDir, "testfile.asc"),
"",
keys,
keyserver,
false,
},
{
"testfile with invalid cleartext signature",
filepath.Join(testdataDir, "testfile-invalid.asc"),
"",
keys,
keyserver,
true,
},
{
"testfile with normal signature",
filepath.Join(testdataDir, "testfile.gpg"),
"",
keys,
keyserver,
false,
},
{
"no keys",
filepath.Join(testdataDir, "testfile"),
filepath.Join(testdataDir, "testfile.sig"),
[]string{},
keyserver,
true,
},
{
"invalid key",
filepath.Join(testdataDir, "testfile.asc"),
"",
[]string{"0x46181433FBB75451"},
keyserver,
true,
},
}
c := common{
sourcesDir: os.TempDir(),
definition: shared.Definition{
Source: shared.DefinitionSource{},
},
ctx: context.TODO(),
}
for i, tt := range tests {
log.Printf("Running test #%d: %s", i, tt.name)
c.definition = shared.Definition{
Source: shared.DefinitionSource{
Keyserver: tt.keyserver,
Keys: tt.keys,
},
}
valid, err := c.VerifyFile(tt.signedFile, tt.signatureFile)
if tt.shouldFail {
require.Error(t, err)
require.False(t, valid)
} else {
require.NoError(t, err)
require.True(t, valid)
}
}
}
func TestCreateGPGKeyring(t *testing.T) {
t.Skip("Skipping test that accesses the Internet")
c := common{
sourcesDir: os.TempDir(),
definition: shared.Definition{
Source: shared.DefinitionSource{
Keyserver: "keyserver.ubuntu.com",
Keys: []string{"0x5DE8949A899C8D99"},
},
},
ctx: context.TODO(),
}
keyring, err := c.CreateGPGKeyring()
require.NoError(t, err)
require.FileExists(t, keyring)
os.RemoveAll(path.Dir(keyring))
c.definition = shared.Definition{}
// This shouldn't fail, but the keyring file should not be created since
// there are no keys to be exported.
keyring, err = c.CreateGPGKeyring()
require.NoError(t, err)
require.False(t, incus.PathExists(keyring), "File should not exist")
os.RemoveAll(path.Dir(keyring))
}
func TestValidateGPGRequirements(t *testing.T) {
tests := []struct {
name string
url string
keys []string
skipVerification bool
expectError bool
expectSkipVerify bool
}{
{
name: "keys provided with https",
url: "https://example.com/file",
keys: []string{"0x12345678"},
skipVerification: false,
expectError: false,
expectSkipVerify: false, // Keys provided, always verify
},
{
name: "keys provided with http",
url: "http://example.com/file",
keys: []string{"0x12345678"},
skipVerification: false,
expectError: false,
expectSkipVerify: false, // Keys provided, always verify
},
{
name: "http without keys",
url: "http://example.com/file",
keys: []string{},
skipVerification: false,
expectError: true, // HTTP requires GPG keys
expectSkipVerify: false,
},
{
name: "https without keys and skip false",
url: "https://example.com/file",
keys: []string{},
skipVerification: false,
expectError: false,
expectSkipVerify: true, // Should be set to true with warning
},
{
name: "https without keys and skip true",
url: "https://example.com/file",
keys: []string{},
skipVerification: true,
expectError: false,
expectSkipVerify: true, // Remains true
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logger := logrus.New()
logger.SetOutput(os.Stdout)
c := common{
logger: logger,
definition: shared.Definition{
Source: shared.DefinitionSource{
Keys: tt.keys,
SkipVerification: tt.skipVerification,
},
},
ctx: context.TODO(),
}
u, err := url.Parse(tt.url)
require.NoError(t, err)
skip, err := c.validateGPGRequirements(u)
if tt.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, tt.expectSkipVerify, skip)
}
})
}
}
|