summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-10-18 08:52:08 -0300
committerEuAndreh <eu@euandre.org>2024-10-18 08:52:08 -0300
commit9b3458e7d263b1535716ed30e27e6507ee9b3c91 (patch)
treef9be1b76127b4b13d17fb4a56961e9fe89080ab2
parentMakefile: Add suport for functional tests, fuzzing and benchmarks (diff)
downloadscrypt-9b3458e7d263b1535716ed30e27e6507ee9b3c91.tar.gz
scrypt-9b3458e7d263b1535716ed30e27e6507ee9b3c91.tar.xz
Add baseline functional test, fuzz target and benchmark
-rw-r--r--.gitignore10
-rw-r--r--deps.mk57
-rwxr-xr-xmkdeps.sh25
l---------tests/benchmarks/hash/main.go1
-rw-r--r--tests/benchmarks/hash/scrypt.go28
l---------tests/functional/hash-and-check/main.go1
-rw-r--r--tests/functional/hash-and-check/scrypt.go40
l---------tests/fuzz/api/main.go1
-rw-r--r--tests/fuzz/api/scrypt.go37
-rw-r--r--tests/libbuild.go22
10 files changed, 199 insertions, 23 deletions
diff --git a/.gitignore b/.gitignore
index f4905f3..d910245 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,14 @@
-/*.bin
/src/version.go
+/*.bin
/src/*.a
/src/*.bin
/tests/*.a
/tests/*.bin
+/tests/functional/*/*.a
+/tests/functional/*/*.bin
+/tests/fuzz/*/*.a
+/tests/fuzz/*/*.bin
+/tests/benchmarks/*/*.a
+/tests/benchmarks/*/*.bin
+/tests/benchmarks/*/*.txt
+/tests/fuzz/corpus/
diff --git a/deps.mk b/deps.mk
index e69de29..1c60984 100644
--- a/deps.mk
+++ b/deps.mk
@@ -0,0 +1,57 @@
+libs.go = \
+ src/scrypt.go \
+ tests/benchmarks/hash/scrypt.go \
+ tests/functional/hash-and-check/scrypt.go \
+ tests/fuzz/api/scrypt.go \
+ tests/scrypt.go \
+
+mains.go = \
+ src/main.go \
+ tests/benchmarks/hash/main.go \
+ tests/functional/hash-and-check/main.go \
+ tests/fuzz/api/main.go \
+ tests/main.go \
+
+functional-tests/libs.go = \
+ tests/functional/hash-and-check/scrypt.go \
+
+functional-tests/main.go = \
+ tests/functional/hash-and-check/main.go \
+
+fuzz-targets/lib.go = \
+ tests/fuzz/api/scrypt.go \
+
+fuzz-targets/main.go = \
+ tests/fuzz/api/main.go \
+
+benchmarks/lib.go = \
+ tests/benchmarks/hash/scrypt.go \
+
+benchmarks/main.go = \
+ tests/benchmarks/hash/main.go \
+
+src/main.a: src/main.go
+src/scrypt.a: src/scrypt.go
+tests/benchmarks/hash/main.a: tests/benchmarks/hash/main.go
+tests/benchmarks/hash/scrypt.a: tests/benchmarks/hash/scrypt.go
+tests/functional/hash-and-check/main.a: tests/functional/hash-and-check/main.go
+tests/functional/hash-and-check/scrypt.a: tests/functional/hash-and-check/scrypt.go
+tests/fuzz/api/main.a: tests/fuzz/api/main.go
+tests/fuzz/api/scrypt.a: tests/fuzz/api/scrypt.go
+tests/main.a: tests/main.go
+tests/scrypt.a: tests/scrypt.go
+src/main.bin: src/main.a
+tests/benchmarks/hash/main.bin: tests/benchmarks/hash/main.a
+tests/functional/hash-and-check/main.bin: tests/functional/hash-and-check/main.a
+tests/fuzz/api/main.bin: tests/fuzz/api/main.a
+tests/main.bin: tests/main.a
+src/main.bin-check: src/main.bin
+tests/benchmarks/hash/main.bin-check: tests/benchmarks/hash/main.bin
+tests/functional/hash-and-check/main.bin-check: tests/functional/hash-and-check/main.bin
+tests/fuzz/api/main.bin-check: tests/fuzz/api/main.bin
+tests/main.bin-check: tests/main.bin
+src/main.a: src/$(NAME).a
+tests/benchmarks/hash/main.a: tests/benchmarks/hash/$(NAME).a
+tests/functional/hash-and-check/main.a: tests/functional/hash-and-check/$(NAME).a
+tests/fuzz/api/main.a: tests/fuzz/api/$(NAME).a
+tests/main.a: tests/$(NAME).a
diff --git a/mkdeps.sh b/mkdeps.sh
index e5606ff..b1c61b3 100755
--- a/mkdeps.sh
+++ b/mkdeps.sh
@@ -2,3 +2,28 @@
set -eu
export LANG=POSIX.UTF-8
+
+
+libs() {
+ find src tests -name '*.go' | grep -v '/main\.go$' |
+ grep -v '/version\.go$'
+}
+
+mains() {
+ find src tests -name '*.go' | grep '/main\.go$'
+}
+
+libs | varlist 'libs.go'
+mains | varlist 'mains.go'
+
+find tests/functional/*/*.go -not -name main.go | varlist 'functional-tests/libs.go'
+find tests/functional/*/main.go | varlist 'functional-tests/main.go'
+find tests/fuzz/*/*.go -not -name main.go | varlist 'fuzz-targets/lib.go'
+find tests/fuzz/*/main.go | varlist 'fuzz-targets/main.go'
+find tests/benchmarks/*/*.go -not -name main.go | varlist 'benchmarks/lib.go'
+find tests/benchmarks/*/main.go | varlist 'benchmarks/main.go'
+
+{ libs; mains; } | sort | sed 's/^\(.*\)\.go$/\1.a:\t\1.go/'
+mains | sort | sed 's/^\(.*\)\.go$/\1.bin:\t\1.a/'
+mains | sort | sed 's/^\(.*\)\.go$/\1.bin-check:\t\1.bin/'
+mains | sort | sed 's|^\(.*\)/main\.go$|\1/main.a:\t\1/$(NAME).a|'
diff --git a/tests/benchmarks/hash/main.go b/tests/benchmarks/hash/main.go
new file mode 120000
index 0000000..f67563d
--- /dev/null
+++ b/tests/benchmarks/hash/main.go
@@ -0,0 +1 @@
+../../main.go \ No newline at end of file
diff --git a/tests/benchmarks/hash/scrypt.go b/tests/benchmarks/hash/scrypt.go
new file mode 100644
index 0000000..82f9ed5
--- /dev/null
+++ b/tests/benchmarks/hash/scrypt.go
@@ -0,0 +1,28 @@
+package scrypt
+
+import (
+ "flag"
+)
+
+
+
+var nFlag = flag.Int(
+ "n",
+ 100,
+ "The number of iterations to execute",
+)
+
+func MainTest() {
+ flag.Parse()
+ n := *nFlag
+
+ password := []byte("password")
+ salt := []byte("salt0123456789abcdef0123456789abcdef")
+
+ for i := 0; i < n; i++ {
+ _, err := Hash(password, salt)
+ if err != nil {
+ panic(err)
+ }
+ }
+}
diff --git a/tests/functional/hash-and-check/main.go b/tests/functional/hash-and-check/main.go
new file mode 120000
index 0000000..f67563d
--- /dev/null
+++ b/tests/functional/hash-and-check/main.go
@@ -0,0 +1 @@
+../../main.go \ No newline at end of file
diff --git a/tests/functional/hash-and-check/scrypt.go b/tests/functional/hash-and-check/scrypt.go
new file mode 100644
index 0000000..17f2982
--- /dev/null
+++ b/tests/functional/hash-and-check/scrypt.go
@@ -0,0 +1,40 @@
+package scrypt
+
+import (
+ g "gobang"
+
+)
+
+
+
+func MainTest() {
+ g.Testing("from a known input we check the hash", func() {
+ const (
+ password = "a fixed password"
+ salt = "a fixed salt____________________"
+ )
+
+ hash, err := Hash([]byte(password), []byte(salt))
+ g.TErrorIf(err)
+
+ ok, err := Check([]byte(password), []byte(salt), hash)
+ g.TErrorIf(err)
+ g.TAssertEqual(ok, true)
+ })
+
+ g.Testing("we can genereate a hash and check it is equal", func() {
+ password, err := Salt()
+ g.TErrorIf(err)
+
+ salt, err := Salt()
+ g.TErrorIf(err)
+
+ hash, err := Hash(password, salt)
+ g.TErrorIf(err)
+
+ ok, err := Check(password, salt, hash)
+ g.TErrorIf(err)
+
+ g.TAssertEqual(ok, true)
+ })
+}
diff --git a/tests/fuzz/api/main.go b/tests/fuzz/api/main.go
new file mode 120000
index 0000000..f67563d
--- /dev/null
+++ b/tests/fuzz/api/main.go
@@ -0,0 +1 @@
+../../main.go \ No newline at end of file
diff --git a/tests/fuzz/api/scrypt.go b/tests/fuzz/api/scrypt.go
new file mode 100644
index 0000000..eb29d2d
--- /dev/null
+++ b/tests/fuzz/api/scrypt.go
@@ -0,0 +1,37 @@
+package scrypt
+
+import (
+ "os"
+ "testing"
+ "testing/internal/testdeps"
+)
+
+
+
+func api(f *testing.F) {
+ f.Fuzz(func(t *testing.T, password []byte, salt []byte) {
+ if len(salt) < saltMinLength {
+ return
+ }
+
+ _, err := Hash(password, salt)
+ if err != nil {
+ t.Errorf("Failed on: %#v\n", err)
+ }
+ })
+}
+
+
+
+func MainTest() {
+ fuzzTargets := []testing.InternalFuzzTarget{
+ { "api", api },
+ }
+
+ deps := testdeps.TestDeps{}
+ tests := []testing.InternalTest {}
+ benchmarks := []testing.InternalBenchmark{}
+ examples := []testing.InternalExample {}
+ m := testing.MainStart(deps, tests, benchmarks, fuzzTargets, examples)
+ os.Exit(m.Run())
+}
diff --git a/tests/libbuild.go b/tests/libbuild.go
deleted file mode 100644
index 31e93c9..0000000
--- a/tests/libbuild.go
+++ /dev/null
@@ -1,22 +0,0 @@
-package main
-
-import (
- "encoding/hex"
-
- g "gobang"
-
- "scrypt"
-)
-
-func main() {
- s1, err := scrypt.Salt()
- g.TErrorIf(err)
-
- s2, err := scrypt.Salt()
- g.TErrorIf(err)
-
- hash, err := scrypt.Hash(s1, s2)
- g.TErrorIf(err)
-
- println(hex.EncodeToString(hash))
-}