storj/pkg/ranger/file_test.go
Egon Elbre 0f5a2f4ef5 Enable more linters (#272)
* enable more linters

* Run gofmt -s

* run goimports

* run unconvert

* fix naked return

* fix misspellings

* fix ineffectual assigments

* fix missing declaration

* don't use deprecated grpc.Errof

* check errors in tests

* run gofmt -w -r "assert.Nil(err) -> assert.NoError(err)"

* fix directory permissions

* don't use nil Context

* simplify boolean expressions

* use bytes.Equal instead of bytes.Compare

* merge variable declarations, remove redundant returns

* fix some golint errors

* run goimports

* handle more errors

* delete empty TestMain

* delete empty TestMain

* ignore examples for now

* fix lint errors

* remove unused values

* more fixes

* run gofmt -w -s .

* add more comments

* fix naming

* more lint fixes

* try switching travis to go1.11

* fix unnecessary conversions

* fix deprecated methods

* use go1.10 and disable gofmt/goimports for now

* switch to 1.10

* don't re-enable gofmt and goimports

* switch covermode to atomic because of -race

* gofmt
2018-08-27 11:28:16 -06:00

93 lines
2.1 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package ranger
import (
"bytes"
"context"
"io/ioutil"
"testing"
)
func TestFileRanger(t *testing.T) {
for _, example := range []struct {
data string
size, offset, length int64
substr string
fail bool
}{
{"", 0, 0, 0, "", false},
{"abcdef", 6, 0, 0, "", false},
{"abcdef", 6, 3, 0, "", false},
{"abcdef", 6, 0, 6, "abcdef", false},
{"abcdef", 6, 0, 5, "abcde", false},
{"abcdef", 6, 0, 4, "abcd", false},
{"abcdef", 6, 1, 4, "bcde", false},
{"abcdef", 6, 2, 4, "cdef", false},
{"abcdefg", 7, 1, 4, "bcde", false},
{"abcdef", 6, 0, 7, "", true},
{"abcdef", 6, -1, 7, "abcde", true},
{"abcdef", 6, 0, -1, "abcde", true},
} {
fh, err := ioutil.TempFile("", "test")
if err != nil {
t.Fatalf("failed making tempfile")
}
_, err = fh.Write([]byte(example.data))
if err != nil {
t.Fatalf("failed writing data")
}
name := fh.Name()
err = fh.Close()
if err != nil {
t.Fatalf("failed closing data")
}
rr, err := FileRanger(name)
if err != nil {
t.Fatalf("failed opening tempfile")
}
defer rr.Close()
if rr.Size() != example.size {
t.Fatalf("invalid size: %v != %v", rr.Size(), example.size)
}
r, err := rr.Range(context.Background(), example.offset, example.length)
if example.fail {
if err == nil {
t.Fatalf("expected error")
}
return
}
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
data, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, []byte(example.substr)) {
t.Fatalf("invalid subrange: %#v != %#v", string(data), example.substr)
}
}
}
func TestFileRangerOpenFileError(t *testing.T) {
rr, err := FileRanger("")
if rr != nil {
t.Fatal("Ranger expected to be nil")
}
if err == nil {
t.Fatal("Error expected")
}
}
func TestFileRangerHandlerFileStatError(t *testing.T) {
rr, err := FileHandleRanger(nil)
if rr != nil {
t.Fatal("Ranger expected to be nil")
}
if err == nil {
t.Fatal("Error expected")
}
}