-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbytesutil_test.go
153 lines (127 loc) · 3.81 KB
/
bytesutil_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
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
145
146
147
148
149
150
151
152
153
package bytesutil
import (
"bytes"
"encoding/base64"
"testing"
)
// TestStruct is a simple structure used for testing gob encoding and decoding.
type TestStruct struct {
Name string
Age int
}
func TestRandom(t *testing.T) {
// Test generating random bytes
byteLen := 16
randomBytes, err := Random(byteLen)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if len(randomBytes) != byteLen {
t.Fatalf("expected random byte slice of length %d, got %d", byteLen, len(randomBytes))
}
// Test randomness by generating another set and comparing
anotherRandomBytes, err := Random(byteLen)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if bytes.Equal(randomBytes, anotherRandomBytes) {
t.Fatalf("expected different random byte slices, got identical slices")
}
}
func TestFromBase64(t *testing.T) {
// Test decoding a valid base64 string
originalBytes := []byte("test message")
base64Str := base64.StdEncoding.EncodeToString(originalBytes)
decodedBytes, err := FromBase64(base64Str)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if !bytes.Equal(decodedBytes, originalBytes) {
t.Fatalf("expected decoded bytes to match original bytes")
}
// Test decoding an invalid base64 string
_, err = FromBase64("invalid base64")
if err == nil {
t.Fatalf("expected error for invalid base64 string, got nil")
}
}
func TestToBase64(t *testing.T) {
// Test encoding bytes to base64
originalBytes := []byte("test message")
base64Str := ToBase64(originalBytes)
expectedBase64Str := base64.StdEncoding.EncodeToString(originalBytes)
if base64Str != expectedBase64Str {
t.Fatalf("expected base64-encoded string to be %s, got %s", expectedBase64Str, base64Str)
}
}
func TestToGob(t *testing.T) {
// Test encoding a struct to gob
originalStruct := TestStruct{Name: "John", Age: 30}
gobBytes, err := ToGob(originalStruct)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if len(gobBytes) == 0 {
t.Fatalf("expected gob-encoded byte slice to be non-empty")
}
}
func TestFromGobInto(t *testing.T) {
// Test decoding a gob-encoded byte slice into a struct
originalStruct := TestStruct{Name: "John", Age: 30}
gobBytes, err := ToGob(originalStruct)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
var decodedStruct TestStruct
err = FromGobInto(gobBytes, &decodedStruct)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if originalStruct != decodedStruct {
t.Fatalf("expected decoded struct to match original struct")
}
// Test decoding a nil byte slice
err = FromGobInto(nil, &decodedStruct)
if err == nil {
t.Fatalf("expected error for nil gobBytes, got nil")
}
// Test decoding into a nil destination
err = FromGobInto(gobBytes, nil)
if err == nil {
t.Fatalf("expected error for nil destination, got nil")
}
}
func TestEdgeCases(t *testing.T) {
// Test Random with a byte length of 0
zeroLengthBytes, err := Random(0)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if len(zeroLengthBytes) != 0 {
t.Fatalf("expected empty byte slice, got %d bytes", len(zeroLengthBytes))
}
// Test FromBase64 with an empty string
emptyBytes, err := FromBase64("")
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if len(emptyBytes) != 0 {
t.Fatalf("expected empty byte slice, got %d bytes", len(emptyBytes))
}
// Test ToBase64 with an empty byte slice
emptyBase64Str := ToBase64([]byte{})
if emptyBase64Str != "" {
t.Fatalf("expected empty base64 string, got %s", emptyBase64Str)
}
// Test ToGob with a nil source
_, err = ToGob(nil)
if err == nil {
t.Fatalf("expected error for nil source, got nil")
}
// Test FromGobInto with empty gob bytes
var decodedStruct TestStruct
err = FromGobInto([]byte{}, &decodedStruct)
if err == nil {
t.Fatalf("expected error for empty gob bytes, got nil")
}
}