initial commit
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
package pow
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestKnownChallenge(t *testing.T) {
|
||||
challenge := Challenge{
|
||||
Algorithm: "DeepSeekHashV1",
|
||||
Challenge: "2f90572ad390d758b5e55b3bb74f14722166388023b3b28876d056a358591197",
|
||||
Salt: "2eeb8f3a703002bfca70",
|
||||
Difficulty: 144000,
|
||||
ExpireAt: 1785483643587,
|
||||
}
|
||||
|
||||
answer, err := challenge.Solve()
|
||||
if err != nil {
|
||||
t.Fatalf("Solve(): %v", err)
|
||||
}
|
||||
|
||||
t.Logf("answer=%d", answer)
|
||||
|
||||
/*
|
||||
* Для этого challenge ожидается 61830.
|
||||
*/
|
||||
const expected uint64 = 61830
|
||||
|
||||
if answer != expected {
|
||||
t.Fatalf(
|
||||
"unexpected answer: got %d, want %d",
|
||||
answer,
|
||||
expected,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidChallenge(t *testing.T) {
|
||||
validDigest := "2f90572ad390d758b5e55b3bb74f14722166388023b3b28876d056a358591197"
|
||||
tests := []struct {
|
||||
name string
|
||||
base string
|
||||
challenge string
|
||||
difficulty uint64
|
||||
}{
|
||||
{name: "empty base", challenge: validDigest, difficulty: 1},
|
||||
{name: "short digest", base: "salt_123_", challenge: "not-hex", difficulty: 1},
|
||||
{name: "non-hex digest", base: "salt_123_", challenge: strings.Repeat("z", 64), difficulty: 1},
|
||||
{name: "zero difficulty", base: "salt_123_", challenge: validDigest},
|
||||
{name: "base exceeds one block", base: strings.Repeat("x", 116), challenge: validDigest, difficulty: 1},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := Solve(tt.base, tt.challenge, tt.difficulty)
|
||||
if !errors.Is(err, ErrInvalidArgument) {
|
||||
t.Fatalf("Solve() error = %v, want ErrInvalidArgument", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChallengeValidation(t *testing.T) {
|
||||
validDigest := "2f90572ad390d758b5e55b3bb74f14722166388023b3b28876d056a358591197"
|
||||
tests := []struct {
|
||||
name string
|
||||
challenge Challenge
|
||||
wantMatch bool
|
||||
}{
|
||||
{name: "unsupported algorithm", challenge: Challenge{Algorithm: "other", Challenge: validDigest, Salt: "salt", Difficulty: 1}},
|
||||
{name: "invalid digest", challenge: Challenge{Challenge: "short", Salt: "salt", Difficulty: 1}, wantMatch: true},
|
||||
{name: "empty salt", challenge: Challenge{Challenge: validDigest, Difficulty: 1}, wantMatch: true},
|
||||
{name: "zero difficulty", challenge: Challenge{Challenge: validDigest, Salt: "salt"}, wantMatch: true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := tt.challenge.Solve()
|
||||
if err == nil {
|
||||
t.Fatal("Challenge.Solve() error = nil")
|
||||
}
|
||||
if tt.wantMatch && !errors.Is(err, ErrInvalidArgument) {
|
||||
t.Fatalf("Challenge.Solve() error = %v, want ErrInvalidArgument", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user