2023-03-01 20:12:04 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"hash/crc32"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"codeberg.org/ac/base62"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPAT_GenerateToken_Hashing(t *testing.T) {
|
2023-03-08 11:36:03 +01:00
|
|
|
hashedToken, plainToken, _ := generateNewToken()
|
2023-03-08 11:30:09 +01:00
|
|
|
expectedToken := sha256.Sum256([]byte(plainToken))
|
|
|
|
assert.Equal(t, hashedToken, string(expectedToken[:]))
|
2023-03-01 20:12:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPAT_GenerateToken_Prefix(t *testing.T) {
|
2023-03-08 11:36:03 +01:00
|
|
|
_, plainToken, _ := generateNewToken()
|
2023-03-06 13:49:07 +01:00
|
|
|
fourCharPrefix := plainToken[:4]
|
2023-03-08 11:54:10 +01:00
|
|
|
assert.Equal(t, PATPrefix, fourCharPrefix)
|
2023-03-01 20:12:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPAT_GenerateToken_Checksum(t *testing.T) {
|
2023-03-08 11:36:03 +01:00
|
|
|
_, plainToken, _ := generateNewToken()
|
2023-03-01 20:12:04 +01:00
|
|
|
tokenWithoutPrefix := strings.Split(plainToken, "_")[1]
|
|
|
|
if len(tokenWithoutPrefix) != 36 {
|
|
|
|
t.Fatal("Token has wrong length")
|
|
|
|
}
|
2023-03-06 13:49:07 +01:00
|
|
|
secret := tokenWithoutPrefix[:len(tokenWithoutPrefix)-6]
|
2023-03-01 20:12:04 +01:00
|
|
|
tokenCheckSum := tokenWithoutPrefix[len(tokenWithoutPrefix)-6:]
|
|
|
|
|
2023-03-06 13:49:07 +01:00
|
|
|
expectedChecksum := crc32.ChecksumIEEE([]byte(secret))
|
2023-03-01 20:12:04 +01:00
|
|
|
actualChecksum, err := base62.Decode(tokenCheckSum)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, expectedChecksum, actualChecksum)
|
|
|
|
}
|