Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index pidx load #394

Merged
merged 12 commits into from
Dec 5, 2024
9 changes: 7 additions & 2 deletions cmd/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,18 @@ Add a pack using the following "<pack>" specification or using packs provided by
The file can be a local file or a file hosted somewhere else on the Internet.
If it's hosted somewhere, cpackget will first download it then extract all pack files into "CMSIS_PACK_ROOT/<vendor>/<packName>/<x.y.z>/"
If "-f" is used, cpackget will call "cpackget pack add" on each URL specified in the <packs list> file.`,
Args: cobra.MinimumNArgs(0),
PersistentPreRunE: configureInstaller,
Args: cobra.MinimumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {

utils.SetEncodedProgress(addCmdFlags.encodedProgress)
utils.SetSkipTouch(addCmdFlags.skipTouch)

createPackRoot = true
err := configureInstaller(cmd, args)
if err != nil {
return err
}

if addCmdFlags.packsListFileName != "" {
log.Infof("Parsing packs urls via file %v", addCmdFlags.packsListFileName)

Expand Down
2 changes: 1 addition & 1 deletion cmd/commands/checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var checksumCreateCmdTests = []TestCase{
{
name: "test using nonexisting hash function",
args: []string{"checksum-create", "Vendor.Pack.1.2.3.pack", "-a", "sha1"},
expectedErr: errors.New("provided hash function is not supported"),
expectedErr: errs.ErrHashNotSupported,
setUpFunc: func(t *TestCase) {
f, _ := os.Create("Vendor.Pack.1.2.3.pack.sha256.checksum")
f.Close()
Expand Down
15 changes: 9 additions & 6 deletions cmd/commands/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package commands_test

import (
"errors"
"testing"

errs "github.com/open-cmsis-pack/cpackget/cmd/errors"
"github.com/open-cmsis-pack/cpackget/cmd/installer"
)

var (
Expand All @@ -25,9 +27,10 @@ var connectionCmdTests = []TestCase{
expectedErr: nil,
},
{
name: "test checking invalid url",
args: []string{"connection", wrongURLPath},
expectedErr: errors.New("remote server is offline or cannot be reached"),
name: "test checking invalid url",
args: []string{"connection", wrongURLPath},
expectedErr: errs.ErrOffline,
expErrUnwwrap: true,
},

{ // set up environment for next test
Expand All @@ -36,8 +39,8 @@ var connectionCmdTests = []TestCase{
noCleanup: true,
setUpFunc: func(t *TestCase) {
server := NewServer()
t.args = append(t.args, server.URL()+"index.pidx")
server.AddRoute("index.pidx", []byte(`<?xml version="1.0" encoding="UTF-8" ?>
t.args = append(t.args, server.URL()+installer.PublicIndex)
server.AddRoute(installer.PublicIndex, []byte(`<?xml version="1.0" encoding="UTF-8" ?>
<index schemaVersion="1.1.0" xs:noNamespaceSchemaLocation="PackIndex.xsd" xmlns:xs="https://www.w3.org/2001/XMLSchema-instance">
<vendor>TheVendor</vendor>
<url>https://www.keil.com/</url>
Expand Down
15 changes: 8 additions & 7 deletions cmd/commands/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
"testing"

errs "github.com/open-cmsis-pack/cpackget/cmd/errors"
"github.com/open-cmsis-pack/cpackget/cmd/installer"
)

// Tests for init command are placed here because there was something wrong
// while putting them into a file init_test.go

var (
pidxFilePath = filepath.Join(testingDir, "SamplePublicIndex.pidx")
notFoundPidxFilePath = filepath.Join("path", "to", "index.pidx")
notFoundPidxFilePath = filepath.Join("path", "to", installer.PublicIndex)
)

var initCmdTests = []TestCase{
Expand All @@ -32,12 +33,12 @@ var initCmdTests = []TestCase{
expectedErr: nil,
},
{
name: "test create using an index.pidx",
name: "test create using an " + installer.PublicIndex,
args: []string{"init"},
setUpFunc: func(t *TestCase) {
server := NewServer()
t.args = append(t.args, server.URL()+"index.pidx")
server.AddRoute("index.pidx", []byte(`<?xml version="1.0" encoding="UTF-8" ?>
t.args = append(t.args, server.URL()+installer.PublicIndex)
server.AddRoute(installer.PublicIndex, []byte(`<?xml version="1.0" encoding="UTF-8" ?>
<index schemaVersion="1.1.0" xs:noNamespaceSchemaLocation="PackIndex.xsd" xmlns:xs="https://www.w3.org/2001/XMLSchema-instance">
<vendor>TheVendor</vendor>
<url>https://the.vendor/</url>
Expand All @@ -49,15 +50,15 @@ var initCmdTests = []TestCase{
},
},
{
name: "test create using local index.pidx",
name: "test create using local " + installer.PublicIndex,
args: []string{"init", pidxFilePath},
createPackRoot: true,
},
{
name: "test create using local index.pidx that do not exist",
name: "test create using local " + installer.PublicIndex + " that does not exist",
args: []string{"init", notFoundPidxFilePath},
createPackRoot: true,
expectedErr: errs.ErrFileNotFound,
expectedErr: errs.ErrFileNotFoundUseInit,
},
{
name: "test create using directory as path",
Expand Down
18 changes: 8 additions & 10 deletions cmd/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ var AllCommands = []*cobra.Command{
// createPackRoot is a flag that determines if the pack root should be created or not
var createPackRoot bool

// defaultPublicIndex is the public index to use in "default mode"
const defaultPublicIndex = "https://www.keil.com/pack/index.pidx"

var viper *viperType.Viper

func configureInstallerGlobalCmd(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -70,37 +67,38 @@ func configureInstaller(cmd *cobra.Command, args []string) error {
}

targetPackRoot := viper.GetString("pack-root")
checkConnection := viper.GetBool("check-connection")
checkConnection := viper.GetBool("check-connection") // TODO: never set

if targetPackRoot == installer.GetDefaultCmsisPackRoot() {
// If using the default pack root path and the public index is not found,
// initialize it
if !checkConnection && !utils.FileExists(filepath.Join(targetPackRoot, ".Web", "index.pidx")) {
err := installer.SetPackRoot(targetPackRoot, true)
if !checkConnection && !utils.FileExists(filepath.Join(targetPackRoot, ".Web", installer.PublicIndex)) {
err := installer.SetPackRoot(targetPackRoot, true, true)
if err != nil {
return err
}
// Exclude index updating commands to not double update
if cmd.Name() != "init" && cmd.Name() != "index" && cmd.Name() != "update-index" {
installer.UnlockPackRoot()
err = installer.UpdatePublicIndex(defaultPublicIndex, true, true, false, false, 0, 0)
err = installer.UpdatePublicIndex(installer.DefaultPublicIndex, true, true, false, false, 0, 0)
if err != nil {
return err
}
err = installer.SetPackRoot(targetPackRoot, false)
err = installer.SetPackRoot(targetPackRoot, false, true)
if err != nil {
return err
}
installer.LockPackRoot()
}
} else {
err := installer.SetPackRoot(targetPackRoot, createPackRoot)
err := installer.SetPackRoot(targetPackRoot, createPackRoot, true)
if err != nil {
return err
}
}
} else {
err := installer.SetPackRoot(targetPackRoot, createPackRoot)
download := cmd.Name() != "init" && cmd.Name() != "connection"
err := installer.SetPackRoot(targetPackRoot, createPackRoot, download)
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type TestCase struct {
expectedStdout []string
expectedStderr []string
expectedErr error
expErrUnwwrap bool
setUpFunc func(t *TestCase)
tearDownFunc func()
validationFunc func(t *testing.T)
Expand Down Expand Up @@ -143,7 +144,7 @@ func runTests(t *testing.T, tests []TestCase) {

os.Setenv("CMSIS_PACK_ROOT", localTestingDir)
if test.createPackRoot {
assert.Nil(installer.SetPackRoot(localTestingDir, test.createPackRoot))
assert.Nil(installer.SetPackRoot(localTestingDir, test.createPackRoot, false))
installer.UnlockPackRoot()
}

Expand Down Expand Up @@ -200,7 +201,11 @@ func runTests(t *testing.T, tests []TestCase) {
outStr := string(outBytes)
errStr := string(errBytes)

assert.Equal(test.expectedErr, cmdErr)
if test.expErrUnwwrap {
assert.Equal(test.expectedErr, errors.Unwrap(cmdErr))
} else {
assert.Equal(test.expectedErr, cmdErr)
}
for _, expectedStr := range test.expectedStdout {
assert.Contains(outStr, expectedStr)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/commands/update_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var UpdateIndexCmd = &cobra.Command{
}

func getLongUpdateDescription() string {
return `Updates the public index in ` + os.Getenv("CMSIS_PACK_ROOT") + `/.Web/index.pidx using the URL in <url> tag inside index.pidx.
return `Updates the public index in ` + os.Getenv("CMSIS_PACK_ROOT") + "/.Web/" + installer.PublicIndex + " using the URL in <url> tag inside " + installer.PublicIndex + `.
By default it will also check if all PDSC files under .Web/ need update as well. This can be disabled via the "--sparse" flag.`
}

Expand Down
10 changes: 5 additions & 5 deletions cmd/commands/update_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var updateIndexServer Server
var updateIndexCmdTests = []TestCase{
{
name: "test no parameter is required",
args: []string{"update-index", "index.pidx"},
args: []string{"update-index", installer.PublicIndex},
expectedErr: errors.New("accepts 0 arg(s), received 1"),
},
{
Expand All @@ -29,7 +29,7 @@ var updateIndexCmdTests = []TestCase{
args: []string{"update-index"},
createPackRoot: true,
defaultMode: true,
expectedStdout: []string{"Updating public index", "Downloading index.pidx"},
expectedStdout: []string{"Updating public index", "Downloading " + installer.PublicIndex},
setUpFunc: func(t *TestCase) {
indexContent := `<?xml version="1.0" encoding="UTF-8" ?>
<index schemaVersion="1.1.0" xs:noNamespaceSchemaLocation="PackIndex.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
Expand All @@ -43,14 +43,14 @@ var updateIndexCmdTests = []TestCase{
indexContent = fmt.Sprintf(indexContent, updateIndexServer.URL())
_ = os.WriteFile(installer.Installation.PublicIndex, []byte(indexContent), 0600)

updateIndexServer.AddRoute("index.pidx", []byte(indexContent))
updateIndexServer.AddRoute(installer.PublicIndex, []byte(indexContent))
},
},
{
name: "test updating index",
args: []string{"update-index"},
createPackRoot: true,
expectedStdout: []string{"Updating public index", "Downloading index.pidx"},
expectedStdout: []string{"Updating public index", "Downloading " + installer.PublicIndex},
setUpFunc: func(t *TestCase) {
indexContent := `<?xml version="1.0" encoding="UTF-8" ?>
<index schemaVersion="1.1.0" xs:noNamespaceSchemaLocation="PackIndex.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
Expand All @@ -64,7 +64,7 @@ var updateIndexCmdTests = []TestCase{
indexContent = fmt.Sprintf(indexContent, updateIndexServer.URL())
_ = os.WriteFile(installer.Installation.PublicIndex, []byte(indexContent), 0600)

updateIndexServer.AddRoute("index.pidx", []byte(indexContent))
updateIndexServer.AddRoute(installer.PublicIndex, []byte(indexContent))
},
},
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/cryptography/checksum.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cryptography

import (
"errors"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -46,7 +45,7 @@ func WriteChecksumFile(digests map[string]string, filename string) error {
// GenerateChecksum creates a .checksum file for a pack.
func GenerateChecksum(sourcePack, destinationDir, hashFunction string) error {
if !isValidHash(hashFunction) {
return errors.New("provided hash function is not supported")
return errs.ErrHashNotSupported
}
if !utils.FileExists(sourcePack) {
log.Errorf("\"%s\" does not exist", sourcePack)
Expand Down Expand Up @@ -106,7 +105,7 @@ func VerifyChecksum(packPath, checksumPath string) error {
}
hashFunction := filepath.Ext(strings.Split(checksumPath, ".checksum")[0])[1:]
if !isValidHash(hashFunction) {
return errors.New("not a valid .checksum file (correct format is [<pack>].[<hash-algorithm>].checksum). Please confirm if the hash is supported")
return errs.ErrNotValidChecksumFile
}

// Compute pack's digests
Expand Down Expand Up @@ -144,7 +143,7 @@ func VerifyChecksum(packPath, checksumPath string) error {
}
}
if failure {
return errors.New("bad pack integrity")
return errs.ErrBadIntegrity
}

log.Info("pack integrity verified, all checksums match.")
Expand Down
1 change: 1 addition & 0 deletions cmd/cryptography/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ func verifyPackFullSignature(zip *zip.ReadCloser, vendor, b64Cert, b64Hash strin
// verifyPackCertOnlySignature validates the integrity of a pack
// by performing some validations on the embed certificate.
func verifyPackCertOnlySignature(zip *zip.ReadCloser, vendor, b64Cert string, skipCertValidation, skipInfo bool) error {
_ = zip
rawCert, err := base64.StdEncoding.DecodeString(b64Cert)
if err != nil {
return err
Expand Down
10 changes: 9 additions & 1 deletion cmd/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ var (
// Errors related to network
ErrBadRequest = errors.New("bad request")
ErrFailedDownloadingFile = errors.New("failed to download file")
ErrOffline = errors.New("remote server is offline or cannot be reached")
ErrHTTPtimeout = errors.New("HTTP get timed out")

// Errors related to file system
ErrFailedCreatingFile = errors.New("failed to create a local file")
Expand All @@ -54,6 +56,7 @@ var (
ErrFailedInflatingFile = errors.New("fail to inflate file")
ErrFailedCreatingDirectory = errors.New("fail to create directory")
ErrFileNotFound = errors.New("file not found")
ErrFileNotFoundUseInit = errors.New("\"index.pidx\" file not found; use cpackget init command to retrieve it")
ErrDirectoryNotFound = errors.New("directory not found")
ErrPathAlreadyExists = errors.New("path already exists")
ErrCopyingEqualPaths = errors.New("failed copying files: source is the same as destination")
Expand All @@ -69,6 +72,9 @@ var (
ErrUnsupportedKeyAlgo = errors.New("unsupported key algorithm")
ErrCannotVerifySignature = errors.New("cannot verify pack signature")
ErrPossibleMaliciousPack = errors.New("bad pack integrity! signature does not match pack contents - might have been tampered")
ErrHashNotSupported = errors.New("provided hash function is not supported")
ErrNotValidChecksumFile = errors.New("not a valid .checksum file (correct format is [<pack>].[<hash-algorithm>].checksum). Please confirm if the hash is supported")
ErrBadIntegrity = errors.New("bad pack integrity")

// Security errors
ErrInsecureZipFileName = errors.New("zip file contains insecure characters: ../")
Expand All @@ -81,7 +87,7 @@ var (
// Cmdline errors
ErrIncorrectCmdArgs = errors.New("incorrect setup of command line arguments")

// Errors on installation strucuture
// Errors on installation structure
ErrCannotOverwritePublicIndex = errors.New("cannot replace \"index.pidx\", use the flag \"-f/--force\" to force overwritting it")
ErrInvalidPublicIndexReference = errors.New("the specified index path can only either empty, a local file or an HTTP(S) URL - not a directory")
ErrPackPdscCannotBeFound = errors.New("the URL is invalid or does not return the file")
Expand All @@ -95,4 +101,6 @@ var (

// Error/Flag to detect when a user has requested early termination
ErrTerminatedByUser = errors.New("terminated by user request")

ErrIndexTooOld = errors.New("public index \"index.pidx\" too old")
)
Loading
Loading