mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 01:53:42 +01:00
133 lines
4.2 KiB
Go
133 lines
4.2 KiB
Go
package data
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestNormalizeLanguage checks codes, tags and English names are read as the
|
|
// code, without case and spaces, and that anything else is refused
|
|
func TestNormalizeLanguage(t *testing.T) {
|
|
cases := map[string]string{
|
|
"da": "da",
|
|
" DA ": "da",
|
|
"da-DK": "da",
|
|
"da_dk": "da",
|
|
"DE-at": "de",
|
|
"zh-Hant-TW": "zh",
|
|
"Danish": "da",
|
|
" danish ": "da",
|
|
"GERMAN": "de",
|
|
"Norwegian": "no",
|
|
"nb-NO": "no",
|
|
"nn": "no",
|
|
"Norwegian Bokmål": "no",
|
|
"norwegian nynorsk": "no",
|
|
"iw": "he",
|
|
"": "",
|
|
" ": "",
|
|
}
|
|
for in, want := range cases {
|
|
got, ok := NormalizeLanguage(in)
|
|
if !ok || got != want {
|
|
t.Errorf("%q: got %q %v, want %q", in, got, ok, want)
|
|
}
|
|
}
|
|
for _, in := range []string{"xx", "Klingon", "d", "dan", "-da", "da DK x", strings.Repeat("a", MaxLanguageInputLength+1)} {
|
|
if got, ok := NormalizeLanguage(in); ok {
|
|
t.Errorf("%q: got %q, want a refusal", in, got)
|
|
}
|
|
}
|
|
// every code and every name of the list is read
|
|
for _, l := range Languages {
|
|
if got, ok := NormalizeLanguage(l.Code); !ok || got != l.Code {
|
|
t.Errorf("code %s: got %q", l.Code, got)
|
|
}
|
|
if got, ok := NormalizeLanguage(strings.ToUpper(l.Name)); !ok || got != l.Code {
|
|
t.Errorf("name %s: got %q", l.Name, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestNormalizeLanguageUnicodeForms checks a name with a separate accent mark
|
|
// (NFD) and values with zero width characters are read like the plain text
|
|
func TestNormalizeLanguageUnicodeForms(t *testing.T) {
|
|
cases := map[string]string{
|
|
"Bokma\u030al": "no",
|
|
"Norwegian Bokma\u030al": "no",
|
|
"NORWEGIAN BOKMA\u030aL": "no",
|
|
"bokm\u00e5l": "no",
|
|
"da\u200b": "da",
|
|
"\ufeffda-DK": "da",
|
|
"Dan\u200cish": "da",
|
|
"Ger\u200dman": "de",
|
|
"\u200b": "",
|
|
"Bokma\u200b\u030al": "no",
|
|
}
|
|
for in, want := range cases {
|
|
got, ok := NormalizeLanguage(in)
|
|
if !ok || got != want {
|
|
t.Errorf("%q: got %q %v, want %q", in, got, ok, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// languagesJSPath is the frontend copy of the language list, seen from this
|
|
// package folder
|
|
var languagesJSPath = filepath.Join("..", "..", "frontend", "src", "lib", "utils", "languages.js")
|
|
|
|
var languageEntryJS = regexp.MustCompile(`\{\s*code:\s*'([^']*)',\s*name:\s*'([^']*)'\s*\}`)
|
|
|
|
// TestLanguageListMatchesFrontend checks the frontend language list holds the
|
|
// same codes and names in the same order. The backend container mounts only
|
|
// the backend, so run it with the frontend mounted next to it, for example
|
|
// docker compose run --rm --no-deps -v ./phishingclub/frontend:/frontend backend
|
|
func TestLanguageListMatchesFrontend(t *testing.T) {
|
|
src, err := os.ReadFile(languagesJSPath)
|
|
if os.IsNotExist(err) {
|
|
t.Skipf("frontend not found at %s", languagesJSPath)
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
matches := languageEntryJS.FindAllStringSubmatch(string(src), -1)
|
|
if len(matches) != len(Languages) {
|
|
t.Fatalf("frontend lists %d languages, backend %d", len(matches), len(Languages))
|
|
}
|
|
for i, m := range matches {
|
|
if m[1] != Languages[i].Code || m[2] != Languages[i].Name {
|
|
t.Errorf("entry %d: frontend %s %s, backend %s %s", i, m[1], m[2], Languages[i].Code, Languages[i].Name)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestLanguageList checks the codes are unique lowercase two letter codes
|
|
func TestLanguageList(t *testing.T) {
|
|
code := regexp.MustCompile(`^[a-z]{2}$`)
|
|
seen := map[string]bool{}
|
|
for _, l := range Languages {
|
|
if !code.MatchString(l.Code) {
|
|
t.Errorf("code %q is not two lowercase letters", l.Code)
|
|
}
|
|
if seen[l.Code] {
|
|
t.Errorf("code %q is listed twice", l.Code)
|
|
}
|
|
seen[l.Code] = true
|
|
if l.Name == "" {
|
|
t.Errorf("code %q has no name", l.Code)
|
|
}
|
|
}
|
|
if len(Languages) != 43 {
|
|
t.Errorf("got %d languages, want 43", len(Languages))
|
|
}
|
|
if !IsValidLanguage("no") || IsValidLanguage("NO") || IsValidLanguage("pt-br") || IsValidLanguage("") {
|
|
t.Error("IsValidLanguage must match list codes exactly")
|
|
}
|
|
if LanguageName("de") != "German" || LanguageName("xx") != "xx" {
|
|
t.Error("LanguageName gives the English name or the code")
|
|
}
|
|
}
|