mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-07-16 01:26:07 +00:00
3f7e141d2e
GetNewX25519Cert, GetNewmldsa65 and GetNewmlkem768 parsed xray's stdout by reading lines[0], lines[1] and each line's second colon-separated field without any length check — unlike GetNewEchCert, which already guards its line count. If the xray binary printed fewer than two lines or reformatted its labels (a version change, or a silent failure that emitted nothing), the fixed slice index panicked and the handler 500'd. Extract the shared parsing into parseXrayKeyPairOutput, which length guards the line count and each label split and returns an error instead of panicking, then route all three generators through it.
38 lines
1.0 KiB
Go
38 lines
1.0 KiB
Go
package service
|
|
|
|
import "testing"
|
|
|
|
// The x25519 / mldsa65 / mlkem768 key generators parse xray's stdout by fixed
|
|
// slice index. A short or reformatted output (a future xray release, or a
|
|
// binary that failed silently) must yield an error, never an out-of-range
|
|
// panic that 500s the handler.
|
|
func TestParseXrayKeyPairOutput(t *testing.T) {
|
|
a, b, err := parseXrayKeyPairOutput("Private key: abc123\nPublic key: def456\n")
|
|
if err != nil {
|
|
t.Fatalf("well-formed output errored: %v", err)
|
|
}
|
|
if a != "abc123" || b != "def456" {
|
|
t.Fatalf("got (%q, %q), want (abc123, def456)", a, b)
|
|
}
|
|
|
|
malformed := []string{
|
|
"",
|
|
"only one line: value",
|
|
"Private key: abc\n",
|
|
"no colon here\nno colon two",
|
|
"Private key\nPublic key",
|
|
}
|
|
for _, out := range malformed {
|
|
func() {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.Errorf("parseXrayKeyPairOutput panicked on %q: %v", out, r)
|
|
}
|
|
}()
|
|
if _, _, err := parseXrayKeyPairOutput(out); err == nil {
|
|
t.Errorf("expected error for malformed output %q, got nil", out)
|
|
}
|
|
}()
|
|
}
|
|
}
|