You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

298 lines
6.5 KiB

  1. // Copyright 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package storage
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. "io"
  20. "io/ioutil"
  21. "net/http"
  22. "strconv"
  23. "strings"
  24. "testing"
  25. "google.golang.org/api/option"
  26. )
  27. const readData = "0123456789"
  28. func TestRangeReader(t *testing.T) {
  29. hc, close := newTestServer(handleRangeRead)
  30. defer close()
  31. ctx := context.Background()
  32. c, err := NewClient(ctx, option.WithHTTPClient(hc))
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. obj := c.Bucket("b").Object("o")
  37. for _, test := range []struct {
  38. offset, length int64
  39. want string
  40. }{
  41. {0, -1, readData},
  42. {0, 10, readData},
  43. {0, 5, readData[:5]},
  44. {1, 3, readData[1:4]},
  45. {6, -1, readData[6:]},
  46. {4, 20, readData[4:]},
  47. } {
  48. r, err := obj.NewRangeReader(ctx, test.offset, test.length)
  49. if err != nil {
  50. t.Errorf("%d/%d: %v", test.offset, test.length, err)
  51. continue
  52. }
  53. gotb, err := ioutil.ReadAll(r)
  54. if err != nil {
  55. t.Errorf("%d/%d: %v", test.offset, test.length, err)
  56. continue
  57. }
  58. if got := string(gotb); got != test.want {
  59. t.Errorf("%d/%d: got %q, want %q", test.offset, test.length, got, test.want)
  60. }
  61. }
  62. }
  63. func handleRangeRead(w http.ResponseWriter, r *http.Request) {
  64. rh := strings.TrimSpace(r.Header.Get("Range"))
  65. data := readData
  66. var from, to int
  67. if rh == "" {
  68. from = 0
  69. to = len(data)
  70. } else {
  71. // assume "bytes=N-" or "bytes=N-M"
  72. var err error
  73. i := strings.IndexRune(rh, '=')
  74. j := strings.IndexRune(rh, '-')
  75. from, err = strconv.Atoi(rh[i+1 : j])
  76. if err != nil {
  77. w.WriteHeader(500)
  78. return
  79. }
  80. to = len(data)
  81. if j+1 < len(rh) {
  82. to, err = strconv.Atoi(rh[j+1:])
  83. if err != nil {
  84. w.WriteHeader(500)
  85. return
  86. }
  87. to++ // Range header is inclusive, Go slice is exclusive
  88. }
  89. if from >= len(data) && to != from {
  90. w.WriteHeader(416)
  91. return
  92. }
  93. if from > len(data) {
  94. from = len(data)
  95. }
  96. if to > len(data) {
  97. to = len(data)
  98. }
  99. }
  100. data = data[from:to]
  101. if data != readData {
  102. w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", from, to-1, len(readData)))
  103. w.WriteHeader(http.StatusPartialContent)
  104. }
  105. if _, err := w.Write([]byte(data)); err != nil {
  106. panic(err)
  107. }
  108. }
  109. type http2Error string
  110. func (h http2Error) Error() string {
  111. return string(h)
  112. }
  113. func TestRangeReaderRetry(t *testing.T) {
  114. retryErr := http2Error("blah blah INTERNAL_ERROR")
  115. readBytes := []byte(readData)
  116. hc, close := newTestServer(handleRangeRead)
  117. defer close()
  118. ctx := context.Background()
  119. c, err := NewClient(ctx, option.WithHTTPClient(hc))
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. obj := c.Bucket("b").Object("o")
  124. for i, test := range []struct {
  125. offset, length int64
  126. bodies []fakeReadCloser
  127. want string
  128. }{
  129. {
  130. offset: 0,
  131. length: -1,
  132. bodies: []fakeReadCloser{
  133. {data: readBytes, counts: []int{10}, err: io.EOF},
  134. },
  135. want: readData,
  136. },
  137. {
  138. offset: 0,
  139. length: -1,
  140. bodies: []fakeReadCloser{
  141. {data: readBytes, counts: []int{3}, err: retryErr},
  142. {data: readBytes[3:], counts: []int{5, 2}, err: io.EOF},
  143. },
  144. want: readData,
  145. },
  146. {
  147. offset: 0,
  148. length: -1,
  149. bodies: []fakeReadCloser{
  150. {data: readBytes, counts: []int{5}, err: retryErr},
  151. {data: readBytes[5:], counts: []int{1, 3}, err: retryErr},
  152. {data: readBytes[9:], counts: []int{1}, err: io.EOF},
  153. },
  154. want: readData,
  155. },
  156. {
  157. offset: 0,
  158. length: 5,
  159. bodies: []fakeReadCloser{
  160. {data: readBytes, counts: []int{3}, err: retryErr},
  161. {data: readBytes[3:], counts: []int{2}, err: io.EOF},
  162. },
  163. want: readData[:5],
  164. },
  165. {
  166. offset: 1,
  167. length: 5,
  168. bodies: []fakeReadCloser{
  169. {data: readBytes, counts: []int{3}, err: retryErr},
  170. {data: readBytes[3:], counts: []int{2}, err: io.EOF},
  171. },
  172. want: readData[:5],
  173. },
  174. {
  175. offset: 1,
  176. length: 3,
  177. bodies: []fakeReadCloser{
  178. {data: readBytes[1:], counts: []int{1}, err: retryErr},
  179. {data: readBytes[2:], counts: []int{2}, err: io.EOF},
  180. },
  181. want: readData[1:4],
  182. },
  183. {
  184. offset: 4,
  185. length: -1,
  186. bodies: []fakeReadCloser{
  187. {data: readBytes[4:], counts: []int{1}, err: retryErr},
  188. {data: readBytes[5:], counts: []int{4}, err: retryErr},
  189. {data: readBytes[9:], counts: []int{1}, err: io.EOF},
  190. },
  191. want: readData[4:],
  192. },
  193. } {
  194. r, err := obj.NewRangeReader(ctx, test.offset, test.length)
  195. if err != nil {
  196. t.Errorf("#%d: %v", i, err)
  197. continue
  198. }
  199. r.body = &test.bodies[0]
  200. b := 0
  201. r.reopen = func(int64) (*http.Response, error) {
  202. b++
  203. return &http.Response{Body: &test.bodies[b]}, nil
  204. }
  205. buf := make([]byte, len(readData)/2)
  206. var gotb []byte
  207. for {
  208. n, err := r.Read(buf)
  209. gotb = append(gotb, buf[:n]...)
  210. if err == io.EOF {
  211. break
  212. }
  213. if err != nil {
  214. t.Fatalf("#%d: %v", i, err)
  215. }
  216. }
  217. if err != nil {
  218. t.Errorf("#%d: %v", i, err)
  219. continue
  220. }
  221. if got := string(gotb); got != test.want {
  222. t.Errorf("#%d: got %q, want %q", i, got, test.want)
  223. }
  224. }
  225. }
  226. type fakeReadCloser struct {
  227. data []byte
  228. counts []int // how much of data to deliver on each read
  229. err error // error to return with last count
  230. d int // current position in data
  231. c int // current position in counts
  232. }
  233. func (f *fakeReadCloser) Close() error {
  234. return nil
  235. }
  236. func (f *fakeReadCloser) Read(buf []byte) (int, error) {
  237. i := f.c
  238. n := 0
  239. if i < len(f.counts) {
  240. n = f.counts[i]
  241. }
  242. var err error
  243. if i >= len(f.counts)-1 {
  244. err = f.err
  245. }
  246. copy(buf, f.data[f.d:f.d+n])
  247. if len(buf) < n {
  248. n = len(buf)
  249. f.counts[i] -= n
  250. err = nil
  251. } else {
  252. f.c++
  253. }
  254. f.d += n
  255. return n, err
  256. }
  257. func TestFakeReadCloser(t *testing.T) {
  258. e := errors.New("")
  259. f := &fakeReadCloser{
  260. data: []byte(readData),
  261. counts: []int{1, 2, 3},
  262. err: e,
  263. }
  264. wants := []string{"0", "12", "345"}
  265. buf := make([]byte, 10)
  266. for i := 0; i < 3; i++ {
  267. n, err := f.Read(buf)
  268. if got, want := n, f.counts[i]; got != want {
  269. t.Fatalf("i=%d: got %d, want %d", i, got, want)
  270. }
  271. var wantErr error
  272. if i == 2 {
  273. wantErr = e
  274. }
  275. if err != wantErr {
  276. t.Fatalf("i=%d: got error %v, want %v", i, err, wantErr)
  277. }
  278. if got, want := string(buf[:n]), wants[i]; got != want {
  279. t.Fatalf("i=%d: got %q, want %q", i, got, want)
  280. }
  281. }
  282. }