From 1bc688055a19a2cd02d4fa96d629c603f86beee4 Mon Sep 17 00:00:00 2001 From: Slavasil Date: Tue, 10 Nov 2026 09:52:41 +0300 Subject: [PATCH] move common test data to contracts/ --- contracts/test_signature_vectors.json | 13 +++++ .../internal/mainserver/reporter_test.go | 56 ++++++++++++++++--- .../internal_signature_service_spec.rb | 20 ++++--- 3 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 contracts/test_signature_vectors.json diff --git a/contracts/test_signature_vectors.json b/contracts/test_signature_vectors.json new file mode 100644 index 0000000..1b64a9e --- /dev/null +++ b/contracts/test_signature_vectors.json @@ -0,0 +1,13 @@ +{ + "description": "Shared test vectors for the HMAC authentication of the internal game-server -> main-server endpoints. Both test suites (main-server/spec/services/internal_signature_service_spec.rb and game-server/internal/mainserver/reporter_test.go) read this file, so it is the single source of truth for the wire format. Signature algorithm: hex(HMAC-SHA256(secret, \"..\")), where path is the request path without the query string.", + "vectors": [ + { + "name": "player1_won report", + "secret": "test-secret-key-base", + "timestamp": 1700000000, + "path": "/api/internal/matches/abc/result", + "body": "{\"status\":\"player1_won\",\"duration_seconds\":42}", + "signature_hex": "e0a7f8b5508639b1afe604b41f00070dd282d02b6444db5b4856959f175e8361" + } + ] +} diff --git a/game-server/internal/mainserver/reporter_test.go b/game-server/internal/mainserver/reporter_test.go index 14467f6..2b0baf3 100644 --- a/game-server/internal/mainserver/reporter_test.go +++ b/game-server/internal/mainserver/reporter_test.go @@ -2,10 +2,14 @@ package mainserver import ( "context" + "encoding/json" "io" "log/slog" "net/http" "net/http/httptest" + "os" + "path/filepath" + "runtime" "strconv" "sync/atomic" "testing" @@ -14,15 +18,51 @@ import ( const testSecret = "test-secret-key-base" -// known vector shared with the main-server (Ruby) test suite -const testVector = "e0a7f8b5508639b1afe604b41f00070dd282d02b6444db5b4856959f175e8361" +// The vectors are shared with the main-server (Ruby) test suite via +// contracts/test_signature_vectors.json — the single source of truth for the +// wire format. Do not change the formula without updating both sides. +func signatureVectorsPath(t *testing.T) string { + t.Helper() + _, file, _, ok := runtime.Caller(0) + if !ok { + t.Fatal("cannot locate the test file") + } + return filepath.Join(filepath.Dir(file), "..", "..", "..", "contracts", "test_signature_vectors.json") +} -func TestSignMatchesSharedVector(t *testing.T) { - body := `{"status":"player1_won","duration_seconds":42}` - path := "/api/internal/matches/abc/result" - got := Sign(1_700_000_000, path, []byte(body), []byte(testSecret)) - if got != testVector { - t.Fatalf("Sign = %q, want %q", got, testVector) +type signatureVector struct { + Name string `json:"name"` + Secret string `json:"secret"` + Timestamp int64 `json:"timestamp"` + Path string `json:"path"` + Body string `json:"body"` + SignatureHex string `json:"signature_hex"` +} + +func loadSignatureVectors(t *testing.T) []signatureVector { + t.Helper() + data, err := os.ReadFile(signatureVectorsPath(t)) + if err != nil { + t.Fatalf("read signature vectors: %v", err) + } + var doc struct { + Vectors []signatureVector `json:"vectors"` + } + if err := json.Unmarshal(data, &doc); err != nil { + t.Fatalf("parse signature vectors: %v", err) + } + if len(doc.Vectors) == 0 { + t.Fatal("no signature vectors found") + } + return doc.Vectors +} + +func TestSignMatchesSharedVectors(t *testing.T) { + for _, v := range loadSignatureVectors(t) { + got := Sign(v.Timestamp, v.Path, []byte(v.Body), []byte(v.Secret)) + if got != v.SignatureHex { + t.Errorf("vector %q: Sign = %q, want %q", v.Name, got, v.SignatureHex) + } } } diff --git a/main-server/spec/services/internal_signature_service_spec.rb b/main-server/spec/services/internal_signature_service_spec.rb index 64f07b2..ecdd2d2 100644 --- a/main-server/spec/services/internal_signature_service_spec.rb +++ b/main-server/spec/services/internal_signature_service_spec.rb @@ -2,15 +2,21 @@ require "rails_helper" RSpec.describe InternalSignatureService do let(:secret) { "test-secret-key-base" } - let(:body) { '{"status":"player1_won","duration_seconds":42}' } - let(:path) { "/api/internal/matches/abc/result" } - let(:timestamp) { 1_700_000_000 } it "produces a stable, cross-implementation signature" do - # Vector shared with the game-server (Go) test suite. Do not change the - # formula without updating both sides. - expect(described_class.sign(body, path, secret: secret, timestamp: timestamp)) - .to eq("e0a7f8b5508639b1afe604b41f00070dd282d02b6444db5b4856959f175e8361") + # Vectors are shared with the game-server (Go) test suite via + # contracts/test_signature_vectors.json — the single source of truth for + # the wire format. Do not change the formula without updating both sides. + vectors_file = Rails.root.join("..", "contracts", "test_signature_vectors.json") + vectors = JSON.parse(File.read(vectors_file))["vectors"] + expect(vectors).not_to be_empty + + vectors.each do |vector| + expect(described_class.sign(vector["body"], vector["path"], + secret: vector["secret"], timestamp: vector["timestamp"])) + .to eq(vector["signature_hex"]), + "signature vector #{vector["name"].inspect} does not match" + end end describe ".valid?" do