From dd7f022223f6d66b7642b4955aeb39cb728fc278 Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Thu, 27 Jun 2024 16:24:52 +0200 Subject: [PATCH] Add tests for Go code --- README.md | 11 ++++--- cmd/tlp/env_test.go | 58 ++++++++++++++++++++++++++++++++++ cmd/wasm/main.go | 2 ++ frontend/src/assets/main.wasm | Bin 3301122 -> 3301122 bytes lhtlp/lhtlp_test.go | 49 ++++++++++++++++++++++++++++ tlp/tlp_test.go | 25 +++++++++++++++ 6 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 cmd/tlp/env_test.go create mode 100644 lhtlp/lhtlp_test.go create mode 100644 tlp/tlp_test.go diff --git a/README.md b/README.md index 1dbd7ea..6e6f67c 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,12 @@ The [second implementation](https://pmuens.github.io/time-lock-puzzle/lhtlp.html 1. `git clone ` 2. `cd ` 3. `go run ./cmd/tlp` -4. `GOOS=js GOARCH=wasm go build -o ./frontend/src/assets/main.wasm cmd/wasm/main.go` -5. `cd frontend` -6. `npm install` -7. `npm run dev` -8. [http://localhost:5173](http://localhost:5173) +4. `go test ./...` +5. `GOOS=js GOARCH=wasm go build -o ./frontend/src/assets/main.wasm cmd/wasm/main.go` +6. `cd frontend` +7. `npm install` +8. `npm run dev` +9. [http://localhost:5173](http://localhost:5173) ## Useful Commands diff --git a/cmd/tlp/env_test.go b/cmd/tlp/env_test.go new file mode 100644 index 0000000..4c361cb --- /dev/null +++ b/cmd/tlp/env_test.go @@ -0,0 +1,58 @@ +package main + +import ( + "io" + "testing" +) + +type parseConfigTest struct { + name string + args []string + want config +} + +func TestParseConfigValidInput(t *testing.T) { + t.Parallel() + + for _, tt := range []parseConfigTest{ + { + name: "no_flags", + args: []string{}, + want: config{bits: 0, message1: 0, message2: 0, difficulty: 100}, + }, + { + name: "all_flags", + args: []string{"-difficulty=100000"}, + want: config{bits: 0, message1: 0, message2: 0, difficulty: 100_000}, + }, + } { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + var got config + if err := parseConfig(&got, tt.args, io.Discard); err != nil { + t.Fatalf("parseConfig() error = %v, want no error", err) + } + if got != tt.want { + t.Errorf("flags = %+v, want %+v", got, tt.want) + } + }) + } +} + +func TestParseConfigInvalidInput(t *testing.T) { + t.Parallel() + + for _, tt := range []parseConfigTest{ + {name: "difficulty_zero", args: []string{"-difficulty=0"}}, + } { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + err := parseConfig(&config{}, tt.args, io.Discard) + if err == nil { + t.Fatal("parseConfig() = nil, want error") + } + }) + } +} diff --git a/cmd/wasm/main.go b/cmd/wasm/main.go index 4ef07ad..d345ece 100644 --- a/cmd/wasm/main.go +++ b/cmd/wasm/main.go @@ -1,3 +1,5 @@ +//go:build wasm + package main import ( diff --git a/frontend/src/assets/main.wasm b/frontend/src/assets/main.wasm index 9bf205f2a17c55e3b50b9a31561d181b12ae94e6..fef2528514049c50e871f65610483aa478d82d01 100755 GIT binary patch delta 397 zcmXZPJ1>J_7{+l=tIpa|XX<>W?}6%M(H4==;w2FajWli3lv3wh3(2Lc?Oi1niGfIv zAQC1f-@st?4NRVfJNf0l{>RVn;#fNEGuFlrH#fYyfvI?6Xeu=l8%ygxHMVmQ*xLyt zQY*&HoH5~w)GhT$y;7glFAYe8 z_imTx^{1>|+*rkxRa{#wm*#i5vM5H)F}tFQb#v3M_{5ocVON5dPm#;otj(cF>b=dO kyJ}-9N!${xh89usEF=%B?JPEn#6W^d z5D62L-Dve6nEV>vOr^ugwS;1Gl$ zY=oW2CLBZ#kxS$e`9uLxNE8vpghDt87g0i#5^kc5@DSxh1yM;<5!FNuQA>DdJ8vp$v+a-7SzrSRAI7r#g16 diff --git a/lhtlp/lhtlp_test.go b/lhtlp/lhtlp_test.go new file mode 100644 index 0000000..8325a23 --- /dev/null +++ b/lhtlp/lhtlp_test.go @@ -0,0 +1,49 @@ +package lhtlp_test + +import ( + "math/big" + "testing" + + "github.com/pmuens/time-lock-puzzle/lhtlp" +) + +const Bits = 1024 + +func TestLHTLP(t *testing.T) { + t.Run("Generate and solve one puzzle", func(t *testing.T) { + message := 42 + difficulty := 1 + + want := big.NewInt(int64(message)) + + params := lhtlp.NewParams(Bits, difficulty) + puzzle := lhtlp.NewPuzzle(*params, message) + + got := lhtlp.Solve(*puzzle) + + if got.String() != want.String() { + t.Errorf("got %s want %s", got, want) + } + }) + + t.Run("Generate and solve multiple puzzles", func(t *testing.T) { + message1 := 42 + message2 := 24 + message3 := 33 + difficulty := 1 + + want := big.NewInt(int64(message1 + message2 + message3)) + + params := lhtlp.NewParams(Bits, difficulty) + puzzle1 := lhtlp.NewPuzzle(*params, message1) + puzzle2 := lhtlp.NewPuzzle(*params, message2) + puzzle3 := lhtlp.NewPuzzle(*params, message3) + puzzle4 := puzzle1.Add(*puzzle2).Add(*puzzle3) + + got := lhtlp.Solve(*puzzle4) + + if got.String() != want.String() { + t.Errorf("got %s want %s", got, want) + } + }) +} diff --git a/tlp/tlp_test.go b/tlp/tlp_test.go new file mode 100644 index 0000000..a500f2a --- /dev/null +++ b/tlp/tlp_test.go @@ -0,0 +1,25 @@ +package tlp_test + +import ( + "math/big" + "testing" + + "github.com/pmuens/time-lock-puzzle/tlp" +) + +func TestTLP(t *testing.T) { + t.Run("Generate and solve", func(t *testing.T) { + bits := 1024 + message := 42 + difficulty := 1 + + want := big.NewInt(int64(message)) + + puzzle := tlp.Generate(bits, message, difficulty) + got := tlp.Solve(puzzle) + + if got.String() != want.String() { + t.Errorf("got %s want %s", got, want) + } + }) +}