🔥 Tremora del Terra: ultimate hmac-file-server fix – final push before the drop 💾🔐
This commit is contained in:
BIN
github_tests/hmac_icon.png
Normal file
BIN
github_tests/hmac_icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 60 KiB |
80
github_tests/hmac_test.go
Normal file
80
github_tests/hmac_test.go
Normal file
@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath" // Added this import for filepath usage
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
serverURL = "http://[::1]:8080" // Replace with your actual server URL
|
||||
secret = "hmac-file-server-is-the-win" // Replace with your HMAC secret key
|
||||
uploadPath = "hmac_icon.png" // Test file to upload
|
||||
protocolType = "v2" // Use v2, v, or token as needed
|
||||
)
|
||||
|
||||
// TestUpload performs a basic HMAC validation and upload test.
|
||||
func TestUpload(t *testing.T) {
|
||||
// File setup for testing
|
||||
file, err := os.Open(uploadPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Error opening file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
fileInfo, _ := file.Stat()
|
||||
fileStorePath := uploadPath
|
||||
contentLength := fileInfo.Size()
|
||||
|
||||
// Generate HMAC based on protocol type
|
||||
hmacValue := generateHMAC(fileStorePath, contentLength, protocolType)
|
||||
|
||||
// Formulate request URL with HMAC in query params
|
||||
reqURL := fmt.Sprintf("%s/%s?%s=%s", serverURL, fileStorePath, protocolType, url.QueryEscape(hmacValue))
|
||||
|
||||
// Prepare HTTP PUT request with file data
|
||||
req, err := http.NewRequest(http.MethodPut, reqURL, file)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating request: %v", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/octet-stream")
|
||||
req.Header.Set("Content-Length", strconv.FormatInt(contentLength, 10))
|
||||
|
||||
// Execute HTTP request
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Error executing request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
t.Logf("Response status: %s", resp.Status)
|
||||
}
|
||||
|
||||
// Generates the HMAC based on your protocol version
|
||||
func generateHMAC(filePath string, contentLength int64, protocol string) string {
|
||||
mac := hmac.New(sha256.New, []byte(secret))
|
||||
macString := ""
|
||||
|
||||
// Calculate HMAC according to protocol
|
||||
if protocol == "v" {
|
||||
mac.Write([]byte(filePath + "\x20" + strconv.FormatInt(contentLength, 10)))
|
||||
macString = hex.EncodeToString(mac.Sum(nil))
|
||||
} else if protocol == "v2" || protocol == "token" {
|
||||
contentType := mime.TypeByExtension(filepath.Ext(filePath))
|
||||
if contentType == "" {
|
||||
contentType = "application/octet-stream"
|
||||
}
|
||||
mac.Write([]byte(filePath + "\x00" + strconv.FormatInt(contentLength, 10) + "\x00" + contentType))
|
||||
macString = hex.EncodeToString(mac.Sum(nil))
|
||||
}
|
||||
|
||||
return macString
|
||||
}
|
39
github_tests/server_flags_test.go
Normal file
39
github_tests/server_flags_test.go
Normal file
@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestGenConfigFlag runs the server with --genconfig and checks output for expected config keys
|
||||
func TestGenConfigFlag(t *testing.T) {
|
||||
cmd := exec.Command("go", "run", "../cmd/server/main.go", "--genconfig")
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil && !strings.Contains(string(output), "[server]") {
|
||||
t.Fatalf("Failed to run with --genconfig: %v\nOutput: %s", err, output)
|
||||
}
|
||||
if !strings.Contains(string(output), "[server]") || !strings.Contains(string(output), "bind_ip") {
|
||||
t.Errorf("Example config missing expected keys. Output: %s", output)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIPv4IPv6Flag runs the server with forceprotocol=ipv4 and ipv6 and checks for startup errors
|
||||
func TestIPv4IPv6Flag(t *testing.T) {
|
||||
for _, proto := range []string{"ipv4", "ipv6", "auto"} {
|
||||
cmd := exec.Command("go", "run", "../cmd/server/main.go", "--config", "../cmd/server/config.toml")
|
||||
cmd.Env = append(os.Environ(), "FORCEPROTOCOL="+proto)
|
||||
// Set Go module cache environment variables if not already set
|
||||
if os.Getenv("GOMODCACHE") == "" {
|
||||
cmd.Env = append(cmd.Env, "GOMODCACHE="+os.Getenv("HOME")+"/go/pkg/mod")
|
||||
}
|
||||
if os.Getenv("GOPATH") == "" {
|
||||
cmd.Env = append(cmd.Env, "GOPATH="+os.Getenv("HOME")+"/go")
|
||||
}
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil && !strings.Contains(string(output), "Configuration loaded successfully") {
|
||||
t.Errorf("Server failed to start with forceprotocol=%s: %v\nOutput: %s", proto, err, output)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user