mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 10:03:46 +01:00
76 lines
3.0 KiB
Go
76 lines
3.0 KiB
Go
package dbcopy
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestSafeText escapes control and format characters and bytes that are
|
|
// not UTF-8, and keeps other text
|
|
func TestSafeText(t *testing.T) {
|
|
cases := map[string]string{
|
|
"campaign_events": "campaign_events",
|
|
"café": "café",
|
|
"zz\x1b]0;T\x07": `zz\u001b]0;T\u0007`,
|
|
"a\r\nb": `a\u000d\u000ab`,
|
|
"x\xffy": `x\xffy`,
|
|
"\u202eevil": `\u202eevil`,
|
|
"c\u0085d": `c\u0085d`,
|
|
"q\x1b[2J\x1b[Hfake\x7f": `q\u001b[2J\u001b[Hfake\u007f`,
|
|
}
|
|
for in, want := range cases {
|
|
if got := SafeText(in); got != want {
|
|
t.Errorf("%q: %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestReportFooter checks the last line says what the run left in the
|
|
// target, and that names from the source are printed safe
|
|
func TestReportFooter(t *testing.T) {
|
|
tables := []TableReport{{Name: "options", SourceRows: 4, Copied: 4}}
|
|
cases := []struct {
|
|
name string
|
|
report Report
|
|
want string
|
|
notWant []string
|
|
}{
|
|
{"refused before the schema", Report{Err: ErrTargetNotEmpty}, "Nothing was written to the target.", []string{"Tables", "Rows:", "schema"}},
|
|
{"cancelled before the schema", Report{Err: ErrCancelled}, "Cancelled, nothing was written", []string{"Rows:"}},
|
|
{"refused after the schema", Report{SchemaMade: true, SourceRead: true, Tables: tables, Err: ErrInvalidValues},
|
|
"Nothing was committed, every row was rolled back. The target keeps the empty schema", []string{"copied"}},
|
|
{"cancelled after the schema", Report{SchemaMade: true, SourceRead: true, Tables: tables, Err: ErrCancelled},
|
|
"Cancelled, every row was rolled back.", []string{"copied"}},
|
|
{"dry run", Report{DryRun: true, SchemaMade: true, SourceRead: true, Tables: tables},
|
|
"Dry run: every row was rolled back. The target keeps the empty schema", []string{"4 copied"}},
|
|
{"dry run refused", Report{DryRun: true, SchemaMade: true, Tables: tables, Err: ErrOrphans},
|
|
"Nothing was committed", []string{"would be copied"}},
|
|
{"committed", Report{Committed: true, SchemaMade: true, SourceRead: true, Tables: tables}, "Copied and committed", nil},
|
|
}
|
|
for _, tc := range cases {
|
|
var out strings.Builder
|
|
tc.report.Print(&out)
|
|
text := out.String()
|
|
if !strings.Contains(text, tc.want) {
|
|
t.Errorf("%s: lacks %q:\n%s", tc.name, tc.want, text)
|
|
}
|
|
for _, nw := range tc.notWant {
|
|
if strings.Contains(text, nw) {
|
|
t.Errorf("%s: has %q:\n%s", tc.name, nw, text)
|
|
}
|
|
}
|
|
}
|
|
r := Report{
|
|
UnknownTables: []UnknownTable{{Name: "zz\x1b]0;PWNED\x07\rCopied and committed", Rows: -1}},
|
|
UnknownColumns: []UnknownColumn{{Table: "options", Column: "c\x1b[31mRED", Values: 1}},
|
|
BaselinesKept: []BaselineItem{{ResourceType: "page\x1b[8m", ResourceID: "x", Reason: "r"}},
|
|
PackagesNotClean: []PackageItem{{PackageID: "ns/pkg\x1b[2J", Version: "1.0.0"}},
|
|
Err: ErrUnknownData,
|
|
}
|
|
var out strings.Builder
|
|
r.Print(&out)
|
|
if strings.ContainsAny(out.String(), "\x1b\x07\r") {
|
|
t.Fatalf("a control character reached the output: %q", out.String())
|
|
}
|
|
}
|