model_shadowsocks_removed_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package model
  2. import (
  3. "encoding/json"
  4. "testing"
  5. )
  6. // TestHealShadowsocksClientMethods_RewritesRemovedCipher covers the last-gate
  7. // build-time heal for xray-core v26.7.11's removed "none"/"plain" ciphers: a
  8. // row that survives to config generation (restored backup, direct DB edit)
  9. // must be rewritten to a supported cipher on both the inbound method and its
  10. // clients so one such inbound cannot keep xray from starting.
  11. func TestHealShadowsocksClientMethods_RewritesRemovedCipher(t *testing.T) {
  12. settings := `{"method": "plain", "clients": [{"email": "a@x", "password": "p", "method": "plain"}]}`
  13. healed, ok := HealShadowsocksClientMethods(settings)
  14. if !ok {
  15. t.Fatal("expected heal to report a change for a removed cipher")
  16. }
  17. var parsed struct {
  18. Method string `json:"method"`
  19. Clients []map[string]any `json:"clients"`
  20. }
  21. if err := json.Unmarshal([]byte(healed), &parsed); err != nil {
  22. t.Fatalf("parse healed settings: %v", err)
  23. }
  24. if parsed.Method != "chacha20-ietf-poly1305" {
  25. t.Fatalf("expected inbound method rewritten to a supported cipher, got %q", parsed.Method)
  26. }
  27. if parsed.Clients[0]["method"] != "chacha20-ietf-poly1305" {
  28. t.Fatalf("expected client method to match the healed cipher, got %v", parsed.Clients[0]["method"])
  29. }
  30. }
  31. func TestReplaceRemovedShadowsocksCipher(t *testing.T) {
  32. for _, method := range []string{"none", "plain"} {
  33. if got, removed := ReplaceRemovedShadowsocksCipher(method); !removed || got != "chacha20-ietf-poly1305" {
  34. t.Fatalf("ReplaceRemovedShadowsocksCipher(%q) = (%q, %v), want a supported replacement", method, got, removed)
  35. }
  36. }
  37. if got, removed := ReplaceRemovedShadowsocksCipher("aes-256-gcm"); removed || got != "aes-256-gcm" {
  38. t.Fatalf("ReplaceRemovedShadowsocksCipher(aes-256-gcm) = (%q, %v), want it left untouched", got, removed)
  39. }
  40. }