first commit
Some checks failed
CI/CD Pipeline / Test (push) Failing after 22m19s
CI/CD Pipeline / Security Scan (push) Failing after 5m57s
CI/CD Pipeline / Build (amd64, darwin) (push) Has been skipped
CI/CD Pipeline / Build (amd64, linux) (push) Has been skipped
CI/CD Pipeline / Build (amd64, windows) (push) Has been skipped
CI/CD Pipeline / Build (arm64, darwin) (push) Has been skipped
CI/CD Pipeline / Build (arm64, linux) (push) Has been skipped
CI/CD Pipeline / Build Docker Image (push) Has been skipped
CI/CD Pipeline / Create Release (push) Has been skipped

This commit is contained in:
Rogee
2025-09-28 10:05:07 +08:00
commit 7fcabe0225
481 changed files with 125127 additions and 0 deletions

223
tests/e2e/app_smoke_test.go Normal file
View File

@@ -0,0 +1,223 @@
package e2e
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/gofiber/fiber/v2"
"github.com/subconverter-go/internal/service"
)
type roundTripFunc func(*http.Request) (*http.Response, error)
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}
func newSubscriptionMockClient() *http.Client {
var counter int32
return &http.Client{
Timeout: 5 * time.Second,
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
atomic.AddInt32(&counter, 1)
body := subscriptionFixture(req.URL.String())
status := http.StatusOK
if body == "" {
status = http.StatusNotFound
}
resp := &http.Response{
StatusCode: status,
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader(body)),
}
resp.Header.Set("Content-Type", "text/plain; charset=utf-8")
return resp, nil
}),
}
}
func subscriptionFixture(u string) string {
switch {
case strings.Contains(u, "mock-subscribe.example.com"):
return "ss://aes-256-cfb:password@192.168.1.1:8388\nvmess://eyJhZGQiOiIxOTIuMTY4LjEuMiIsICJwcyI6IlRlc3QgVk1lc3MiLCAicG9ydCI6NDQzLCAiaWQiOiI2YjZkZTQ3Zi1kZjQ1LTQ1M2ItODI1MS1hZjM0ZTM0ODc1Y2UifQ==\ntrojan://password@192.168.1.3:443"
case strings.Contains(u, "example.com/upload"):
return "uploaded"
case strings.Contains(u, "ss-subscribe"):
return "ss://aes-256-cfb:password@192.168.1.1:8388"
case strings.Contains(u, "vmess-subscribe"):
return "vmess://eyJhZGQiOiIxOTIuMTY4LjEuMiIsICJwcyI6IlRlc3QgVk1lc3MiLCAicG9ydCI6NDQzLCAiaWQiOiI2YjZkZTQ3Zi1kZjQ1LTQ1M2ItODI1MS1hZjM0ZTM0ODc1Y2UifQ=="
case strings.Contains(u, "multi-source.example.com/first"):
return "ss://aes-256-gcm:password@198.51.100.20:8443#multi-ss"
case strings.Contains(u, "multi-source.example.com/second"):
return "vmess://eyJhZGQiOiJtdWx0aS5leGFtcGxlLmNvbSIsICJhaWQiOiIwIiwgImhvc3QiOiJtdWx0aS5leGFtcGxlLmNvbSIsICJpZCI6IjEyMzQ1Njc4LTEyMzQtMTIzNC0xMjM0LTEyMzQ1Njc4OWFiYyIsICJuZXQiOiJ0Y3AiLCAicGF0aCI6Ii8iLCAicHMiOiJtdWx0aS12bWVzcyIsICJ0bHMiOiJ0bHMiLCAidHlwZSI6Im5vbmUiLCAidXJsX3Rlc3QiOiJodHRwOi8vd3d3Lmdvb2dsZS5jb20vZ2VuZXJhdGVfMjA0IiwgInYiOiIyIiwgInBvcnQiOiI0NDMiLCAic2N5IjoiYXV0byJ9"
default:
return ""
}
}
func TestApplicationBaselineRoutes(t *testing.T) {
app, _, cleanup := setupTestApplication(t)
t.Cleanup(cleanup)
server := app.GetHTTPServer()
t.Run("health endpoint", func(t *testing.T) {
resp := doRequest(t, server, http.MethodGet, "/health")
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected status 200, got %d", resp.StatusCode)
}
var body map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
t.Fatalf("failed to decode body: %v", err)
}
status, ok := body["status"].(string)
if !ok || status == "" {
t.Fatalf("expected status field in response, got: %v", body)
}
})
t.Run("version endpoint", func(t *testing.T) {
resp := doRequest(t, server, http.MethodGet, "/version")
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected status 200, got %d", resp.StatusCode)
}
var body map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
t.Fatalf("failed to decode body: %v", err)
}
serviceName, ok := body["service"].(string)
if !ok || serviceName == "" {
t.Fatalf("expected service field in response, got: %v", body)
}
})
t.Run("subscription endpoint", func(t *testing.T) {
resp := doRequest(t, server, http.MethodGet, "/sub?target=clash&url=https://mock-subscribe.example.com")
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected status 200, got %d", resp.StatusCode)
}
contentType := resp.Header.Get("Content-Type")
if contentType == "" {
t.Fatalf("expected content type header, got empty")
}
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read body: %v", err)
}
if len(data) == 0 {
t.Fatalf("expected non-empty body from /sub endpoint")
}
})
}
func setupTestApplication(t *testing.T) (*service.Application, string, func()) {
t.Helper()
defaultConfig := `server:
host: "127.0.0.1"
port: 26500
logging:
level: "error"
format: "text"
`
return setupTestApplicationWithConfig(t, defaultConfig)
}
func setupTestApplicationWithConfig(t *testing.T, configYAML string) (*service.Application, string, func()) {
t.Helper()
originalWD, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory: %v", err)
}
projectRoot, err := findProjectRoot(originalWD)
if err != nil {
t.Fatalf("failed to locate project root: %v", err)
}
if err := os.Chdir(projectRoot); err != nil {
t.Fatalf("failed to change directory to project root: %v", err)
}
t.Cleanup(func() {
_ = os.Chdir(originalWD)
})
configDir := t.TempDir()
configPath := filepath.Join(configDir, "config.yaml")
if err := os.WriteFile(configPath, []byte(configYAML), 0o644); err != nil {
t.Fatalf("failed to write temp config: %v", err)
}
app, err := service.NewApplication(configPath)
if err != nil {
t.Fatalf("failed to create application: %v", err)
}
if engine := app.GetConversionEngine(); engine != nil {
engine.SetHTTPClient(newSubscriptionMockClient())
}
cleanup := func() {
_ = app.Stop()
_ = os.Remove(configPath)
}
return app, configPath, cleanup
}
func doRequest(t *testing.T, app *fiber.App, method, target string) *http.Response {
t.Helper()
return doRequestWithBody(t, app, method, target, nil, nil)
}
func doRequestWithBody(t *testing.T, app *fiber.App, method, target string, body io.Reader, headers map[string]string) *http.Response {
t.Helper()
req := httptest.NewRequest(method, target, body)
for k, v := range headers {
req.Header.Set(k, v)
}
resp, err := app.Test(req, -1)
if err != nil {
t.Fatalf("fiber test request failed: %v", err)
}
return resp
}
func findProjectRoot(start string) (string, error) {
dir := start
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir, nil
}
parent := filepath.Dir(dir)
if parent == dir {
return "", fmt.Errorf("go.mod not found from %s", start)
}
dir = parent
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,283 @@
package e2e
import (
"fmt"
"io"
"net/http"
"os"
"strings"
"testing"
)
func TestMaintenanceEndpointsRequireToken(t *testing.T) {
config := `server:
host: "127.0.0.1"
port: 26550
logging:
level: "error"
format: "text"
security:
access_tokens:
- "legacy-token"
conversion:
cache_timeout: 60
`
app, _, cleanup := setupTestApplicationWithConfig(t, config)
t.Cleanup(cleanup)
server := app.GetHTTPServer()
token := "legacy-token"
tests := []struct {
name string
method string
path string
authorizedPath string
expectedBody string
}{
{
name: "RefreshRules",
method: http.MethodGet,
path: "/refreshrules",
authorizedPath: fmt.Sprintf("/refreshrules?token=%s", token),
expectedBody: "done\n",
},
{
name: "ReadConf",
method: http.MethodGet,
path: "/readconf",
authorizedPath: fmt.Sprintf("/readconf?token=%s", token),
expectedBody: "done\n",
},
{
name: "FlushCache",
method: http.MethodGet,
path: "/flushcache",
authorizedPath: fmt.Sprintf("/flushcache?token=%s", token),
expectedBody: "done",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name+"/Unauthorized", func(t *testing.T) {
resp := doRequest(t, server, tt.method, tt.path)
defer resp.Body.Close()
if resp.StatusCode != http.StatusForbidden {
t.Fatalf("expected 403 for %s, got %d", tt.path, resp.StatusCode)
}
})
t.Run(tt.name+"/Authorized", func(t *testing.T) {
resp := doRequest(t, server, tt.method, tt.authorizedPath)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected 200 for %s, got %d", tt.authorizedPath, resp.StatusCode)
}
if ct := resp.Header.Get("Content-Type"); !strings.HasPrefix(ct, "text/plain") {
t.Fatalf("expected text/plain content type, got %q", ct)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read response body: %v", err)
}
if string(body) != tt.expectedBody {
t.Fatalf("unexpected body for %s: got %q, want %q", tt.authorizedPath, string(body), tt.expectedBody)
}
})
}
}
func TestUpdateConfRewritesConfiguration(t *testing.T) {
initialConfig := `server:
host: "127.0.0.1"
port: 26560
logging:
level: "error"
format: "text"
security:
access_tokens:
- "legacy-token"
`
app, configPath, cleanup := setupTestApplicationWithConfig(t, initialConfig)
t.Cleanup(cleanup)
server := app.GetHTTPServer()
token := "legacy-token"
updatedConfig := `server:
host: "127.0.0.1"
port: 28600
logging:
level: "warning"
format: "text"
security:
access_tokens:
- "legacy-token"
conversion:
default_target: "surge"
`
unauthorized := doRequestWithBody(t, server, http.MethodPost, "/updateconf?type=form", strings.NewReader(updatedConfig), map[string]string{"Content-Type": "text/plain"})
defer unauthorized.Body.Close()
if unauthorized.StatusCode != http.StatusForbidden {
t.Fatalf("expected 403 when missing token, got %d", unauthorized.StatusCode)
}
resp := doRequestWithBody(t, server, http.MethodPost, fmt.Sprintf("/updateconf?type=form&token=%s", token), strings.NewReader(updatedConfig), map[string]string{"Content-Type": "text/plain"})
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected 200 for authorized updateconf, got %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read body: %v", err)
}
if string(body) != "done\n" {
t.Fatalf("unexpected response body: got %q", string(body))
}
content, err := os.ReadFile(configPath)
if err != nil {
t.Fatalf("failed to read config file: %v", err)
}
if string(content) != updatedConfig {
t.Fatalf("config file not updated, got %q, want %q", string(content), updatedConfig)
}
serverConfig := app.GetConfig().GetServerConfig()
if serverConfig.Port != 28600 {
t.Fatalf("expected server port to be 28600 after update, got %d", serverConfig.Port)
}
if app.GetConfig().GetConfig().Conversion.DefaultTarget != "surge" {
t.Fatalf("expected default target to update to surge")
}
}
func TestReadConfReloadsConfiguration(t *testing.T) {
baseConfig := `server:
host: "127.0.0.1"
port: 26570
logging:
level: "error"
format: "text"
security:
access_tokens:
- "legacy-token"
`
app, configPath, cleanup := setupTestApplicationWithConfig(t, baseConfig)
t.Cleanup(cleanup)
server := app.GetHTTPServer()
token := "legacy-token"
replacement := `server:
host: "127.0.0.1"
port: 29999
logging:
level: "debug"
format: "text"
security:
access_tokens:
- "legacy-token"
`
if err := os.WriteFile(configPath, []byte(replacement), 0o644); err != nil {
t.Fatalf("failed to overwrite config: %v", err)
}
resp := doRequest(t, server, http.MethodGet, fmt.Sprintf("/readconf?token=%s", token))
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected 200 from readconf, got %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read body: %v", err)
}
if string(body) != "done\n" {
t.Fatalf("unexpected readconf body: got %q", string(body))
}
if app.GetConfig().GetServerConfig().Port != 29999 {
t.Fatalf("expected server port reload to 29999")
}
if app.GetConfig().GetConfig().Logging.Level != "debug" {
t.Fatalf("expected logging level to reload to debug")
}
}
func TestFlushCacheClearsCache(t *testing.T) {
config := `server:
host: "127.0.0.1"
port: 26580
logging:
level: "error"
format: "text"
security:
access_tokens:
- "legacy-token"
conversion:
cache_timeout: 60
`
app, _, cleanup := setupTestApplicationWithConfig(t, config)
t.Cleanup(cleanup)
server := app.GetHTTPServer()
token := "legacy-token"
unauthorized := doRequest(t, server, http.MethodGet, "/flushcache")
defer unauthorized.Body.Close()
if unauthorized.StatusCode != http.StatusForbidden {
t.Fatalf("expected 403 for unauthorized flushcache, got %d", unauthorized.StatusCode)
}
resp := doRequest(t, server, http.MethodGet, fmt.Sprintf("/api/convert?target=clash&url=https://mock-subscribe.example.com&token=%s", token))
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected 200 from conversion request, got %d", resp.StatusCode)
}
stats := app.GetCacheStats()
if stats.TotalSize == 0 && stats.Size == 0 {
t.Fatalf("expected cache to record entries after conversion request")
}
flush := doRequest(t, server, http.MethodGet, fmt.Sprintf("/flushcache?token=%s", token))
defer flush.Body.Close()
if flush.StatusCode != http.StatusOK {
t.Fatalf("expected 200 from flushcache, got %d", flush.StatusCode)
}
body, err := io.ReadAll(flush.Body)
if err != nil {
t.Fatalf("failed to read flush body: %v", err)
}
if string(body) != "done" {
t.Fatalf("unexpected flush body: got %q", string(body))
}
if app.GetCacheStats().Size != 0 {
t.Fatalf("expected cache size to reset to 0 after flush")
}
}

View File

@@ -0,0 +1,147 @@
package e2e
import (
"encoding/base64"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
)
func TestExtendedHealthEndpoints(t *testing.T) {
config := `server:
host: "127.0.0.1"
port: 26660
logging:
level: "error"
format: "text"
`
app, _, cleanup := setupTestApplicationWithConfig(t, config)
t.Cleanup(cleanup)
server := app.GetHTTPServer()
endpoints := []string{"/ready", "/live", "/metrics", "/ping"}
for _, ep := range endpoints {
ep := ep
t.Run(ep, func(t *testing.T) {
resp := doRequest(t, server, http.MethodGet, ep)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected 200 for %s, got %d", ep, resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read %s response: %v", ep, err)
}
if len(body) == 0 {
t.Fatalf("expected non-empty body for %s", ep)
}
})
}
}
func TestVersionPlainTextMode(t *testing.T) {
config := `server:
host: "127.0.0.1"
port: 26670
logging:
level: "error"
format: "text"
`
app, _, cleanup := setupTestApplicationWithConfig(t, config)
t.Cleanup(cleanup)
server := app.GetHTTPServer()
resp := doRequest(t, server, http.MethodGet, "/version?format=plain")
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected 200 for plain version, got %d", resp.StatusCode)
}
if ct := resp.Header.Get("Content-Type"); ct != "text/plain; charset=utf-8" {
t.Fatalf("expected text/plain content type, got %q", ct)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read version body: %v", err)
}
if len(body) == 0 || body[len(body)-1] != '\n' {
t.Fatalf("expected newline terminated version string, got %q", string(body))
}
}
func TestCompatibilityRouteSmoke(t *testing.T) {
config := `server:
host: "127.0.0.1"
port: 26680
logging:
level: "error"
format: "text"
security:
access_tokens:
- "legacy-token"
conversion:
cache_timeout: 60
`
app, _, cleanup := setupTestApplicationWithConfig(t, config)
t.Cleanup(cleanup)
server := app.GetHTTPServer()
token := "legacy-token"
type routeExpect struct {
method string
path string
status int
}
localFile := filepath.Join(t.TempDir(), "sample.txt")
if err := os.WriteFile(localFile, []byte("content"), 0o644); err != nil {
t.Fatalf("failed to write local file: %v", err)
}
rulesetEncoded := base64.StdEncoding.EncodeToString([]byte("ruleset," + filepath.ToSlash(filepath.Join("rules", "LocalAreaNetwork.list"))))
remoteServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
}))
defer remoteServer.Close()
routes := []routeExpect{
{http.MethodGet, "/sub?target=clash&url=https://mock-subscribe.example.com", http.StatusOK},
{http.MethodGet, "/sub2clashr?sublink=https://mock-subscribe.example.com", http.StatusOK},
{http.MethodGet, "/surge2clash?link=https://mock-subscribe.example.com", http.StatusOK},
{http.MethodGet, "/getruleset?type=1&url=" + url.QueryEscape(rulesetEncoded), http.StatusOK},
{http.MethodGet, "/getprofile?name=" + url.QueryEscape(localFile) + "&token=" + token, http.StatusForbidden},
{http.MethodGet, "/refreshrules?token=" + token, http.StatusOK},
{http.MethodGet, "/readconf?token=" + token, http.StatusOK},
{http.MethodPost, "/updateconf?type=form&token=" + token, http.StatusBadRequest},
{http.MethodGet, "/flushcache?token=" + token, http.StatusOK},
{http.MethodGet, "/get?url=" + url.QueryEscape(remoteServer.URL), http.StatusOK},
{http.MethodGet, "/getlocal?path=" + url.QueryEscape(localFile), http.StatusOK},
}
for _, rt := range routes {
rt := rt
t.Run(rt.path, func(t *testing.T) {
var resp *http.Response
if rt.method == http.MethodPost {
resp = doRequestWithBody(t, server, rt.method, rt.path, strings.NewReader(""), map[string]string{"Content-Type": "text/plain"})
} else {
resp = doRequest(t, server, rt.method, rt.path)
}
defer resp.Body.Close()
if resp.StatusCode != rt.status {
t.Fatalf("expected status %d for %s %s, got %d", rt.status, rt.method, rt.path, resp.StatusCode)
}
})
}
}