@@ -6,9 +6,11 @@ package os_test
66
77import (
88 "io"
9+ "io/ioutil"
910 . "os"
1011 "runtime"
1112 "strings"
13+ "syscall"
1214 "testing"
1315)
1416
@@ -20,7 +22,7 @@ func localTmp() string {
2022func newFile (testName string , t * testing.T ) (f * File ) {
2123 f , err := CreateTemp ("" , testName )
2224 if err != nil {
23- t .Fatalf ("TempFile %s: %s" , testName , err )
25+ t .Fatalf ("newFile %s: CreateTemp fails with %s" , testName , err )
2426 }
2527 return
2628}
@@ -86,6 +88,54 @@ func checkMode(t *testing.T, path string, mode FileMode) {
8688 }
8789}
8890
91+ func TestSeek (t * testing.T ) {
92+ f := newFile ("TestSeek" , t )
93+ if f == nil {
94+ t .Fatalf ("f is nil" )
95+ return // TODO: remove
96+ }
97+ defer Remove (f .Name ())
98+ defer f .Close ()
99+
100+ const data = "hello, world\n "
101+ io .WriteString (f , data )
102+
103+ type test struct {
104+ in int64
105+ whence int
106+ out int64
107+ }
108+ var tests = []test {
109+ {0 , io .SeekCurrent , int64 (len (data ))},
110+ {0 , io .SeekStart , 0 },
111+ {5 , io .SeekStart , 5 },
112+ {0 , io .SeekEnd , int64 (len (data ))},
113+ {0 , io .SeekStart , 0 },
114+ {- 1 , io .SeekEnd , int64 (len (data )) - 1 },
115+ {1 << 33 , io .SeekStart , 1 << 33 },
116+ {1 << 33 , io .SeekEnd , 1 << 33 + int64 (len (data ))},
117+
118+ // Issue 21681, Windows 4G-1, etc:
119+ {1 << 32 - 1 , io .SeekStart , 1 << 32 - 1 },
120+ {0 , io .SeekCurrent , 1 << 32 - 1 },
121+ {2 << 32 - 1 , io .SeekStart , 2 << 32 - 1 },
122+ {0 , io .SeekCurrent , 2 << 32 - 1 },
123+ }
124+ for i , tt := range tests {
125+ off , err := f .Seek (tt .in , tt .whence )
126+ if off != tt .out || err != nil {
127+ if e , ok := err .(* PathError ); ok && e .Err == syscall .EINVAL && tt .out > 1 << 32 && runtime .GOOS == "linux" {
128+ mounts , _ := ioutil .ReadFile ("/proc/mounts" )
129+ if strings .Contains (string (mounts ), "reiserfs" ) {
130+ // Reiserfs rejects the big seeks.
131+ t .Skipf ("skipping test known to fail on reiserfs; https://golang.org/issue/91" )
132+ }
133+ }
134+ t .Errorf ("#%d: Seek(%v, %v) = %v, %v want %v, nil" , i , tt .in , tt .whence , off , err , tt .out )
135+ }
136+ }
137+ }
138+
89139func TestReadAt (t * testing.T ) {
90140 if runtime .GOOS == "windows" {
91141 t .Log ("TODO: implement Pread for Windows" )
0 commit comments