github.com/hanwen/go-fuse@v1.0.0/internal/testutil/helpers.go (about)

     1  // Copyright 2018 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 testutil
     6  
     7  import (
     8  	"syscall"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/hanwen/go-fuse/fuse"
    13  )
    14  
    15  // Check that loopback Utimens() works as expected.
    16  // Called by TestLoopbackFileUtimens and TestLoopbackFileSystemUtimens.
    17  //
    18  // Parameters:
    19  //   path ........ path to the backing file
    20  //   utimensFn ... Utimens() function that acts on the backing file
    21  func TestLoopbackUtimens(t *testing.T, path string, utimensFn func(atime *time.Time, mtime *time.Time) fuse.Status) {
    22  	// Arbitrary date: 05/02/2018 @ 7:57pm (UTC)
    23  	t0sec := int64(1525291058)
    24  
    25  	// Read original timestamp
    26  	var st syscall.Stat_t
    27  	err := syscall.Stat(path, &st)
    28  	if err != nil {
    29  		t.Fatal("Stat", err)
    30  	}
    31  	// FromStat handles the differently-named Stat_t fields on Linux and
    32  	// Darwin
    33  	var a1 fuse.Attr
    34  	a1.FromStat(&st)
    35  
    36  	// Change atime, keep mtime
    37  	t0 := time.Unix(t0sec, 0)
    38  	status := utimensFn(&t0, nil)
    39  	if !status.Ok() {
    40  		t.Fatal("utimensFn", status)
    41  	}
    42  	err = syscall.Stat(path, &st)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	var a2 fuse.Attr
    47  	a2.FromStat(&st)
    48  	if a1.Mtime != a2.Mtime {
    49  		t.Errorf("mtime has changed: %v -> %v", a1.Mtime, a2.Mtime)
    50  	}
    51  	if a2.Atime != uint64(t0.Unix()) {
    52  		t.Errorf("wrong atime: got %v want %v", a2.Atime, t0.Unix())
    53  	}
    54  
    55  	// Change mtime, keep atime
    56  	t1 := time.Unix(t0sec+123, 0)
    57  	status = utimensFn(nil, &t1)
    58  	if !status.Ok() {
    59  		t.Fatal("utimensFn", status)
    60  	}
    61  	err = syscall.Stat(path, &st)
    62  	if err != nil {
    63  		t.Fatal("Stat", err)
    64  	}
    65  	var a3 fuse.Attr
    66  	a3.FromStat(&st)
    67  	if a2.Atime != a3.Atime {
    68  		t.Errorf("atime has changed: %v -> %v", a2.Atime, a3.Atime)
    69  	}
    70  	if a3.Mtime != uint64(t1.Unix()) {
    71  		t.Errorf("got mtime %v, want %v", a3.Mtime, t1.Unix())
    72  	}
    73  
    74  	// Change both mtime and atime
    75  	ta := time.Unix(t0sec+456, 0)
    76  	tm := time.Unix(t0sec+789, 0)
    77  	status = utimensFn(&ta, &tm)
    78  	if !status.Ok() {
    79  		t.Fatal("utimensFn", status)
    80  	}
    81  	err = syscall.Stat(path, &st)
    82  	if err != nil {
    83  		t.Fatal("Stat", err)
    84  	}
    85  	var a4 fuse.Attr
    86  	a4.FromStat(&st)
    87  	if a4.Atime != uint64(ta.Unix()) {
    88  		t.Errorf("got atime %v, want %v", a4.Atime, ta.Unix())
    89  	}
    90  	if a4.Mtime != uint64(tm.Unix()) {
    91  		t.Errorf("got mtime %v, want %v", a4.Mtime, tm.Unix())
    92  	}
    93  }