-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathconnection_test.go
More file actions
144 lines (136 loc) · 3.57 KB
/
Copy pathconnection_test.go
File metadata and controls
144 lines (136 loc) · 3.57 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2026 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fuse
import (
"strings"
"testing"
"github.com/jacobsa/fuse/internal/buffer"
)
func TestSanitizeMaxPagesAndWrite(t *testing.T) {
pageSize := uint32(buffer.GetPageSize())
defaultMaxWrite := uint32(buffer.MaxWriteSize)
defaultMaxPages := uint16(max(uint32(buffer.MaxReadSize), uint32(buffer.MaxWriteSize)) / pageSize)
tests := []struct {
name string
inputPages uint16
inputWrite uint32
wantPages uint16
wantWrite uint32
wantErr bool
errSubstr string
}{
{
name: "neither given",
inputPages: 0,
inputWrite: 0,
wantPages: defaultMaxPages,
wantWrite: defaultMaxWrite,
wantErr: false,
},
{
name: "only MaxWrite given (128KB)",
inputPages: 0,
inputWrite: 128 * 1024,
wantPages: defaultMaxPages, // max(128KB, 1MB) / 4KB
wantWrite: 128 * 1024,
wantErr: false,
},
{
name: "only MaxWrite given (not page aligned, 129KB)",
inputPages: 0,
inputWrite: 129 * 1024,
wantPages: defaultMaxPages, // max(129KB, 1MB) / 4KB
wantWrite: 129 * 1024,
wantErr: false,
},
{
name: "only MaxWrite given (larger than 1MB, e.g. 2MB)",
inputPages: 0,
inputWrite: 2 * 1024 * 1024,
wantPages: 512, // 2MB / 4KB
wantWrite: 2 * 1024 * 1024,
wantErr: false,
},
{
name: "only MaxPages given (valid, e.g. 256)",
inputPages: 256,
inputWrite: 0,
wantPages: 256,
wantWrite: defaultMaxWrite, // 256 * 4KB
wantErr: false,
},
{
name: "only MaxPages given (small, e.g. 32)",
inputPages: 32,
inputWrite: 0,
wantPages: 32,
wantWrite: 128 * 1024, // 32 * 4KB
wantErr: false,
},
{
name: "only MaxPages given (very small, e.g. 1)",
inputPages: 1,
inputWrite: 0,
wantPages: 1,
wantWrite: 4096, // 1 * 4KB
wantErr: false,
},
{
name: "both given (valid, e.g. MaxPages=32, MaxWrite=128KB)",
inputPages: 32,
inputWrite: 128 * 1024,
wantPages: 32,
wantWrite: 128 * 1024,
wantErr: false,
},
{
name: "both given (invalid, MaxPages=16, MaxWrite=128KB)",
inputPages: 16,
inputWrite: 128 * 1024,
wantPages: 16,
wantWrite: 128 * 1024,
wantErr: true,
errSubstr: "must be at least MaxWrite",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
c := &Connection{
cfg: MountConfig{
MaxPages: tc.inputPages,
MaxWrite: tc.inputWrite,
},
}
err := c.sanitizeMaxPagesAndWrite()
if tc.wantErr {
if err == nil {
t.Fatalf("expected error, got nil")
}
if !strings.Contains(err.Error(), tc.errSubstr) {
t.Errorf("expected error to contain %q, got: %v", tc.errSubstr, err)
}
} else {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if c.cfg.MaxPages != tc.wantPages {
t.Errorf("MaxPages = %d, want %d", c.cfg.MaxPages, tc.wantPages)
}
if c.cfg.MaxWrite != tc.wantWrite {
t.Errorf("MaxWrite = %d, want %d", c.cfg.MaxWrite, tc.wantWrite)
}
}
})
}
}