server_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package server
  4. import (
  5. "testing"
  6. )
  7. func TestDetermineListenTargets(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. addresses []string
  11. certDomain string
  12. certFile string
  13. keyFile string
  14. expected []listenTarget
  15. }{
  16. {
  17. name: "single HTTP listener",
  18. addresses: []string{":8080"},
  19. expected: []listenTarget{
  20. {address: ":8080", mode: modeHTTP},
  21. },
  22. },
  23. {
  24. name: "multiple HTTP listeners",
  25. addresses: []string{":8080", ":9090"},
  26. expected: []listenTarget{
  27. {address: ":8080", mode: modeHTTP},
  28. {address: ":9090", mode: modeHTTP},
  29. },
  30. },
  31. {
  32. name: "TLS with cert files",
  33. addresses: []string{":443"},
  34. certFile: "/path/to/cert.pem",
  35. keyFile: "/path/to/key.pem",
  36. expected: []listenTarget{
  37. {address: ":443", mode: modeTLS, certFile: "/path/to/cert.pem", keyFile: "/path/to/key.pem"},
  38. },
  39. },
  40. {
  41. name: "cert file without key file falls back to HTTP",
  42. addresses: []string{":8080"},
  43. certFile: "/path/to/cert.pem",
  44. expected: []listenTarget{
  45. {address: ":8080", mode: modeHTTP},
  46. },
  47. },
  48. {
  49. name: "key file without cert file falls back to HTTP",
  50. addresses: []string{":8080"},
  51. keyFile: "/path/to/key.pem",
  52. expected: []listenTarget{
  53. {address: ":8080", mode: modeHTTP},
  54. },
  55. },
  56. {
  57. name: "autocert with :https address",
  58. addresses: []string{":https"},
  59. certDomain: "example.com",
  60. expected: []listenTarget{
  61. {address: ":https", mode: modeAutocertTLS},
  62. },
  63. },
  64. {
  65. name: "autocert with first address containing colon",
  66. addresses: []string{":443"},
  67. certDomain: "example.com",
  68. expected: []listenTarget{
  69. {address: ":443", mode: modeAutocertTLS},
  70. },
  71. },
  72. {
  73. name: "autocert does not apply to second non-https address",
  74. addresses: []string{":https", ":8080"},
  75. certDomain: "example.com",
  76. expected: []listenTarget{
  77. {address: ":https", mode: modeAutocertTLS},
  78. {address: ":8080", mode: modeHTTP},
  79. },
  80. },
  81. {
  82. name: "unix socket",
  83. addresses: []string{"/var/run/miniflux.sock"},
  84. expected: []listenTarget{
  85. {address: "/var/run/miniflux.sock", mode: modeUnixSocket},
  86. },
  87. },
  88. {
  89. name: "unix socket with TLS",
  90. addresses: []string{"/var/run/miniflux.sock"},
  91. certFile: "/path/to/cert.pem",
  92. keyFile: "/path/to/key.pem",
  93. expected: []listenTarget{
  94. {address: "/var/run/miniflux.sock", mode: modeUnixSocketTLS, certFile: "/path/to/cert.pem", keyFile: "/path/to/key.pem"},
  95. },
  96. },
  97. {
  98. name: "mixed unix socket and TCP",
  99. addresses: []string{"/var/run/miniflux.sock", ":8080"},
  100. certFile: "/path/to/cert.pem",
  101. keyFile: "/path/to/key.pem",
  102. expected: []listenTarget{
  103. {address: "/var/run/miniflux.sock", mode: modeUnixSocketTLS, certFile: "/path/to/cert.pem", keyFile: "/path/to/key.pem"},
  104. {address: ":8080", mode: modeTLS, certFile: "/path/to/cert.pem", keyFile: "/path/to/key.pem"},
  105. },
  106. },
  107. {
  108. name: "empty address list",
  109. addresses: []string{},
  110. expected: nil,
  111. },
  112. }
  113. for _, tc := range tests {
  114. t.Run(tc.name, func(t *testing.T) {
  115. got := determineListenTargets(tc.addresses, tc.certDomain, tc.certFile, tc.keyFile)
  116. if len(got) != len(tc.expected) {
  117. t.Fatalf("got %d targets, want %d", len(got), len(tc.expected))
  118. }
  119. for i := range got {
  120. if got[i] != tc.expected[i] {
  121. t.Errorf("target[%d] = %+v, want %+v", i, got[i], tc.expected[i])
  122. }
  123. }
  124. })
  125. }
  126. }
  127. func TestAnyTLS(t *testing.T) {
  128. tests := []struct {
  129. name string
  130. targets []listenTarget
  131. expected bool
  132. }{
  133. {
  134. name: "empty list",
  135. targets: nil,
  136. expected: false,
  137. },
  138. {
  139. name: "HTTP only",
  140. targets: []listenTarget{{mode: modeHTTP}},
  141. expected: false,
  142. },
  143. {
  144. name: "systemd only",
  145. targets: []listenTarget{{mode: modeSystemd}},
  146. expected: false,
  147. },
  148. {
  149. name: "unix socket without TLS",
  150. targets: []listenTarget{{mode: modeUnixSocket}},
  151. expected: false,
  152. },
  153. {
  154. name: "TLS mode",
  155. targets: []listenTarget{{mode: modeTLS}},
  156. expected: true,
  157. },
  158. {
  159. name: "autocert TLS mode",
  160. targets: []listenTarget{{mode: modeAutocertTLS}},
  161. expected: true,
  162. },
  163. {
  164. name: "unix socket TLS mode",
  165. targets: []listenTarget{{mode: modeUnixSocketTLS}},
  166. expected: true,
  167. },
  168. {
  169. name: "mixed with one TLS",
  170. targets: []listenTarget{{mode: modeHTTP}, {mode: modeTLS}, {mode: modeUnixSocket}},
  171. expected: true,
  172. },
  173. }
  174. for _, tc := range tests {
  175. t.Run(tc.name, func(t *testing.T) {
  176. if got := anyTLS(tc.targets); got != tc.expected {
  177. t.Errorf("anyTLS() = %v, want %v", got, tc.expected)
  178. }
  179. })
  180. }
  181. }