~seirdy/moac

2869b0497cf1d84fe534971e8e0fe623bd998cf5 — Rohan Kumar 15 days ago d8481b5
Tests: add tests for givens.go
1 files changed, 67 insertions(+), 0 deletions(-)

A givens_test.go
A givens_test.go => givens_test.go +67 -0
@@ 0,0 1,67 @@
package moac

import (
	"math"
	"testing"
)

const margin = 0.05 // acceptable error

var tests = []struct {
	name    string
	given   Givens
	quantum bool
	// Expected values should be within 10% error
	expectedBF float64
	expectedME float64
}{
	{ // from README
		name:    "hitchhiker",
		quantum: true,
		given: Givens{
			Mass:     5.97e24,
			Time:     1.45e17,
			Password: "v¢JÊÙúQ§4mÀÛªZûYÍé©mËiÐ× \"½J6y.ñíí'è¦ïϵ°~",
		},
		expectedBF: 0.6807,
		expectedME: 427.3,
	},
	{ // from blog post: https://seirdy.one/2021/01/12/password-strength.html
		name:    "universe",
		quantum: false,
		given: Givens{
			// default mass is the mass of the observable universe
			Entropy: 510,
		},
		expectedBF: 9.527e-62,
		expectedME: 307.3,
	},
}

func TestBruteForceability(t *testing.T) {
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			got, err := BruteForceability(&test.given, test.quantum)
			if err != nil {
				t.Fatalf("BruteForceability() = %v", err)
			}
			if math.Abs(got-test.expectedBF)/test.expectedBF > margin {
				t.Errorf("Bruteforceability() = %.4g; want %.4g", got, test.expectedBF)
			}
		})
	}
}

func TestMinEntropy(t *testing.T) {
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			got, err := MinEntropy(&test.given, test.quantum)
			if err != nil {
				t.Fatalf("MinEntropy() = %v", err)
			}
			if math.Abs(got-test.expectedME)/test.expectedME > margin {
				t.Errorf("MinEntropy() = %.4g; want %.4g", got, test.expectedME)
			}
		})
	}
}