mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-30 19:22:09 +00:00
- the Handler struct becomes handler (lowercase) - the Handler interface is defined to be the existing methods - isClosed() is added and used only in tests - setgcAt() is added and used only in tests --- This is to allow mocking the Handler interface for testing. <!--start release-notes-assistant--> <!--URL:https://code.forgejo.org/forgejo/runner--> - other - [PR](https://code.forgejo.org/forgejo/runner/pulls/934): <!--number 934 --><!--line 0 --><!--description Y2hvcmU6IHJlZmFjdG9yIGFjdC9hcnRpZmFjdGNhY2hlIEhhbmRsZXIgdG8gYW4gaW50ZXJmYWNl-->chore: refactor act/artifactcache Handler to an interface<!--description--> <!--end release-notes-assistant--> Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/934 Reviewed-by: Mathieu Fenniak <mfenniak@noreply.code.forgejo.org> Co-authored-by: Earl Warren <contact@earl-warren.org> Co-committed-by: Earl Warren <contact@earl-warren.org>
83 lines
2.2 KiB
Go
83 lines
2.2 KiB
Go
package artifactcache
|
|
|
|
import (
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"code.forgejo.org/forgejo/runner/v9/act/cacheproxy"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestMac(t *testing.T) {
|
|
handler := &handler{
|
|
secret: "secret for testing",
|
|
}
|
|
|
|
t.Run("validate correct mac", func(t *testing.T) {
|
|
name := "org/reponame"
|
|
run := "1"
|
|
ts := strconv.FormatInt(time.Now().Unix(), 10)
|
|
|
|
mac := computeMac(handler.secret, name, run, ts, "")
|
|
rundata := cacheproxy.RunData{
|
|
RepositoryFullName: name,
|
|
RunNumber: run,
|
|
Timestamp: ts,
|
|
RepositoryMAC: mac,
|
|
}
|
|
|
|
repoName, err := handler.validateMac(rundata)
|
|
require.NoError(t, err)
|
|
require.Equal(t, name, repoName)
|
|
})
|
|
|
|
t.Run("validate incorrect timestamp", func(t *testing.T) {
|
|
name := "org/reponame"
|
|
run := "1"
|
|
ts := "9223372036854775807" // This should last us for a while...
|
|
|
|
mac := computeMac(handler.secret, name, run, ts, "")
|
|
rundata := cacheproxy.RunData{
|
|
RepositoryFullName: name,
|
|
RunNumber: run,
|
|
Timestamp: ts,
|
|
RepositoryMAC: mac,
|
|
}
|
|
|
|
_, err := handler.validateMac(rundata)
|
|
require.Error(t, err)
|
|
})
|
|
|
|
t.Run("validate incorrect mac", func(t *testing.T) {
|
|
name := "org/reponame"
|
|
run := "1"
|
|
ts := strconv.FormatInt(time.Now().Unix(), 10)
|
|
|
|
rundata := cacheproxy.RunData{
|
|
RepositoryFullName: name,
|
|
RunNumber: run,
|
|
Timestamp: ts,
|
|
RepositoryMAC: "this is not the right mac :D",
|
|
}
|
|
|
|
repoName, err := handler.validateMac(rundata)
|
|
require.Error(t, err)
|
|
require.Equal(t, "", repoName)
|
|
})
|
|
|
|
t.Run("compute correct mac", func(t *testing.T) {
|
|
secret := "this is my cool secret string :3"
|
|
name := "org/reponame"
|
|
run := "42"
|
|
ts := "1337"
|
|
|
|
mac := computeMac(secret, name, run, ts, "")
|
|
expectedMac := "4754474b21329e8beadd2b4054aa4be803965d66e710fa1fee091334ed804f29" // * Precomputed, anytime the computeMac function changes this needs to be recalculated
|
|
require.Equal(t, expectedMac, mac)
|
|
|
|
mac = computeMac(secret, name, run, ts, "refs/pull/12/head")
|
|
expectedMac = "9ca8f4cb5e1b083ee8cd215215bc00f379b28511d3ef7930bf054767de34766d" // * Precomputed, anytime the computeMac function changes this needs to be recalculated
|
|
require.Equal(t, expectedMac, mac)
|
|
})
|
|
}
|