Files
mev-beta/orig/pkg/security/safe_conversions_test.go
Administrator 803de231ba feat: create v2-prep branch with comprehensive planning
Restructured project for V2 refactor:

**Structure Changes:**
- Moved all V1 code to orig/ folder (preserved with git mv)
- Created docs/planning/ directory
- Added orig/README_V1.md explaining V1 preservation

**Planning Documents:**
- 00_V2_MASTER_PLAN.md: Complete architecture overview
  - Executive summary of critical V1 issues
  - High-level component architecture diagrams
  - 5-phase implementation roadmap
  - Success metrics and risk mitigation

- 07_TASK_BREAKDOWN.md: Atomic task breakdown
  - 99+ hours of detailed tasks
  - Every task < 2 hours (atomic)
  - Clear dependencies and success criteria
  - Organized by implementation phase

**V2 Key Improvements:**
- Per-exchange parsers (factory pattern)
- Multi-layer strict validation
- Multi-index pool cache
- Background validation pipeline
- Comprehensive observability

**Critical Issues Addressed:**
- Zero address tokens (strict validation + cache enrichment)
- Parsing accuracy (protocol-specific parsers)
- No audit trail (background validation channel)
- Inefficient lookups (multi-index cache)
- Stats disconnection (event-driven metrics)

Next Steps:
1. Review planning documents
2. Begin Phase 1: Foundation (P1-001 through P1-010)
3. Implement parsers in Phase 2
4. Build cache system in Phase 3
5. Add validation pipeline in Phase 4
6. Migrate and test in Phase 5

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-10 10:14:26 +01:00

344 lines
7.0 KiB
Go

package security
import (
"math"
"testing"
)
func TestSafeUint64ToUint32(t *testing.T) {
tests := []struct {
name string
input uint64
expected uint32
expectError bool
}{
{
name: "zero value",
input: 0,
expected: 0,
expectError: false,
},
{
name: "small positive value",
input: 1000,
expected: 1000,
expectError: false,
},
{
name: "max uint32 value",
input: math.MaxUint32,
expected: math.MaxUint32,
expectError: false,
},
{
name: "overflow value",
input: math.MaxUint32 + 1,
expected: 0,
expectError: true,
},
{
name: "large overflow value",
input: math.MaxUint64,
expected: 0,
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := SafeUint64ToUint32(tt.input)
if tt.expectError {
if err == nil {
t.Errorf("SafeUint64ToUint32(%d) expected error but got none", tt.input)
}
} else {
if err != nil {
t.Errorf("SafeUint64ToUint32(%d) unexpected error: %v", tt.input, err)
}
if result != tt.expected {
t.Errorf("SafeUint64ToUint32(%d) = %d, want %d", tt.input, result, tt.expected)
}
}
})
}
}
func TestSafeUint64ToInt64(t *testing.T) {
tests := []struct {
name string
input uint64
expected int64
expectError bool
}{
{
name: "zero value",
input: 0,
expected: 0,
expectError: false,
},
{
name: "small positive value",
input: 1000,
expected: 1000,
expectError: false,
},
{
name: "max int64 value",
input: math.MaxInt64,
expected: math.MaxInt64,
expectError: false,
},
{
name: "overflow value",
input: math.MaxInt64 + 1,
expected: 0,
expectError: true,
},
{
name: "max uint64 value",
input: math.MaxUint64,
expected: 0,
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := SafeUint64ToInt64(tt.input)
if tt.expectError {
if err == nil {
t.Errorf("SafeUint64ToInt64(%d) expected error but got none", tt.input)
}
} else {
if err != nil {
t.Errorf("SafeUint64ToInt64(%d) unexpected error: %v", tt.input, err)
}
if result != tt.expected {
t.Errorf("SafeUint64ToInt64(%d) = %d, want %d", tt.input, result, tt.expected)
}
}
})
}
}
func TestSafeUint64ToUint32WithDefault(t *testing.T) {
tests := []struct {
name string
input uint64
defaultValue uint32
expected uint32
}{
{
name: "valid value",
input: 1000,
defaultValue: 500,
expected: 1000,
},
{
name: "overflow uses default",
input: math.MaxUint32 + 1,
defaultValue: 500,
expected: 500,
},
{
name: "max valid value",
input: math.MaxUint32,
defaultValue: 500,
expected: math.MaxUint32,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := SafeUint64ToUint32WithDefault(tt.input, tt.defaultValue)
if result != tt.expected {
t.Errorf("SafeUint64ToUint32WithDefault(%d, %d) = %d, want %d",
tt.input, tt.defaultValue, result, tt.expected)
}
})
}
}
func TestSafeAddUint64(t *testing.T) {
tests := []struct {
name string
a uint64
b uint64
expected uint64
expectError bool
}{
{
name: "small values",
a: 100,
b: 200,
expected: 300,
expectError: false,
},
{
name: "zero addition",
a: 1000,
b: 0,
expected: 1000,
expectError: false,
},
{
name: "overflow case",
a: math.MaxUint64,
b: 1,
expected: 0,
expectError: true,
},
{
name: "near max valid",
a: math.MaxUint64 - 1,
b: 1,
expected: math.MaxUint64,
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := SafeAddUint64(tt.a, tt.b)
if tt.expectError {
if err == nil {
t.Errorf("SafeAddUint64(%d, %d) expected error but got none", tt.a, tt.b)
}
} else {
if err != nil {
t.Errorf("SafeAddUint64(%d, %d) unexpected error: %v", tt.a, tt.b, err)
}
if result != tt.expected {
t.Errorf("SafeAddUint64(%d, %d) = %d, want %d", tt.a, tt.b, result, tt.expected)
}
}
})
}
}
func TestSafeMultiplyUint64(t *testing.T) {
tests := []struct {
name string
a uint64
b uint64
expected uint64
expectError bool
}{
{
name: "small values",
a: 100,
b: 200,
expected: 20000,
expectError: false,
},
{
name: "zero multiplication",
a: 1000,
b: 0,
expected: 0,
expectError: false,
},
{
name: "one multiplication",
a: 1000,
b: 1,
expected: 1000,
expectError: false,
},
{
name: "overflow case",
a: math.MaxUint64,
b: 2,
expected: 0,
expectError: true,
},
{
name: "large values overflow",
a: math.MaxUint64 / 2,
b: 3,
expected: 0,
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := SafeMultiplyUint64(tt.a, tt.b)
if tt.expectError {
if err == nil {
t.Errorf("SafeMultiplyUint64(%d, %d) expected error but got none", tt.a, tt.b)
}
} else {
if err != nil {
t.Errorf("SafeMultiplyUint64(%d, %d) unexpected error: %v", tt.a, tt.b, err)
}
if result != tt.expected {
t.Errorf("SafeMultiplyUint64(%d, %d) = %d, want %d", tt.a, tt.b, result, tt.expected)
}
}
})
}
}
func TestSafeSubtractUint64(t *testing.T) {
tests := []struct {
name string
a uint64
b uint64
expected uint64
expectError bool
}{
{
name: "normal subtraction",
a: 1000,
b: 200,
expected: 800,
expectError: false,
},
{
name: "zero result",
a: 1000,
b: 1000,
expected: 0,
expectError: false,
},
{
name: "underflow case",
a: 100,
b: 200,
expected: 0,
expectError: true,
},
{
name: "subtract zero",
a: 1000,
b: 0,
expected: 1000,
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := SafeSubtractUint64(tt.a, tt.b)
if tt.expectError {
if err == nil {
t.Errorf("SafeSubtractUint64(%d, %d) expected error but got none", tt.a, tt.b)
}
} else {
if err != nil {
t.Errorf("SafeSubtractUint64(%d, %d) unexpected error: %v", tt.a, tt.b, err)
}
if result != tt.expected {
t.Errorf("SafeSubtractUint64(%d, %d) = %d, want %d", tt.a, tt.b, result, tt.expected)
}
}
})
}
}