coreapi: add some seeker tests
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Jan 21, 2019 at 12:30 UTC
3f4311b6cc77d764cbb23545ce6420c443b5487d
1 file changed
+105
core/coreapi/interface/tests/unixfs.go
+105
@@ -3,9 +3,11 @@ package tests
3
import (
4
"bytes"
5
"context"
6
+ "fmt"
7
"io"
8
"io/ioutil"
9
"math"
10
+ "math/rand"
11
"os"
12
"strconv"
13
"strings"
@@ -43,6 +45,7 @@ func (tp *provider) TestUnixfs(t *testing.T) {
45
t.Run("TestLsEmptyDir", tp.TestLsEmptyDir)
46
t.Run("TestLsNonUnixfs", tp.TestLsNonUnixfs)
47
t.Run("TestAddCloses", tp.TestAddCloses)
48
+ t.Run("TestGetSeek", tp.TestGetSeek)
49
}
50
51
// `echo -n 'hello, world!' | ipfs add`
@@ -934,5 +937,107 @@ func (tp *provider) TestAddCloses(t *testing.T) {
937
t.Errorf("dir %d not closed!", i)
938
}
939
}
940
+}
941
+
942
+func (tp *provider) TestGetSeek(t *testing.T) {
943
+ ctx, cancel := context.WithCancel(context.Background())
944
+ defer cancel()
945
+ api, err := tp.makeAPI(ctx)
946
+ if err != nil {
947
+ t.Error(err)
948
+ }
949
+
950
+ dataSize := int64(100000)
951
+ tf := files.NewReaderFile(io.LimitReader(rand.New(rand.NewSource(1403768328)), dataSize))
952
+
953
+ p, err := api.Unixfs().Add(ctx, tf, options.Unixfs.Chunker("size-100"))
954
+ if err != nil {
955
+ t.Fatal(err)
956
+ }
957
+
958
+ r, err := api.Unixfs().Get(ctx, p)
959
+ if err != nil {
960
+ t.Fatal(err)
961
+ }
962
+
963
+ f := files.ToFile(r)
964
+ if f == nil {
965
+ t.Fatal("not a file")
966
+ }
967
+
968
+ orig := make([]byte, dataSize)
969
+ if _, err := f.Read(orig); err != nil {
970
+ t.Fatal(err)
971
+ }
972
+ f.Close()
973
+
974
+ origR := bytes.NewReader(orig)
975
+
976
+ r, err = api.Unixfs().Get(ctx, p)
977
+ if err != nil {
978
+ t.Fatal(err)
979
+ }
980
+
981
+ f = files.ToFile(r)
982
+ if f == nil {
983
+ t.Fatal("not a file")
984
+ }
985
+
986
+ test := func(offset int64, whence int, read int, expect int64, shouldEof bool) {
987
+ t.Run(fmt.Sprintf("seek%d+%d-r%d-%d", whence, offset, read, expect), func(t *testing.T) {
988
+ n, err := f.Seek(offset, whence)
989
+ if err != nil {
990
+ t.Fatal(err)
991
+ }
992
+ origN, err := origR.Seek(offset, whence)
993
+ if err != nil {
994
+ t.Fatal(err)
995
+ }
996
+
997
+ if n != origN {
998
+ t.Fatalf("offsets didn't match, expected %d, got %d", origN, n)
999
+ }
1000
+
1001
+ buf := make([]byte, read)
1002
+ origBuf := make([]byte, read)
1003
+ origRead, err := origR.Read(origBuf)
1004
+ if err != nil {
1005
+ t.Fatalf("orig: %s", err)
1006
+ }
1007
+ r, err := f.Read(buf)
1008
+ switch {
1009
+ case shouldEof && err != nil && err != io.EOF:
1010
+ fallthrough
1011
+ case !shouldEof && err != nil:
1012
+ t.Fatalf("f: %s", err)
1013
+ case shouldEof:
1014
+ _, err := f.Read([]byte{0})
1015
+ if err != io.EOF {
1016
+ t.Fatal("expected EOF")
1017
+ }
1018
+ _, err = origR.Read([]byte{0})
1019
+ if err != io.EOF {
1020
+ t.Fatal("expected EOF (orig)")
1021
+ }
1022
+ }
1023
+
1024
+ if int64(r) != expect {
1025
+ t.Fatal("read wrong amount of data")
1026
+ }
1027
+ if r != origRead {
1028
+ t.Fatal("read different amount of data than bytes.Reader")
1029
+ }
1030
+ if !bytes.Equal(buf, origBuf) {
1031
+ t.Fatal("data didn't match")
1032
+ }
1033
+ })
1034
+ }
1035
1036
+ test(3, io.SeekCurrent, 10, 10, false)
1037
+ test(3, io.SeekCurrent, 10, 10, false)
1038
+ test(500, io.SeekCurrent, 10, 10, false)
1039
+ test(350, io.SeekStart, 100, 100, false)
1040
+ test(-123, io.SeekCurrent, 100, 100, false)
1041
+ test(dataSize-50, io.SeekStart, 100, 50, true)
1042
+ test(-5, io.SeekEnd, 100, 5, true)
1043
}