Add integration tests and service management for SteamCache

- Introduced integration tests for SteamCache to validate caching behavior with real Steam URLs.
- Implemented a ServiceManager to manage service configurations, allowing for dynamic detection of services based on User-Agent.
- Updated cache key generation to include service prefixes, enhancing cache organization and retrieval.
- Enhanced the caching logic to support multiple services, starting with Steam and Epic Games.
- Improved .gitignore to exclude test cache files while retaining necessary structure.
This commit is contained in:
2025-09-21 20:07:18 -05:00
parent cc3497bc3a
commit 694c223b00
6 changed files with 1368 additions and 102 deletions

4
.gitignore vendored
View File

@@ -9,3 +9,7 @@
#windows executables
*.exe
#test cache
/steamcache/test_cache/*
!/steamcache/test_cache/.gitkeep

View File

@@ -0,0 +1,279 @@
package steamcache
import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
)
const SteamHostname = "cache2-den-iwst.steamcontent.com"
func TestSteamIntegration(t *testing.T) {
// Skip this test if we don't have internet access or want to avoid hitting Steam servers
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Test URLs from real Steam usage - these should be cached when requested by Steam clients
testURLs := []string{
"/depot/516751/patch/288061881745926019/4378193572994177373",
"/depot/516751/chunk/42e7c13eb4b4e426ec5cf6d1010abfd528e5065a",
"/depot/516751/chunk/f949f71e102d77ed6e364e2054d06429d54bebb1",
"/depot/516751/chunk/6790f5105833556d37797657be72c1c8dd2e7074",
}
for _, testURL := range testURLs {
t.Run(fmt.Sprintf("URL_%s", testURL), func(t *testing.T) {
testSteamURL(t, testURL)
})
}
}
func testSteamURL(t *testing.T, urlPath string) {
// Create a unique temporary directory for this test to avoid cache persistence issues
tempDir, err := os.MkdirTemp("", "steamcache_test_*")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempDir) // Clean up after test
// Create SteamCache instance with unique temp directory
sc := New(":0", "100MB", "1GB", tempDir, "", "LRU", "LRU", 10, 5)
// Use real Steam server
steamURL := "https://" + SteamHostname + urlPath
// Test direct download from Steam server
directResp, directBody := downloadDirectly(t, steamURL)
// Test download through SteamCache
cacheResp, cacheBody := downloadThroughCache(t, sc, steamURL, urlPath)
// Compare responses
compareResponses(t, directResp, directBody, cacheResp, cacheBody, urlPath)
}
func downloadDirectly(t *testing.T, url string) (*http.Response, []byte) {
client := &http.Client{Timeout: 30 * time.Second}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
// Add Steam user agent
req.Header.Set("User-Agent", "Valve/Steam HTTP Client 1.0")
resp, err := client.Do(req)
if err != nil {
t.Fatalf("Failed to download directly from Steam: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read direct response body: %v", err)
}
return resp, body
}
func downloadThroughCache(t *testing.T, sc *SteamCache, upstreamURL, urlPath string) (*http.Response, []byte) {
// Create a test server for SteamCache
cacheServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// For real Steam URLs, we need to set the upstream to the Steam hostname
// and let SteamCache handle the full URL construction
sc.upstream = "https://" + SteamHostname
sc.ServeHTTP(w, r)
}))
defer cacheServer.Close()
// First request - should be a MISS and cache the file
client := &http.Client{Timeout: 30 * time.Second}
req1, err := http.NewRequest("GET", cacheServer.URL+urlPath, nil)
if err != nil {
t.Fatalf("Failed to create first request: %v", err)
}
req1.Header.Set("User-Agent", "Valve/Steam HTTP Client 1.0")
resp1, err := client.Do(req1)
if err != nil {
t.Fatalf("Failed to download through cache (first request): %v", err)
}
defer resp1.Body.Close()
body1, err := io.ReadAll(resp1.Body)
if err != nil {
t.Fatalf("Failed to read cache response body (first request): %v", err)
}
// Verify first request was a MISS
if resp1.Header.Get("X-LanCache-Status") != "MISS" {
t.Errorf("Expected first request to be MISS, got %s", resp1.Header.Get("X-LanCache-Status"))
}
// Second request - should be a HIT from cache
req2, err := http.NewRequest("GET", cacheServer.URL+urlPath, nil)
if err != nil {
t.Fatalf("Failed to create second request: %v", err)
}
req2.Header.Set("User-Agent", "Valve/Steam HTTP Client 1.0")
resp2, err := client.Do(req2)
if err != nil {
t.Fatalf("Failed to download through cache (second request): %v", err)
}
defer resp2.Body.Close()
body2, err := io.ReadAll(resp2.Body)
if err != nil {
t.Fatalf("Failed to read cache response body (second request): %v", err)
}
// Verify second request was a HIT (unless hash verification failed)
status2 := resp2.Header.Get("X-LanCache-Status")
if status2 != "HIT" && status2 != "MISS" {
t.Errorf("Expected second request to be HIT or MISS, got %s", status2)
}
// If it's a MISS, it means hash verification failed and content wasn't cached
// This is correct behavior - we shouldn't cache content that doesn't match the expected hash
if status2 == "MISS" {
t.Logf("Second request was MISS (hash verification failed) - this is correct behavior")
}
// Verify both cache responses are identical
if !bytes.Equal(body1, body2) {
t.Error("First and second cache responses should be identical")
}
// Return the second response (from cache)
return resp2, body2
}
func compareResponses(t *testing.T, directResp *http.Response, directBody []byte, cacheResp *http.Response, cacheBody []byte, urlPath string) {
// Compare status codes
if directResp.StatusCode != cacheResp.StatusCode {
t.Errorf("Status code mismatch: direct=%d, cache=%d", directResp.StatusCode, cacheResp.StatusCode)
}
// Compare response bodies (this is the most important test)
if !bytes.Equal(directBody, cacheBody) {
t.Errorf("Response body mismatch for URL %s", urlPath)
t.Errorf("Direct body length: %d, Cache body length: %d", len(directBody), len(cacheBody))
// Find first difference
minLen := len(directBody)
if len(cacheBody) < minLen {
minLen = len(cacheBody)
}
for i := 0; i < minLen; i++ {
if directBody[i] != cacheBody[i] {
t.Errorf("First difference at byte %d: direct=0x%02x, cache=0x%02x", i, directBody[i], cacheBody[i])
break
}
}
}
// Compare important headers (excluding cache-specific ones)
importantHeaders := []string{
"Content-Type",
"Content-Length",
"X-Sha1",
"Cache-Control",
}
for _, header := range importantHeaders {
directValue := directResp.Header.Get(header)
cacheValue := cacheResp.Header.Get(header)
if directValue != cacheValue {
t.Errorf("Header %s mismatch: direct=%s, cache=%s", header, directValue, cacheValue)
}
}
// Verify cache-specific headers are present
if cacheResp.Header.Get("X-LanCache-Status") == "" {
t.Error("Cache response should have X-LanCache-Status header")
}
if cacheResp.Header.Get("X-LanCache-Processed-By") != "SteamCache2" {
t.Error("Cache response should have X-LanCache-Processed-By header set to SteamCache2")
}
t.Logf("✅ URL %s: Direct and cache responses are identical", urlPath)
}
// TestCacheFileFormat tests the cache file format directly
func TestCacheFileFormat(t *testing.T) {
// Create test data
bodyData := []byte("test steam content")
contentHash := calculateSHA256(bodyData)
// Create mock response
resp := &http.Response{
StatusCode: 200,
Status: "200 OK",
Header: make(http.Header),
Body: http.NoBody,
}
resp.Header.Set("Content-Type", "application/x-steam-chunk")
resp.Header.Set("Content-Length", "18")
resp.Header.Set("X-Sha1", contentHash)
// Create SteamCache instance
sc := &SteamCache{}
// Reconstruct raw response
rawResponse := sc.reconstructRawResponse(resp, bodyData)
// Serialize to cache format
cacheData, err := serializeRawResponse("/test/format", rawResponse, contentHash, "sha256")
if err != nil {
t.Fatalf("Failed to serialize cache file: %v", err)
}
// Deserialize from cache format
cacheFile, err := deserializeCacheFile(cacheData)
if err != nil {
t.Fatalf("Failed to deserialize cache file: %v", err)
}
// Verify cache file structure
if cacheFile.ContentHash != contentHash {
t.Errorf("ContentHash mismatch: expected %s, got %s", contentHash, cacheFile.ContentHash)
}
if cacheFile.ResponseSize != int64(len(rawResponse)) {
t.Errorf("ResponseSize mismatch: expected %d, got %d", len(rawResponse), cacheFile.ResponseSize)
}
// Verify raw response is preserved
if !bytes.Equal(cacheFile.Response, rawResponse) {
t.Error("Raw response not preserved in cache file")
}
// Test streaming the cached response
recorder := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/test/format", nil)
sc.streamCachedResponse(recorder, req, cacheFile, "test-key", "127.0.0.1", time.Now())
// Verify streamed response
if recorder.Code != 200 {
t.Errorf("Expected status code 200, got %d", recorder.Code)
}
if !bytes.Equal(recorder.Body.Bytes(), bodyData) {
t.Error("Streamed response body does not match original")
}
t.Log("✅ Cache file format test passed")
}

File diff suppressed because it is too large Load Diff

View File

@@ -111,7 +111,8 @@ func TestCacheMissAndHit(t *testing.T) {
}
func TestURLHashing(t *testing.T) {
// Test the new SHA256-based cache key generation
// Test the SHA256-based cache key generation for Steam client requests
// The "steam/" prefix indicates the request came from a Steam client (User-Agent based)
testCases := []struct {
input string
@@ -129,40 +130,188 @@ func TestURLHashing(t *testing.T) {
shouldCache: true,
},
{
input: "/depot/invalid/path",
desc: "invalid depot URL format",
shouldCache: true, // Still gets hashed, just not a proper Steam format
input: "/appinfo/123456",
desc: "app info URL",
shouldCache: true,
},
{
input: "/some/other/path",
desc: "non-Steam URL",
shouldCache: false, // Not cached
desc: "any URL from Steam client",
shouldCache: true, // All URLs from Steam clients (detected via User-Agent) are cached
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
result := generateSteamCacheKey(tc.input)
result := generateServiceCacheKey(tc.input, "steam")
if tc.shouldCache {
// Should return a cache key with "steam/" prefix
if !strings.HasPrefix(result, "steam/") {
t.Errorf("generateSteamCacheKey(%s) = %s, expected steam/ prefix", tc.input, result)
t.Errorf("generateServiceCacheKey(%s, \"steam\") = %s, expected steam/ prefix", tc.input, result)
}
// Should be exactly 70 characters (6 for "steam/" + 64 for SHA256 hex)
if len(result) != 70 {
t.Errorf("generateSteamCacheKey(%s) length = %d, expected 70", tc.input, len(result))
t.Errorf("generateServiceCacheKey(%s, \"steam\") length = %d, expected 70", tc.input, len(result))
}
} else {
// Should return empty string for non-Steam URLs
if result != "" {
t.Errorf("generateSteamCacheKey(%s) = %s, expected empty string", tc.input, result)
t.Errorf("generateServiceCacheKey(%s, \"steam\") = %s, expected empty string", tc.input, result)
}
}
})
}
}
func TestServiceDetection(t *testing.T) {
// Create a service manager for testing
sm := NewServiceManager()
testCases := []struct {
userAgent string
expectedName string
expectedFound bool
desc string
}{
{
userAgent: "Valve/Steam HTTP Client 1.0",
expectedName: "steam",
expectedFound: true,
desc: "Valve Steam HTTP Client",
},
{
userAgent: "Steam",
expectedName: "steam",
expectedFound: true,
desc: "Simple Steam user agent",
},
{
userAgent: "SteamClient/1.0",
expectedName: "steam",
expectedFound: true,
desc: "SteamClient with version",
},
{
userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
expectedName: "",
expectedFound: false,
desc: "Browser user agent",
},
{
userAgent: "",
expectedName: "",
expectedFound: false,
desc: "Empty user agent",
},
{
userAgent: "curl/7.68.0",
expectedName: "",
expectedFound: false,
desc: "curl user agent",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
service, found := sm.DetectService(tc.userAgent)
if found != tc.expectedFound {
t.Errorf("DetectService(%s) found = %v, expected %v", tc.userAgent, found, tc.expectedFound)
}
if found && service.Name != tc.expectedName {
t.Errorf("DetectService(%s) service name = %s, expected %s", tc.userAgent, service.Name, tc.expectedName)
}
})
}
}
func TestServiceManagerExpandability(t *testing.T) {
// Create a service manager for testing
sm := NewServiceManager()
// Test adding a new service (Epic Games)
epicConfig := &ServiceConfig{
Name: "epic",
Prefix: "epic",
UserAgents: []string{
`EpicGamesLauncher`,
`EpicGames`,
`Epic.*Launcher`,
},
}
err := sm.AddService(epicConfig)
if err != nil {
t.Fatalf("Failed to add Epic service: %v", err)
}
// Test Epic Games detection
epicTestCases := []struct {
userAgent string
expectedName string
expectedFound bool
desc string
}{
{
userAgent: "EpicGamesLauncher/1.0",
expectedName: "epic",
expectedFound: true,
desc: "Epic Games Launcher",
},
{
userAgent: "EpicGames/2.0",
expectedName: "epic",
expectedFound: true,
desc: "Epic Games client",
},
{
userAgent: "Epic Launcher 1.5",
expectedName: "epic",
expectedFound: true,
desc: "Epic Launcher with regex match",
},
{
userAgent: "Steam",
expectedName: "steam",
expectedFound: true,
desc: "Steam should still work",
},
{
userAgent: "Mozilla/5.0",
expectedName: "",
expectedFound: false,
desc: "Browser should not match any service",
},
}
for _, tc := range epicTestCases {
t.Run(tc.desc, func(t *testing.T) {
service, found := sm.DetectService(tc.userAgent)
if found != tc.expectedFound {
t.Errorf("DetectService(%s) found = %v, expected %v", tc.userAgent, found, tc.expectedFound)
}
if found && service.Name != tc.expectedName {
t.Errorf("DetectService(%s) service name = %s, expected %s", tc.userAgent, service.Name, tc.expectedName)
}
})
}
// Test cache key generation for different services
steamKey := generateServiceCacheKey("/depot/123/chunk/abc", "steam")
epicKey := generateServiceCacheKey("/epic/123/chunk/abc", "epic")
if !strings.HasPrefix(steamKey, "steam/") {
t.Errorf("Steam cache key should start with 'steam/', got: %s", steamKey)
}
if !strings.HasPrefix(epicKey, "epic/") {
t.Errorf("Epic cache key should start with 'epic/', got: %s", epicKey)
}
}
// Removed hash calculation tests since we switched to lightweight validation
func TestSteamKeySharding(t *testing.T) {

View File

View File

@@ -184,7 +184,12 @@ func (d *DiskFS) init() {
d.mu.Lock()
// Extract key from sharded path: remove root and convert sharding back
relPath := strings.ReplaceAll(npath[len(d.root)+1:], "\\", "/")
// Handle both "./disk" and "disk" root paths
rootPath := d.root
if strings.HasPrefix(rootPath, "./") {
rootPath = rootPath[2:] // Remove "./" prefix
}
relPath := strings.ReplaceAll(npath[len(rootPath)+1:], "\\", "/")
// Extract the original key from the sharded path
k := d.extractKeyFromPath(relPath)