commit 904e1888746017b8082fe78eaab0675622cf85b8
parent 1f5ac72c069e3c3b21093b5ed388eba41978c072
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sat, 20 Jan 2024 09:10:11 +0900
add tests for client.TestFileWrite
Diffstat:
1 file changed, 37 insertions(+), 5 deletions(-)
diff --git a/client/file_test.go b/client/file_test.go
@@ -145,6 +145,15 @@ func TestFileRead(t *testing.T) {
 }
 
 func TestFileWrite(t *testing.T) {
+	tests := []struct{
+		b []byte
+	}{
+		{[]byte("")},
+		{[]byte("fuga")},
+		{[]byte("fugafuga")},
+		{[]byte("fugafugafuga")},
+		{make([]byte, 9000)},
+	}
 	ctx, cancel := context.WithCancel(context.Background())
 	cfs, err := mount(ctx, testfs)
 	if err != nil {
@@ -156,20 +165,43 @@ func TestFileWrite(t *testing.T) {
 		t.Fatal(err)
 	}
 	orig := bytes.Clone(tf.content)
+	defer func() { tf.content = bytes.Clone(orig) }()
+	for i, test := range tests {
+		cf, err := cfs.OpenFile("dir/file", lib9p.O_RDWR)
+		if err != nil {
+			t.Fatal(err)
+		}
+		_, err = cf.(lib9p.WriterFile).Write(test.b)
+		if err != nil {
+			t.Fatal(err)
+		}
+		if !bytes.Equal(tf.content[:len(test.b)], test.b) {
+			t.Errorf("%d: not written propperly: want: %v, got: %v",
+				i, string(test.b), string(tf.content[:len(test.b)]))
+		}
+		cf.Close()
+		tf.content = bytes.Clone(orig)
+	}
+	// consecutive writes to the same file without closing it.
+	ctnt0 := []byte("hogehoge")
+	ctnt1 := []byte("fugafuga")
 	cf, err := cfs.OpenFile("dir/file", lib9p.O_RDWR)
 	if err != nil {
 		t.Fatal(err)
 	}
-	ctnt := []byte("fuga")
-	_, err = cf.(lib9p.WriterFile).Write(ctnt)
+	_, err = cf.(lib9p.WriterFile).Write(ctnt0)
+	if err != nil {
+		t.Fatal(err)
+	}
+	_, err = cf.(lib9p.WriterFile).Write(ctnt1)
 	if err != nil {
 		t.Fatal(err)
 	}
-	if !bytes.Equal(tf.content[:len(ctnt)], ctnt) {
+	if !bytes.Equal(tf.content[:len(ctnt0)+len(ctnt1)], bytes.Join([][]byte{ctnt0, ctnt1}, nil)) {
 		t.Errorf("not written propperly: want: %v, got: %v",
-			string(ctnt), string(tf.content[:len(ctnt)]))
+			string(bytes.Join([][]byte{ctnt0, ctnt1}, nil)),
+			string(tf.content[:len(ctnt0)+len(ctnt1)]))
 	}
-	tf.content = orig
 }
 
 // TestReadDir tests whether ReadDir returns the same dir entries as testfs