mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-08-11 17:50:58 +00:00
81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
|
package artifactcache
|
||
|
|
||
|
import (
|
||
|
"strconv"
|
||
|
"testing"
|
||
|
"time"
|
||
|
|
||
|
"github.com/nektos/act/pkg/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, name, 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 := "09b0e9111660359d319c2d55c0664b5a6c5915c2f705b08af61aa63e7542f511" // * Precomputed, anytime the computeMac function changes this needs to be recalculated
|
||
|
|
||
|
require.Equal(t, mac, expectedMac)
|
||
|
})
|
||
|
}
|