Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jacobsa-fuse
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
jacobsa-fuse
Commits
40b51632
Commit
40b51632
authored
Dec 19, 2016
by
Aaron Jacobs
Browse files
Options
Browse Files
Download
Plain Diff
buffer: expand test coverage.
In preparation for changing the public API.
parents
102b1184
06b9c19b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
171 additions
and
13 deletions
+171
-13
internal/buffer/out_message_test.go
internal/buffer/out_message_test.go
+171
-13
No files found.
internal/buffer/out_message_test.go
View file @
40b51632
package
buffer
package
buffer
import
(
import
(
"bytes"
"crypto/rand"
"crypto/rand"
"fmt"
"fmt"
"io"
"io"
"reflect"
"reflect"
"testing"
"testing"
"unsafe"
"unsafe"
"github.com/jacobsa/fuse/internal/fusekernel"
"github.com/kylelemons/godebug/pretty"
)
)
func
toByteSlice
(
p
unsafe
.
Pointer
,
n
int
)
[]
byte
{
func
toByteSlice
(
p
unsafe
.
Pointer
,
n
int
)
[]
byte
{
...
@@ -86,18 +90,154 @@ func TestMemclr(t *testing.T) {
...
@@ -86,18 +90,154 @@ func TestMemclr(t *testing.T) {
}
}
}
}
func
TestOutMessageAppend
(
t
*
testing
.
T
)
{
var
om
OutMessage
om
.
Reset
()
// Append some payload.
const
wantPayloadStr
=
"tacoburrito"
wantPayload
:=
[]
byte
(
wantPayloadStr
)
om
.
Append
(
wantPayload
[
:
4
])
om
.
Append
(
wantPayload
[
4
:
])
// The result should be a zeroed header followed by the desired payload.
const
wantLen
=
int
(
OutMessageInitialSize
)
+
len
(
wantPayloadStr
)
if
got
,
want
:=
om
.
Len
(),
wantLen
;
got
!=
want
{
t
.
Errorf
(
"om.Len() = %d, want %d"
,
got
,
want
)
}
b
:=
om
.
Bytes
()
if
got
,
want
:=
len
(
b
),
wantLen
;
got
!=
want
{
t
.
Fatalf
(
"len(om.Bytes()) = %d, want %d"
,
got
,
want
)
}
want
:=
append
(
make
([]
byte
,
OutMessageInitialSize
),
wantPayload
...
)
if
!
bytes
.
Equal
(
b
,
want
)
{
t
.
Error
(
"messages differ"
)
}
}
func
TestOutMessageAppendString
(
t
*
testing
.
T
)
{
var
om
OutMessage
om
.
Reset
()
// Append some payload.
const
wantPayload
=
"tacoburrito"
om
.
AppendString
(
wantPayload
[
:
4
])
om
.
AppendString
(
wantPayload
[
4
:
])
// The result should be a zeroed header followed by the desired payload.
const
wantLen
=
int
(
OutMessageInitialSize
)
+
len
(
wantPayload
)
if
got
,
want
:=
om
.
Len
(),
wantLen
;
got
!=
want
{
t
.
Errorf
(
"om.Len() = %d, want %d"
,
got
,
want
)
}
b
:=
om
.
Bytes
()
if
got
,
want
:=
len
(
b
),
wantLen
;
got
!=
want
{
t
.
Fatalf
(
"len(om.Bytes()) = %d, want %d"
,
got
,
want
)
}
want
:=
append
(
make
([]
byte
,
OutMessageInitialSize
),
wantPayload
...
)
if
!
bytes
.
Equal
(
b
,
want
)
{
t
.
Error
(
"messages differ"
)
}
}
func
TestOutMessageShrinkTo
(
t
*
testing
.
T
)
{
// Set up a buffer with some payload.
var
om
OutMessage
om
.
Reset
()
om
.
AppendString
(
"taco"
)
om
.
AppendString
(
"burrito"
)
// Shrink it.
om
.
ShrinkTo
(
OutMessageInitialSize
+
uintptr
(
len
(
"taco"
)))
// The result should be a zeroed header followed by "taco".
const
wantLen
=
int
(
OutMessageInitialSize
)
+
len
(
"taco"
)
if
got
,
want
:=
om
.
Len
(),
wantLen
;
got
!=
want
{
t
.
Errorf
(
"om.Len() = %d, want %d"
,
got
,
want
)
}
b
:=
om
.
Bytes
()
if
got
,
want
:=
len
(
b
),
wantLen
;
got
!=
want
{
t
.
Fatalf
(
"len(om.Bytes()) = %d, want %d"
,
got
,
want
)
}
want
:=
append
(
make
([]
byte
,
OutMessageInitialSize
),
"taco"
...
)
if
!
bytes
.
Equal
(
b
,
want
)
{
t
.
Error
(
"messages differ"
)
}
}
func
TestOutMessageHeader
(
t
*
testing
.
T
)
{
var
om
OutMessage
om
.
Reset
()
// Fill in the header.
want
:=
fusekernel
.
OutHeader
{
Len
:
0xdeadbeef
,
Error
:
-
31231917
,
Unique
:
0xcafebabeba5eba11
,
}
h
:=
om
.
OutHeader
()
if
h
==
nil
{
t
.
Fatal
(
"OutHeader returned nil"
)
}
*
h
=
want
// Check that the result is as expected.
b
:=
om
.
Bytes
()
if
len
(
b
)
!=
int
(
unsafe
.
Sizeof
(
want
))
{
t
.
Fatalf
(
"unexpected length %d; want %d"
,
len
(
b
),
unsafe
.
Sizeof
(
want
))
}
got
:=
*
(
*
fusekernel
.
OutHeader
)(
unsafe
.
Pointer
(
&
b
[
0
]))
if
diff
:=
pretty
.
Compare
(
got
,
want
);
diff
!=
""
{
t
.
Errorf
(
"diff -got +want:
\n
%s"
,
diff
)
}
}
func
TestOutMessageReset
(
t
*
testing
.
T
)
{
func
TestOutMessageReset
(
t
*
testing
.
T
)
{
var
om
OutMessage
var
om
OutMessage
h
:=
om
.
OutHeader
()
h
:=
om
.
OutHeader
()
const
trials
=
10
0
const
trials
=
10
for
i
:=
0
;
i
<
trials
;
i
++
{
for
i
:=
0
;
i
<
trials
;
i
++
{
// Fill the header with garbage.
err
:=
fillWithGarbage
(
unsafe
.
Pointer
(
h
),
int
(
unsafe
.
Sizeof
(
*
h
)))
err
:=
fillWithGarbage
(
unsafe
.
Pointer
(
h
),
int
(
unsafe
.
Sizeof
(
*
h
)))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"fillWithGarbage: %v"
,
err
)
t
.
Fatalf
(
"fillWithGarbage: %v"
,
err
)
}
}
// Ensure a non-zero payload length.
if
p
:=
om
.
GrowNoZero
(
128
);
p
==
nil
{
t
.
Fatal
(
"GrowNoZero failed"
)
}
// Reset.
om
.
Reset
()
om
.
Reset
()
// Check that the length was updated.
if
got
,
want
:=
int
(
om
.
Len
()),
int
(
OutMessageInitialSize
);
got
!=
want
{
t
.
Fatalf
(
"om.Len() = %d, want %d"
,
got
,
want
)
}
// Check that the header was zeroed.
if
h
.
Len
!=
0
{
if
h
.
Len
!=
0
{
t
.
Fatalf
(
"non-zero Len %v"
,
h
.
Len
)
t
.
Fatalf
(
"non-zero Len %v"
,
h
.
Len
)
}
}
...
@@ -114,26 +254,44 @@ func TestOutMessageReset(t *testing.T) {
...
@@ -114,26 +254,44 @@ func TestOutMessageReset(t *testing.T) {
func
TestOutMessageGrow
(
t
*
testing
.
T
)
{
func
TestOutMessageGrow
(
t
*
testing
.
T
)
{
var
om
OutMessage
var
om
OutMessage
om
.
Reset
()
// Set up garbage where the payload will soon be.
const
payloadSize
=
1234
{
p
:=
om
.
GrowNoZero
(
payloadSize
)
if
p
==
nil
{
t
.
Fatal
(
"GrowNoZero failed"
)
}
// Overwrite with garbage.
err
:=
fillWithGarbage
(
p
,
payloadSize
)
err
:=
fillWithGarbage
(
unsafe
.
Pointer
(
&
om
),
int
(
unsafe
.
Sizeof
(
om
)))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"fillWithGarbage: %v"
,
err
)
t
.
Fatalf
(
"fillWithGarbage: %v"
,
err
)
}
om
.
ShrinkTo
(
OutMessageInitialSize
)
}
}
// Zero the header.
// Call Grow.
om
.
Reset
()
if
p
:=
om
.
Grow
(
payloadSize
);
p
==
nil
{
t
.
Fatal
(
"Grow failed"
)
}
// Grow to the max size. This should zero the message.
// Check the resulting length in two ways.
if
p
:=
om
.
Grow
(
MaxReadSize
);
p
==
nil
{
const
wantLen
=
int
(
payloadSize
+
OutMessageInitialSize
)
t
.
Fatal
(
"Grow returned nil"
)
if
got
,
want
:=
om
.
Len
(),
wantLen
;
got
!=
want
{
t
.
Errorf
(
"om.Len() = %d, want %d"
,
got
)
}
}
// Check that everything has been zeroed.
b
:=
om
.
Bytes
()
b
:=
om
.
Bytes
()
for
i
,
x
:=
range
b
{
if
got
,
want
:=
len
(
b
),
wantLen
;
got
!=
want
{
t
.
Fatalf
(
"len(om.Len()) = %d, want %d"
,
got
)
}
// Check that the payload was zeroed.
for
i
,
x
:=
range
b
[
OutMessageInitialSize
:
]
{
if
x
!=
0
{
if
x
!=
0
{
t
.
Fatalf
(
"non-zero byte 0x%02x at offset %d"
,
x
,
i
)
t
.
Fatalf
(
"non-zero byte 0x%02x at
payload
offset %d"
,
x
,
i
)
}
}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment