Implement machine fingerprint hashing (T-501)
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package windows
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"softbox.local/core/licensing"
|
||||
)
|
||||
|
||||
func TestMachineHashFromUsesBothRequiredSources(t *testing.T) {
|
||||
const machineGUID = "6F9619FF-8B86-D011-B42D-00C04FC964FF"
|
||||
got, err := machineHashFrom(
|
||||
func() (string, error) { return machineGUID, nil },
|
||||
func() (uint32, error) { return 0x1a2b3c4d, nil },
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("machineHashFrom() error = %v", err)
|
||||
}
|
||||
want, err := licensing.DeriveMachineHash(machineGUID, 0x1a2b3c4d)
|
||||
if err != nil {
|
||||
t.Fatalf("DeriveMachineHash() error = %v", err)
|
||||
}
|
||||
if got != want {
|
||||
t.Fatalf("machineHashFrom() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMachineHashFromFailsClosedWithoutLeakingSourceValues(t *testing.T) {
|
||||
const secret = "synthetic-machine-guid-secret"
|
||||
tests := []struct {
|
||||
name string
|
||||
readGUID machineGUIDReader
|
||||
readSerial systemVolumeSerialReader
|
||||
}{
|
||||
{
|
||||
name: "GUID reader failure",
|
||||
readGUID: func() (string, error) { return "", errors.New(secret) },
|
||||
readSerial: func() (uint32, error) { return 1, nil },
|
||||
},
|
||||
{
|
||||
name: "volume reader failure",
|
||||
readGUID: func() (string, error) { return "6f9619ff-8b86-d011-b42d-00c04fc964ff", nil },
|
||||
readSerial: func() (uint32, error) { return 0, errors.New(secret) },
|
||||
},
|
||||
{
|
||||
name: "invalid GUID",
|
||||
readGUID: func() (string, error) { return secret, nil },
|
||||
readSerial: func() (uint32, error) { return 1, nil },
|
||||
},
|
||||
{
|
||||
name: "missing GUID reader",
|
||||
readSerial: func() (uint32, error) { return 1, nil },
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
_, err := machineHashFrom(test.readGUID, test.readSerial)
|
||||
if !errors.Is(err, ErrMachineFingerprintUnavailable) {
|
||||
t.Fatalf("machineHashFrom() error = %v, want ErrMachineFingerprintUnavailable", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), secret) {
|
||||
t.Fatalf("machineHashFrom() leaked a source value in %q", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user