move common test data to contracts/
This commit is contained in:
@@ -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, \"<timestamp>.<path>.<body>\")), 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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -2,10 +2,14 @@ package mainserver
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -14,15 +18,51 @@ import (
|
|||||||
|
|
||||||
const testSecret = "test-secret-key-base"
|
const testSecret = "test-secret-key-base"
|
||||||
|
|
||||||
// known vector shared with the main-server (Ruby) test suite
|
// The vectors are shared with the main-server (Ruby) test suite via
|
||||||
const testVector = "e0a7f8b5508639b1afe604b41f00070dd282d02b6444db5b4856959f175e8361"
|
// 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) {
|
type signatureVector struct {
|
||||||
body := `{"status":"player1_won","duration_seconds":42}`
|
Name string `json:"name"`
|
||||||
path := "/api/internal/matches/abc/result"
|
Secret string `json:"secret"`
|
||||||
got := Sign(1_700_000_000, path, []byte(body), []byte(testSecret))
|
Timestamp int64 `json:"timestamp"`
|
||||||
if got != testVector {
|
Path string `json:"path"`
|
||||||
t.Fatalf("Sign = %q, want %q", got, testVector)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,15 +2,21 @@ require "rails_helper"
|
|||||||
|
|
||||||
RSpec.describe InternalSignatureService do
|
RSpec.describe InternalSignatureService do
|
||||||
let(:secret) { "test-secret-key-base" }
|
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
|
it "produces a stable, cross-implementation signature" do
|
||||||
# Vector shared with the game-server (Go) test suite. Do not change the
|
# Vectors are shared with the game-server (Go) test suite via
|
||||||
# formula without updating both sides.
|
# contracts/test_signature_vectors.json — the single source of truth for
|
||||||
expect(described_class.sign(body, path, secret: secret, timestamp: timestamp))
|
# the wire format. Do not change the formula without updating both sides.
|
||||||
.to eq("e0a7f8b5508639b1afe604b41f00070dd282d02b6444db5b4856959f175e8361")
|
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
|
end
|
||||||
|
|
||||||
describe ".valid?" do
|
describe ".valid?" do
|
||||||
|
|||||||
Reference in New Issue
Block a user