commit cac539e010e709f24bcd43c790387dfd34e59488
parent f285e8682be36f29eae75e2671cda5efbb984083
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Fri,  5 Jan 2024 15:03:59 +0900
add client.TestFileRead
Diffstat:
| M | client/file_test.go | | | 126 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------ | 
1 file changed, 88 insertions(+), 38 deletions(-)
diff --git a/client/file_test.go b/client/file_test.go
@@ -1,6 +1,7 @@
 package client
 
 import (
+	"bytes"
 	"context"
 	"fmt"
 	"io"
@@ -13,6 +14,7 @@ import (
 	"git.mtkn.jp/lib9p"
 )
 
+// Mount runs a server with fs and mounts it as *FS.
 // cancel is to stop the server.
 func mount(fs lib9p.FS) (cfs *FS, cancel context.CancelFunc, err error) {
 	cr, sw := io.Pipe()
@@ -56,6 +58,92 @@ func TestFileStat(t *testing.T) {
 	}
 }
 
+// TestClose checks it *File.Close calls are transmitted to the server and
+// *testFile.Close is called for each *File.Close.
+func TestClose(t *testing.T) {
+	cfs, cancel, err := mount(testfs)
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer cancel()
+	defer cfs.Unmount()
+	var walk = func(path string, d fs.DirEntry, err error) error {
+		if err != nil {
+			if err == io.EOF {
+				return nil
+			}
+			return err
+		}
+		tf, err := testfs.walkPath(path)
+		if err != nil {
+			t.Error(err)
+			return nil
+		}
+		cf, err := cfs.OpenFile(path, lib9p.OREAD)
+		if err != nil {
+			if strings.Contains(err.Error(), "permission") {
+				return nil
+			}
+			return err
+		}
+		cc := make(chan struct{})
+		tf.closec = cc
+		cf.Close()
+		<-cc
+		return nil
+	}
+	if err := fs.WalkDir(lib9p.ExportFS{cfs}, ".", walk); err != nil {
+		t.Error(err)
+	}
+}
+
+// TestFileRead checks *File.Read reads the same []byte as *testFile.content.
+func TestFileRead(t *testing.T) {
+	cfs, cancel, err := mount(testfs)
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer cancel()
+	defer cfs.Unmount()
+	var walk = func(path string, d fs.DirEntry, err error) error {
+		if err != nil {
+			if err == io.EOF {
+				return nil
+			}
+			return err
+		}
+		if d.IsDir() {
+			return nil
+		}
+		tf, err := testfs.walkPath(path)
+		if err != nil {
+			t.Error(err)
+			return nil
+		}
+		cf, err := cfs.OpenFile(path, lib9p.OREAD)
+		if err != nil {
+			if strings.Contains(err.Error(), "permission") {
+				return nil
+			}
+			return err
+		}
+		defer cf.Close()
+		cb := make([]byte, len(tf.content) * 2)
+		n, err := cf.Read(cb)
+		if err != nil {
+			t.Error(err)
+			return nil
+		}
+		if n != len(tf.content) || !bytes.Equal(cb[:n], tf.content) {
+			t.Errorf("read wrong content: want: %v, got: %v", tf.content, cb[:n])
+			return nil
+		}
+		return nil
+	}
+	if err := fs.WalkDir(lib9p.ExportFS{cfs}, ".", walk); err != nil {
+		t.Error(err)
+	}
+}
 
 // TestReadDir tests whether ReadDir returns the same dir entries as testfs.Fsys
 // has.
@@ -109,40 +197,3 @@ L:
 		t.Errorf("file name not in the testfs: %s", f.Name())
 	}
 }
-
-func TestClose(t *testing.T) {
-	cfs, cancel, err := mount(testfs)
-	if err != nil {
-		t.Fatal(err)
-	}
-	defer cancel()
-	defer cfs.Unmount()
-	var walk = func(path string, d fs.DirEntry, err error) error {
-		if err != nil {
-			if err == io.EOF {
-				return nil
-			}
-			return err
-		}
-		tf, err := testfs.walkPath(path)
-		if err != nil {
-			t.Error(err)
-			return nil
-		}
-		cf, err := cfs.OpenFile(path, lib9p.OREAD)
-		if err != nil {
-			if strings.Contains(err.Error(), "permission") {
-				return nil
-			}
-			return err
-		}
-		cc := make(chan struct{})
-		tf.closec = cc
-		cf.Close()
-		<-cc
-		return nil
-	}
-	if err := fs.WalkDir(lib9p.ExportFS{cfs}, ".", walk); err != nil {
-		t.Error(err)
-	}
-}
-\ No newline at end of file