Skip to content

Commit 3d61814

Browse files
committed
Fixed download checker condition
1 parent 298922b commit 3d61814

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

Diff for: arduino/resources/checksums.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (r *DownloadResource) TestLocalArchiveSize(downloadDir *paths.Path) (bool,
8484
if err != nil {
8585
return false, fmt.Errorf("getting archive info: %s", err)
8686
}
87-
return info.Size() != r.Size, nil
87+
return info.Size() == r.Size, nil
8888
}
8989

9090
// TestLocalArchiveIntegrity checks for integrity of the local archive.

Diff for: arduino/resources/resources_test.go

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to [email protected].
16+
*/
17+
18+
package resources
19+
20+
import (
21+
"crypto"
22+
"encoding/hex"
23+
"testing"
24+
25+
paths "github.com/arduino/go-paths-helper"
26+
"github.com/stretchr/testify/require"
27+
)
28+
29+
func TestDownloadAndChecksums(t *testing.T) {
30+
tmp, err := paths.MkTempDir("", "")
31+
require.NoError(t, err)
32+
defer tmp.RemoveAll()
33+
testFile := tmp.Join("cache", "asciilogo.txt")
34+
35+
r := &DownloadResource{
36+
ArchiveFileName: "asciilogo.txt",
37+
CachePath: "cache",
38+
Checksum: "SHA-256:618d6c3d3f02388d4ddbe13c893902422a8656365b67ba19ef80873bf1da0f1f",
39+
Size: 2263,
40+
URL: "https://arduino.cc/asciilogo.txt",
41+
}
42+
digest, err := hex.DecodeString("618d6c3d3f02388d4ddbe13c893902422a8656365b67ba19ef80873bf1da0f1f")
43+
require.NoError(t, err)
44+
45+
downloadAndTestChecksum := func() {
46+
d, err := r.Download(tmp)
47+
require.NoError(t, err)
48+
err = d.Run()
49+
require.NoError(t, err)
50+
51+
data, err := testFile.ReadFile()
52+
require.NoError(t, err)
53+
algo := crypto.SHA256.New()
54+
algo.Write(data)
55+
require.EqualValues(t, digest, algo.Sum(nil))
56+
}
57+
58+
// Normal download
59+
downloadAndTestChecksum()
60+
61+
// Download with cached file
62+
d, err := r.Download(tmp)
63+
require.NoError(t, err)
64+
require.Nil(t, d)
65+
66+
// Download if cached file has data in excess (redownload)
67+
data, err := testFile.ReadFile()
68+
require.NoError(t, err)
69+
data = append(data, []byte("123123123")...)
70+
err = testFile.WriteFile(data)
71+
require.NoError(t, err)
72+
downloadAndTestChecksum()
73+
74+
// Download if cached file has less data (resume)
75+
data, err = testFile.ReadFile()
76+
require.NoError(t, err)
77+
err = testFile.WriteFile(data[:1000])
78+
require.NoError(t, err)
79+
downloadAndTestChecksum()
80+
81+
r.Checksum = "BOH:12312312312313123123123123123123"
82+
_, err = r.TestLocalArchiveChecksum(tmp)
83+
require.Error(t, err)
84+
85+
r.Checksum = "MD5 667cf48afcc12c38c8c1637947a04224"
86+
_, err = r.TestLocalArchiveChecksum(tmp)
87+
require.Error(t, err)
88+
89+
r.Checksum = "MD5:zmxcbzxmncbzxmnbczxmnbczxmnbcnnb"
90+
_, err = r.TestLocalArchiveChecksum(tmp)
91+
require.Error(t, err)
92+
93+
r.Checksum = "SHA-1:960f50b4326ba28304039f3d2c0fb30a0463372f"
94+
res, err := r.TestLocalArchiveChecksum(tmp)
95+
require.NoError(t, err)
96+
require.True(t, res)
97+
98+
r.Checksum = "MD5:667cf48afcc12c38c8c1637947a04224"
99+
res, err = r.TestLocalArchiveChecksum(tmp)
100+
require.NoError(t, err)
101+
require.True(t, res)
102+
103+
r.Checksum = "MD5:12312312312312312312313131231231"
104+
res, err = r.TestLocalArchiveChecksum(tmp)
105+
require.NoError(t, err)
106+
require.False(t, res)
107+
108+
_, err = r.TestLocalArchiveChecksum(paths.New("/not-existent"))
109+
require.Error(t, err)
110+
111+
r.ArchiveFileName = "not-existent.zip"
112+
_, err = r.TestLocalArchiveChecksum(tmp)
113+
require.Error(t, err)
114+
}

0 commit comments

Comments
 (0)