package spectre import ( _ "embed" "encoding/xml" "strconv" "testing" ) func TestSpectre(t *testing.T) { var tests TestCases if err := xml.Unmarshal(testsXML, &tests); err != nil { t.Log("could not load test data") t.FailNow() } dc := tests.Cases[0] for _, tc := range tests.Cases[1:] { t.Run(tc.ID, func(t *testing.T) { user := def(dc.UserName, tc.UserName) secret := def(dc.UserSecret, tc.UserSecret) s, err := New(user, secret) if err != nil { t.Logf("could not initialize spectre: %v", err) t.Fail() } siteName := def(dc.SiteName, tc.SiteName) template := def(dc.ResultType, tc.ResultType) counterStr := def(dc.KeyCounter, tc.KeyCounter) counter, err := strconv.Atoi(counterStr) if err != nil { t.Log("could not convert counter") t.Fail() } scope := def(dc.KeyPurpose, tc.KeyPurpose) pass := s.Site(siteName, WithTemplate(Template(template)), WithCounter(counter), WithScope(Scope(scope)), ) if pass != tc.Result { t.Log("passwords did not match") t.Fail() } }) } } // From the website sanity check func TestSanity(t *testing.T) { s, err := New("Robert Lee Mitchell", "banana colored duckling") if err != nil { t.Logf("failed sanity check: %v", err) t.FailNow() } pw := s.Site("masterpasswordapp.com") if pw != "Jejr5[RepuSosp" { t.Log("failed sanity check") t.FailNow() } } //go:embed spectre_tests.xml var testsXML []byte type TestCases struct { Cases []TestCase `xml:"case"` } type TestCase struct { ID string `xml:"id,attr"` UserName string `xml:"userName"` UserSecret string `xml:"userSecret"` SiteName string `xml:"siteName"` ResultType string `xml:"resultType"` KeyCounter string `xml:"keyCounter"` KeyPurpose string `xml:"keyPurpose"` Result string `xml:"result"` } func def(def, alt string) string { if alt != "" { return alt } return def }