go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cipd/appengine/impl/repo/processing/reader_test.go (about)

     1  // Copyright 2018 The LUCI Authors.
     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  
    15  package processing
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"io"
    21  	"strings"
    22  	"testing"
    23  
    24  	"go.chromium.org/luci/cipd/appengine/impl/testutil"
    25  
    26  	. "github.com/smartystreets/goconvey/convey"
    27  )
    28  
    29  type cbReaderAt struct {
    30  	readAt func(p []byte, off int64) (int, error)
    31  }
    32  
    33  func (c *cbReaderAt) ReadAt(b []byte, off int64) (int, error) {
    34  	return c.readAt(b, off)
    35  }
    36  
    37  func TestPackageReader(t *testing.T) {
    38  	t.Parallel()
    39  
    40  	testZip := testutil.MakeZip(map[string]string{
    41  		"file1": strings.Repeat("hello", 50),
    42  		"file2": "blah",
    43  	})
    44  	reader := bytes.NewReader(testZip)
    45  	size := int64(reader.Len())
    46  
    47  	readErr := fmt.Errorf("some read error")
    48  
    49  	Convey("Happy path", t, func() {
    50  		pkg, err := NewPackageReader(reader, size)
    51  		So(err, ShouldBeNil)
    52  
    53  		So(pkg.Files(), ShouldResemble, []string{"file1", "file2"})
    54  
    55  		fr, actualSize, err := pkg.Open("file2")
    56  		So(err, ShouldBeNil)
    57  		So(actualSize, ShouldEqual, 4)
    58  		blob, err := io.ReadAll(fr)
    59  		So(err, ShouldBeNil)
    60  		So(string(blob), ShouldEqual, "blah")
    61  	})
    62  
    63  	Convey("No such file", t, func() {
    64  		pkg, err := NewPackageReader(reader, size)
    65  		So(err, ShouldBeNil)
    66  
    67  		_, _, err = pkg.Open("zzz")
    68  		So(err.Error(), ShouldEqual, `no file "zzz" inside the package`)
    69  	})
    70  
    71  	Convey("Propagates errors when opening", t, func() {
    72  		calls := 0
    73  		r := &cbReaderAt{
    74  			readAt: func(p []byte, off int64) (int, error) {
    75  				// Fail the second read call, it makes more interesting test case.
    76  				calls++
    77  				if calls == 2 {
    78  					return 0, readErr
    79  				}
    80  				return reader.ReadAt(p, off)
    81  			},
    82  		}
    83  
    84  		_, err := NewPackageReader(r, size)
    85  		So(err, ShouldEqual, readErr) // exact same error object
    86  	})
    87  
    88  	Convey("Propagates errors when reading", t, func() {
    89  		r := &cbReaderAt{readAt: reader.ReadAt}
    90  
    91  		// Let the directory be read successfully.
    92  		pkg, err := NewPackageReader(r, size)
    93  		So(err, ShouldBeNil)
    94  
    95  		// Now inject errors.
    96  		r.readAt = func([]byte, int64) (int, error) { return 0, readErr }
    97  		_, _, err = pkg.Open("file1")
    98  		So(err, ShouldEqual, readErr) // exact same error object
    99  	})
   100  }