|
| 1 | +// +build gofuzz |
| 2 | + |
| 3 | +/* |
| 4 | + Copyright The containerd Authors. |
| 5 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + you may not use this file except in compliance with the License. |
| 7 | + You may obtain a copy of the License at |
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 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 | +*/ |
| 15 | + |
| 16 | +/* |
| 17 | + This fuzzer is run continuously by OSS-fuzz. |
| 18 | + It is stored in contrib/fuzz for organization, |
| 19 | + but in order to execute it, it must be moved to |
| 20 | + remotes/docker first. |
| 21 | +*/ |
| 22 | + |
| 23 | +package docker |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "fmt" |
| 28 | + "io/ioutil" |
| 29 | + "net/http" |
| 30 | + "net/http/httptest" |
| 31 | + "net/url" |
| 32 | +) |
| 33 | + |
| 34 | +func FuzzFetcher(data []byte) int { |
| 35 | + dataLen := len(data) |
| 36 | + if dataLen == 0 { |
| 37 | + return -1 |
| 38 | + } |
| 39 | + |
| 40 | + s := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 41 | + rw.Header().Set("content-range", fmt.Sprintf("bytes %d-%d/%d", 0, dataLen-1, dataLen)) |
| 42 | + rw.Header().Set("content-length", fmt.Sprintf("%d", dataLen)) |
| 43 | + rw.Write(data) |
| 44 | + })) |
| 45 | + defer s.Close() |
| 46 | + |
| 47 | + u, err := url.Parse(s.URL) |
| 48 | + if err != nil { |
| 49 | + return 0 |
| 50 | + } |
| 51 | + |
| 52 | + f := dockerFetcher{&dockerBase{ |
| 53 | + repository: "nonempty", |
| 54 | + }} |
| 55 | + host := RegistryHost{ |
| 56 | + Client: s.Client(), |
| 57 | + Host: u.Host, |
| 58 | + Scheme: u.Scheme, |
| 59 | + Path: u.Path, |
| 60 | + } |
| 61 | + |
| 62 | + ctx := context.Background() |
| 63 | + req := f.request(host, http.MethodGet) |
| 64 | + rc, err := f.open(ctx, req, "", 0) |
| 65 | + if err != nil { |
| 66 | + return 0 |
| 67 | + } |
| 68 | + b, err := ioutil.ReadAll(rc) |
| 69 | + if err != nil { |
| 70 | + return 0 |
| 71 | + } |
| 72 | + |
| 73 | + expected := data |
| 74 | + if len(b) != len(expected) { |
| 75 | + panic("len of request is not equal to len of expected but should be") |
| 76 | + } |
| 77 | + return 1 |
| 78 | +} |
0 commit comments