File: zipfs_test.go

package info (click to toggle)
golang-github-hanwen-go-fuse 2.1.0%2Bgit20220822.58a7e14-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,292 kB
  • sloc: cpp: 78; sh: 43; makefile: 16
file content (113 lines) | stat: -rw-r--r-- 2,492 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2016 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package zipfs

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"runtime"
	"syscall"
	"testing"
	"time"

	"github.com/hanwen/go-fuse/v2/fs"
	"github.com/hanwen/go-fuse/v2/fuse"
	"github.com/hanwen/go-fuse/v2/internal/testutil"
)

func testZipFile() string {
	_, file, _, ok := runtime.Caller(0)
	if !ok {
		panic("need runtime.Caller()'s file name to discover testdata")
	}
	dir, _ := filepath.Split(file)
	return filepath.Join(dir, "test.zip")
}

func setupZipfs(t *testing.T) (mountPoint string, cleanup func()) {
	root, err := NewArchiveFileSystem(testZipFile())
	if err != nil {
		t.Fatalf("NewArchiveFileSystem failed: %v", err)
	}

	mountPoint = testutil.TempDir()
	opts := &fs.Options{}
	opts.Debug = testutil.VerboseTest()
	server, err := fs.Mount(mountPoint, root, opts)

	return mountPoint, func() {
		server.Unmount()
		os.RemoveAll(mountPoint)
	}
}

func TestZipFs(t *testing.T) {
	mountPoint, clean := setupZipfs(t)
	defer clean()
	entries, err := ioutil.ReadDir(mountPoint)
	if err != nil {
		t.Fatalf("ReadDir failed: %v", err)
	}

	if len(entries) != 2 {
		t.Error("wrong length", entries)
	}
	fi, err := os.Stat(mountPoint + "/subdir")
	if err != nil {
		t.Fatalf("Stat failed: %v", err)
	}
	if !fi.IsDir() {
		t.Error("directory type", fi)
	}

	fi, err = os.Stat(mountPoint + "/file.txt")
	if err != nil {
		t.Fatalf("Stat failed: %v", err)
	}
	if got, want := fi.Mode(), 0664; int(got) != want {
		t.Fatalf("File mode: got 0%o want 0%o", got, want)
	}
	if st := fi.Sys().(*syscall.Stat_t); st.Blocks != 1 {
		t.Errorf("got block count %d, want 1", st.Blocks)
	}

	if want, err := time.Parse(time.RFC3339, "2011-02-22T12:56:12Z"); err != nil {
		panic(err)
	} else if !fi.ModTime().Equal(want) {
		t.Fatalf("File mtime got %v, want %v", fi.ModTime(), want)
	}

	if fi.IsDir() {
		t.Error("file type", fi)
	}

	f, err := os.Open(mountPoint + "/file.txt")
	if err != nil {
		t.Fatalf("Open failed: %v", err)
	}

	b := make([]byte, 1024)
	n, err := f.Read(b)

	b = b[:n]
	if string(b) != "hello\n" {
		t.Error("content fail", b[:n])
	}
	f.Close()
}

func TestLinkCount(t *testing.T) {
	mp, clean := setupZipfs(t)
	defer clean()

	fi, err := os.Stat(mp + "/file.txt")
	if err != nil {
		t.Fatalf("Stat failed: %v", err)
	}
	if fuse.ToStatT(fi).Nlink != 1 {
		t.Fatal("wrong link count", fuse.ToStatT(fi).Nlink)
	}
}