-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils_test.go
70 lines (66 loc) · 1.68 KB
/
utils_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
package elf_reader
import (
"encoding/binary"
"testing"
)
func TestReadStringAtOffset(t *testing.T) {
buffer := []byte("\x00Hi there!\x00ASDFASDF")
s, e := ReadStringAtOffset(0, buffer)
if e != nil {
t.Logf("Failed reading empty string: %s\n", e)
t.FailNow()
}
if string(s) != "" {
t.Logf("Read wrong string, expected \"\", got \"%s\"\n", string(s))
t.FailNow()
}
s, e = ReadStringAtOffset(999, buffer)
if e == nil {
t.Logf("Didn't get expected error for reading invalid offset.\n")
t.FailNow()
}
t.Logf("Got expected error for reading invalid offset: %s\n", e)
s, e = ReadStringAtOffset(15, buffer)
if e == nil {
t.Logf("Didn't get expected error for reading unterminated string.\n")
t.FailNow()
}
t.Logf("Got expected error for reading unterminated string: %s\n", e)
s, e = ReadStringAtOffset(1, buffer)
if e != nil {
t.Logf("Failed reading valid string: %s\n", e)
t.FailNow()
}
if string(s) != "Hi there!" {
t.Logf("Read incorrect valid string: \"%s\"\n", string(s))
t.FailNow()
}
}
func TestELF32Hash(t *testing.T) {
data := []byte{}
hash := ELF32Hash(data)
if hash != 0 {
t.Logf("Got hash of 0x%08x for no data (expected 0).\n", hash)
t.Fail()
}
data = []byte("Hi there lol")
hash = ELF32Hash(data)
if hash != 0x086c29bc {
t.Logf("Got incorrect PJW hash: 0x%08x\n", hash)
t.Fail()
}
}
func TestWriteAtOffset(t *testing.T) {
data := []byte("Hi there")
toWrite := uint32(0x20212121)
data, e := WriteAtOffset(data, uint64(len(data)), binary.BigEndian,
toWrite)
if e != nil {
t.Logf("Failed writing data at offset: %s\n", e)
t.FailNow()
}
if string(data) != "Hi there !!!" {
t.Logf("Got wrong data after writing: %s\n", data)
t.FailNow()
}
}