Skip to content

Commit

Permalink
add unit tests, rename files to conventions, wip
Browse files Browse the repository at this point in the history
  • Loading branch information
patinthehat committed May 14, 2024
1 parent b7d4cf4 commit deb55d4
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 53 deletions.
27 changes: 0 additions & 27 deletions lib/verifiers/NoVerifier.go

This file was deleted.

10 changes: 4 additions & 6 deletions lib/verifiers/Sha256SumFileAssetVerifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,29 @@ import (
"bufio"
"crypto/sha256"
"fmt"
"go/types"
"regexp"

"github.com/permafrost-dev/eget/lib/assets"
"github.com/permafrost-dev/eget/lib/download"
)

type Sha256SumFileAssetVerifier struct {
Client *download.Client
Client download.ClientContract
Sha256SumAssetURL string
RealAssetURL string
BinaryName string
Asset *assets.Asset
types.Type
Verifier
}

func (s256 *Sha256SumFileAssetVerifier) GetAsset() *assets.Asset {
return s256.Asset
}

func (s256 *Sha256SumFileAssetVerifier) WithClient(client *download.Client) *Verifier {
func (s256 *Sha256SumFileAssetVerifier) WithClient(client download.ClientContract) Verifier {
s256.Client = client
var intf interface{} = s256
var result Verifier = intf.(Verifier)
return &result
return intf.(Verifier)
}

func (s256 *Sha256SumFileAssetVerifier) Verify(b []byte) error {
Expand Down
9 changes: 4 additions & 5 deletions lib/verifiers/Sha256Verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"go/types"

"github.com/permafrost-dev/eget/lib/assets"
"github.com/permafrost-dev/eget/lib/download"
)

type Sha256Verifier struct {
Expected []byte
client *download.Client
client download.ClientContract
Asset *assets.Asset
types.Type
Verifier
}

func NewSha256Verifier(client *download.Client, expectedHex string) (*Sha256Verifier, error) {
Expand All @@ -33,11 +32,11 @@ func (s256 *Sha256Verifier) GetAsset() *assets.Asset {
return s256.Asset
}

func (s256 *Sha256Verifier) WithClient(client *download.Client) *Verifier {
func (s256 *Sha256Verifier) WithClient(client download.ClientContract) Verifier {
s256.client = client
var intf interface{} = s256
var result Verifier = intf.(Verifier)
return &result
return result
}

func (s256 *Sha256Verifier) Verify(b []byte) error {
Expand Down
28 changes: 28 additions & 0 deletions lib/verifiers/no_verifier.go
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"
}
45 changes: 45 additions & 0 deletions lib/verifiers/no_verifier_test.go
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())
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,31 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"go/types"
"io"

"github.com/permafrost-dev/eget/lib/assets"
"github.com/permafrost-dev/eget/lib/download"
)

type Sha256AssetVerifier struct {
client *download.Client
client download.ClientContract
AssetURL string
Asset *assets.Asset
types.Type
Verifier
}

func (s256 *Sha256AssetVerifier) GetAsset() *assets.Asset {
return s256.Asset
}

func (s256 *Sha256AssetVerifier) WithClient(client *download.Client) *Verifier {
func (s256 *Sha256AssetVerifier) WithClient(client download.ClientContract) Verifier {
s256.client = client
var intf interface{} = s256
var result Verifier = intf.(Verifier)
return &result
return s256
}

func (s256 *Sha256AssetVerifier) Verify(b []byte) error {
resp, err := s256.client.GetJSON(s256.AssetURL)

if err != nil {
return err
}
Expand All @@ -40,6 +38,7 @@ func (s256 *Sha256AssetVerifier) Verify(b []byte) error {
if err != nil {
return err
}

expected := make([]byte, sha256.Size)

n, _ := hex.Decode(expected, data)
Expand Down
87 changes: 87 additions & 0 deletions lib/verifiers/sha256_asset_verifier_test.go
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.
21 changes: 21 additions & 0 deletions lib/verifiers/sha256_error_test.go
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"))
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ type Sha256Printer struct {
types.Type
}

func (s256 *Sha256Printer) GetAsset() *assets.Asset {
func (s256 Sha256Printer) GetAsset() *assets.Asset {
return s256.Asset
}

func (s256 *Sha256Printer) WithClient(_ *download.Client) *Verifier {
var intf interface{} = s256
var result Verifier = intf.(Verifier)
return &result
func (s256 Sha256Printer) WithClient(_ download.ClientContract) Verifier {
return s256
}

func (s256 *Sha256Printer) Verify(b []byte) error {
func (s256 Sha256Printer) Verify(b []byte) error {
sum := sha256.Sum256(b)
fmt.Printf("%x\n", sum)
return nil
}

func (s256 *Sha256Printer) String() string {
func (s256 Sha256Printer) String() string {
return "sha256:print"
}
Loading

0 comments on commit deb55d4

Please sign in to comment.