Home

go-spectre @7661556e93beda3525190fc2767bb91fed3a284d - refs - log -
-
https://git.jolheiser.com/go-spectre.git
Go implementation for spectre/masterpassword
go-spectre / spectre_test.go
- raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package spectre

import (
	_ "embed"
	"encoding/xml"
	"strconv"
	"strings"
	"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()
	}

	scoper := SimpleScope{
		Key: "com.lyndir.masterpassword",
	}
	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, scoper)
			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 !strings.EqualFold(pass, tc.Result) {
				t.Log("passwords did not match")
				t.Fail()
			}
		})
	}
}

//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
}