| 1 | package github |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/rastrillo/rastrillo/gormlite" |
| 5 | "context" |
| 6 | "database/sql" |
| 7 | "errors" |
| 8 | "gorm.io/gorm/logger" |
| 9 | "gorm.io/plugin/dbresolver" |
| 10 | "net/http" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "gorm.io/gorm" |
| 17 | ) |
| 18 | |
| 19 | func TestForgeLateResults(t *testing.T) { |
| 20 | for _, phase := range []string{"convert", "check"} { |
| 21 | for _, action := range []string{"disconnect", "supersede", "remove owner"} { |
| 22 | t.Run(phase+"/"+action, func(t *testing.T) { |
| 23 | r := storeRig(t) |
| 24 | ctx := context.Background() |
| 25 | began, e := r.s.Start(ctx, r.owner, r.g.Login, r.g.AccountType) |
| 26 | if e != nil { |
| 27 | t.Fatal(e) |
| 28 | } |
| 29 | id := began.View.ID |
| 30 | if phase == "check" { |
| 31 | if _, e = r.s.Convert(ctx, r.owner, began.State, r.g.Code); e != nil { |
| 32 | t.Fatal(e) |
| 33 | } |
| 34 | } |
| 35 | entered, release := make(chan struct{}), make(chan struct{}) |
| 36 | base := r.g.Transport() |
| 37 | r.s.API = NewAPI(transportFunc(func(req *http.Request) (*http.Response, error) { |
| 38 | block := phase == "convert" && strings.Contains(req.URL.Path, "/conversions") || phase == "check" && req.URL.Path == "/installation/repositories" |
| 39 | response, e := base.RoundTrip(req) |
| 40 | if block { |
| 41 | close(entered) |
| 42 | <-release |
| 43 | } |
| 44 | return response, e |
| 45 | }), func() time.Time { return r.now }) |
| 46 | done := make(chan error, 1) |
| 47 | go func() { |
| 48 | if phase == "convert" { |
| 49 | _, e := r.s.Convert(ctx, r.owner, began.State, r.g.Code) |
| 50 | done <- e |
| 51 | } else { |
| 52 | _, e := r.s.Check(ctx, r.owner, id) |
| 53 | done <- e |
| 54 | } |
| 55 | }() |
| 56 | select { |
| 57 | case <-entered: |
| 58 | case <-time.After(5 * time.Second): |
| 59 | close(release) |
| 60 | t.Fatal("API did not enter") |
| 61 | } |
| 62 | // Another process uses the primary database, not an in-memory authority lock. |
| 63 | other := *r.s |
| 64 | other.API = NewAPI(r.g.Transport(), func() time.Time { return r.now }) |
| 65 | switch action { |
| 66 | case "disconnect": |
| 67 | _, e = other.Disconnect(ctx, r.other, id) |
| 68 | case "supersede": |
| 69 | _, e = other.Start(ctx, r.other, "another-account", "Organization") |
| 70 | case "remove owner": |
| 71 | e = r.s.DB.Model(&testOwner{}).Where("address = ?", r.owner.Address).Update("active", false).Error |
| 72 | } |
| 73 | close(release) |
| 74 | if e != nil { |
| 75 | t.Fatal(e) |
| 76 | } |
| 77 | if e = <-done; e == nil { |
| 78 | t.Fatal("late result restored lost authority") |
| 79 | } |
| 80 | var c Connection |
| 81 | if e = r.s.DB.First(&c, "id = ?", id).Error; e != nil { |
| 82 | t.Fatal(e) |
| 83 | } |
| 84 | if action != "remove owner" && c.SealedKey != "" { |
| 85 | t.Fatal("late result restored key") |
| 86 | } |
| 87 | if c.Confirmed { |
| 88 | t.Fatal("late result activated") |
| 89 | } |
| 90 | r.s.API.mu.Lock() |
| 91 | n := len(r.s.API.tokens) |
| 92 | r.s.API.mu.Unlock() |
| 93 | if n != 0 { |
| 94 | t.Fatal("late inventory retained a token") |
| 95 | } |
| 96 | }) |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func TestForgeClaimAcrossProcesses(t *testing.T) { |
| 102 | r := storeRig(t) |
| 103 | ctx := context.Background() |
| 104 | began, e := r.s.Start(ctx, r.owner, r.g.Login, r.g.AccountType) |
| 105 | if e != nil { |
| 106 | t.Fatal(e) |
| 107 | } |
| 108 | entered, release := make(chan struct{}), make(chan struct{}) |
| 109 | base := r.g.Transport() |
| 110 | r.s.API = NewAPI(transportFunc(func(req *http.Request) (*http.Response, error) { |
| 111 | if strings.Contains(req.URL.Path, "/conversions") { |
| 112 | close(entered) |
| 113 | <-release |
| 114 | } |
| 115 | return base.RoundTrip(req) |
| 116 | }), func() time.Time { return r.now }) |
| 117 | done := make(chan error, 1) |
| 118 | go func() { _, e := r.s.Convert(ctx, r.owner, began.State, r.g.Code); done <- e }() |
| 119 | <-entered |
| 120 | other := *r.s |
| 121 | other.API = NewAPI(r.g.Transport(), func() time.Time { return r.now }) |
| 122 | if _, e = other.Convert(ctx, r.owner, began.State, r.g.Code); e == nil { |
| 123 | close(release) |
| 124 | t.Fatal("concurrent state reused") |
| 125 | } |
| 126 | close(release) |
| 127 | if e = <-done; e != nil { |
| 128 | t.Fatal(e) |
| 129 | } |
| 130 | if r.g.Calls("POST", "/app-manifests/"+r.g.Code+"/conversions") != 1 { |
| 131 | t.Fatal("multiple conversions") |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func TestForgeRosterAndAuditGuards(t *testing.T) { |
| 136 | for _, action := range []string{"start", "convert", "install", "check", "connect", "disconnect"} { |
| 137 | for _, failure := range []string{"roster", "audit"} { |
| 138 | t.Run(action+"/"+failure, func(t *testing.T) { |
| 139 | r := storeRig(t) |
| 140 | ctx := context.Background() |
| 141 | began, e := r.s.Start(ctx, r.owner, r.g.Login, r.g.AccountType) |
| 142 | if e != nil { |
| 143 | t.Fatal(e) |
| 144 | } |
| 145 | if action != "start" && action != "convert" { |
| 146 | if _, e = r.s.Convert(ctx, r.owner, began.State, r.g.Code); e != nil { |
| 147 | t.Fatal(e) |
| 148 | } |
| 149 | } |
| 150 | var before Connection |
| 151 | r.s.DB.First(&before, "id = ?", began.View.ID) |
| 152 | if failure == "roster" { |
| 153 | r.s.DB.Model(&testOwner{}).Where("address = ?", r.owner.Address).Update("active", false) |
| 154 | } else { |
| 155 | r.s.Audit = func(*gorm.DB, string, string, string) error { return errors.New("audit refused") } |
| 156 | } |
| 157 | switch action { |
| 158 | case "start": |
| 159 | _, e = r.s.Start(ctx, r.owner, "next-account", "Organization") |
| 160 | case "convert": |
| 161 | _, e = r.s.Convert(ctx, r.owner, began.State, r.g.Code) |
| 162 | case "install": |
| 163 | _, e = r.s.Install(ctx, r.owner, r.s.state(before, "installation")) |
| 164 | case "check": |
| 165 | _, e = r.s.Check(ctx, r.owner, began.View.ID) |
| 166 | case "connect": |
| 167 | _, e = r.s.Connect(ctx, r.owner, began.View.ID) |
| 168 | case "disconnect": |
| 169 | _, e = r.s.Disconnect(ctx, r.owner, began.View.ID) |
| 170 | } |
| 171 | if e == nil { |
| 172 | t.Fatal("forbidden write accepted") |
| 173 | } |
| 174 | var after Connection |
| 175 | r.s.DB.First(&after, "id = ?", began.View.ID) |
| 176 | if before != after { |
| 177 | t.Fatal("refused transaction changed connection") |
| 178 | } |
| 179 | }) |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | func TestForgeUnreadableKey(t *testing.T) { |
| 184 | for _, mode := range []string{"root", "ciphertext"} { |
| 185 | t.Run(mode, func(t *testing.T) { |
| 186 | r := storeRig(t) |
| 187 | v := r.created(t) |
| 188 | if mode == "root" { |
| 189 | r.s.Root = "new-instance-key" |
| 190 | } else { |
| 191 | r.s.DB.Model(&Connection{}).Where("id = ?", v.ID).Update("sealed_key", "corrupt") |
| 192 | } |
| 193 | got, e := r.s.Check(context.Background(), r.other, v.ID) |
| 194 | if !errors.Is(e, ErrReconnect) || got.State != "reconnect" || got.SettingsURL == "" { |
| 195 | t.Fatal("unreadable key not reconnect", e) |
| 196 | } |
| 197 | var c Connection |
| 198 | r.s.DB.First(&c, "id = ?", v.ID) |
| 199 | if c.SealedKey != "" { |
| 200 | t.Fatal("unreadable key retained") |
| 201 | } |
| 202 | }) |
| 203 | } |
| 204 | } |
| 205 | func TestForgeRecheckSuspensionAndRateLimit(t *testing.T) { |
| 206 | r := storeRig(t) |
| 207 | v := r.created(t) |
| 208 | ctx := context.Background() |
| 209 | r.g.Suspended = true |
| 210 | got, e := r.s.Connect(ctx, r.owner, v.ID) |
| 211 | if e == nil || got.Confirmed || got.State != "suspended" { |
| 212 | t.Fatal("suspended Connect admitted") |
| 213 | } |
| 214 | r.g.Suspended = false |
| 215 | if _, e = r.s.Connect(ctx, r.owner, v.ID); e != nil { |
| 216 | t.Fatal(e) |
| 217 | } |
| 218 | r.g.FailPath = "/app/installations" |
| 219 | r.g.FailStatus = 403 |
| 220 | r.g.FailHeaders = http.Header{"X-Ratelimit-Remaining": {"0"}} |
| 221 | got, e = r.s.Check(ctx, r.owner, v.ID) |
| 222 | if !errors.Is(e, ErrRateLimit) || got.State != "check_failed" { |
| 223 | t.Fatal("rate limit became lost access") |
| 224 | } |
| 225 | var c Connection |
| 226 | r.s.DB.First(&c, "id = ?", v.ID) |
| 227 | if c.SealedKey == "" || !c.UninstalledAt.IsZero() || c.ActiveAccount == nil { |
| 228 | t.Fatal("rate limit revoked connection") |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | func TestForgeConversionVerification(t *testing.T) { |
| 233 | r := storeRig(t) |
| 234 | ctx := context.Background() |
| 235 | began, e := r.s.Start(ctx, r.owner, r.g.Login, r.g.AccountType) |
| 236 | if e != nil { |
| 237 | t.Fatal(e) |
| 238 | } |
| 239 | r.g.FailPath = "/app" |
| 240 | r.g.FailStatus = 403 |
| 241 | got, e := r.s.Convert(ctx, r.owner, began.State, r.g.Code) |
| 242 | if e == nil || got.State != "failed" { |
| 243 | t.Fatal("conversion activated without verified App") |
| 244 | } |
| 245 | var c Connection |
| 246 | r.s.DB.First(&c, "id = ?", began.View.ID) |
| 247 | if c.SealedKey != "" { |
| 248 | t.Fatal("failed verification retained key") |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | func TestForgeUniqueAppAndNewNames(t *testing.T) { |
| 253 | r := storeRig(t) |
| 254 | ctx := context.Background() |
| 255 | first, e := r.s.Start(ctx, r.owner, r.g.Login, r.g.AccountType) |
| 256 | if e != nil { |
| 257 | t.Fatal(e) |
| 258 | } |
| 259 | second, e := r.s.Start(ctx, r.other, r.g.Login, r.g.AccountType) |
| 260 | if e != nil { |
| 261 | t.Fatal(e) |
| 262 | } |
| 263 | if first.Name == second.Name || first.State == second.State { |
| 264 | t.Fatal("reconnect reused App name or state") |
| 265 | } |
| 266 | account := r.g.AccountID |
| 267 | if e = r.s.DB.Create(&Connection{ID: "live-a", ActiveAccount: &account}).Error; e != nil { |
| 268 | t.Fatal(e) |
| 269 | } |
| 270 | if e = r.s.DB.Create(&Connection{ID: "live-b", ActiveAccount: &account}).Error; e == nil { |
| 271 | t.Fatal("duplicate active account admitted") |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | func TestForgeOwnerBindingIndependentOfSession(t *testing.T) { |
| 276 | r := storeRig(t) |
| 277 | ctx := context.Background() |
| 278 | began, e := r.s.Start(ctx, r.owner, r.g.Login, r.g.AccountType) |
| 279 | if e != nil { |
| 280 | t.Fatal(e) |
| 281 | } |
| 282 | p := r.other |
| 283 | p.Session = r.owner.Session |
| 284 | if _, e = r.s.Convert(ctx, p, began.State, r.g.Code); e == nil { |
| 285 | t.Fatal("callback owner binding omitted") |
| 286 | } |
| 287 | if r.g.Calls("POST", "/app-manifests/"+r.g.Code+"/conversions") != 0 { |
| 288 | t.Fatal("wrong owner consumed code") |
| 289 | } |
| 290 | } |
| 291 | func TestForgeFlagBeforeTransaction(t *testing.T) { |
| 292 | r := storeRig(t) |
| 293 | r.s.Enabled = false |
| 294 | called := false |
| 295 | write := r.s.Write |
| 296 | r.s.Write = func(ctx context.Context, fn func(*gorm.DB) error) error { called = true; return write(ctx, fn) } |
| 297 | if _, e := r.s.Start(context.Background(), r.owner, r.g.Login, r.g.AccountType); e == nil || called { |
| 298 | t.Fatal("disabled Start entered a write") |
| 299 | } |
| 300 | } |
| 301 | func TestForgeConversionGeneration(t *testing.T) { |
| 302 | r := storeRig(t) |
| 303 | ctx := context.Background() |
| 304 | began, e := r.s.Start(ctx, r.owner, r.g.Login, r.g.AccountType) |
| 305 | if e != nil { |
| 306 | t.Fatal(e) |
| 307 | } |
| 308 | base := r.g.Transport() |
| 309 | r.s.API = NewAPI(transportFunc(func(req *http.Request) (*http.Response, error) { |
| 310 | response, e := base.RoundTrip(req) |
| 311 | if strings.Contains(req.URL.Path, "/conversions") { |
| 312 | if err := r.s.DB.Model(&Connection{}).Where("id = ?", began.View.ID).Update("generation", gorm.Expr("generation + 1")).Error; err != nil { |
| 313 | t.Fatal(err) |
| 314 | } |
| 315 | } |
| 316 | return response, e |
| 317 | }), func() time.Time { return r.now }) |
| 318 | if _, e = r.s.Convert(ctx, r.owner, began.State, r.g.Code); e == nil { |
| 319 | t.Fatal("conversion ignored generation fence") |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | func TestForgeAccountAndStageBinding(t *testing.T) { |
| 324 | for _, change := range []string{"login", "type", "stage"} { |
| 325 | t.Run(change, func(t *testing.T) { |
| 326 | r := storeRig(t) |
| 327 | ctx := context.Background() |
| 328 | began, e := r.s.Start(ctx, r.owner, r.g.Login, r.g.AccountType) |
| 329 | if e != nil { |
| 330 | t.Fatal(e) |
| 331 | } |
| 332 | switch change { |
| 333 | case "login": |
| 334 | r.g.Login = "different-account" |
| 335 | case "type": |
| 336 | r.g.AccountType = "User" |
| 337 | case "stage": |
| 338 | if _, e = r.s.Install(ctx, r.owner, began.State); e == nil { |
| 339 | t.Fatal("manifest state used for installation") |
| 340 | } |
| 341 | return |
| 342 | } |
| 343 | v, e := r.s.Convert(ctx, r.owner, began.State, r.g.Code) |
| 344 | if e == nil || v.State != "failed" { |
| 345 | t.Fatal("wrong intended account accepted") |
| 346 | } |
| 347 | }) |
| 348 | } |
| 349 | } |
| 350 | func TestForgeReadsUsePrimary(t *testing.T) { |
| 351 | r := storeRig(t) |
| 352 | v := r.created(t) |
| 353 | writer, e := r.s.DB.DB() |
| 354 | if e != nil { |
| 355 | t.Fatal(e) |
| 356 | } |
| 357 | replica, e := sql.Open("sqlite", filepath.Join(t.TempDir(), "empty-replica.db")) |
| 358 | if e != nil { |
| 359 | t.Fatal(e) |
| 360 | } |
| 361 | defer replica.Close() |
| 362 | g, e := gorm.Open(gormlite.Dialector{Conn: writer}, &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)}) |
| 363 | if e != nil { |
| 364 | t.Fatal(e) |
| 365 | } |
| 366 | if e = g.Use(dbresolver.Register(dbresolver.Config{Replicas: []gorm.Dialector{gormlite.Dialector{Conn: replica}}})); e != nil { |
| 367 | t.Fatal(e) |
| 368 | } |
| 369 | r.s.DB = g |
| 370 | rows, e := r.s.List(context.Background(), r.other) |
| 371 | if e != nil || len(rows) != 1 { |
| 372 | t.Fatal("owner read hit stale replica", e) |
| 373 | } |
| 374 | if _, e = r.s.InstallationURL(context.Background(), r.owner, v.ID); e != nil { |
| 375 | t.Fatal("installation redirect hit replica", e) |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | func TestForgeSameAppRename(t *testing.T) { |
| 380 | r := storeRig(t) |
| 381 | v := r.created(t) |
| 382 | r.s.API = NewAPI(alter(r.g, "/app", func(body map[string]any) { body["slug"] = "renamed-app" }), func() time.Time { return r.now }) |
| 383 | got, e := r.s.Check(context.Background(), r.other, v.ID) |
| 384 | if e != nil || got.State != "ready" || !strings.HasSuffix(got.SettingsURL, "/renamed-app") { |
| 385 | t.Fatal("same numeric App identity lost after rename", e) |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | func TestForgeVerifiedInventoryScope(t *testing.T) { |
| 390 | r := storeRig(t) |
| 391 | v := r.created(t) |
| 392 | base := r.g.Transport() |
| 393 | conditional := 0 |
| 394 | r.s.API = NewAPI(transportFunc(func(req *http.Request) (*http.Response, error) { |
| 395 | if req.URL.Path == "/installation/repositories" && req.Header.Get("If-None-Match") != "" { |
| 396 | conditional++ |
| 397 | } |
| 398 | return base.RoundTrip(req) |
| 399 | }), func() time.Time { return r.now }) |
| 400 | for i := 0; i < 2; i++ { |
| 401 | if _, err := r.s.Check(context.Background(), r.other, v.ID); err != nil { |
| 402 | t.Fatal(err) |
| 403 | } |
| 404 | } |
| 405 | if conditional != 0 { |
| 406 | t.Fatal("verified App discarded the connection generation; stale ETag reused") |
| 407 | } |
| 408 | } |
| 409 | |