|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/pilot-protocol/common/reqsig" |
| 11 | +) |
| 12 | + |
| 13 | +// IPC cmd codes for the request-signature envelope commands — must match |
| 14 | +// daemon CmdSignEnvelope/CmdVerifyEnvelope and driver cmdSignEnvelope/ |
| 15 | +// cmdVerifyEnvelope. Kept here (not zz_fake_daemon_test.go) alongside the |
| 16 | +// only tests that use them. |
| 17 | +const ( |
| 18 | + tdCmdSignEnvelope byte = 0x33 |
| 19 | + tdCmdSignEnvelopeOK byte = 0x34 |
| 20 | + tdCmdVerifyEnvelope byte = 0x35 |
| 21 | + tdCmdVerifyEnvelopeOK byte = 0x36 |
| 22 | +) |
| 23 | + |
| 24 | +// receivedPayload scans the fake daemon's received frames for cmd and |
| 25 | +// returns the decoded JSON payload of the first match. |
| 26 | +func receivedPayload(t *testing.T, d *fakeDaemon, cmd byte) map[string]interface{} { |
| 27 | + t.Helper() |
| 28 | + d.mu.Lock() |
| 29 | + defer d.mu.Unlock() |
| 30 | + for _, frame := range d.received { |
| 31 | + if len(frame) > 1 && frame[0] == cmd { |
| 32 | + var got map[string]interface{} |
| 33 | + if err := json.Unmarshal(frame[1:], &got); err != nil { |
| 34 | + t.Fatalf("frame 0x%02X payload not JSON: %v", cmd, err) |
| 35 | + } |
| 36 | + return got |
| 37 | + } |
| 38 | + } |
| 39 | + t.Fatalf("no frame with cmd 0x%02X received", cmd) |
| 40 | + return nil |
| 41 | +} |
| 42 | + |
| 43 | +// TestCLISignRequestEnvelope covers the happy path: the CLI hashes --body |
| 44 | +// locally, sends {audience, body_hash} to the daemon, and prints |
| 45 | +// {envelope, signature, address}. |
| 46 | +func TestCLISignRequestEnvelope(t *testing.T) { |
| 47 | + t.Parallel() |
| 48 | + d := newFakeDaemon(t) |
| 49 | + d.onJSON(tdCmdSignEnvelope, tdCmdSignEnvelopeOK, |
| 50 | + `{"type":"sign_envelope_ok","envelope":"pilot-req-v1|env","signature":"c2ln","address":"0:0000.0000.0007"}`) |
| 51 | + |
| 52 | + stdout, stderr, code := runCLI(t, []string{ |
| 53 | + "--json", "sign-request", |
| 54 | + "--audience", "svc.example.io", |
| 55 | + "--body", "hello envelope", |
| 56 | + }, map[string]string{"PILOT_SOCKET": d.path}) |
| 57 | + if code != 0 { |
| 58 | + t.Fatalf("exit=%d\nstdout=%s\nstderr=%s", code, stdout, stderr) |
| 59 | + } |
| 60 | + |
| 61 | + var env map[string]interface{} |
| 62 | + if err := json.Unmarshal([]byte(stdout), &env); err != nil { |
| 63 | + t.Fatalf("json: %v (stdout=%q)", err, stdout) |
| 64 | + } |
| 65 | + data, _ := env["data"].(map[string]interface{}) |
| 66 | + if data["envelope"] != "pilot-req-v1|env" || data["signature"] != "c2ln" || data["address"] != "0:0000.0000.0007" { |
| 67 | + t.Errorf("data = %+v", data) |
| 68 | + } |
| 69 | + |
| 70 | + // The daemon must have been asked for the locally computed sha256, never |
| 71 | + // the raw body. |
| 72 | + got := receivedPayload(t, d, tdCmdSignEnvelope) |
| 73 | + if got["audience"] != "svc.example.io" { |
| 74 | + t.Errorf("audience = %v", got["audience"]) |
| 75 | + } |
| 76 | + if got["body_hash"] != reqsig.HashBody([]byte("hello envelope")) { |
| 77 | + t.Errorf("body_hash = %v, want sha256 of --body", got["body_hash"]) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestCLISignRequestEnvelopeRequiresAudience(t *testing.T) { |
| 82 | + t.Parallel() |
| 83 | + _, stderr, code := runCLI(t, []string{"sign-request", "--body", "x"}, nil) |
| 84 | + if code == 0 { |
| 85 | + t.Error("expected non-zero exit without --audience") |
| 86 | + } |
| 87 | + if !strings.Contains(stderr, "audience") { |
| 88 | + t.Errorf("expected usage mention of --audience, got: %s", stderr) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestCLISignRequestEnvelopeRequiresOneBodySource(t *testing.T) { |
| 93 | + t.Parallel() |
| 94 | + // None given. |
| 95 | + _, stderr, code := runCLI(t, []string{"sign-request", "--audience", "svc.example.io"}, nil) |
| 96 | + if code == 0 { |
| 97 | + t.Error("expected non-zero exit without a body source") |
| 98 | + } |
| 99 | + if !strings.Contains(stderr, "exactly one") { |
| 100 | + t.Errorf("expected 'exactly one' error, got: %s", stderr) |
| 101 | + } |
| 102 | + // Two given. |
| 103 | + _, stderr, code = runCLI(t, []string{ |
| 104 | + "sign-request", "--audience", "svc.example.io", |
| 105 | + "--body", "x", "--body-hash", strings.Repeat("a", 64), |
| 106 | + }, nil) |
| 107 | + if code == 0 { |
| 108 | + t.Error("expected non-zero exit with two body sources") |
| 109 | + } |
| 110 | + if !strings.Contains(stderr, "exactly one") { |
| 111 | + t.Errorf("expected 'exactly one' error, got: %s", stderr) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// TestCLIVerifyRequestEnvelopeValid: a valid verdict prints the daemon reply |
| 116 | +// and exits 0; flags round-trip onto the wire. |
| 117 | +func TestCLIVerifyRequestEnvelopeValid(t *testing.T) { |
| 118 | + t.Parallel() |
| 119 | + d := newFakeDaemon(t) |
| 120 | + d.onJSON(tdCmdVerifyEnvelope, tdCmdVerifyEnvelopeOK, |
| 121 | + `{"type":"verify_envelope_ok","valid":true,"node_id":7,"address":"0:0000.0000.0007","verified_via":"cache","trusted":true}`) |
| 122 | + |
| 123 | + stdout, stderr, code := runCLI(t, []string{ |
| 124 | + "--json", "verify-request", |
| 125 | + "--envelope", "pilot-req-v1|env", |
| 126 | + "--signature", "c2ln", |
| 127 | + "--standing", |
| 128 | + "--max-skew", "600", |
| 129 | + }, map[string]string{"PILOT_SOCKET": d.path}) |
| 130 | + if code != 0 { |
| 131 | + t.Fatalf("exit=%d\nstdout=%s\nstderr=%s", code, stdout, stderr) |
| 132 | + } |
| 133 | + var env map[string]interface{} |
| 134 | + if err := json.Unmarshal([]byte(stdout), &env); err != nil { |
| 135 | + t.Fatalf("json: %v (stdout=%q)", err, stdout) |
| 136 | + } |
| 137 | + data, _ := env["data"].(map[string]interface{}) |
| 138 | + if valid, _ := data["valid"].(bool); !valid { |
| 139 | + t.Errorf("data = %+v, want valid=true", data) |
| 140 | + } |
| 141 | + |
| 142 | + got := receivedPayload(t, d, tdCmdVerifyEnvelope) |
| 143 | + if got["envelope"] != "pilot-req-v1|env" || got["signature"] != "c2ln" { |
| 144 | + t.Errorf("payload = %+v", got) |
| 145 | + } |
| 146 | + if cs, _ := got["check_standing"].(bool); !cs { |
| 147 | + t.Errorf("check_standing = %v, want true (--standing)", got["check_standing"]) |
| 148 | + } |
| 149 | + if skew, _ := got["max_skew_secs"].(float64); skew != 600 { |
| 150 | + t.Errorf("max_skew_secs = %v, want 600 (--max-skew)", got["max_skew_secs"]) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +// TestCLIVerifyRequestEnvelopeInvalidExitsOne: valid:false still prints the |
| 155 | +// verdict but the process exits 1 (scriptable gate). |
| 156 | +func TestCLIVerifyRequestEnvelopeInvalidExitsOne(t *testing.T) { |
| 157 | + t.Parallel() |
| 158 | + d := newFakeDaemon(t) |
| 159 | + d.onJSON(tdCmdVerifyEnvelope, tdCmdVerifyEnvelopeOK, |
| 160 | + `{"type":"verify_envelope_ok","valid":false,"reason":"reqsig: signature verification failed"}`) |
| 161 | + |
| 162 | + stdout, _, code := runCLI(t, []string{ |
| 163 | + "--json", "verify-request", |
| 164 | + "--envelope", "pilot-req-v1|env", |
| 165 | + "--signature", "c2ln", |
| 166 | + }, map[string]string{"PILOT_SOCKET": d.path}) |
| 167 | + if code != 1 { |
| 168 | + t.Fatalf("exit=%d, want 1 on valid:false", code) |
| 169 | + } |
| 170 | + if !strings.Contains(stdout, "signature verification failed") { |
| 171 | + t.Errorf("verdict reason missing from output: %s", stdout) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func TestCLIVerifyRequestEnvelopeRequiresArgs(t *testing.T) { |
| 176 | + t.Parallel() |
| 177 | + _, stderr, code := runCLI(t, []string{"verify-request", "--envelope", "e"}, nil) |
| 178 | + if code == 0 { |
| 179 | + t.Error("expected non-zero exit without --signature") |
| 180 | + } |
| 181 | + if !strings.Contains(stderr, "--signature") && !strings.Contains(stderr, "signature") { |
| 182 | + t.Errorf("expected usage mention of --signature, got: %s", stderr) |
| 183 | + } |
| 184 | +} |
0 commit comments