import { test, expect } from "bun:test"; import { createSecureServer, type ServerHttp2Stream } from "node:http2"; import { mkdtemp, readFile, rm } from "node:fs/promises"; import { resolve } from "node:path"; import { local } from "../scripts/runtime"; import { createCA, issue } from "../scripts/pki"; import { grpcProbe } from "./grpc-probe"; test("native HTTP2 gRPC probe requires a valid Export response and checks request framing", async () => { const scratch = await mkdtemp(resolve(local, "grpc-test-")), role = "grpc-probe-" + crypto.randomUUID(), rolePath = resolve(local, "pki", role), password = "probe-fixture-operator-passphrase"; let server: ReturnType | undefined; try { await createCA(resolve(scratch, "ca"), password); await issue( resolve(scratch, "ca"), resolve(scratch, "server"), "tempo-gateway", password, ); await issue(resolve(scratch, "ca"), rolePath, "collector", password); const [ca, key, cert] = await Promise.all( ["ca/ca.crt", "server/key.pem", "server/cert.pem"].map((path) => readFile(resolve(scratch, path)), ), ); let bodyOK = false, reject = false; server = createSecureServer({ ca, key, cert, requestCert: true, rejectUnauthorized: true, }); server.on("stream", (stream, headers) => { const chunks: Buffer[] = []; stream.on("data", (chunk) => chunks.push(Buffer.from(chunk))); stream.on("end", () => { bodyOK = Buffer.concat(chunks).equals(Buffer.alloc(5)) && headers[":path"] === "/opentelemetry.proto.collector.trace.v1.TraceService/Export"; (stream as ServerHttp2Stream).respond({ ":status": 200, "content-type": "application/grpc", "grpc-status": reject ? "7" : "0", }); stream.end(Buffer.alloc(5)); }); }); await new Promise((done) => server!.listen(0, "127.0.0.1", done)); const address = server.address(); if (!address || typeof address === "string") throw new Error("FIXTURE_NO_TCP_PORT"); await grpcProbe("127.0.0.1", address.port, "tempo-gateway", role); expect(bodyOK).toBe(true); reject = true; await expect( grpcProbe("127.0.0.1", address.port, "tempo-gateway", role), ).rejects.toThrow("TEMPO_GRPC_EXPORT_NOT_ACCEPTED"); } finally { if (server) await new Promise((done) => server!.close(() => done())); await rm(scratch, { recursive: true, force: true }); await rm(rolePath, { recursive: true, force: true }); } }, 30_000);