From 408f0d2b36d7632f44f6ef6992ef7ebb8c386669 Mon Sep 17 00:00:00 2001 From: chaosinthecrd Date: Mon, 26 Oct 2026 07:56:20 +0000 Subject: [PATCH] ipn,feature/acme,client/local: add tests for the cert-status localapi endpoint Tests that the localapi cert-status/ handler checks read access and returns the status as JSON, that LocalBackend.CertStatus goes through the hook, and that feature/acme's hook reports the same missing, valid and expired results as the C2N /tls-cert-status handler. Also tests that Client.CertStatus returns ErrCertStatusUnsupported for a 404 and not for other errors. Updates tailscale/corp#38175 Signed-off-by: chaosinthecrd --- client/local/cert_test.go | 52 +++++++++++++++++++++++++++++ feature/acme/c2n_test.go | 8 +++++ ipn/ipnlocal/cert_test.go | 39 ++++++++++++++++++++++ ipn/localapi/cert_test.go | 70 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+) create mode 100644 client/local/cert_test.go create mode 100644 ipn/ipnlocal/cert_test.go create mode 100644 ipn/localapi/cert_test.go diff --git a/client/local/cert_test.go b/client/local/cert_test.go new file mode 100644 index 000000000..c41a2cbf4 --- /dev/null +++ b/client/local/cert_test.go @@ -0,0 +1,52 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +//go:build !js && !ts_omit_acme + +package local + +import ( + "context" + "errors" + "net" + "net/http" + "testing" + + "tailscale.com/tstest/nettest" +) + +func TestCertStatusUnsupported(t *testing.T) { + tests := []struct { + name string + status int + wantUnsupported bool + }{ + {name: "not_found", status: http.StatusNotFound, wantUnsupported: true}, + {name: "server_error", status: http.StatusInternalServerError, wantUnsupported: false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + nw := nettest.GetNetwork(t) + ts := nettest.NewHTTPServer(nw, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(tt.status) + })) + defer ts.Close() + + lc := &Client{ + Dial: func(ctx context.Context, network, addr string) (net.Conn, error) { + return nw.Dial(ctx, network, ts.Listener.Addr().String()) + }, + } + _, err := lc.CertStatus(context.Background(), "node.example.ts.net") + if err == nil { + t.Fatal("CertStatus: got nil error") + } + if got := errors.Is(err, ErrCertStatusUnsupported); got != tt.wantUnsupported { + t.Errorf("errors.Is(%v, ErrCertStatusUnsupported) = %v, want %v", err, got, tt.wantUnsupported) + } + if got := errors.Is(err, errors.ErrUnsupported); got != tt.wantUnsupported { + t.Errorf("errors.Is(%v, errors.ErrUnsupported) = %v, want %v", err, got, tt.wantUnsupported) + } + }) + } +} diff --git a/feature/acme/c2n_test.go b/feature/acme/c2n_test.go index d6eb4f898..88bdd1af4 100644 --- a/feature/acme/c2n_test.go +++ b/feature/acme/c2n_test.go @@ -125,6 +125,14 @@ func TestHandleC2NTLSCertStatus(t *testing.T) { if !reflect.DeepEqual(&got, tt.want) { t.Errorf("got %v; want %v", logger.AsJSON(got), logger.AsJSON(tt.want)) } + // The localapi cert-status endpoint reads the same cache via LocalBackend.CertStatus. + gotLocal, err := b.CertStatus(tt.domain) + if err != nil { + t.Fatalf("CertStatus: %v", err) + } + if !reflect.DeepEqual(gotLocal, tt.want) { + t.Errorf("CertStatus = %v; want %v", logger.AsJSON(gotLocal), logger.AsJSON(tt.want)) + } } else if tt.wantError != "" { if got := rec.Body.String(); got != tt.wantError { t.Errorf("body = %q; want %q", got, tt.wantError) diff --git a/ipn/ipnlocal/cert_test.go b/ipn/ipnlocal/cert_test.go new file mode 100644 index 000000000..4e377bc87 --- /dev/null +++ b/ipn/ipnlocal/cert_test.go @@ -0,0 +1,39 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package ipnlocal + +import ( + "errors" + "testing" + + "tailscale.com/tailcfg" +) + +func TestCertStatus(t *testing.T) { + b := &LocalBackend{} + + if _, err := b.CertStatus("node.example.ts.net"); !errors.Is(err, errNoCerts) { + t.Fatalf("CertStatus without ACME support: err = %v, want %v", err, errNoCerts) + } + + want := &tailcfg.C2NTLSCertInfo{Missing: true, Error: "no certificate"} + var gotDomain string + t.Cleanup(HookCertStatus.SetForTest(func(hb *LocalBackend, domain string) (*tailcfg.C2NTLSCertInfo, error) { + if hb != b { + t.Errorf("hook called with a different LocalBackend") + } + gotDomain = domain + return want, nil + })) + got, err := b.CertStatus("node.example.ts.net") + if err != nil { + t.Fatalf("CertStatus: %v", err) + } + if got != want { + t.Errorf("CertStatus = %+v, want %+v", got, want) + } + if gotDomain != "node.example.ts.net" { + t.Errorf("hook got domain %q, want %q", gotDomain, "node.example.ts.net") + } +} diff --git a/ipn/localapi/cert_test.go b/ipn/localapi/cert_test.go new file mode 100644 index 000000000..2c88f7d90 --- /dev/null +++ b/ipn/localapi/cert_test.go @@ -0,0 +1,70 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +//go:build !ios && !android && !js && !ts_omit_acme + +package localapi + +import ( + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "reflect" + "testing" + + "tailscale.com/ipn/ipnlocal" + "tailscale.com/tailcfg" +) + +func TestServeCertStatus(t *testing.T) { + valid := &tailcfg.C2NTLSCertInfo{ + Valid: true, + NotBefore: "2026-01-01T00:00:00Z", + NotAfter: "2026-04-01T00:00:00Z", + } + missing := &tailcfg.C2NTLSCertInfo{Missing: true, Error: "no certificate"} + t.Cleanup(ipnlocal.HookCertStatus.SetForTest(func(_ *ipnlocal.LocalBackend, domain string) (*tailcfg.C2NTLSCertInfo, error) { + switch domain { + case "valid.example.ts.net": + return valid, nil + case "broken.example.ts.net": + return nil, errors.New("cert store unavailable") + } + return missing, nil + })) + + tests := []struct { + name string + permitRead bool + domain string + wantCode int + want *tailcfg.C2NTLSCertInfo + }{ + {"valid", true, "valid.example.ts.net", http.StatusOK, valid}, + {"missing", true, "missing.example.ts.net", http.StatusOK, missing}, + {"store_error", true, "broken.example.ts.net", http.StatusInternalServerError, nil}, + {"forbidden", false, "valid.example.ts.net", http.StatusForbidden, nil}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h := handlerForTest(t, &Handler{PermitRead: tt.permitRead}) + rec := httptest.NewRecorder() + req := httptest.NewRequest("GET", "/localapi/v0/cert-status/"+tt.domain, nil) + h.serveCertStatus(rec, req) + if rec.Code != tt.wantCode { + t.Fatalf("status = %d, want %d; body=%q", rec.Code, tt.wantCode, rec.Body.String()) + } + if tt.want == nil { + return + } + var got tailcfg.C2NTLSCertInfo + if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil { + t.Fatalf("unmarshal body %q: %v", rec.Body.Bytes(), err) + } + if !reflect.DeepEqual(&got, tt.want) { + t.Errorf("got %+v, want %+v", got, *tt.want) + } + }) + } +}