Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance EUI-64 Calculator with Robust Validation, Logging, and Testing #34

Merged
merged 13 commits into from
Feb 20, 2025

Conversation

nicholas-fedor
Copy link
Owner

This pull request significantly improves the EUI-64 Calculator by refactoring core components, adding comprehensive validation, introducing structured logging, and establishing a robust test suite. The changes enhance reliability, maintainability, and observability while preserving the application's core functionality.

Key Changes:

  1. Server Initialization (cmd/server/main.go)

    • Added Config struct and LoadConfig for port and TRUSTED_PROXIES env parsing (default port ":8080").
    • Introduced SetupRouter for modular Gin setup in release mode with middleware and trusted proxies.
    • Integrated slog for structured error logging, exiting on failures.
  2. Test Suite (cmd/server/main_test.go)

    • Added new test file with setupRouter helper and two test functions:
      • TestRouterSetup: Verifies routing for home, calculate, static files, and 404 responses.
      • TestTrustedProxies: Ensures correct client IP handling with/without trusted proxies and error cases.
    • Uses httptest and testify/assert for robust HTTP testing.
  3. EUI-64 Logic (internal/eui64/eui64.go)

    • Added constants (e.g., macBytes, prefixMaxHextets) for clarity and maintainability.
    • Enhanced error handling for MAC length and prefix validation (≤4 hextets, valid hex).
    • Replaced net.IP with custom []uint16 hextet array and ip6ToString for zero-compressed IPv6 formatting.
    • Improved prefix parsing to support partial inputs (e.g., "2001:db8::").
  4. Handlers (internal/handlers/handlers.go)

    • Added validators for MAC and IPv6 prefix checks with specific error messages.
    • Integrated slog for logging validation and rendering errors.
    • Replaced c.HTML with ui.Render, introduced renderResult helper for consistent error handling.
    • Enhanced Home and Calculate with detailed error logging and HTTP status codes.
  5. Input Validation (internal/validators/)

    • ipv6_prefix_validator.go: New file validating prefixes (≤4 hextets, hex digits, trailing "::" allowed).
    • mac_validator.go: New file ensuring 6-byte MAC addresses via net.ParseMAC.
    • Both provide detailed error messages for malformed inputs.

Impact:

  • Reliability: Stricter input validation and error handling prevent invalid data processing.
  • Observability: Structured logging via slog improves debugging and monitoring.
  • Testability: Comprehensive tests ensure core functionality and edge cases are covered.
  • Maintainability: Modular design and constants enhance code readability and future updates.

Testing:

  • Verified via new test suite in main_test.go, covering routing, proxy handling, and response rendering.
  • Manual testing confirmed correct EUI-64 calculations and error displays in the UI.

The main Dockerfile is specific to production builds via GitHub Actions.
Adding Dockerfile-dev and updating instructions in README.md.
- Added port configuration support
- Implemented slog for structured error and failure handling
- Refactored inline logic into modular functions for improved maintainability
Updated internal/eui64/eui64.go with:
- Added constants for EUI-64 and IPv6 sizes/markers (e.g., macBytes, fffeMarkerLow)
- Enhanced error handling for MAC length and prefix hextet validation
- Replaced net.IP with custom []uint16 hextet array for IPv6 construction
- Introduced ip6ToString for canonical IPv6 formatting with zero compression
- Improved prefix parsing to support partial inputs and trim trailing "::"
- Split logic into modular CalculateEUI64 and ip6ToString functions
Strengthens robustness, flexibility, and maintainability of EUI-64 calculator.
Updated internal/handlers/handlers.go with:
- Integrated validators for MAC and IPv6 prefix with specific error messages
- Added slog for structured logging of validation and rendering errors
- Replaced c.HTML with ui.Render for home and result pages
- Introduced renderResult helper for consistent rendering with error handling
- Enhanced Home handler with error logging and 500 status on failure
- Restructured Calculate to validate inputs and log warnings/errors
Improves input validation, observability, and code maintainability.
Introduced internal/validators/ipv6_prefix_validator.go with:
- Defined constants maxHextets (4) and maxHextetLength (4) for prefix constraints
- Implemented ValidateIPv6Prefix to trim whitespace, validate non-empty input, and check:
  - Maximum 4 hextets with proper hexadecimal digits (0-9, a-f, A-F)
  - No internal empty hextets, allowing trailing "::" for zero compression
  - Hextet length up to 4 characters
- Added isHexDigit helper to verify hexadecimal characters
Provides robust IPv6 prefix validation for EUI-64 calculator.
Introduced internal/validators/mac_validator.go with:
- Defined constant macBytes (6) for standard 48-bit MAC address length
- Implemented ValidateMAC to:
  - Trim whitespace and ensure non-empty input
  - Parse MAC address using net.ParseMAC
  - Verify length is exactly 6 bytes
- Returns detailed errors for parsing failures or incorrect lengths
Ensures valid MAC address input for EUI-64 calculations.
Updated cmd/server/main.go with:
- Added StaticDir to Config, defaulting to "static" at project root
- Enhanced LoadConfig to use os.Executable() for dynamic path resolution and STATIC_DIR env var override
- Modified SetupRouter to use config.StaticDir for static file serving
- Logged static_dir in server startup message
Resolves static file access issues for both runtime and test environments.
Updated cmd/server/main_test.go with:
- Modified setupRouter to set config.StaticDir to "../../static" using filepath.Join
- Ensures static file serving points to root-level static/ during tests
- Added path/filepath import
Maintains test reliability with configurable static directory in main.go.
Copy link

codecov bot commented Feb 20, 2025

Codecov Report

Attention: Patch coverage is 75.58140% with 42 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/server/main.go 45.83% 20 Missing and 6 partials ⚠️
internal/eui64/eui64.go 89.47% 4 Missing and 2 partials ⚠️
internal/handlers/handlers.go 76.00% 6 Missing ⚠️
internal/validators/mac_validator.go 76.92% 2 Missing and 1 partial ⚠️
internal/validators/ipv6_prefix_validator.go 96.55% 1 Missing ⚠️
Files with missing lines Coverage Δ
internal/validators/ipv6_prefix_validator.go 96.55% <96.55%> (ø)
internal/validators/mac_validator.go 76.92% <76.92%> (ø)
internal/eui64/eui64.go 91.39% <89.47%> (-1.29%) ⬇️
internal/handlers/handlers.go 67.56% <76.00%> (+10.42%) ⬆️
cmd/server/main.go 48.14% <45.83%> (+48.14%) ⬆️

@nicholas-fedor nicholas-fedor merged commit 16f41d6 into main Feb 20, 2025
4 checks passed
@nicholas-fedor nicholas-fedor deleted the dev branch February 20, 2025 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant