1
0

apisrv_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright (C) 2018 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "context"
  9. "crypto/tls"
  10. "fmt"
  11. "io"
  12. "net"
  13. "net/http"
  14. "net/http/httptest"
  15. "os"
  16. "regexp"
  17. "strings"
  18. "testing"
  19. "github.com/syncthing/syncthing/lib/protocol"
  20. "github.com/syncthing/syncthing/lib/tlsutil"
  21. )
  22. func TestFixupAddresses(t *testing.T) {
  23. cases := []struct {
  24. remote *net.TCPAddr
  25. in []string
  26. out []string
  27. }{
  28. { // verbatim passthrough
  29. in: []string{"tcp://1.2.3.4:22000"},
  30. out: []string{"tcp://1.2.3.4:22000"},
  31. }, { // unspecified replaced by remote
  32. remote: addr("1.2.3.4", 22000),
  33. in: []string{"tcp://:22000", "tcp://192.0.2.42:22000"},
  34. out: []string{"tcp://1.2.3.4:22000", "tcp://192.0.2.42:22000"},
  35. }, { // unspecified not used as replacement
  36. remote: addr("0.0.0.0", 22000),
  37. in: []string{"tcp://:22000", "tcp://192.0.2.42:22000"},
  38. out: []string{"tcp://192.0.2.42:22000"},
  39. }, { // unspecified not used as replacement
  40. remote: addr("::", 22000),
  41. in: []string{"tcp://:22000", "tcp://192.0.2.42:22000"},
  42. out: []string{"tcp://192.0.2.42:22000"},
  43. }, { // localhost not used as replacement
  44. remote: addr("127.0.0.1", 22000),
  45. in: []string{"tcp://:22000", "tcp://192.0.2.42:22000"},
  46. out: []string{"tcp://192.0.2.42:22000"},
  47. }, { // localhost not used as replacement
  48. remote: addr("::1", 22000),
  49. in: []string{"tcp://:22000", "tcp://192.0.2.42:22000"},
  50. out: []string{"tcp://192.0.2.42:22000"},
  51. }, { // multicast not used as replacement
  52. remote: addr("224.0.0.1", 22000),
  53. in: []string{"tcp://:22000", "tcp://192.0.2.42:22000"},
  54. out: []string{"tcp://192.0.2.42:22000"},
  55. }, { // multicast not used as replacement
  56. remote: addr("ff80::42", 22000),
  57. in: []string{"tcp://:22000", "tcp://192.0.2.42:22000"},
  58. out: []string{"tcp://192.0.2.42:22000"},
  59. }, { // explicitly announced weirdness is also filtered
  60. remote: addr("192.0.2.42", 22000),
  61. in: []string{"tcp://:22000", "tcp://127.1.2.3:22000", "tcp://[::1]:22000", "tcp://[ff80::42]:22000"},
  62. out: []string{"tcp://192.0.2.42:22000"},
  63. }, { // port remapping
  64. remote: addr("123.123.123.123", 9000),
  65. in: []string{"tcp://0.0.0.0:0"},
  66. out: []string{"tcp://123.123.123.123:9000"},
  67. }, { // unspecified port remapping
  68. remote: addr("123.123.123.123", 9000),
  69. in: []string{"tcp://:0"},
  70. out: []string{"tcp://123.123.123.123:9000"},
  71. }, { // empty remapping
  72. remote: addr("123.123.123.123", 9000),
  73. in: []string{"tcp://"},
  74. out: []string{},
  75. }, { // port only remapping
  76. remote: addr("123.123.123.123", 9000),
  77. in: []string{"tcp://44.44.44.44:0"},
  78. out: []string{"tcp://44.44.44.44:9000"},
  79. }, { // remote ip nil
  80. remote: addr("", 9000),
  81. in: []string{"tcp://:22000", "tcp://44.44.44.44:9000"},
  82. out: []string{"tcp://44.44.44.44:9000"},
  83. }, { // remote port 0
  84. remote: addr("123.123.123.123", 0),
  85. in: []string{"tcp://:22000", "tcp://44.44.44.44"},
  86. out: []string{"tcp://123.123.123.123:22000"},
  87. },
  88. }
  89. for _, tc := range cases {
  90. out := fixupAddresses(tc.remote, tc.in)
  91. if fmt.Sprint(out) != fmt.Sprint(tc.out) {
  92. t.Errorf("fixupAddresses(%v, %v) => %v, expected %v", tc.remote, tc.in, out, tc.out)
  93. }
  94. }
  95. }
  96. func addr(host string, port int) *net.TCPAddr {
  97. return &net.TCPAddr{
  98. IP: net.ParseIP(host),
  99. Port: port,
  100. }
  101. }
  102. func BenchmarkAPIRequests(b *testing.B) {
  103. db := newInMemoryStore(b.TempDir(), 0, nil)
  104. ctx, cancel := context.WithCancel(context.Background())
  105. defer cancel()
  106. go db.Serve(ctx)
  107. api := newAPISrv("127.0.0.1:0", tls.Certificate{}, db, nil, true, true, 1000)
  108. srv := httptest.NewServer(http.HandlerFunc(api.handler))
  109. kf := b.TempDir() + "/cert"
  110. crt, err := tlsutil.NewCertificate(kf+".crt", kf+".key", "localhost", 7, true)
  111. if err != nil {
  112. b.Fatal(err)
  113. }
  114. certBs, err := os.ReadFile(kf + ".crt")
  115. if err != nil {
  116. b.Fatal(err)
  117. }
  118. certBs = regexp.MustCompile(`---[^\n]+---\n`).ReplaceAll(certBs, nil)
  119. certString := string(strings.ReplaceAll(string(certBs), "\n", " "))
  120. devID := protocol.NewDeviceID(crt.Certificate[0])
  121. devIDString := devID.String()
  122. b.Run("Announce", func(b *testing.B) {
  123. b.ReportAllocs()
  124. url := srv.URL + "/v2/?device=" + devIDString
  125. for i := 0; i < b.N; i++ {
  126. req, _ := http.NewRequest(http.MethodPost, url, strings.NewReader(`{"addresses":["tcp://10.10.10.10:42000"]}`))
  127. req.Header.Set("X-Forwarded-Tls-Client-Cert", certString)
  128. resp, err := http.DefaultClient.Do(req)
  129. if err != nil {
  130. b.Fatal(err)
  131. }
  132. resp.Body.Close()
  133. if resp.StatusCode != http.StatusNoContent {
  134. b.Fatalf("unexpected status %s", resp.Status)
  135. }
  136. }
  137. })
  138. b.Run("Lookup", func(b *testing.B) {
  139. b.ReportAllocs()
  140. url := srv.URL + "/v2/?device=" + devIDString
  141. for i := 0; i < b.N; i++ {
  142. req, _ := http.NewRequest(http.MethodGet, url, nil)
  143. resp, err := http.DefaultClient.Do(req)
  144. if err != nil {
  145. b.Fatal(err)
  146. }
  147. io.Copy(io.Discard, resp.Body)
  148. resp.Body.Close()
  149. if resp.StatusCode != http.StatusOK {
  150. b.Fatalf("unexpected status %s", resp.Status)
  151. }
  152. }
  153. })
  154. b.Run("LookupNoCompression", func(b *testing.B) {
  155. b.ReportAllocs()
  156. url := srv.URL + "/v2/?device=" + devIDString
  157. for i := 0; i < b.N; i++ {
  158. req, _ := http.NewRequest(http.MethodGet, url, nil)
  159. req.Header.Set("Accept-Encoding", "identity") // disable compression
  160. resp, err := http.DefaultClient.Do(req)
  161. if err != nil {
  162. b.Fatal(err)
  163. }
  164. io.Copy(io.Discard, resp.Body)
  165. resp.Body.Close()
  166. if resp.StatusCode != http.StatusOK {
  167. b.Fatalf("unexpected status %s", resp.Status)
  168. }
  169. }
  170. })
  171. }