Skip to content

Commit 59b6277

Browse files
Make archive validation error messages friendlier (#1188)
* Make archive validation error messages friendlier * change behaviour a little + fix tests
1 parent 550179e commit 59b6277

File tree

3 files changed

+46
-32
lines changed

3 files changed

+46
-32
lines changed

arduino/resources/checksums.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ import (
3434

3535
// TestLocalArchiveChecksum test if the checksum of the local archive match the checksum of the DownloadResource
3636
func (r *DownloadResource) TestLocalArchiveChecksum(downloadDir *paths.Path) (bool, error) {
37+
if r.Checksum == "" {
38+
return false, fmt.Errorf("missing checksum for: %s", r.ArchiveFileName)
39+
}
3740
split := strings.SplitN(r.Checksum, ":", 2)
3841
if len(split) != 2 {
3942
return false, fmt.Errorf("invalid checksum format: %s", r.Checksum)
@@ -69,7 +72,12 @@ func (r *DownloadResource) TestLocalArchiveChecksum(downloadDir *paths.Path) (bo
6972
if _, err := io.Copy(algo, file); err != nil {
7073
return false, fmt.Errorf("computing hash: %s", err)
7174
}
72-
return bytes.Compare(algo.Sum(nil), digest) == 0, nil
75+
76+
if bytes.Compare(algo.Sum(nil), digest) != 0 {
77+
return false, fmt.Errorf("archive hash differs from hash in index")
78+
}
79+
80+
return true, nil
7381
}
7482

7583
// TestLocalArchiveSize test if the local archive size match the DownloadResource size
@@ -82,7 +90,11 @@ func (r *DownloadResource) TestLocalArchiveSize(downloadDir *paths.Path) (bool,
8290
if err != nil {
8391
return false, fmt.Errorf("getting archive info: %s", err)
8492
}
85-
return info.Size() == r.Size, nil
93+
if info.Size() != r.Size {
94+
return false, fmt.Errorf("fetched archive size differs from size specified in index")
95+
}
96+
97+
return true, nil
8698
}
8799

88100
// TestLocalArchiveIntegrity checks for integrity of the local archive.
@@ -94,7 +106,7 @@ func (r *DownloadResource) TestLocalArchiveIntegrity(downloadDir *paths.Path) (b
94106
}
95107

96108
if ok, err := r.TestLocalArchiveSize(downloadDir); err != nil {
97-
return false, fmt.Errorf("teting archive size: %s", err)
109+
return false, fmt.Errorf("testing archive size: %s", err)
98110
} else if !ok {
99111
return false, nil
100112
}
@@ -163,5 +175,9 @@ func CheckDirChecksum(root string) (bool, error) {
163175
if err != nil {
164176
return false, err
165177
}
166-
return file.Checksum == checksum, nil
178+
if file.Checksum != checksum {
179+
return false, fmt.Errorf("Checksum differs from checksum in package.json")
180+
}
181+
182+
return true, nil
167183
}

arduino/resources/helpers.go

+11-14
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,23 @@ func (r *DownloadResource) IsCached(downloadDir *paths.Path) (bool, error) {
4444

4545
// Download a DownloadResource.
4646
func (r *DownloadResource) Download(downloadDir *paths.Path, config *downloader.Config) (*downloader.Downloader, error) {
47-
cached, err := r.TestLocalArchiveIntegrity(downloadDir)
48-
if err != nil {
49-
return nil, fmt.Errorf("testing local archive integrity: %s", err)
50-
}
51-
if cached {
52-
// File is cached, nothing to do here
53-
return nil, nil
54-
}
55-
5647
path, err := r.ArchivePath(downloadDir)
5748
if err != nil {
5849
return nil, fmt.Errorf("getting archive path: %s", err)
5950
}
6051

61-
if stats, err := path.Stat(); os.IsNotExist(err) {
52+
if _, err := path.Stat(); os.IsNotExist(err) {
6253
// normal download
63-
} else if err == nil && stats.Size() > r.Size {
64-
// file is bigger than expected, retry download...
65-
if err := path.Remove(); err != nil {
66-
return nil, fmt.Errorf("removing corrupted archive file: %s", err)
54+
} else if err == nil {
55+
// check local file integrity
56+
ok, err := r.TestLocalArchiveIntegrity(downloadDir)
57+
if err != nil || !ok {
58+
if err := path.Remove(); err != nil {
59+
return nil, fmt.Errorf("removing corrupted archive file: %s", err)
60+
}
61+
} else {
62+
// File is cached, nothing to do here
63+
return nil, nil
6764
}
6865
} else if err == nil {
6966
// resume download

arduino/resources/resources_test.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,21 @@ import (
2626
)
2727

2828
func TestDownloadAndChecksums(t *testing.T) {
29+
testFileName := "core.zip"
2930
tmp, err := paths.MkTempDir("", "")
3031
require.NoError(t, err)
3132
defer tmp.RemoveAll()
32-
testFile := tmp.Join("cache", "index.html")
33+
testFile := tmp.Join("cache", testFileName)
3334

35+
// taken from test/testdata/test_index.json
3436
r := &DownloadResource{
35-
ArchiveFileName: "index.html",
37+
ArchiveFileName: testFileName,
3638
CachePath: "cache",
37-
Checksum: "SHA-256:e021e1a223d03069d5f08dea25a58ca445a7376d9bdf980f756034f118449e66",
38-
Size: 1119,
39-
URL: "https://downloads.arduino.cc/index.html",
39+
Checksum: "SHA-256:6a338cf4d6d501176a2d352c87a8d72ac7488b8c5b82cdf2a4e2cef630391092",
40+
Size: 486,
41+
URL: "https://raw.githubusercontent.com/arduino/arduino-cli/master/test/testdata/core.zip",
4042
}
41-
digest, err := hex.DecodeString("e021e1a223d03069d5f08dea25a58ca445a7376d9bdf980f756034f118449e66")
43+
digest, err := hex.DecodeString("6a338cf4d6d501176a2d352c87a8d72ac7488b8c5b82cdf2a4e2cef630391092")
4244
require.NoError(t, err)
4345

4446
downloadAndTestChecksum := func() {
@@ -73,10 +75,14 @@ func TestDownloadAndChecksums(t *testing.T) {
7375
// Download if cached file has less data (resume)
7476
data, err = testFile.ReadFile()
7577
require.NoError(t, err)
76-
err = testFile.WriteFile(data[:1000])
78+
err = testFile.WriteFile(data[:100])
7779
require.NoError(t, err)
7880
downloadAndTestChecksum()
7981

82+
r.Checksum = ""
83+
_, err = r.TestLocalArchiveChecksum(tmp)
84+
require.Error(t, err)
85+
8086
r.Checksum = "BOH:12312312312313123123123123123123"
8187
_, err = r.TestLocalArchiveChecksum(tmp)
8288
require.Error(t, err)
@@ -89,21 +95,16 @@ func TestDownloadAndChecksums(t *testing.T) {
8995
_, err = r.TestLocalArchiveChecksum(tmp)
9096
require.Error(t, err)
9197

92-
r.Checksum = "SHA-1:c007e47637cc6ad6176e7d94aeffc232ee34c1c1"
98+
r.Checksum = "SHA-1:16e1495aff482f2650733e1661d5f7c69040ec13"
9399
res, err := r.TestLocalArchiveChecksum(tmp)
94100
require.NoError(t, err)
95101
require.True(t, res)
96102

97-
r.Checksum = "MD5:2e388576eefd92a15967868d5f566f29"
103+
r.Checksum = "MD5:3bcc3aab6842ff124df6a5cfba678ca1"
98104
res, err = r.TestLocalArchiveChecksum(tmp)
99105
require.NoError(t, err)
100106
require.True(t, res)
101107

102-
r.Checksum = "MD5:12312312312312312312313131231231"
103-
res, err = r.TestLocalArchiveChecksum(tmp)
104-
require.NoError(t, err)
105-
require.False(t, res)
106-
107108
_, err = r.TestLocalArchiveChecksum(paths.New("/not-existent"))
108109
require.Error(t, err)
109110

0 commit comments

Comments
 (0)