forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hexbytes_test.go
83 lines (66 loc) · 1.4 KB
/
hexbytes_test.go
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
//
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"io"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewHexByteReader(t *testing.T) {
assert := assert.New(t)
file := "/tmp/foo.txt"
r := NewHexByteReader(file)
assert.Equal(r.file, file)
assert.Nil(r.f)
}
func TestNewHexByteReaderStdin(t *testing.T) {
assert := assert.New(t)
file := "-"
r := NewHexByteReader(file)
assert.Equal(r.file, file)
assert.Equal(r.f, os.Stdin)
}
func TestHexByteReaderRead(t *testing.T) {
assert := assert.New(t)
dir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
defer func() {
err = os.RemoveAll(dir)
assert.NoError(err)
}()
type testData struct {
contents string
result string
expectError bool
}
data := []testData{
{"", "", true},
// Valid
{" ", " ", false},
{"hello world", "hello world", false},
{`\x00`, `\\x00`, false},
{`\x00\x01`, `\\x00\\x01`, false},
}
for i, d := range data {
file := filepath.Join(dir, "file.log")
err := createFile(file, d.contents)
assert.NoError(err)
reader := NewHexByteReader(file)
bytes, err := io.ReadAll(reader)
if d.expectError {
assert.Errorf(err, "test[%d]: %+v", i, d)
} else {
assert.NoErrorf(err, "test[%d]: %+v", i, d)
assert.Equal([]byte(d.result), bytes)
}
err = os.Remove(file)
assert.NoError(err)
}
}