58 lines
1.8 KiB
Go
58 lines
1.8 KiB
Go
package panel
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"sea-vhost/internal/schedulerbridge"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSystemSchedulerUsesOnlyFixedFreePanelTask(t *testing.T) {
|
|
created := false
|
|
mutations := 0
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.FormValue("action") {
|
|
case "GetCrontab":
|
|
if r.FormValue("search") != schedulerbridge.CronName {
|
|
t.Error("unbounded cron lookup")
|
|
}
|
|
if !created {
|
|
fmt.Fprint(w, `[]`)
|
|
} else {
|
|
json.NewEncoder(w).Encode([]map[string]any{{"id": 123, "name": schedulerbridge.CronName, "sBody": schedulerbridge.Command, "type": "minute-n", "where1": 1, "status": 1}})
|
|
}
|
|
case "AddCrontab":
|
|
if r.FormValue("sBody") != schedulerbridge.Command || r.FormValue("user") != "root" || r.FormValue("where1") != "1" {
|
|
t.Error("non-fixed scheduler mutation")
|
|
}
|
|
mutations++
|
|
created = true
|
|
fmt.Fprint(w, `{"status":true,"id":123}`)
|
|
default:
|
|
t.Error("unexpected native operation")
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
client := NewClient(srv.URL, "test-key", false, time.Second)
|
|
for i := 0; i < 2; i++ {
|
|
id, e := client.EnsureSystemScheduler(context.Background())
|
|
if e != nil || id != 123 {
|
|
t.Fatalf("scheduler id=%d error=%v", id, e)
|
|
}
|
|
}
|
|
if mutations != 1 {
|
|
t.Fatal("duplicate native system crons created")
|
|
}
|
|
if validateFreePanelRequest("POST", "/crontab", url.Values{"action": {"AddCrontab"}, "name": {schedulerbridge.CronName}, "sBody": {"touch /etc/pwned"}, "type": {"minute-n"}, "where1": {"1"}, "sType": {"toShell"}, "user": {"root"}}) == nil {
|
|
t.Fatal("arbitrary root cron accepted")
|
|
}
|
|
if validateFreePanelRequest("POST", "/crontab", url.Values{"action": {"GetCrontab"}}) == nil {
|
|
t.Fatal("unbounded native cron read accepted")
|
|
}
|
|
}
|