forked from zyedidia/eget
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unit tests, rename files to conventions, wip
- Loading branch information
1 parent
b7d4cf4
commit deb55d4
Showing
12 changed files
with
258 additions
and
53 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package verifiers | ||
|
||
import ( | ||
"github.com/permafrost-dev/eget/lib/assets" | ||
"github.com/permafrost-dev/eget/lib/download" | ||
) | ||
|
||
type NoVerifier struct { | ||
Asset *assets.Asset | ||
Verifier | ||
// types.Type | ||
} | ||
|
||
func (n NoVerifier) GetAsset() *assets.Asset { | ||
return n.Asset | ||
} | ||
|
||
func (n NoVerifier) Verify(_ []byte) error { | ||
return nil | ||
} | ||
|
||
func (n NoVerifier) WithClient(_ download.ClientContract) Verifier { | ||
return n | ||
} | ||
|
||
func (n NoVerifier) String() string { | ||
return "NoVerifier" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package verifiers_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/permafrost-dev/eget/lib/assets" | ||
"github.com/permafrost-dev/eget/lib/download" | ||
. "github.com/permafrost-dev/eget/lib/verifiers" | ||
) | ||
|
||
var _ = Describe("NoVerifier", func() { | ||
var ( | ||
noVerifier *NoVerifier | ||
asset *assets.Asset | ||
) | ||
|
||
BeforeEach(func() { | ||
asset = &assets.Asset{Name: "test-asset", DownloadURL: "http://example.com/test-asset"} | ||
noVerifier = &NoVerifier{Asset: asset} | ||
}) | ||
|
||
Describe("GetAsset", func() { | ||
It("should return the correct asset", func() { | ||
Expect(noVerifier.GetAsset()).To(Equal(asset)) | ||
}) | ||
}) | ||
|
||
Describe("Verify", func() { | ||
It("should always return nil", func() { | ||
Expect(noVerifier.Verify(nil)).To(BeNil()) | ||
}) | ||
}) | ||
|
||
Describe("WithClient", func() { | ||
It("should return a Verifier type", func() { | ||
client := &download.Client{} | ||
Expect(noVerifier.WithClient(client)).To(BeAssignableToTypeOf(NoVerifier{})) | ||
}) | ||
|
||
It("should not panic", func() { | ||
client := &download.Client{} | ||
Expect(func() { noVerifier.WithClient(client) }).ToNot(Panic()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package verifiers_test | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/permafrost-dev/eget/lib/assets" | ||
"github.com/permafrost-dev/eget/lib/mockhttp" | ||
"github.com/permafrost-dev/eget/lib/verifiers" | ||
) | ||
|
||
var _ = Describe("Sha256AssetVerifier", func() { | ||
var ( | ||
mockClient mockhttp.HTTPClient | ||
verifier verifiers.Sha256AssetVerifier | ||
assetURL string | ||
asset assets.Asset | ||
) | ||
|
||
BeforeEach(func() { | ||
mockClient = mockhttp.NewMockHTTPClient() | ||
|
||
assetURL = "https://example.com/asset" | ||
asset = assets.Asset{Name: "test", DownloadURL: assetURL, Filters: nil, ReleaseDate: time.Now().Round(time.Second)} | ||
verifier = verifiers.Sha256AssetVerifier{ | ||
AssetURL: assetURL, | ||
Asset: &asset, | ||
} | ||
verifier.WithClient(&mockClient) | ||
}) | ||
|
||
Context("Verify", func() { | ||
It("should verify asset with correct sha256 checksum", func() { | ||
// Simulate downloading asset with known checksum | ||
data := []byte("test data") | ||
sum := sha256.Sum256(data) | ||
hexSum := hex.EncodeToString(sum[0:]) | ||
|
||
mockClient.AddJSONResponse(assetURL, ``+hexSum+``, 200) | ||
verifier.WithClient(&mockClient) | ||
|
||
verifier.AssetURL = assetURL | ||
err := verifier.Verify(data) | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
}) | ||
|
||
It("should fail to verify asset with incorrect sha256 checksum", func() { | ||
// Simulate downloading asset with incorrect checksum | ||
data := []byte("test data") | ||
mockClient.AddJSONResponse(assetURL, `e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855`, 200) | ||
verifier.WithClient(&mockClient) | ||
verifier.AssetURL = assetURL | ||
|
||
err := verifier.Verify(data) | ||
Expect(err).Should(HaveOccurred()) | ||
Expect(err).Should(BeAssignableToTypeOf(&verifiers.Sha256Error{})) | ||
}) | ||
|
||
It("should fail if asset URL is not reachable", func() { | ||
// Simulate HTTP error | ||
mockClient.AddJSONResponse(assetURL, ``, 500) | ||
verifier.WithClient(&mockClient) | ||
|
||
verifier.AssetURL = assetURL | ||
err := verifier.Verify([]byte("test data")) | ||
Expect(err).Should(HaveOccurred()) | ||
}) | ||
}) | ||
|
||
Context("String", func() { | ||
It("should return a descriptive string", func() { | ||
str := verifier.String() | ||
Expect(str).Should(ContainSubstring("checksum verified with " + assetURL)) | ||
}) | ||
}) | ||
|
||
Context("WithClient", func() { | ||
It("should set the client", func() { | ||
newMockClient := mockhttp.NewMockHTTPClient() | ||
newVerifier := verifier.WithClient(&newMockClient) | ||
Expect(newVerifier).ShouldNot(BeNil()) | ||
}) | ||
}) | ||
}) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package verifiers_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/permafrost-dev/eget/lib/verifiers" | ||
) | ||
|
||
var _ = Describe("Sha256Error", func() { | ||
It("should return the correct error message", func() { | ||
expected := []byte{1, 2, 3, 4} | ||
got := []byte{4, 3, 2, 1} | ||
err := &verifiers.Sha256Error{ | ||
Expected: expected, | ||
Got: got, | ||
} | ||
|
||
Expect(err.Error()).To(Equal("sha256 checksum mismatch:\nexpected: 01020304\ngot: 04030201")) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.