github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/util/fsutil/mode_test.go (about)

     1  package fsutil
     2  
     3  import (
     4  	"io/fs"
     5  	"testing"
     6  )
     7  
     8  func TestFileModeFromUnix(t *testing.T) {
     9  	tests := []struct {
    10  		mode fs.FileMode
    11  		want fs.FileMode
    12  	}{
    13  		{0010754, 0754 | fs.ModeNamedPipe},
    14  		{0020754, 0754 | fs.ModeCharDevice | fs.ModeDevice},
    15  		{0040754, 0754 | fs.ModeDir},
    16  		{0060754, 0754 | fs.ModeDevice},
    17  		{0100754, 0754},
    18  		{0120754, 0754 | fs.ModeSymlink},
    19  		{0140754, 0754 | fs.ModeSocket},
    20  		{0170754, 0754 | fs.ModeIrregular},
    21  	}
    22  	for _, tt := range tests {
    23  		t.Run(tt.mode.String(), func(t *testing.T) {
    24  			if got := FileModeFromUnix(tt.mode); got != tt.want {
    25  				t.Errorf("fixMode() = %o, want %o", got, tt.want)
    26  			}
    27  		})
    28  	}
    29  }
    30  
    31  func FuzzParseFileMode(f *testing.F) {
    32  	f.Add("---------")
    33  	f.Add("rwxrwxrwx")
    34  	f.Add("----------")
    35  	f.Add("-rwxrwxrwx")
    36  	f.Add("b")
    37  	f.Add("b---------")
    38  	f.Add("drwxrwxrwx")
    39  	f.Add("dalTLDpSugct?")
    40  	f.Add("dalTLDpSugct?---------")
    41  	f.Add("dalTLDpSugct?rwxrwxrwx")
    42  	f.Add("dalTLDpSugct?----------")
    43  
    44  	f.Fuzz(func(t *testing.T, str string) {
    45  		mode, err := ParseFileMode(str)
    46  		if err != nil {
    47  			return
    48  		}
    49  		got := mode.String()
    50  		if got != str {
    51  			t.Errorf("was %q, got %q (%o)", str, got, mode)
    52  		}
    53  	})
    54  }