tshttpproxy_synology_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build linux
  4. package tshttpproxy
  5. import (
  6. "errors"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "net/url"
  11. "os"
  12. "path/filepath"
  13. "strings"
  14. "testing"
  15. "time"
  16. "tailscale.com/tstest"
  17. )
  18. func TestSynologyProxyFromConfigCached(t *testing.T) {
  19. req, err := http.NewRequest("GET", "http://example.org/", nil)
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. tstest.Replace(t, &synologyProxyConfigPath, filepath.Join(t.TempDir(), "proxy.conf"))
  24. t.Run("no config file", func(t *testing.T) {
  25. if _, err := os.Stat(synologyProxyConfigPath); err == nil {
  26. t.Fatalf("%s must not exist for this test", synologyProxyConfigPath)
  27. }
  28. cache.updated = time.Time{}
  29. cache.httpProxy = nil
  30. cache.httpsProxy = nil
  31. if val, err := synologyProxyFromConfigCached(req); val != nil || err != nil {
  32. t.Fatalf("got %s, %v; want nil, nil", val, err)
  33. }
  34. if got, want := cache.updated, time.Unix(0, 0); got != want {
  35. t.Fatalf("got %s, want %s", got, want)
  36. }
  37. if cache.httpProxy != nil {
  38. t.Fatalf("got %s, want nil", cache.httpProxy)
  39. }
  40. if cache.httpsProxy != nil {
  41. t.Fatalf("got %s, want nil", cache.httpsProxy)
  42. }
  43. })
  44. t.Run("config file updated", func(t *testing.T) {
  45. cache.updated = time.Now()
  46. cache.httpProxy = nil
  47. cache.httpsProxy = nil
  48. if err := os.WriteFile(synologyProxyConfigPath, []byte(`
  49. proxy_enabled=yes
  50. http_host=10.0.0.55
  51. http_port=80
  52. https_host=10.0.0.66
  53. https_port=443
  54. `), 0600); err != nil {
  55. t.Fatal(err)
  56. }
  57. val, err := synologyProxyFromConfigCached(req)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. if cache.httpProxy == nil {
  62. t.Fatal("http proxy was not cached")
  63. }
  64. if cache.httpsProxy == nil {
  65. t.Fatal("https proxy was not cached")
  66. }
  67. if want := urlMustParse("http://10.0.0.55:80"); val.String() != want.String() {
  68. t.Fatalf("got %s; want %s", val, want)
  69. }
  70. })
  71. t.Run("config file removed", func(t *testing.T) {
  72. cache.updated = time.Now()
  73. cache.httpProxy = urlMustParse("http://127.0.0.1/")
  74. cache.httpsProxy = urlMustParse("http://127.0.0.1/")
  75. if err := os.Remove(synologyProxyConfigPath); err != nil && !os.IsNotExist(err) {
  76. t.Fatal(err)
  77. }
  78. val, err := synologyProxyFromConfigCached(req)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. if val != nil {
  83. t.Fatalf("got %s; want nil", val)
  84. }
  85. if cache.httpProxy != nil {
  86. t.Fatalf("got %s, want nil", cache.httpProxy)
  87. }
  88. if cache.httpsProxy != nil {
  89. t.Fatalf("got %s, want nil", cache.httpsProxy)
  90. }
  91. })
  92. t.Run("picks proxy from request scheme", func(t *testing.T) {
  93. cache.updated = time.Now()
  94. cache.httpProxy = nil
  95. cache.httpsProxy = nil
  96. if err := os.WriteFile(synologyProxyConfigPath, []byte(`
  97. proxy_enabled=yes
  98. http_host=10.0.0.55
  99. http_port=80
  100. https_host=10.0.0.66
  101. https_port=443
  102. `), 0600); err != nil {
  103. t.Fatal(err)
  104. }
  105. httpReq, err := http.NewRequest("GET", "http://example.com", nil)
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. val, err := synologyProxyFromConfigCached(httpReq)
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. if val == nil {
  114. t.Fatalf("got nil, want an http URL")
  115. }
  116. if got, want := val.String(), "http://10.0.0.55:80"; got != want {
  117. t.Fatalf("got %q, want %q", got, want)
  118. }
  119. httpsReq, err := http.NewRequest("GET", "https://example.com", nil)
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. val, err = synologyProxyFromConfigCached(httpsReq)
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. if val == nil {
  128. t.Fatalf("got nil, want an http URL")
  129. }
  130. if got, want := val.String(), "http://10.0.0.66:443"; got != want {
  131. t.Fatalf("got %q, want %q", got, want)
  132. }
  133. })
  134. }
  135. func TestSynologyProxiesFromConfig(t *testing.T) {
  136. var (
  137. openReader io.ReadCloser
  138. openErr error
  139. )
  140. tstest.Replace(t, &openSynologyProxyConf, func() (io.ReadCloser, error) {
  141. return openReader, openErr
  142. })
  143. t.Run("with config", func(t *testing.T) {
  144. mc := &mustCloser{Reader: strings.NewReader(`
  145. proxy_user=foo
  146. proxy_pwd=bar
  147. proxy_enabled=yes
  148. adv_enabled=yes
  149. bypass_enabled=yes
  150. auth_enabled=yes
  151. https_host=10.0.0.66
  152. https_port=8443
  153. http_host=10.0.0.55
  154. http_port=80
  155. `)}
  156. defer mc.check(t)
  157. openReader = mc
  158. httpProxy, httpsProxy, err := synologyProxiesFromConfig()
  159. if got, want := err, openErr; got != want {
  160. t.Fatalf("got %s, want %s", got, want)
  161. }
  162. if got, want := httpsProxy, urlMustParse("http://foo:[email protected]:8443"); got.String() != want.String() {
  163. t.Fatalf("got %s, want %s", got, want)
  164. }
  165. if got, want := err, openErr; got != want {
  166. t.Fatalf("got %s, want %s", got, want)
  167. }
  168. if got, want := httpProxy, urlMustParse("http://foo:[email protected]:80"); got.String() != want.String() {
  169. t.Fatalf("got %s, want %s", got, want)
  170. }
  171. })
  172. t.Run("nonexistent config", func(t *testing.T) {
  173. openReader = nil
  174. openErr = os.ErrNotExist
  175. httpProxy, httpsProxy, err := synologyProxiesFromConfig()
  176. if err != nil {
  177. t.Fatalf("expected no error, got %s", err)
  178. }
  179. if httpProxy != nil {
  180. t.Fatalf("expected no url, got %s", httpProxy)
  181. }
  182. if httpsProxy != nil {
  183. t.Fatalf("expected no url, got %s", httpsProxy)
  184. }
  185. })
  186. t.Run("error opening config", func(t *testing.T) {
  187. openReader = nil
  188. openErr = errors.New("example error")
  189. httpProxy, httpsProxy, err := synologyProxiesFromConfig()
  190. if err != openErr {
  191. t.Fatalf("expected %s, got %s", openErr, err)
  192. }
  193. if httpProxy != nil {
  194. t.Fatalf("expected no url, got %s", httpProxy)
  195. }
  196. if httpsProxy != nil {
  197. t.Fatalf("expected no url, got %s", httpsProxy)
  198. }
  199. })
  200. }
  201. func TestParseSynologyConfig(t *testing.T) {
  202. cases := map[string]struct {
  203. input string
  204. httpProxy *url.URL
  205. httpsProxy *url.URL
  206. err error
  207. }{
  208. "populated": {
  209. input: `
  210. proxy_user=foo
  211. proxy_pwd=bar
  212. proxy_enabled=yes
  213. adv_enabled=yes
  214. bypass_enabled=yes
  215. auth_enabled=yes
  216. https_host=10.0.0.66
  217. https_port=8443
  218. http_host=10.0.0.55
  219. http_port=80
  220. `,
  221. httpProxy: urlMustParse("http://foo:[email protected]:80"),
  222. httpsProxy: urlMustParse("http://foo:[email protected]:8443"),
  223. err: nil,
  224. },
  225. "no-auth": {
  226. input: `
  227. proxy_user=foo
  228. proxy_pwd=bar
  229. proxy_enabled=yes
  230. adv_enabled=yes
  231. bypass_enabled=yes
  232. auth_enabled=no
  233. https_host=10.0.0.66
  234. https_port=8443
  235. http_host=10.0.0.55
  236. http_port=80
  237. `,
  238. httpProxy: urlMustParse("http://10.0.0.55:80"),
  239. httpsProxy: urlMustParse("http://10.0.0.66:8443"),
  240. err: nil,
  241. },
  242. "http-only": {
  243. input: `
  244. proxy_user=foo
  245. proxy_pwd=bar
  246. proxy_enabled=yes
  247. adv_enabled=yes
  248. bypass_enabled=yes
  249. auth_enabled=yes
  250. https_host=
  251. https_port=8443
  252. http_host=10.0.0.55
  253. http_port=80
  254. `,
  255. httpProxy: urlMustParse("http://foo:[email protected]:80"),
  256. httpsProxy: nil,
  257. err: nil,
  258. },
  259. "empty": {
  260. input: `
  261. proxy_user=
  262. proxy_pwd=
  263. proxy_enabled=
  264. adv_enabled=
  265. bypass_enabled=
  266. auth_enabled=
  267. https_host=
  268. https_port=
  269. http_host=
  270. http_port=
  271. `,
  272. httpProxy: nil,
  273. httpsProxy: nil,
  274. err: nil,
  275. },
  276. }
  277. for name, example := range cases {
  278. t.Run(name, func(t *testing.T) {
  279. httpProxy, httpsProxy, err := parseSynologyConfig(strings.NewReader(example.input))
  280. if err != example.err {
  281. t.Fatal(err)
  282. }
  283. if example.err != nil {
  284. return
  285. }
  286. if example.httpProxy == nil && httpProxy != nil {
  287. t.Fatalf("got %s, want nil", httpProxy)
  288. }
  289. if example.httpProxy != nil {
  290. if httpProxy == nil {
  291. t.Fatalf("got nil, want %s", example.httpProxy)
  292. }
  293. if got, want := example.httpProxy.String(), httpProxy.String(); got != want {
  294. t.Fatalf("got %s, want %s", got, want)
  295. }
  296. }
  297. if example.httpsProxy == nil && httpsProxy != nil {
  298. t.Fatalf("got %s, want nil", httpProxy)
  299. }
  300. if example.httpsProxy != nil {
  301. if httpsProxy == nil {
  302. t.Fatalf("got nil, want %s", example.httpsProxy)
  303. }
  304. if got, want := example.httpsProxy.String(), httpsProxy.String(); got != want {
  305. t.Fatalf("got %s, want %s", got, want)
  306. }
  307. }
  308. })
  309. }
  310. }
  311. func urlMustParse(u string) *url.URL {
  312. r, err := url.Parse(u)
  313. if err != nil {
  314. panic(fmt.Sprintf("urlMustParse: %s", err))
  315. }
  316. return r
  317. }
  318. type mustCloser struct {
  319. io.Reader
  320. closed bool
  321. }
  322. func (m *mustCloser) Close() error {
  323. m.closed = true
  324. return nil
  325. }
  326. func (m *mustCloser) check(t *testing.T) {
  327. if !m.closed {
  328. t.Errorf("mustCloser wrapping %#v was not closed at time of check", m.Reader)
  329. }
  330. }