|
| 1 | +package secrets |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +var errCodesignFailed = errors.New("codesign failed") |
| 9 | + |
| 10 | +func TestResolveKeychainTrustApplication(t *testing.T) { |
| 11 | + t.Parallel() |
| 12 | + |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + goos string |
| 16 | + backend string |
| 17 | + override string |
| 18 | + output string |
| 19 | + runnerErr error |
| 20 | + want bool |
| 21 | + wantForced bool |
| 22 | + wantRuns int |
| 23 | + wantApplies bool |
| 24 | + }{ |
| 25 | + {name: "unsigned", goos: "darwin", backend: "keychain", output: "code object is not signed at all", runnerErr: errCodesignFailed, wantRuns: 1, wantApplies: true}, |
| 26 | + {name: "ad-hoc", goos: "darwin", backend: "keychain", output: "Executable=/tmp/gog\nSignature=adhoc\nTeamIdentifier=not set", wantRuns: 1, wantApplies: true}, |
| 27 | + {name: "developer id", goos: "darwin", backend: "keychain", output: "Executable=/tmp/gog\nIdentifier=org.openclaw.gog\nTeamIdentifier=Y5PE65HELJ", want: true, wantRuns: 1, wantApplies: true}, |
| 28 | + {name: "team identifier not set", goos: "darwin", backend: "auto", output: "Signature=adhoc\nTeamIdentifier=not set", wantRuns: 1, wantApplies: true}, |
| 29 | + {name: "runner error", goos: "darwin", backend: "auto", runnerErr: errCodesignFailed, wantRuns: 1, wantApplies: true}, |
| 30 | + {name: "non-darwin", goos: "linux", backend: "keychain", override: "true", wantRuns: 0}, |
| 31 | + {name: "file backend", goos: "darwin", backend: "file", override: "true", wantRuns: 0}, |
| 32 | + {name: "forced true", goos: "darwin", backend: "keychain", override: "TrUe", want: true, wantForced: true, wantRuns: 0, wantApplies: true}, |
| 33 | + {name: "forced one", goos: "darwin", backend: "keychain", override: "1", want: true, wantForced: true, wantRuns: 0, wantApplies: true}, |
| 34 | + {name: "forced false", goos: "darwin", backend: "keychain", override: "FALSE", wantForced: true, wantRuns: 0, wantApplies: true}, |
| 35 | + {name: "forced zero", goos: "darwin", backend: "keychain", override: "0", wantForced: true, wantRuns: 0, wantApplies: true}, |
| 36 | + {name: "invalid uses auto", goos: "darwin", backend: "keychain", override: "sometimes", output: "TeamIdentifier=Y5PE65HELJ", want: true, wantRuns: 1, wantApplies: true}, |
| 37 | + {name: "explicit auto", goos: "darwin", backend: "keychain", override: "AUTO", output: "TeamIdentifier=Y5PE65HELJ", want: true, wantRuns: 1, wantApplies: true}, |
| 38 | + } |
| 39 | + |
| 40 | + for _, tt := range tests { |
| 41 | + t.Run(tt.name, func(t *testing.T) { |
| 42 | + t.Parallel() |
| 43 | + |
| 44 | + runs := 0 |
| 45 | + options := OpenOptions{ |
| 46 | + GOOS: tt.goos, |
| 47 | + KeychainTrustApplication: tt.override, |
| 48 | + codesignRunner: func(path string) ([]byte, error) { |
| 49 | + runs++ |
| 50 | + |
| 51 | + if path == "" { |
| 52 | + t.Fatal("codesign path is empty") |
| 53 | + } |
| 54 | + |
| 55 | + return []byte(tt.output), tt.runnerErr |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + info := ResolveKeychainTrustApplication(options, KeyringBackendInfo{Value: tt.backend}) |
| 60 | + if info.Enabled != tt.want || info.Forced != tt.wantForced || info.Applicable != tt.wantApplies { |
| 61 | + t.Fatalf("info = %#v, want enabled=%t forced=%t applicable=%t", info, tt.want, tt.wantForced, tt.wantApplies) |
| 62 | + } |
| 63 | + |
| 64 | + if runs != tt.wantRuns { |
| 65 | + t.Fatalf("codesign runs = %d, want %d", runs, tt.wantRuns) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestCodesignOutputHasStableIdentityRejectsAdhocBeforeTeamIdentifier(t *testing.T) { |
| 72 | + t.Parallel() |
| 73 | + |
| 74 | + output := []byte("Signature=adhoc\nTeamIdentifier=Y5PE65HELJ\n") |
| 75 | + if codesignOutputHasStableIdentity(output) { |
| 76 | + t.Fatal("ad-hoc signature must not be trusted") |
| 77 | + } |
| 78 | +} |
0 commit comments