remote_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package runtime
  2. import (
  3. "encoding/json"
  4. "testing"
  5. )
  6. func TestSanitizeStreamSettingsForRemote(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. input string
  10. // wantCertFile / wantKeyFile: expected presence after sanitize
  11. wantCertFile bool
  12. wantKeyFile bool
  13. }{
  14. {
  15. name: "file paths only — kept intact (remote node paths)",
  16. input: `{
  17. "tlsSettings": {
  18. "certificates": [{
  19. "certificateFile": "/etc/ssl/cert.crt",
  20. "keyFile": "/etc/ssl/key.key"
  21. }]
  22. }
  23. }`,
  24. wantCertFile: true,
  25. wantKeyFile: true,
  26. },
  27. {
  28. name: "inline content only — unchanged",
  29. input: `{
  30. "tlsSettings": {
  31. "certificates": [{
  32. "certificate": ["-----BEGIN CERTIFICATE-----"],
  33. "key": ["-----BEGIN PRIVATE KEY-----"]
  34. }]
  35. }
  36. }`,
  37. wantCertFile: false,
  38. wantKeyFile: false,
  39. },
  40. {
  41. name: "both file paths and inline content — file paths stripped (redundant)",
  42. input: `{
  43. "tlsSettings": {
  44. "certificates": [{
  45. "certificateFile": "/etc/ssl/cert.crt",
  46. "keyFile": "/etc/ssl/key.key",
  47. "certificate": ["-----BEGIN CERTIFICATE-----"],
  48. "key": ["-----BEGIN PRIVATE KEY-----"]
  49. }]
  50. }
  51. }`,
  52. wantCertFile: false,
  53. wantKeyFile: false,
  54. },
  55. {
  56. name: "empty stream settings",
  57. input: "",
  58. // empty input returns empty, nothing to check
  59. },
  60. }
  61. for _, tc := range tests {
  62. t.Run(tc.name, func(t *testing.T) {
  63. if tc.input == "" {
  64. if got := sanitizeStreamSettingsForRemote(tc.input); got != "" {
  65. t.Errorf("expected empty string, got %q", got)
  66. }
  67. return
  68. }
  69. got := sanitizeStreamSettingsForRemote(tc.input)
  70. var out map[string]any
  71. if err := json.Unmarshal([]byte(got), &out); err != nil {
  72. t.Fatalf("output is not valid JSON: %v\noutput: %s", err, got)
  73. }
  74. tls, _ := out["tlsSettings"].(map[string]any)
  75. certs, _ := tls["certificates"].([]any)
  76. if len(certs) == 0 {
  77. t.Fatal("certificates array missing in output")
  78. }
  79. cert, _ := certs[0].(map[string]any)
  80. _, hasCertFile := cert["certificateFile"]
  81. _, hasKeyFile := cert["keyFile"]
  82. if hasCertFile != tc.wantCertFile {
  83. t.Errorf("certificateFile present=%v, want %v", hasCertFile, tc.wantCertFile)
  84. }
  85. if hasKeyFile != tc.wantKeyFile {
  86. t.Errorf("keyFile present=%v, want %v", hasKeyFile, tc.wantKeyFile)
  87. }
  88. })
  89. }
  90. }