feat(assessment): find the attempt behind a leaked task text
This commit is contained in:
1 parent
bb917bf62e
commit
5a731c25b7
26 files changed
+2500
No files matched your search
@@ -0,0 +1,482 @@
|
||||
package assessment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
const leakStatement = "A gateway protects an endpoint with a sliding-window limiter. A request arriving at second t is accepted only when fewer than 3 previously accepted requests have an arrival time in the half-open interval (t - 5, t]. A rejected request is not remembered and does not count against later requests. The gateway receives 8 requests at these arrival times, in seconds and in non-decreasing order: 0 0 1 2 2 3 4 6. How many requests does the gateway reject?"
|
||||
|
||||
var leakNow = time.Date(2026, time.October, 11, 9, 0, 0, 0, time.UTC)
|
||||
|
||||
type leakDigesterFake struct{}
|
||||
|
||||
func (leakDigesterFake) AssessmentLeakTextDigest(text []byte) domain.AssessmentDigest {
|
||||
return domain.AssessmentDigest(sha256.Sum256(append([]byte("leak:"), text...)))
|
||||
}
|
||||
|
||||
func (leakDigesterFake) AssessmentDigestKeyVersion() int16 { return 7 }
|
||||
|
||||
// leakRepositoryFake holds attempts with their statements and records the audit.
|
||||
type leakRepositoryFake struct {
|
||||
attempts []outbound.AssessmentTraceAttempt
|
||||
statements []outbound.AssessmentTraceStatement
|
||||
records []domain.AssessmentLeakTraceRecord
|
||||
recordErr error
|
||||
}
|
||||
|
||||
func (repository *leakRepositoryFake) ListRealAttempts(_ context.Context, after domain.AssessmentAttemptID, limit int32) ([]outbound.AssessmentTraceAttempt, error) {
|
||||
var page []outbound.AssessmentTraceAttempt
|
||||
for _, attempt := range repository.attempts {
|
||||
if attempt.ID.UUID().String() > after.UUID().String() || after.IsZero() {
|
||||
page = append(page, attempt)
|
||||
}
|
||||
if int32(len(page)) == limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
|
||||
func (repository *leakRepositoryFake) FindStatementsContaining(_ context.Context, fragment string, limit int32) ([]outbound.AssessmentTraceStatement, error) {
|
||||
var found []outbound.AssessmentTraceStatement
|
||||
for _, stored := range repository.statements {
|
||||
if strings.Contains(strings.Join(strings.Fields(stored.Statement), " "), fragment) {
|
||||
found = append(found, stored)
|
||||
}
|
||||
if int32(len(found)) == limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
return found, nil
|
||||
}
|
||||
|
||||
func (repository *leakRepositoryFake) RecordSearch(_ context.Context, record domain.AssessmentLeakTraceRecord) error {
|
||||
if repository.recordErr != nil {
|
||||
return repository.recordErr
|
||||
}
|
||||
if err := record.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
repository.records = append(repository.records, record)
|
||||
return nil
|
||||
}
|
||||
|
||||
type leakUnitFake struct {
|
||||
outbound.UnitOfWork
|
||||
|
||||
repository *leakRepositoryFake
|
||||
}
|
||||
|
||||
func (unit *leakUnitFake) AssessmentLeakTraces() outbound.AssessmentLeakTraceRepository {
|
||||
return unit.repository
|
||||
}
|
||||
|
||||
func (unit *leakUnitFake) AssessmentBank() outbound.AssessmentBankRepository { return bankClockFake{} }
|
||||
|
||||
type runningTransactor struct{ unit outbound.UnitOfWork }
|
||||
|
||||
func (transactor runningTransactor) WithinTransaction(ctx context.Context, fn func(context.Context, outbound.UnitOfWork) error) error {
|
||||
return fn(ctx, transactor.unit)
|
||||
}
|
||||
|
||||
func newLeakTracer(t *testing.T, repository *leakRepositoryFake) *LeakTracer {
|
||||
t.Helper()
|
||||
tracer, err := NewLeakTracer(runningTransactor{unit: &leakUnitFake{repository: repository}}, hashedSeeds{}, leakDigesterFake{})
|
||||
if err != nil {
|
||||
t.Fatalf("NewLeakTracer() error = %v", err)
|
||||
}
|
||||
return tracer
|
||||
}
|
||||
|
||||
func (repository *leakRepositoryFake) serve(id domain.AssessmentAttemptID, statement string) {
|
||||
repository.attempts = append(repository.attempts, outbound.AssessmentTraceAttempt{
|
||||
ID: id, CreatedAt: leakNow.Add(-30 * time.Minute), ExpiresAt: leakNow.Add(30 * time.Minute),
|
||||
})
|
||||
repository.statements = append(repository.statements, outbound.AssessmentTraceStatement{AttemptID: id, Statement: statement})
|
||||
}
|
||||
|
||||
func leakWatermark(t *testing.T, id domain.AssessmentAttemptID) domain.AssessmentWatermark {
|
||||
t.Helper()
|
||||
watermark, err := domain.NewAssessmentWatermark(hashedSeeds{}.AssessmentWatermarkSeed(id.UUID()))
|
||||
if err != nil {
|
||||
t.Fatalf("NewAssessmentWatermark() error = %v", err)
|
||||
}
|
||||
return watermark
|
||||
}
|
||||
|
||||
// attemptsThatWriteTheStatementDifferently returns count attempts that were
|
||||
// served the same statement and chose different phrases for at least one group.
|
||||
func attemptsThatWriteTheStatementDifferently(t *testing.T, count int) []domain.AssessmentAttemptID {
|
||||
t.Helper()
|
||||
var ids []domain.AssessmentAttemptID
|
||||
seen := map[string]struct{}{}
|
||||
for range 400 {
|
||||
id := domain.AssessmentAttemptID(uuid.New())
|
||||
wording := domain.VisibleAssessmentText(leakWatermark(t, id).Mark(leakStatement))
|
||||
if _, duplicate := seen[wording]; duplicate {
|
||||
continue
|
||||
}
|
||||
seen[wording] = struct{}{}
|
||||
ids = append(ids, id)
|
||||
if len(ids) == count {
|
||||
return ids
|
||||
}
|
||||
}
|
||||
t.Fatalf("no %d attempts with different wording found", count)
|
||||
return nil
|
||||
}
|
||||
|
||||
func traceAs(t *testing.T, tracer *LeakTracer, text, code string) LeakTraceResult {
|
||||
t.Helper()
|
||||
result, err := tracer.Trace(context.Background(), LeakTraceCommand{Actor: "operator:night.shift", Text: text, Code: code})
|
||||
if err != nil {
|
||||
t.Fatalf("Trace() error = %v", err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func TestLeakedTextOfAVariantFindsTheAttemptByItsInvisibleMark(t *testing.T) {
|
||||
repository := &leakRepositoryFake{}
|
||||
ids := attemptsThatWriteTheStatementDifferently(t, 3)
|
||||
for _, id := range ids {
|
||||
repository.serve(id, leakStatement)
|
||||
}
|
||||
tracer := newLeakTracer(t, repository)
|
||||
for index, id := range ids {
|
||||
leaked := leakWatermark(t, id).Mark(leakStatement)
|
||||
// A leak usually holds the statement among other text.
|
||||
result := traceAs(t, tracer, "Question from the exam: "+leaked+" Thanks!", "")
|
||||
if result.Status != domain.AssessmentLeakTraceFound || result.Method != domain.AssessmentLeakTraceInvisibleMark || result.AttemptID != id {
|
||||
t.Fatalf("attempt %d: result = %+v, want found by the invisible mark in %s", index, result, id)
|
||||
}
|
||||
if result.EvidenceBits != 50 || result.Candidates != 1 {
|
||||
t.Fatalf("attempt %d: evidence = %d bits, %d candidates, want 50 and 1", index, result.EvidenceBits, result.Candidates)
|
||||
}
|
||||
if !strings.Contains(strings.Join(result.Notes, " "), "matches an item of that attempt") {
|
||||
t.Fatalf("attempt %d: notes = %v, want the corroboration by the statement text", index, result.Notes)
|
||||
}
|
||||
}
|
||||
// Every search was audited, with the attempt it found.
|
||||
if len(repository.records) != len(ids) || repository.records[0].AttemptID != ids[0] || repository.records[0].Method != domain.AssessmentLeakTraceInvisibleMark {
|
||||
t.Fatalf("audit = %+v, want one record per search", repository.records)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithoutTheInvisibleCharactersTheSearchFallsBackToTheVisibleCodeAndTheWording(t *testing.T) {
|
||||
repository := &leakRepositoryFake{}
|
||||
ids := attemptsThatWriteTheStatementDifferently(t, 3)
|
||||
for _, id := range ids {
|
||||
repository.serve(id, leakStatement)
|
||||
}
|
||||
tracer := newLeakTracer(t, repository)
|
||||
target := ids[1]
|
||||
watermark := leakWatermark(t, target)
|
||||
retyped := domain.VisibleAssessmentText(watermark.Mark(leakStatement))
|
||||
if retyped == leakStatement {
|
||||
t.Fatal("the fixture attempt wrote the statement as the bank did, so the wording cannot show it")
|
||||
}
|
||||
|
||||
// The OCR text of a screenshot: the statement, and the tile with its code and time.
|
||||
tile := fmt.Sprintf("%s %s", watermark.Code().String(), leakNow.Format("2006-01-02 15:04")+"Z")
|
||||
screenshot := traceAs(t, tracer, retyped+"\n"+tile, "")
|
||||
if screenshot.Status != domain.AssessmentLeakTraceFound || screenshot.Method != domain.AssessmentLeakTraceVisibleCode || screenshot.AttemptID != target {
|
||||
t.Fatalf("screenshot text result = %+v, want found by the visible code", screenshot)
|
||||
}
|
||||
if !strings.Contains(strings.Join(screenshot.Notes, " "), "falls within the life of the attempt") {
|
||||
t.Fatalf("notes = %v, want the tile time within the life of the attempt", screenshot.Notes)
|
||||
}
|
||||
|
||||
// The statement alone, with the invisible characters gone and no tile: only
|
||||
// the wording separates the three attempts that were served it.
|
||||
wordingOnly := traceAs(t, tracer, retyped, "")
|
||||
if wordingOnly.Status != domain.AssessmentLeakTraceFound {
|
||||
t.Fatalf("wording result = %+v, want the three differently worded attempts to be told apart", wordingOnly)
|
||||
}
|
||||
if wordingOnly.Method != domain.AssessmentLeakTraceWording || wordingOnly.AttemptID != target || wordingOnly.EvidenceBits < 1 {
|
||||
t.Fatalf("wording result = %+v, want the wording to find %s", wordingOnly, target)
|
||||
}
|
||||
|
||||
// An operator who read the code by eye gives it with --code and searches the statement text too.
|
||||
byEye := traceAs(t, tracer, "", watermark.Code().String())
|
||||
if byEye.Status != domain.AssessmentLeakTraceFound || byEye.Method != domain.AssessmentLeakTraceVisibleCode || byEye.AttemptID != target {
|
||||
t.Fatalf("code only result = %+v, want found by the code", byEye)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheVisibleCodeToleratesOneMisreadCharacterAndNoMore(t *testing.T) {
|
||||
repository := &leakRepositoryFake{}
|
||||
ids := attemptsThatWriteTheStatementDifferently(t, 2)
|
||||
for _, id := range ids {
|
||||
repository.serve(id, leakStatement)
|
||||
}
|
||||
tracer := newLeakTracer(t, repository)
|
||||
code := leakWatermark(t, ids[0]).Code().String()
|
||||
|
||||
misread := []rune(code)
|
||||
misread[2] = otherSymbol(misread[2])
|
||||
one := traceAs(t, tracer, "", string(misread))
|
||||
if one.Status != domain.AssessmentLeakTraceFound || one.AttemptID != ids[0] || one.EvidenceBits != 45 ||
|
||||
!strings.Contains(strings.Join(one.Notes, " "), "one character of the recognized code differs") {
|
||||
t.Fatalf("one misread character: %+v, want found with 45 bits and a note", one)
|
||||
}
|
||||
misread[6] = otherSymbol(misread[6])
|
||||
two := traceAs(t, tracer, "", string(misread))
|
||||
if two.Status != domain.AssessmentLeakTraceNotFound {
|
||||
t.Fatalf("two misread characters: %+v, want not found", two)
|
||||
}
|
||||
}
|
||||
|
||||
func otherSymbol(symbol rune) rune {
|
||||
if symbol == '2' {
|
||||
return '3'
|
||||
}
|
||||
return '2'
|
||||
}
|
||||
|
||||
func TestTextThatMatchesNothingIsNotFoundAndSaysSo(t *testing.T) {
|
||||
repository := &leakRepositoryFake{}
|
||||
id := attemptsThatWriteTheStatementDifferently(t, 1)[0]
|
||||
repository.serve(id, leakStatement)
|
||||
tracer := newLeakTracer(t, repository)
|
||||
|
||||
for name, text := range map[string]string{
|
||||
"an unrelated paragraph": "The committee will meet on Thursday to discuss the budget for the coming year and the new office.",
|
||||
"a text that is too short": "How many requests?",
|
||||
"a statement of another family": "An in-memory cache holds at most 3 entries and evicts the least recently used entry when it is full.",
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
result := traceAs(t, tracer, text, "")
|
||||
if result.Status != domain.AssessmentLeakTraceNotFound || !result.AttemptID.IsZero() || len(result.Notes) == 0 {
|
||||
t.Fatalf("result = %+v, want not found with a reason and no attempt", result)
|
||||
}
|
||||
})
|
||||
}
|
||||
if len(repository.records) != 3 {
|
||||
t.Fatalf("audit holds %d records, want one for every search, found or not", len(repository.records))
|
||||
}
|
||||
for _, record := range repository.records {
|
||||
if record.Status != domain.AssessmentLeakTraceNotFound || !record.AttemptID.IsZero() {
|
||||
t.Fatalf("audit record = %+v, want not found without an attempt", record)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAStatementThatOnlyOneAttemptHoldsIsFoundByTheVariantAndExcludedByItsWording(t *testing.T) {
|
||||
repository := &leakRepositoryFake{}
|
||||
ids := attemptsThatWriteTheStatementDifferently(t, 2)
|
||||
repository.serve(ids[0], leakStatement)
|
||||
tracer := newLeakTracer(t, repository)
|
||||
|
||||
// The leak was written by the attempt that holds the statement.
|
||||
own := traceAs(t, tracer, domain.VisibleAssessmentText(leakWatermark(t, ids[0]).Mark(leakStatement)), "")
|
||||
if own.Status != domain.AssessmentLeakTraceFound || own.AttemptID != ids[0] || own.Candidates != 1 {
|
||||
t.Fatalf("own text result = %+v, want found", own)
|
||||
}
|
||||
// The same statement with the phrases of another attempt: that attempt may have
|
||||
// been deleted, so the only holder left is excluded by its own wording.
|
||||
foreign := traceAs(t, tracer, domain.VisibleAssessmentText(leakWatermark(t, ids[1]).Mark(leakStatement)), "")
|
||||
if foreign.Status != domain.AssessmentLeakTraceNotFound || !strings.Contains(strings.Join(foreign.Notes, " "), "disagrees with every attempt") {
|
||||
t.Fatalf("foreign wording result = %+v, want not found: the wording excludes the only holder", foreign)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnotherVariantOfTheSameFamilyDoesNotCompeteWithTheLeakedOne(t *testing.T) {
|
||||
repository := &leakRepositoryFake{}
|
||||
ids := attemptsThatWriteTheStatementDifferently(t, 2)
|
||||
// The other variant shares all the text of the family and differs in its numbers.
|
||||
otherVariant := strings.NewReplacer("fewer than 3", "fewer than 4", "(t - 5, t]", "(t - 6, t]", "8 requests", "9 requests", "0 0 1 2 2 3 4 6.", "0 1 1 3 3 4 5 8 9.").Replace(leakStatement)
|
||||
if otherVariant == leakStatement {
|
||||
t.Fatal("the fixture variants are the same")
|
||||
}
|
||||
repository.serve(ids[0], leakStatement)
|
||||
repository.serve(ids[1], otherVariant)
|
||||
tracer := newLeakTracer(t, repository)
|
||||
|
||||
// The leak has no mark and no code, and the family text alone matches both attempts.
|
||||
leak := "Posted in the group chat: " + domain.VisibleAssessmentText(leakWatermark(t, ids[0]).Mark(leakStatement)) + " please help"
|
||||
result := traceAs(t, tracer, leak, "")
|
||||
if result.Status != domain.AssessmentLeakTraceFound || result.AttemptID != ids[0] ||
|
||||
(result.Method != domain.AssessmentLeakTraceWording && result.Method != domain.AssessmentLeakTraceVariant) {
|
||||
t.Fatalf("result = %+v, want the attempt that was served the leaked variant, not the other one of its family", result)
|
||||
}
|
||||
if result.Candidates != 1 {
|
||||
t.Fatalf("candidates = %d, want only the attempt whose statement the text follows", result.Candidates)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheLeakOfADeletedAttemptIsNotAttributedToAnotherVariantOfItsFamily(t *testing.T) {
|
||||
repository := &leakRepositoryFake{}
|
||||
ids := attemptsThatWriteTheStatementDifferently(t, 2)
|
||||
// Only another variant of the family is left: the attempt of the leak was deleted.
|
||||
otherVariant := strings.NewReplacer("fewer than 3", "fewer than 4", "(t - 5, t]", "(t - 6, t]", "8 requests", "9 requests", "0 0 1 2 2 3 4 6.", "0 1 1 3 3 4 5 8 9.").Replace(leakStatement)
|
||||
repository.serve(ids[1], otherVariant)
|
||||
tracer := newLeakTracer(t, repository)
|
||||
|
||||
whole := domain.VisibleAssessmentText(leakWatermark(t, ids[0]).Mark(leakStatement))
|
||||
result := traceAs(t, tracer, whole, "")
|
||||
if result.Status != domain.AssessmentLeakTraceNotFound || !result.AttemptID.IsZero() {
|
||||
t.Fatalf("result = %+v, want not found: the text shares only the text of the family with the attempt that is left", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestACutLeakIsFoundButOtherWordsInsideTheStatementAreNot(t *testing.T) {
|
||||
repository := &leakRepositoryFake{}
|
||||
ids := attemptsThatWriteTheStatementDifferently(t, 1)
|
||||
repository.serve(ids[0], leakStatement)
|
||||
tracer := newLeakTracer(t, repository)
|
||||
whole := domain.VisibleAssessmentText(leakWatermark(t, ids[0]).Mark(leakStatement))
|
||||
|
||||
// The leak stops inside the statement, as a cut or a screenshot of the top of the screen does.
|
||||
cut := whole[:len(whole)*2/3]
|
||||
if result := traceAs(t, tracer, cut, ""); result.Status != domain.AssessmentLeakTraceFound || result.AttemptID != ids[0] {
|
||||
t.Fatalf("cut leak result = %+v, want the attempt that was served the statement", result)
|
||||
}
|
||||
// Other words where the statement continues are not a copy of it.
|
||||
if result := traceAs(t, tracer, cut+" and then somebody wrote something else entirely here", ""); result.Status != domain.AssessmentLeakTraceNotFound {
|
||||
t.Fatalf("diverging leak result = %+v, want not found", result)
|
||||
}
|
||||
// Words around the whole statement are fine.
|
||||
if result := traceAs(t, tracer, "Look: "+whole+" What now?", ""); result.Status != domain.AssessmentLeakTraceFound || result.AttemptID != ids[0] {
|
||||
t.Fatalf("leak with words around = %+v, want found", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTextHeldByAttemptsThatTheWordingCannotSeparateIsAmbiguous(t *testing.T) {
|
||||
repository := &leakRepositoryFake{}
|
||||
// A statement without any interchangeable phrase: the wording tells nothing.
|
||||
plain := "The queue holds 12 jobs numbered from 1 to 12 and the worker takes the smallest number first every time."
|
||||
ids := []domain.AssessmentAttemptID{domain.AssessmentAttemptID(uuid.New()), domain.AssessmentAttemptID(uuid.New())}
|
||||
for _, id := range ids {
|
||||
repository.serve(id, plain)
|
||||
}
|
||||
tracer := newLeakTracer(t, repository)
|
||||
result := traceAs(t, tracer, plain, "")
|
||||
if result.Status != domain.AssessmentLeakTraceAmbiguous || result.Candidates != 2 || !result.AttemptID.IsZero() {
|
||||
t.Fatalf("result = %+v, want ambiguous between the two attempts without naming one", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAMarkCopiedOntoAnotherTextIsFoundButFlagged(t *testing.T) {
|
||||
repository := &leakRepositoryFake{}
|
||||
ids := attemptsThatWriteTheStatementDifferently(t, 2)
|
||||
const other = "A scheduler runs 5 jobs one by one and needs 40 seconds to finish all of them in total."
|
||||
repository.serve(ids[0], leakStatement)
|
||||
repository.serve(ids[1], other)
|
||||
tracer := newLeakTracer(t, repository)
|
||||
|
||||
// The invisible run of attempt 0 sits in the text of the other statement.
|
||||
var run strings.Builder
|
||||
for _, r := range leakWatermark(t, ids[0]).Mark(leakStatement) {
|
||||
if unicode.Is(unicode.Cf, r) {
|
||||
run.WriteRune(r)
|
||||
} else if run.Len() > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
result := traceAs(t, tracer, other[:20]+run.String()+other[20:], "")
|
||||
if result.Status != domain.AssessmentLeakTraceFound || result.Method != domain.AssessmentLeakTraceInvisibleMark || result.AttemptID != ids[0] {
|
||||
t.Fatalf("result = %+v, want the mark to name attempt 0", result)
|
||||
}
|
||||
if !strings.Contains(strings.Join(result.Notes, " "), "attached to another text") {
|
||||
t.Fatalf("notes = %v, want the warning that the text does not match the attempt", result.Notes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAMarkOfNoAttemptIsNotFound(t *testing.T) {
|
||||
repository := &leakRepositoryFake{}
|
||||
repository.serve(domain.AssessmentAttemptID(uuid.New()), leakStatement)
|
||||
tracer := newLeakTracer(t, repository)
|
||||
// The attempt of this mark is not in the table: deleted, or issued under another secret.
|
||||
unknown := leakWatermark(t, domain.AssessmentAttemptID(uuid.New()))
|
||||
result := traceAs(t, tracer, unknown.Mark("An entirely different paragraph that no statement of the bank contains at all."), "")
|
||||
if result.Status != domain.AssessmentLeakTraceNotFound || !strings.Contains(strings.Join(result.Notes, " "), "matches no attempt") {
|
||||
t.Fatalf("result = %+v, want not found with a note about the mark", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkAndVisibleCodeOfDifferentAttemptsAreAmbiguous(t *testing.T) {
|
||||
repository := &leakRepositoryFake{}
|
||||
ids := attemptsThatWriteTheStatementDifferently(t, 2)
|
||||
for _, id := range ids {
|
||||
repository.serve(id, leakStatement)
|
||||
}
|
||||
tracer := newLeakTracer(t, repository)
|
||||
marked := leakWatermark(t, ids[0]).Mark(leakStatement)
|
||||
result := traceAs(t, tracer, marked+" "+leakWatermark(t, ids[1]).Code().String()+" "+leakNow.Format("2006-01-02 15:04"), "")
|
||||
if result.Status != domain.AssessmentLeakTraceAmbiguous || !result.AttemptID.IsZero() {
|
||||
t.Fatalf("result = %+v, want ambiguous: the mark and the code name different attempts", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEverySearchIsAuditedWithoutTheTextAndAFailedAuditFailsTheSearch(t *testing.T) {
|
||||
repository := &leakRepositoryFake{}
|
||||
id := attemptsThatWriteTheStatementDifferently(t, 1)[0]
|
||||
repository.serve(id, leakStatement)
|
||||
tracer := newLeakTracer(t, repository)
|
||||
leaked := leakWatermark(t, id).Mark(leakStatement)
|
||||
|
||||
traceAs(t, tracer, leaked, "")
|
||||
traceAs(t, tracer, leaked, "")
|
||||
traceAs(t, tracer, "A paragraph that matches no statement of any attempt in the whole bank at all.", "")
|
||||
if len(repository.records) != 3 {
|
||||
t.Fatalf("audit holds %d records, want 3", len(repository.records))
|
||||
}
|
||||
first, second, third := repository.records[0], repository.records[1], repository.records[2]
|
||||
if first.ID == second.ID || first.TextDigest != second.TextDigest || first.TextDigest == third.TextDigest {
|
||||
t.Fatal("the digest must tie the same text together and tell another text apart, and every search has its own ID")
|
||||
}
|
||||
if first.Actor != "operator:night.shift" || first.KeyVersion != 7 || first.TextRunes < 100 {
|
||||
t.Fatalf("record = %+v, want the actor, the key version, and the length of the text", first)
|
||||
}
|
||||
stored := fmt.Sprintf("%+v", repository.records)
|
||||
for _, secret := range []string{"gateway", "sliding-window", "arrival times"} {
|
||||
if strings.Contains(stored, secret) {
|
||||
t.Fatalf("the audit holds %q, want no part of the leaked text", secret)
|
||||
}
|
||||
}
|
||||
|
||||
repository.recordErr = errors.New("audit storage is down")
|
||||
if result, err := tracer.Trace(context.Background(), LeakTraceCommand{Actor: "operator:night.shift", Text: leaked}); err == nil || !result.AttemptID.IsZero() {
|
||||
t.Fatalf("Trace() = %+v, %v, want no result when the audit cannot be written", result, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLeakTraceRefusesBadRequestsWithoutQuotingTheText(t *testing.T) {
|
||||
tracer := newLeakTracer(t, &leakRepositoryFake{})
|
||||
secret := "SECRET-LEAKED-TEXT"
|
||||
tests := map[string]LeakTraceCommand{
|
||||
"no actor": {Text: secret},
|
||||
"a foreign actor": {Actor: "alice", Text: secret},
|
||||
"no text, no code": {Actor: "operator:night.shift"},
|
||||
"white space only": {Actor: "operator:night.shift", Text: " \n\t "},
|
||||
"a bad code": {Actor: "operator:night.shift", Code: "NOT A CODE"},
|
||||
"an oversized text": {Actor: "operator:night.shift", Text: strings.Repeat("a", maxLeakTextBytes+1)},
|
||||
"invalid UTF-8": {Actor: "operator:night.shift", Text: secret + "\xff\xfe"},
|
||||
}
|
||||
for name, command := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
_, err := tracer.Trace(context.Background(), command)
|
||||
if err == nil {
|
||||
t.Fatal("Trace() succeeded, want an error")
|
||||
}
|
||||
if strings.Contains(err.Error(), secret) {
|
||||
t.Fatalf("the error quotes the text: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
if _, err := NewLeakTracer(nil, hashedSeeds{}, leakDigesterFake{}); err == nil {
|
||||
t.Fatal("NewLeakTracer(nil transactor) succeeded")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user