lib9p

Go 9P library.
Log | Files | Refs | LICENSE

fs2_test.go (1736B)


      1 package client
      2 
      3 import (
      4 	"context"
      5 	"io/fs"
      6 	"reflect"
      7 	"strings"
      8 	"testing"
      9 
     10 	"git.mtkn.jp/lib9p"
     11 )
     12 
     13 func TestCleanPath(t *testing.T) {
     14 	tests := []struct {
     15 		name string
     16 		want string
     17 	}{
     18 		{"../../../hoge", "hoge"},
     19 		{"..", "."},
     20 		{".", "."},
     21 		{"/", "."},
     22 		{"a", "a"},
     23 		{"a/b/", "a/b"},
     24 		{".../", "..."},
     25 		{"../a/../b/../c/../d/", "d"},
     26 	}
     27 	for _, test := range tests {
     28 		got := CleanPath(test.name)
     29 		if got != test.want {
     30 			t.Errorf("CleanPath(%q) = %q, want: %q", test.name, got, test.want)
     31 		}
     32 	}
     33 }
     34 
     35 // TestOpenFile checks if *FS.OpenFile opens every file in testfs without error
     36 // other than permission error and
     37 // every opened file is the same as that of testfs by comparing their lib9p.Stat.
     38 func TestOpenFile(t *testing.T) {
     39 	ctx, cancel := context.WithCancel(context.Background())
     40 	cfs, err := mount(ctx, testfs)
     41 	if err != nil {
     42 		t.Fatal(err)
     43 	}
     44 	defer cancel()
     45 	walk := func(path string, d fs.DirEntry, err error) error {
     46 		cf, err := cfs.OpenFile(path, lib9p.O_RDONLY)
     47 		if err != nil {
     48 			if !strings.Contains(err.Error(), "permission") {
     49 				t.Error(err)
     50 			}
     51 			return nil
     52 		}
     53 		cfi, err := cf.Stat()
     54 		if err != nil {
     55 			t.Error(err)
     56 			return err
     57 		}
     58 		cst := cfi.Sys().(*lib9p.Stat)
     59 		tf, err := testfs.OpenFile(path, lib9p.O_RDONLY)
     60 		if err != nil {
     61 			if !strings.Contains(err.Error(), "permission") {
     62 				t.Error(err)
     63 			}
     64 			return nil
     65 		}
     66 		tfi, err := tf.Stat()
     67 		if err != nil {
     68 			t.Error(err)
     69 			return err
     70 		}
     71 		tst := tfi.Sys().(*lib9p.Stat)
     72 		if !reflect.DeepEqual(cst, tst) {
     73 			t.Errorf("%s: stat does not match:\n\twant: %v\n\tgot:  %v",
     74 				path, tst, cst)
     75 			return nil
     76 		}
     77 		return nil
     78 	}
     79 	if err := fs.WalkDir(lib9p.ExportFS{FS: testfs}, ".", walk); err != nil {
     80 		t.Error(err)
     81 	}
     82 }