Set up testing framework and utilities
This commit is contained in:
parent
2a4060bc8a
commit
47d1280556
|
|
@ -3,10 +3,10 @@
|
||||||
## Phase 1: Proof of Work Package Implementation
|
## Phase 1: Proof of Work Package Implementation
|
||||||
**Goal**: Create standalone, testable PoW package with HMAC-signed stateless challenges
|
**Goal**: Create standalone, testable PoW package with HMAC-signed stateless challenges
|
||||||
|
|
||||||
- [ ] **Project Setup**
|
- [X] **Project Setup**
|
||||||
- [X] Initialize Go module and basic project structure
|
- [X] Initialize Go module and basic project structure
|
||||||
- [X] Create PoW challenge structure and types
|
- [X] Create PoW challenge structure and types
|
||||||
- [ ] Set up testing framework and utilities
|
- [X] Set up testing framework and utilities
|
||||||
|
|
||||||
- [ ] **Challenge Generation & HMAC Security**
|
- [ ] **Challenge Generation & HMAC Security**
|
||||||
- [ ] Implement HMAC-signed challenge generation (stateless)
|
- [ ] Implement HMAC-signed challenge generation (stateless)
|
||||||
|
|
|
||||||
2
go.mod
2
go.mod
|
|
@ -1,3 +1,5 @@
|
||||||
module word-of-wisdom
|
module word-of-wisdom
|
||||||
|
|
||||||
go 1.24.3
|
go 1.24.3
|
||||||
|
|
||||||
|
require github.com/stretchr/testify v1.10.0 // indirect
|
||||||
|
|
|
||||||
2
go.sum
Normal file
2
go.sum
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
63
internal/pow/test_utils.go
Normal file
63
internal/pow/test_utils.go
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
package pow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestConfig returns a configuration suitable for testing
|
||||||
|
func TestConfig() *Config {
|
||||||
|
secret := make([]byte, 32)
|
||||||
|
rand.Read(secret)
|
||||||
|
|
||||||
|
config, err := NewConfig(
|
||||||
|
WithHMACSecret(secret),
|
||||||
|
WithDefaultDifficulty(4),
|
||||||
|
WithRandomBytes(6),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
panic("Failed to create test config: " + err.Error())
|
||||||
|
}
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestChallenge returns a valid challenge for testing
|
||||||
|
func TestChallenge() *Challenge {
|
||||||
|
return &Challenge{
|
||||||
|
Timestamp: time.Now().Unix(),
|
||||||
|
Difficulty: 4,
|
||||||
|
Resource: "quotes",
|
||||||
|
Random: []byte{0xa1, 0xb2, 0xc3, 0xd4, 0xe5, 0xf6},
|
||||||
|
HMAC: nil, // To be filled by generator
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSolution returns a valid solution for testing
|
||||||
|
func TestSolution() *Solution {
|
||||||
|
return &Solution{
|
||||||
|
Challenge: *TestChallenge(),
|
||||||
|
Nonce: 42,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandomHex generates a random hex string of given length
|
||||||
|
func RandomHex(length int) string {
|
||||||
|
bytes := make([]byte, length/2)
|
||||||
|
rand.Read(bytes)
|
||||||
|
return hex.EncodeToString(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExpiredChallenge returns a challenge that has expired
|
||||||
|
func ExpiredChallenge() *Challenge {
|
||||||
|
challenge := TestChallenge()
|
||||||
|
challenge.Timestamp = time.Now().Add(-10 * time.Minute).Unix()
|
||||||
|
return challenge
|
||||||
|
}
|
||||||
|
|
||||||
|
// FutureChallenge returns a challenge with future timestamp
|
||||||
|
func FutureChallenge() *Challenge {
|
||||||
|
challenge := TestChallenge()
|
||||||
|
challenge.Timestamp = time.Now().Add(10 * time.Minute).Unix()
|
||||||
|
return challenge
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue