|
| 1 | +package license |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +func TestCommercialTrustOK(t *testing.T) { |
| 10 | + now := time.Date(2026, 9, 11, 12, 0, 0, 0, time.UTC) |
| 11 | + origAir, origVerified := airGappedFn, verifiedAtFn |
| 12 | + t.Cleanup(func() { |
| 13 | + airGappedFn = origAir |
| 14 | + verifiedAtFn = origVerified |
| 15 | + }) |
| 16 | + |
| 17 | + t.Run("airgapped always ok", func(t *testing.T) { |
| 18 | + airGappedFn = func() bool { return true } |
| 19 | + verifiedAtFn = func() string { return "" } |
| 20 | + if !CommercialTrustOK(now) { |
| 21 | + t.Fatal("expected AirGapped trust OK") |
| 22 | + } |
| 23 | + }) |
| 24 | + |
| 25 | + t.Run("missing stamp not trusted", func(t *testing.T) { |
| 26 | + airGappedFn = func() bool { return false } |
| 27 | + verifiedAtFn = func() string { return "" } |
| 28 | + if CommercialTrustOK(now) { |
| 29 | + t.Fatal("missing stamp must not trust") |
| 30 | + } |
| 31 | + }) |
| 32 | + |
| 33 | + t.Run("fresh stamp trusted", func(t *testing.T) { |
| 34 | + airGappedFn = func() bool { return false } |
| 35 | + verifiedAtFn = func() string { return now.Add(-24 * time.Hour).Format(time.RFC3339) } |
| 36 | + if !CommercialTrustOK(now) { |
| 37 | + t.Fatal("expected fresh stamp trusted") |
| 38 | + } |
| 39 | + }) |
| 40 | + |
| 41 | + t.Run("stale stamp not trusted", func(t *testing.T) { |
| 42 | + airGappedFn = func() bool { return false } |
| 43 | + verifiedAtFn = func() string { |
| 44 | + return now.Add(-(LicenseVerifyMaxAge + time.Hour)).Format(time.RFC3339) |
| 45 | + } |
| 46 | + if CommercialTrustOK(now) { |
| 47 | + t.Fatal("stale stamp must not trust") |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + t.Run("exactly 30d still trusted", func(t *testing.T) { |
| 52 | + airGappedFn = func() bool { return false } |
| 53 | + verifiedAtFn = func() string { return now.Add(-LicenseVerifyMaxAge).Format(time.RFC3339) } |
| 54 | + if !CommercialTrustOK(now) { |
| 55 | + t.Fatal("age == 30d should still trust") |
| 56 | + } |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +func TestLicenseVerifyAttemptDue(t *testing.T) { |
| 61 | + now := time.Date(2026, 9, 11, 12, 0, 0, 0, time.UTC) |
| 62 | + origAir, origVerified, origAttempt := airGappedFn, verifiedAtFn, verifyAttemptAtFn |
| 63 | + t.Cleanup(func() { |
| 64 | + airGappedFn = origAir |
| 65 | + verifiedAtFn = origVerified |
| 66 | + verifyAttemptAtFn = origAttempt |
| 67 | + }) |
| 68 | + |
| 69 | + airGappedFn = func() bool { return false } |
| 70 | + verifiedAtFn = func() string { return now.Add(-time.Hour).Format(time.RFC3339) } |
| 71 | + |
| 72 | + t.Run("missing attempt due", func(t *testing.T) { |
| 73 | + verifyAttemptAtFn = func() string { return "" } |
| 74 | + if !LicenseVerifyAttemptDue(now) { |
| 75 | + t.Fatal("expected due") |
| 76 | + } |
| 77 | + }) |
| 78 | + |
| 79 | + t.Run("recent attempt not due", func(t *testing.T) { |
| 80 | + verifyAttemptAtFn = func() string { return now.Add(-time.Hour).Format(time.RFC3339) } |
| 81 | + if LicenseVerifyAttemptDue(now) { |
| 82 | + t.Fatal("expected not due") |
| 83 | + } |
| 84 | + }) |
| 85 | + |
| 86 | + t.Run("interval elapsed due", func(t *testing.T) { |
| 87 | + verifyAttemptAtFn = func() string { |
| 88 | + return now.Add(-(LicenseVerifyAttemptInterval + time.Minute)).Format(time.RFC3339) |
| 89 | + } |
| 90 | + if !LicenseVerifyAttemptDue(now) { |
| 91 | + t.Fatal("expected due after interval") |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("missing success stamp due immediately", func(t *testing.T) { |
| 96 | + verifiedAtFn = func() string { return "" } |
| 97 | + verifyAttemptAtFn = func() string { return now.Format(time.RFC3339) } |
| 98 | + if !LicenseVerifyAttemptDue(now) { |
| 99 | + t.Fatal("missing success stamp must force attempt") |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + t.Run("airgapped never due", func(t *testing.T) { |
| 104 | + airGappedFn = func() bool { return true } |
| 105 | + verifiedAtFn = func() string { return "" } |
| 106 | + verifyAttemptAtFn = func() string { return "" } |
| 107 | + if LicenseVerifyAttemptDue(now) { |
| 108 | + t.Fatal("AirGapped must skip attempts") |
| 109 | + } |
| 110 | + }) |
| 111 | +} |
| 112 | + |
| 113 | +func TestVerifyAndStampAccessTokenLeavesTokenOnFailure(t *testing.T) { |
| 114 | + now := time.Date(2026, 9, 11, 12, 0, 0, 0, time.UTC) |
| 115 | + puts := map[string]string{} |
| 116 | + origPut, origSet := putSettingFn, setAccessToken |
| 117 | + t.Cleanup(func() { |
| 118 | + putSettingFn = origPut |
| 119 | + setAccessToken = origSet |
| 120 | + }) |
| 121 | + |
| 122 | + putSettingFn = func(name string, value interface{}) error { |
| 123 | + puts[name] = value.(string) |
| 124 | + return nil |
| 125 | + } |
| 126 | + setAccessToken = func(string) error { |
| 127 | + return errors.New("revoked") |
| 128 | + } |
| 129 | + |
| 130 | + err := VerifyAndStampAccessToken("tok", now) |
| 131 | + if err == nil { |
| 132 | + t.Fatal("expected verify error") |
| 133 | + } |
| 134 | + if _, ok := puts["last_license_verify_attempt_at"]; !ok { |
| 135 | + t.Fatal("expected attempt stamp") |
| 136 | + } |
| 137 | + if _, ok := puts["last_license_verified_at"]; ok { |
| 138 | + t.Fatal("must not stamp success on failure") |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestVerifyAndStampAccessTokenSuccess(t *testing.T) { |
| 143 | + now := time.Date(2026, 9, 11, 12, 0, 0, 0, time.UTC) |
| 144 | + puts := map[string]string{} |
| 145 | + origPut, origSet := putSettingFn, setAccessToken |
| 146 | + t.Cleanup(func() { |
| 147 | + putSettingFn = origPut |
| 148 | + setAccessToken = origSet |
| 149 | + }) |
| 150 | + |
| 151 | + putSettingFn = func(name string, value interface{}) error { |
| 152 | + puts[name] = value.(string) |
| 153 | + return nil |
| 154 | + } |
| 155 | + setAccessToken = func(string) error { return nil } |
| 156 | + |
| 157 | + if err := VerifyAndStampAccessToken("tok", now); err != nil { |
| 158 | + t.Fatal(err) |
| 159 | + } |
| 160 | + if puts["last_license_verified_at"] != now.UTC().Format(time.RFC3339) { |
| 161 | + t.Fatalf("verified stamp = %q", puts["last_license_verified_at"]) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +func withCommercialTrustOK(t *testing.T) { |
| 166 | + t.Helper() |
| 167 | + origAir, origVerified := airGappedFn, verifiedAtFn |
| 168 | + airGappedFn = func() bool { return false } |
| 169 | + verifiedAtFn = func() string { return time.Now().UTC().Format(time.RFC3339) } |
| 170 | + t.Cleanup(func() { |
| 171 | + airGappedFn = origAir |
| 172 | + verifiedAtFn = origVerified |
| 173 | + }) |
| 174 | +} |
0 commit comments