CodeSampleX

示例

github.com/caddyserver/certmagic v0.23.0: Cache

已验证示例 — golang github.com/caddyserver/certmagic v0.23.0: Cache. contract 在 go 1.26 · linux alpine/x64 · docker 上运行并通过: certmagic.NewCache panics if…

sha256:a76f8f18502d9bc773fc57ffe0c768544c02254d18f7b4fba5ab65a5403e4849

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-28

案例

HOW
目标
verify github.com/caddyserver/certmagic.Cache in pkg:golang/github.com/caddyserver/certmagic@v0.23.0
包
符号
  • github.com/caddyserver/certmagic.Cache
创建时间
2026-08-28T02:03:32Z

契约

  1. certmagic.NewCache panics if GetConfigForCert is not provided in CacheOptions
  2. certmagic.NewCache returns a valid non-nil Cache when GetConfigForCert callback is configured
  3. certmagic.Cache stores and indexes certificates by exact domain SAN and wildcard SAN
  4. certmagic.Cache.AllMatchingCertificates retrieves certificates matching exact name or wildcard domain
  5. certmagic.Cache.AllMatchingCertificates returns empty slice for non-matching domain
  6. certmagic.Cache.Remove removes cached certificates by hash
  7. certmagic.Cache.SetOptions dynamically updates cache options while preserving configuration
  8. certmagic.Cache.Stop terminates background maintenance routines without error

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify github.com/caddyserver/certmagic.Cache in pkg:golang/github.com/caddyserver/certmagic@v0.23.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/caddyserver/certmagic@v0.23.0
Demonstrate these symbols/APIs:
  - github.com/caddyserver/certmagic.Cache

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:c651e21e41462925b83e67d94b2a190fe58d15cc6d96b8829b7012ca59b1c5ca","contract":["certmagic.NewCache panics if GetConfigForCert is not provided in CacheOptions","certmagic.NewCache returns a valid non-nil Cache when GetConfigForCert callback is configured","certmagic.Cache stores and indexes certificates by exact domain SAN and wildcard SAN","certmagic.Cache.AllMatchingCertificates retrieves certificates matching exact name or wildcard domain","certmagic.Cache.AllMatchingCertificates returns empty slice for non-matching domain","certmagic.Cache.Remove removes cached certificates by hash","certmagic.Cache.SetOptions dynamically updates cache options while preserving configuration","certmagic.Cache.Stop terminates background maintenance routines without error"],"goal":"verify github.com/caddyserver/certmagic.Cache in pkg:golang/github.com/caddyserver/certmagic@v0.23.0","kind":"HOW","packages":["pkg:golang/github.com/caddyserver/certmagic@v0.23.0"],"schemaVersion":1,"symbols":["github.com/caddyserver/certmagic.Cache"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/caddyserver/certmagic@v0.23.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/caddyserver/certmagic@v0.23.0","symbols":["github.com/caddyserver/certmagic.Cache"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.25.0

require github.com/caddyserver/certmagic v0.23.0

require (
	github.com/caddyserver/zerossl v0.1.5 // indirect
	github.com/klauspost/cpuid/v2 v2.3.0 // indirect
	github.com/libdns/libdns v1.1.1 // indirect
	github.com/mholt/acmez/v3 v3.1.6 // indirect
	github.com/miekg/dns v1.1.72 // indirect
	github.com/zeebo/blake3 v0.2.4 // indirect
	go.uber.org/multierr v1.11.0 // indirect
	go.uber.org/zap v1.27.1 // indirect
	go.uber.org/zap/exp v0.3.0 // indirect
	golang.org/x/crypto v0.50.0 // indirect
	golang.org/x/mod v0.35.0 // indirect
	golang.org/x/net v0.53.0 // indirect
	golang.org/x/sync v0.20.0 // indirect
	golang.org/x/sys v0.43.0 // indirect
	golang.org/x/text v0.36.0 // indirect
	golang.org/x/tools v0.44.0 // indirect
)
go.sum
code.pfad.fr/check v1.1.0 h1:GWvjdzhSEgHvEHe2uJujDcpmZoySKuHQNrZMfzfO0bE=
code.pfad.fr/check v1.1.0/go.mod h1:NiUH13DtYsb7xp5wll0U4SXx7KhXQVCtRgdC96IPfoM=
github.com/caddyserver/certmagic v0.23.0 h1:CfpZ/50jMfG4+1J/u2LV6piJq4HOfO6ppOnOf7DkFEU=
github.com/caddyserver/certmagic v0.23.0/go.mod h1:9mEZIWqqWoI+Gf+4Trh04MOVPD0tGSxtqsxg87hAIH4=
github.com/caddyserver/certmagic v0.25.4 h1:8eIXh0HC3MsGnNo8One+BCxMGTbe5zb/oz+2KsxBFQg=
github.com/caddyserver/certmagic v0.25.4/go.mod h1:YVs43D5+H/Dckt4bTga1KSO/xYfFBfVZainGDywYPAA=
github.com/caddyserver/zerossl v0.1.5 h1:dkvOjBAEEtY6LIGAHei7sw2UgqSD6TrWweXpV7lvEvE=
github.com/caddyserver/zerossl v0.1.5/go.mod h1:CxA0acn7oEGO6//4rtrRjYgEoa4MFw/XofZnrYwGqG4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-jose/go-jose/v4 v4.1.3 h1:CVLmWDhDVRa6Mi/IgCgaopNosCaHz7zrMeF9MlZRkrs=
github.com/go-jose/go-jose/v4 v4.1.3/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
github.com/letsencrypt/challtestsrv v1.4.2 h1:0ON3ldMhZyWlfVNYYpFuWRTmZNnyfiL9Hh5YzC3JVwU=
github.com/letsencrypt/challtestsrv v1.4.2/go.mod h1:GhqMqcSoeGpYd5zX5TgwA6er/1MbWzx/o7yuuVya+Wk=
github.com/letsencrypt/pebble/v2 v2.10.0 h1:Wq6gYXlsY6ubqI3hhxsTzdyotvfdjFBxuwYqCLCnj/U=
github.com/letsencrypt/pebble/v2 v2.10.0/go.mod h1:Sk8cmUIPcIdv2nINo+9PB4L+ZBhzY+F9A1a/h/xmWiQ=
github.com/libdns/libdns v1.1.1 h1:wPrHrXILoSHKWJKGd0EiAVmiJbFShguILTg9leS/P/U=
github.com/libdns/libdns v1.1.1/go.mod h1:4Bj9+5CQiNMVGf87wjX4CY3HQJypUHRuLvlsfsZqLWQ=
github.com/mholt/acmez/v3 v3.1.6 h1:eGVQNObP0pBN4sxqrXeg7MYqTOWyoiYpQqITVWlrevk=
github.com/mholt/acmez/v3 v3.1.6/go.mod h1:5nTPosTGosLxF3+LU4ygbgMRFDhbAVpqMI4+a4aHLBY=
github.com/miekg/dns v1.1.72 h1:vhmr+TF2A3tuoGNkLDFK9zi36F2LS+hKTRW0Uf8kbzI=
github.com/miekg/dns v1.1.72/go.mod h1:+EuEPhdHOsfk6Wk5TT2CzssZdqkmFhf8r+aVyDEToIs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/zeebo/assert v1.1.0 h1:hU1L1vLTHsnO8x8c9KAR5GmM5QscxHg5RNU5z5qbUWY=
github.com/zeebo/assert v1.1.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
github.com/zeebo/blake3 v0.2.4 h1:KYQPkhpRtcqh0ssGYcKLG1JYvddkEA8QwCM/yBqhaZI=
github.com/zeebo/blake3 v0.2.4/go.mod h1:7eeQ6d2iXWRGF6npfaxl2CU+xy2Fjo2gxeyZGCRUjcE=
github.com/zeebo/pcg v1.0.1 h1:lyqfGeWiv4ahac6ttHs+I5hwtH/+1mrhlCtVNQM2kHo=
github.com/zeebo/pcg v1.0.1/go.mod h1:09F0S9iiKrwn9rlI5yjLkmrug154/YRW6KnnXVDM/l4=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.1 h1:08RqriUEv8+ArZRYSTXy1LeBScaMpVSTBhCeaZYfMYc=
go.uber.org/zap v1.27.1/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
go.uber.org/zap/exp v0.3.0 h1:6JYzdifzYkGmTdRR59oYH+Ng7k49H9qVpWwNSsGJj3U=
go.uber.org/zap/exp v0.3.0/go.mod h1:5I384qq7XGxYyByIhHm6jg5CHkGY0nsTfbDLgDDlgJQ=
golang.org/x/crypto v0.50.0 h1:zO47/JPrL6vsNkINmLoo/PH1gcxpls50DNogFvB5ZGI=
golang.org/x/crypto v0.50.0/go.mod h1:3muZ7vA7PBCE6xgPX7nkzzjiUq87kRItoJQM1Yo8S+Q=
golang.org/x/mod v0.35.0 h1:Ww1D637e6Pg+Zb2KrWfHQUnH2dQRLBQyAtpr/haaJeM=
golang.org/x/mod v0.35.0/go.mod h1:+GwiRhIInF8wPm+4AoT6L0FA1QWAad3OMdTRx4tFYlU=
golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA=
golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs=
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
golang.org/x/tools v0.44.0 h1:UP4ajHPIcuMjT1GqzDWRlalUEoY+uzoZKnhOjbIPD2c=
golang.org/x/tools v0.44.0/go.mod h1:KA0AfVErSdxRZIsOVipbv3rQhVXTnlU6UhKxHd1seDI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
main.go
package main

import (
	"context"
	"crypto/ed25519"
	"crypto/rand"
	"crypto/x509"
	"crypto/x509/pkix"
	"encoding/pem"
	"fmt"
	"math/big"
	"time"

	"github.com/caddyserver/certmagic"
)

// generateTestCertificate creates an in-memory self-signed TLS certificate for testing Cache
func generateTestCertificate(dnsNames []string) ([]byte, []byte, error) {
	pub, priv, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		return nil, nil, err
	}

	template := x509.Certificate{
		SerialNumber: big.NewInt(1),
		Subject: pkix.Name{
			CommonName: dnsNames[0],
		},
		DNSNames:              dnsNames,
		NotBefore:             time.Now().Add(-1 * time.Hour),
		NotAfter:              time.Now().Add(24 * time.Hour),
		KeyUsage:              x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
		ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
		BasicConstraintsValid: true,
	}

	certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, pub, priv)
	if err != nil {
		return nil, nil, err
	}

	certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})

	privDER, err := x509.MarshalPKCS8PrivateKey(priv)
	if err != nil {
		return nil, nil, err
	}
	keyPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privDER})

	return certPEM, keyPEM, nil
}

func main() {
	ctx := context.Background()

	// 1. Initialize Cache with required GetConfigForCert callback
	var cache *certmagic.Cache
	cache = certmagic.NewCache(certmagic.CacheOptions{
		GetConfigForCert: func(cert certmagic.Certificate) (*certmagic.Config, error) {
			return certmagic.New(cache, certmagic.Config{}), nil
		},
		Capacity: 100,
	})
	defer cache.Stop()

	// 2. Associate Cache with a Config
	cfg := certmagic.New(cache, certmagic.Config{})

	// 3. Generate test certificate for example.com and *.example.com
	certPEM, keyPEM, err := generateTestCertificate([]string{"example.com", "*.example.com"})
	if err != nil {
		panic(err)
	}

	// 4. Cache unmanaged certificate with tags
	hash, err := cfg.CacheUnmanagedCertificatePEMBytes(ctx, certPEM, keyPEM, []string{"custom-tag"})
	if err != nil {
		panic(err)
	}
	fmt.Printf("Cached certificate hash: %s\n", hash)

	// 5. Query cached certificates by SAN
	matches := cache.AllMatchingCertificates("example.com")
	fmt.Printf("Matches for example.com: %d\n", len(matches))
	for _, m := range matches {
		fmt.Printf("  Names: %v, Tags: %v, Hash: %s, Expired: %v\n", m.Names, m.Tags, m.Hash(), m.Expired())
	}

	// Query wildcard match
	wildcardMatches := cache.AllMatchingCertificates("sub.example.com")
	fmt.Printf("Matches for sub.example.com (wildcard): %d\n", len(wildcardMatches))

	// 6. Remove certificate from cache
	cache.Remove([]string{hash})
	afterRemove := cache.AllMatchingCertificates("example.com")
	fmt.Printf("Matches for example.com after removal: %d\n", len(afterRemove))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/caddyserver/certmagic.Cache in pkg:golang/github.com/caddyserver/certmagic@v0.23.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/caddyserver/certmagic@v0.23.0"
  ],
  "symbols": [
    "github.com/caddyserver/certmagic.Cache"
  ]
}
test/contract.go
package main

import (
	"context"
	"crypto/ed25519"
	"crypto/rand"
	"crypto/x509"
	"crypto/x509/pkix"
	"encoding/pem"
	"fmt"
	"math/big"
	"os"
	"time"

	"github.com/caddyserver/certmagic"
)

// generateTestCertificate generates a self-signed TLS cert/key pair in PEM format purely in memory.
func generateTestCertificate(dnsNames []string) ([]byte, []byte, error) {
	pub, priv, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		return nil, nil, err
	}

	template := x509.Certificate{
		SerialNumber: big.NewInt(100),
		Subject: pkix.Name{
			CommonName: dnsNames[0],
		},
		DNSNames:              dnsNames,
		NotBefore:             time.Now().Add(-1 * time.Hour),
		NotAfter:              time.Now().Add(24 * time.Hour),
		KeyUsage:              x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
		ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
		BasicConstraintsValid: true,
	}

	certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, pub, priv)
	if err != nil {
		return nil, nil, err
	}

	certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})

	privDER, err := x509.MarshalPKCS8PrivateKey(priv)
	if err != nil {
		return nil, nil, err
	}
	keyPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privDER})

	return certPEM, keyPEM, nil
}

func main() {
	ctx := context.Background()

	// 1. certmagic.NewCache panics if GetConfigForCert is not provided in CacheOptions
	var panicked bool
	func() {
		defer func() {
			if r := recover(); r != nil {
				panicked = true
			}
		}()
		_ = certmagic.NewCache(certmagic.CacheOptions{})
	}()
	if !panicked {
		fmt.Fprintf(os.Stderr, "Contract failure: NewCache did not panic when GetConfigForCert was omitted\n")
		os.Exit(1)
	}

	// 2. certmagic.NewCache returns a valid non-nil Cache when GetConfigForCert callback is configured
	var cache *certmagic.Cache
	cache = certmagic.NewCache(certmagic.CacheOptions{
		GetConfigForCert: func(cert certmagic.Certificate) (*certmagic.Config, error) {
			return certmagic.New(cache, certmagic.Config{}), nil
		},
		Capacity:           50,
		RenewCheckInterval: 2 * time.Hour,
		OCSPCheckInterval:  1 * time.Hour,
	})
	if cache == nil {
		fmt.Fprintf(os.Stderr, "Contract failure: NewCache returned nil\n")
		os.Exit(1)
	}

	// 3. certmagic.Cache stores and indexes certificates by exact domain SAN and wildcard SAN
	cfg := certmagic.New(cache, certmagic.Config{})
	certPEM, keyPEM, err := generateTestCertificate([]string{"example.com", "*.example.com"})
	if err != nil {
		fmt.Fprintf(os.Stderr, "Contract failure: Failed to generate test cert: %v\n", err)
		os.Exit(1)
	}

	hash, err := cfg.CacheUnmanagedCertificatePEMBytes(ctx, certPEM, keyPEM, []string{"production", "v1"})
	if err != nil || hash == "" {
		fmt.Fprintf(os.Stderr, "Contract failure: CacheUnmanagedCertificatePEMBytes failed: %v\n", err)
		os.Exit(1)
	}

	// 4. certmagic.Cache.AllMatchingCertificates retrieves certificates matching exact name or wildcard domain
	exactMatches := cache.AllMatchingCertificates("example.com")
	if len(exactMatches) == 0 {
		fmt.Fprintf(os.Stderr, "Contract failure: AllMatchingCertificates found no match for example.com\n")
		os.Exit(1)
	}
	matchedCert := exactMatches[0]
	if matchedCert.Hash() != hash {
		fmt.Fprintf(os.Stderr, "Contract failure: Matched cert hash mismatch: got %s, expected %s\n", matchedCert.Hash(), hash)
		os.Exit(1)
	}
	if !matchedCert.HasTag("production") || !matchedCert.HasTag("v1") {
		fmt.Fprintf(os.Stderr, "Contract failure: Matched cert missing expected tags\n")
		os.Exit(1)
	}
	if matchedCert.Empty() || matchedCert.Expired() {
		fmt.Fprintf(os.Stderr, "Contract failure: Matched cert is unexpectedly empty or expired\n")
		os.Exit(1)
	}

	wildcardMatches := cache.AllMatchingCertificates("api.example.com")
	if len(wildcardMatches) == 0 {
		fmt.Fprintf(os.Stderr, "Contract failure: AllMatchingCertificates found no wildcard match for api.example.com\n")
		os.Exit(1)
	}

	// 5. certmagic.Cache.AllMatchingCertificates returns empty slice for non-matching domain
	nonMatches := cache.AllMatchingCertificates("otherdomain.org")
	if len(nonMatches) != 0 {
		fmt.Fprintf(os.Stderr, "Contract failure: AllMatchingCertificates returned match for unrelated domain\n")
		os.Exit(1)
	}

	// 6. certmagic.Cache.Remove removes cached certificates by hash
	cache.Remove([]string{hash})
	postRemoveMatches := cache.AllMatchingCertificates("example.com")
	if len(postRemoveMatches) != 0 {
		fmt.Fprintf(os.Stderr, "Contract failure: Certificate still in cache after Remove\n")
		os.Exit(1)
	}

	// 7. certmagic.Cache.SetOptions dynamically updates cache options while preserving configuration
	cache.SetOptions(certmagic.CacheOptions{
		GetConfigForCert: func(cert certmagic.Certificate) (*certmagic.Config, error) {
			return certmagic.New(cache, certmagic.Config{}), nil
		},
		Capacity:           100,
		RenewCheckInterval: 4 * time.Hour,
	})

	// 8. certmagic.Cache.Stop terminates background maintenance routines without error
	cache.Stop()

	fmt.Println("All certmagic.Cache contract assertions passed successfully.")
}

原始种子者

匿名