Skip to content

Commit

Permalink
fix wrong IndexFile truncate (#2346)
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini authored Oct 2, 2023
1 parent 6aa1be0 commit dc5c56e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
11 changes: 8 additions & 3 deletions arduino/resources/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ type IndexResource struct {
// IndexFileName returns the index file name as it is saved in data dir (package_xxx_index.json).
func (res *IndexResource) IndexFileName() (string, error) {
filename := path.Base(res.URL.Path) // == package_index.json[.gz] || packacge_index.tar.bz2
if filename == "." || filename == "" {
if filename == "." || filename == "" || filename == "/" {
return "", &arduino.InvalidURLError{}
}
if i := strings.Index(filename, "."); i != -1 {
filename = filename[:i]
switch {
case strings.HasSuffix(filename, ".json"):
return filename, nil
case strings.HasSuffix(filename, ".gz"):
return strings.TrimSuffix(filename, ".gz"), nil
case strings.HasSuffix(filename, ".tar.bz2"):
return strings.TrimSuffix(filename, ".tar.bz2") + ".json", nil
}
return filename + ".json", nil
}
Expand Down
23 changes: 23 additions & 0 deletions arduino/resources/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package resources
import (
"crypto"
"encoding/hex"
"fmt"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -148,3 +149,25 @@ func TestIndexDownloadAndSignatureWithinArchive(t *testing.T) {
require.False(t, invDestDir.Join("package_index.json").Exist())
require.False(t, invDestDir.Join("package_index.json.sig").Exist())
}

func TestIndexFileName(t *testing.T) {
tests := []struct {
url string
expected string
}{
{url: "package_index.json", expected: "package_index.json"},
{url: "package_index.json.gz", expected: "package_index.json"},
{url: "package_index.tar.bz2", expected: "package_index.json"},
// https://github.com/arduino/arduino-cli/issues/2345
{url: "package_arduino.cc_index.json", expected: "package_arduino.cc_index.json"},
{url: "package_arduino.cc_index.json.gz", expected: "package_arduino.cc_index.json"},
{url: "package_arduino.cc_index.tar.bz2", expected: "package_arduino.cc_index.json"},
{url: "http://drazzy.com/package_drazzy.com_index.json", expected: "package_drazzy.com_index.json"},
}
for _, tc := range tests {
ir := IndexResource{URL: &url.URL{Path: tc.url}}
name, err := ir.IndexFileName()
require.NoError(t, err, fmt.Sprintf("error trying url: %v", tc))
require.Equal(t, tc.expected, name)
}
}

0 comments on commit dc5c56e

Please sign in to comment.