-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_test.go
188 lines (164 loc) · 5.64 KB
/
main_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
"os"
"path/filepath"
"reflect"
"testing"
)
func TestComputePrefixTable(t *testing.T) {
// Test case with needle: "abcabc"
needle := []byte("abcabc")
expectedPrefixTable := []int{0, 0, 0, 1, 2, 3}
result := computePrefixTable(needle)
if !reflect.DeepEqual(result, expectedPrefixTable) {
t.Errorf("computePrefixTable() returned incorrect result for needle 'abcabc'.\nExpected: %v\nGot: %v", expectedPrefixTable, result)
}
// Test case with needle: "aaa"
needle = []byte("aaa")
expectedPrefixTable = []int{0, 1, 2}
result = computePrefixTable(needle)
if !reflect.DeepEqual(result, expectedPrefixTable) {
t.Errorf("computePrefixTable() returned incorrect result for needle 'aaa'.\nExpected: %v\nGot: %v", expectedPrefixTable, result)
}
// Test case with needle: "abcdabcdabc"
needle = []byte("abcdabcdabc")
expectedPrefixTable = []int{0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7}
result = computePrefixTable(needle)
if !reflect.DeepEqual(result, expectedPrefixTable) {
t.Errorf("computePrefixTable() returned incorrect result for needle 'abcdabcdabc'.\nExpected: %v\nGot: %v", expectedPrefixTable, result)
}
}
func TestFindBytes(t *testing.T) {
originalBytes := []byte("0123456")
targetBytes := []byte("6")
// Test scenario where the targetBytes are present in the originalBytes
expectedIndex := 6
result := findBytes(originalBytes, targetBytes)
if result != expectedIndex {
t.Errorf("findBytes() did not return the correct index. Expected: %d, Got: %d", expectedIndex, result)
}
// Test scenario where the targetBytes are not present in the originalBytes
targetBytes = []byte("missing")
expectedIndex = -1
result = findBytes(originalBytes, targetBytes)
if result != expectedIndex {
t.Errorf("findBytes() did not return the correct index. Expected: %d, Got: %d", expectedIndex, result)
}
}
func TestPatchBinaryKMP(t *testing.T) {
dump := []byte("0123456789")
target := []byte("5678")
patch := []byte("0000")
expected := []byte("0123400009")
_ = patchBinaryKMP(dump, target, patch)
if string(dump) != string(expected) {
t.Errorf("patchBinaryKMP() did not return the correct result. Expected: %s, Got: %s", string(expected), dump)
}
}
func TestPatcherWrapper(t *testing.T) {
dump := []byte("0123456789")
target := []byte("5678")
patch := []byte("0000")
expected := []byte("0123400009")
_ = patcherWrapper(dump, target, patch)
if string(dump) != string(expected) {
t.Errorf("patchBinaryKMP() did not return the correct result. Expected: %s, Got: %s", string(expected), dump)
}
}
func TestIsBinaryPatched(t *testing.T) {
// Create a temporary file to test with
tmpfile, err := os.CreateTemp("./", "test_file")
if err != nil {
t.Fatalf("Failed to create temporary file: %v", err)
}
fmt.Print(tmpfile.Name())
defer func(name string) {
err := os.Remove(name)
if err != nil {
log.Fatalf("Failed to remove temporary file: %v", err)
}
}(tmpfile.Name())
// Write some content to the temporary file
content := []byte("This is some content with a pattern to be matched.")
if _, err := tmpfile.Write(content); err != nil {
t.Fatalf("Failed to write to temporary file: %v", err)
}
defer func(tmpfile *os.File) {
err := tmpfile.Close()
if err != nil {
log.Fatalf("Failed to close temporary file: %v", err)
}
}(tmpfile)
// Test scenario where the replacementBytes are not present in the file
replacementBytes := []byte("xyz")
result := isBinaryPatched(tmpfile.Name(), replacementBytes)
if result != false {
t.Errorf("isBinaryPatched() did not return the correct result. Expected: false, Got: %v", result)
}
// Test scenario where the replacementBytes are present in the file
replacementBytes = []byte("pattern")
result = isBinaryPatched(tmpfile.Name(), replacementBytes)
if result != true {
t.Errorf("isBinaryPatched() did not return the correct result. Expected: true, Got: %v", result)
}
}
func TestComputeFileHash(t *testing.T) {
// Create a temporary file to test with
tmpfile, err := os.CreateTemp("./", "test_file")
if err != nil {
t.Fatalf("Failed to create temporary file: %v", err)
}
defer func(name string) {
err := os.Remove(name)
if err != nil {
log.Fatalf("Failed to remove temporary file: %v", err)
}
}(tmpfile.Name())
// Write some content to the temporary file
content := []byte("This is some content to be hashed.")
if _, err := tmpfile.Write(content); err != nil {
t.Fatalf("Failed to write to temporary file: %v", err)
}
defer func(tmpfile *os.File) {
err := tmpfile.Close()
if err != nil {
log.Fatalf("Failed to close temporary file: %v", err)
}
}(tmpfile)
// Get the expected SHA-256 hash of the content
expectedHash := sha256.Sum256(content)
expectedHashString := hex.EncodeToString(expectedHash[:])
// Call the function under test
hash, err := computeFileHash(tmpfile.Name())
if err != nil {
t.Fatalf("computeFileHash() returned an error: %v", err)
}
// Verify if the computed hash matches the expected hash
if hash != expectedHashString {
t.Errorf("computeFileHash() returned incorrect hash. Expected: %s, Got: %s", expectedHashString, hash)
}
}
func TestGetFileVersion(t *testing.T) {
// Get the current working directory
currentWorkingDir, err := os.Getwd()
if err != nil {
t.Errorf("Error getting current working directory: %v", err)
return
}
// Assuming your file is in the current working directory
fileName := "./Norland.exe"
filePath := filepath.Join(currentWorkingDir, fileName)
fileVersion, err := GetFileVersion(filePath)
if err != nil {
t.Errorf("Error getting file version: %v", err)
return
}
// Add assertions as needed
if fileVersion == "" {
t.Errorf("File version should not be empty")
}
}