github.com/hanwen/go-fuse@v1.0.0/fuse/nodefs/fileless_test.go (about)

     1  // Copyright 2016 the Go-FUSE Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package nodefs
     6  
     7  import (
     8  	"io/ioutil"
     9  	"testing"
    10  
    11  	"github.com/hanwen/go-fuse/fuse"
    12  	"github.com/hanwen/go-fuse/internal/testutil"
    13  )
    14  
    15  type nodeReadNode struct {
    16  	Node
    17  	dir    bool
    18  	noOpen bool
    19  	data   []byte
    20  }
    21  
    22  func newNodeReadNode(noOpen, dir bool, d []byte) *nodeReadNode {
    23  	return &nodeReadNode{
    24  		Node:   NewDefaultNode(),
    25  		noOpen: noOpen,
    26  		dir:    dir,
    27  		data:   d,
    28  	}
    29  }
    30  
    31  func (n *nodeReadNode) Open(flags uint32, context *fuse.Context) (file File, code fuse.Status) {
    32  	if n.noOpen {
    33  		return nil, fuse.ENOSYS
    34  	}
    35  	return nil, fuse.OK
    36  }
    37  
    38  func (n *nodeReadNode) Read(file File, dest []byte, off int64, context *fuse.Context) (fuse.ReadResult, fuse.Status) {
    39  	e := off + int64(len(dest))
    40  	if int(e) > len(n.data) {
    41  		e = int64(len(n.data))
    42  	}
    43  	return fuse.ReadResultData(n.data[off:int(e)]), fuse.OK
    44  }
    45  
    46  func (n *nodeReadNode) GetAttr(out *fuse.Attr, file File, context *fuse.Context) (code fuse.Status) {
    47  	if n.dir {
    48  		out.Mode = fuse.S_IFDIR | 0755
    49  	} else {
    50  		out.Mode = fuse.S_IFREG | 0644
    51  	}
    52  	out.Size = uint64(len(n.data))
    53  	return fuse.OK
    54  }
    55  
    56  func (n *nodeReadNode) Lookup(out *fuse.Attr, name string, context *fuse.Context) (*Inode, fuse.Status) {
    57  	ch := n.Inode().NewChild(name, false, newNodeReadNode(n.noOpen, false, []byte(name)))
    58  	return ch, ch.Node().GetAttr(out, nil, context)
    59  }
    60  
    61  func TestNoOpen(t *testing.T) {
    62  	dir, err := ioutil.TempDir("", "nodefs")
    63  	if err != nil {
    64  		t.Fatalf("TempDir: %v", err)
    65  	}
    66  
    67  	root := newNodeReadNode(true, true, nil)
    68  	root.noOpen = true
    69  
    70  	s, _, err := MountRoot(dir, root, &Options{Debug: testutil.VerboseTest()})
    71  	if err != nil {
    72  		t.Fatalf("MountRoot: %v", err)
    73  	}
    74  	defer s.Unmount()
    75  	go s.Serve()
    76  	if err := s.WaitMount(); err != nil {
    77  		t.Fatal("WaitMount", err)
    78  	}
    79  
    80  	if s.KernelSettings().Minor < 23 {
    81  		t.Skip("Kernel does not support open-less read/writes. Skipping test.")
    82  	}
    83  
    84  	content, err := ioutil.ReadFile(dir + "/file")
    85  	if err != nil {
    86  		t.Fatalf("ReadFile: %v", err)
    87  	}
    88  	want := "file"
    89  	if string(content) != want {
    90  		t.Fatalf("got %q, want %q", content, want)
    91  	}
    92  
    93  	content, err = ioutil.ReadFile(dir + "/file2")
    94  	if err != nil {
    95  		t.Fatalf("ReadFile: %v", err)
    96  	}
    97  
    98  	want = "file2"
    99  	if string(content) != want {
   100  		t.Fatalf("got %q, want %q", content, want)
   101  	}
   102  }
   103  
   104  func TestNodeRead(t *testing.T) {
   105  	dir, err := ioutil.TempDir("", "nodefs")
   106  	if err != nil {
   107  		t.Fatalf("TempDir: %v", err)
   108  	}
   109  
   110  	root := newNodeReadNode(false, true, nil)
   111  	opts := NewOptions()
   112  	opts.Debug = testutil.VerboseTest()
   113  	s, _, err := MountRoot(dir, root, opts)
   114  	if err != nil {
   115  		t.Fatalf("MountRoot: %v", err)
   116  	}
   117  	defer s.Unmount()
   118  	go s.Serve()
   119  	if err := s.WaitMount(); err != nil {
   120  		t.Fatal("WaitMount", err)
   121  	}
   122  	content, err := ioutil.ReadFile(dir + "/file")
   123  	if err != nil {
   124  		t.Fatalf("ReadFile: %v", err)
   125  	}
   126  	want := "file"
   127  	if string(content) != want {
   128  		t.Fatalf("got %q, want %q", content, want)
   129  	}
   130  }