|
| 1 | +// Package main is a WASI guest fixture that exercises DNS resolution |
| 2 | +// through the WarpGrid DNS shim (warpgrid_shim dns_resolve). |
| 3 | +// |
| 4 | +// Each exported function tests a specific DNS resolution scenario via |
| 5 | +// dns.DefaultResolver(). The host integration test registers a service |
| 6 | +// registry mapping test hostnames to known IPs, then calls these exports |
| 7 | +// and validates the results. |
| 8 | +// |
| 9 | +// Build: tinygo build -target=wasi -buildmode=c-shared -o go-dns-dial-guest.wasm . |
| 10 | +// |
| 11 | +// Reactor mode (-buildmode=c-shared) is required so that //go:wasmexport |
| 12 | +// functions work after _initialize. |
| 13 | +package main |
| 14 | + |
| 15 | +import ( |
| 16 | + "errors" |
| 17 | + "fmt" |
| 18 | + "net" |
| 19 | + "strings" |
| 20 | + "unsafe" |
| 21 | + |
| 22 | + wgdns "github.com/anthropics/warpgrid/packages/warpgrid-go/dns" |
| 23 | + wgnet "github.com/anthropics/warpgrid/packages/warpgrid-go/net" |
| 24 | +) |
| 25 | + |
| 26 | +func main() {} |
| 27 | + |
| 28 | +// ── Exported test functions ───────────────────────────────────────── |
| 29 | +// |
| 30 | +// Each function returns a packed uint64: high 32 bits = pointer to |
| 31 | +// result string, low 32 bits = length. The host reads the string from |
| 32 | +// linear memory. Format: "OK:<data>" on success, "ERR:<message>" on failure. |
| 33 | + |
| 34 | +// testResolveRegistry resolves a hostname expected to be in the service |
| 35 | +// registry and returns the first resolved IP address. |
| 36 | +// |
| 37 | +//go:wasmexport test-resolve-registry |
| 38 | +func testResolveRegistry() uint64 { |
| 39 | + resolver := wgdns.DefaultResolver() |
| 40 | + ips, err := resolver.Resolve("echo-server.test.warp.local") |
| 41 | + if err != nil { |
| 42 | + return writeResult(fmt.Sprintf("ERR:resolve failed: %v", err)) |
| 43 | + } |
| 44 | + if len(ips) == 0 { |
| 45 | + return writeResult("ERR:no addresses returned") |
| 46 | + } |
| 47 | + return writeResult(fmt.Sprintf("OK:%s", ips[0].String())) |
| 48 | +} |
| 49 | + |
| 50 | +// testResolveMultiple resolves a hostname with multiple A records and |
| 51 | +// returns all addresses comma-separated. |
| 52 | +// |
| 53 | +//go:wasmexport test-resolve-multiple |
| 54 | +func testResolveMultiple() uint64 { |
| 55 | + resolver := wgdns.DefaultResolver() |
| 56 | + ips, err := resolver.Resolve("multi.test.warp.local") |
| 57 | + if err != nil { |
| 58 | + return writeResult(fmt.Sprintf("ERR:resolve failed: %v", err)) |
| 59 | + } |
| 60 | + if len(ips) == 0 { |
| 61 | + return writeResult("ERR:no addresses returned") |
| 62 | + } |
| 63 | + parts := make([]string, len(ips)) |
| 64 | + for i, ip := range ips { |
| 65 | + parts[i] = ip.String() |
| 66 | + } |
| 67 | + return writeResult(fmt.Sprintf("OK:%s", strings.Join(parts, ","))) |
| 68 | +} |
| 69 | + |
| 70 | +// testResolveNonexistent attempts to resolve a hostname that does not |
| 71 | +// exist and returns the error message. |
| 72 | +// |
| 73 | +//go:wasmexport test-resolve-nonexistent |
| 74 | +func testResolveNonexistent() uint64 { |
| 75 | + resolver := wgdns.DefaultResolver() |
| 76 | + _, err := resolver.Resolve("nonexistent.invalid") |
| 77 | + if err == nil { |
| 78 | + return writeResult("ERR:expected error for nonexistent hostname, got nil") |
| 79 | + } |
| 80 | + return writeResult(fmt.Sprintf("OK:%s", err.Error())) |
| 81 | +} |
| 82 | + |
| 83 | +// testResolveIPLiteral verifies that IP literals bypass DNS resolution |
| 84 | +// and are returned directly. |
| 85 | +// |
| 86 | +//go:wasmexport test-resolve-ip-literal |
| 87 | +func testResolveIPLiteral() uint64 { |
| 88 | + resolver := wgdns.DefaultResolver() |
| 89 | + ips, err := resolver.Resolve("192.168.1.1") |
| 90 | + if err != nil { |
| 91 | + return writeResult(fmt.Sprintf("ERR:resolve failed: %v", err)) |
| 92 | + } |
| 93 | + if len(ips) != 1 { |
| 94 | + return writeResult(fmt.Sprintf("ERR:expected 1 address, got %d", len(ips))) |
| 95 | + } |
| 96 | + if ips[0].String() != "192.168.1.1" { |
| 97 | + return writeResult(fmt.Sprintf("ERR:expected 192.168.1.1, got %s", ips[0].String())) |
| 98 | + } |
| 99 | + return writeResult("OK:192.168.1.1") |
| 100 | +} |
| 101 | + |
| 102 | +// testDialerDNSErrorWrapping exercises the Dialer with a hostname that |
| 103 | +// fails DNS resolution and verifies proper error wrapping. |
| 104 | +// |
| 105 | +//go:wasmexport test-dialer-dns-error |
| 106 | +func testDialerDNSErrorWrapping() uint64 { |
| 107 | + dialer := wgnet.DefaultDialer() |
| 108 | + _, err := dialer.Dial("tcp", "nonexistent.invalid:5432") |
| 109 | + if err == nil { |
| 110 | + return writeResult("ERR:expected error, got nil") |
| 111 | + } |
| 112 | + |
| 113 | + // Verify *net.OpError wrapping |
| 114 | + var opErr *net.OpError |
| 115 | + if !errors.As(err, &opErr) { |
| 116 | + return writeResult(fmt.Sprintf("ERR:expected *net.OpError, got %T: %v", err, err)) |
| 117 | + } |
| 118 | + if opErr.Op != "dial" { |
| 119 | + return writeResult(fmt.Sprintf("ERR:expected Op=dial, got %s", opErr.Op)) |
| 120 | + } |
| 121 | + |
| 122 | + // Verify inner *wgnet.DNSError |
| 123 | + var dnsErr *wgnet.DNSError |
| 124 | + if !errors.As(opErr.Err, &dnsErr) { |
| 125 | + return writeResult(fmt.Sprintf("ERR:expected inner *DNSError, got %T: %v", opErr.Err, opErr.Err)) |
| 126 | + } |
| 127 | + if dnsErr.Name != "nonexistent.invalid" { |
| 128 | + return writeResult(fmt.Sprintf("ERR:DNSError.Name=%q, want nonexistent.invalid", dnsErr.Name)) |
| 129 | + } |
| 130 | + |
| 131 | + return writeResult("OK:correctly wrapped as *net.OpError{*DNSError}") |
| 132 | +} |
| 133 | + |
| 134 | +// ── Helper: pack result string into uint64 (ptr << 32 | len) ─────── |
| 135 | + |
| 136 | +var resultBuf []byte |
| 137 | + |
| 138 | +func writeResult(s string) uint64 { |
| 139 | + resultBuf = []byte(s) |
| 140 | + ptr := uint64(uintptr(unsafe.Pointer(&resultBuf[0]))) |
| 141 | + length := uint64(len(resultBuf)) |
| 142 | + return (ptr << 32) | length |
| 143 | +} |
0 commit comments