64 lines
2.2 KiB
Go
64 lines
2.2 KiB
Go
package licensing
|
|
|
|
import (
|
|
"crypto/ed25519"
|
|
"crypto/rand"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSignedLicense(t *testing.T) {
|
|
pub, priv, err := ed25519.GenerateKey(rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Unix(1_800_000_000, 0)
|
|
claims := Claims{Version: 1, Customer: "Example", Plan: "pro", Features: []string{FeatureCustomText}, Domains: []string{"*.example.org"}, IssuedAt: now.Add(-time.Hour).Unix(), ExpiresAt: now.Add(24 * time.Hour).Unix()}
|
|
token, err := Sign(priv, claims)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m := New(EncodeKey(pub), token, "https://ai.example.org", false, now)
|
|
if !m.Has(FeatureCustomText) || m.Status().Edition != "pro" {
|
|
t.Fatalf("unexpected status: %#v", m.Status())
|
|
}
|
|
}
|
|
|
|
func TestExpiredLicenseFallsBackToCommunity(t *testing.T) {
|
|
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
|
|
now := time.Unix(1_800_000_000, 0)
|
|
claims := Claims{Version: 1, Customer: "Example", Plan: "pro", Features: []string{FeatureCustomBadge}, IssuedAt: now.Add(-2 * time.Hour).Unix(), ExpiresAt: now.Add(-time.Hour).Unix()}
|
|
// Sign validates relative to issuedAt, so an already-expired token can still be created for the verification test.
|
|
token, err := Sign(priv, claims)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m := New(EncodeKey(pub), token, "https://example.org", false, now)
|
|
if m.Status().Edition != "community" || m.Has(FeatureCustomBadge) {
|
|
t.Fatalf("unexpected status: %#v", m.Status())
|
|
}
|
|
}
|
|
|
|
func TestGlobalDomainWildcardAllowsAnyHost(t *testing.T) {
|
|
pub, priv, err := ed25519.GenerateKey(rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Unix(1_800_000_000, 0)
|
|
claims := Claims{
|
|
Version: 1, Customer: "Global", Plan: "pro",
|
|
Features: []string{FeatureCustomBadge}, Domains: []string{"*"},
|
|
IssuedAt: now.Add(-time.Hour).Unix(), ExpiresAt: now.Add(24 * time.Hour).Unix(),
|
|
}
|
|
token, err := Sign(priv, claims)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, baseURL := range []string{"https://example.org", "https://ai.customer.test", "http://localhost:8080"} {
|
|
m := New(EncodeKey(pub), token, baseURL, false, now)
|
|
if !m.Has(FeatureCustomBadge) || m.Status().Edition != "pro" {
|
|
t.Fatalf("global wildcard did not allow %s: %#v", baseURL, m.Status())
|
|
}
|
|
}
|
|
}
|