needle_test.gno
6.08 Kb ยท 157 lines
1package needle
2
3import (
4 "bytes"
5 "crypto/sha256"
6 "encoding/hex"
7 "testing"
8)
9
10func TestNeedle(t *testing.T) {
11 t.Parallel()
12 t.Run("Bytes", func(t *testing.T) {
13 t.Parallel()
14 p, _ := hex.DecodeString("40e4350b03d8b0c9e340321210b259d9a20b19632929b4a219254a4269c11f820c75168c6a91d309f4b134a7d715a5ac408991e1cf9415995053cf8a4e185dae22a06617ac51ebf7d232bc49e567f90be4db815c2b88ca0d9a4ef7a5119c0e592c88dfb96706e6510fb8a657c0f70f6695ea310d24786e6d980e9b33cf2665342b965b2391f6bb982c4c5f6058b9cba58038d32452e07cdee9420a8bd7f514e1")
15 n, _ := New(p)
16 b := n.Bytes()
17 b[0], b[1], b[2], b[3] = 0, 0, 0, 0
18 if bytes.Equal(n.Bytes(), b) {
19 t.Error("mutating Bytes() changed needle bytes")
20 }
21 })
22 t.Run("Payload", func(t *testing.T) {
23 t.Parallel()
24 p, _ := hex.DecodeString("40e4350b03d8b0c9e340321210b259d9a20b19632929b4a219254a4269c11f820c75168c6a91d309f4b134a7d715a5ac408991e1cf9415995053cf8a4e185dae22a06617ac51ebf7d232bc49e567f90be4db815c2b88ca0d9a4ef7a5119c0e592c88dfb96706e6510fb8a657c0f70f6695ea310d24786e6d980e9b33cf2665342b965b2391f6bb982c4c5f6058b9cba58038d32452e07cdee9420a8bd7f514e1")
25 n, _ := New(p)
26 payload := n.Payload()
27 if !bytes.Equal(p, payload) {
28 t.Error("payload imported by New does not match needle.Payload()")
29 }
30 payload[0] = 0
31 pl := n.Payload()
32 if bytes.Equal(pl, payload) {
33 t.Error("mutating Payload() changed needle payload")
34 }
35 })
36 t.Run("Hash", func(t *testing.T) {
37 t.Parallel()
38 p, _ := hex.DecodeString("40e4350b03d8b0c9e340321210b259d9a20b19632929b4a219254a4269c11f820c75168c6a91d309f4b134a7d715a5ac408991e1cf9415995053cf8a4e185dae22a06617ac51ebf7d232bc49e567f90be4db815c2b88ca0d9a4ef7a5119c0e592c88dfb96706e6510fb8a657c0f70f6695ea310d24786e6d980e9b33cf2665342b965b2391f6bb982c4c5f6058b9cba58038d32452e07cdee9420a8bd7f514e1")
39 n, _ := New(p)
40 hash := n.Hash()
41 h := sha256.Sum256(p)
42 if !bytes.Equal(h[:], hash) {
43 t.Error("exported hash is invalid")
44 }
45 hash[0] = 0
46 h2 := n.Hash()
47 if bytes.Equal(h2, hash) {
48 t.Error("mutating Hash() changed needle hash")
49 }
50 })
51}
52
53func TestNew(t *testing.T) {
54 t.Parallel()
55
56 p, _ := hex.DecodeString("40e4350b03d8b0c9e340321210b259d9a20b19632929b4a219254a4269c11f820c75168c6a91d309f4b134a7d715a5ac408991e1cf9415995053cf8a4e185dae22a06617ac51ebf7d232bc49e567f90be4db815c2b88ca0d9a4ef7a5119c0e592c88dfb96706e6510fb8a657c0f70f6695ea310d24786e6d980e9b33cf2665342b965b2391f6bb982c4c5f6058b9cba58038d32452e07cdee9420a8bd7f514e1")
57 expected, _ := hex.DecodeString("f1b462c84a0c51dad44293951f0b084a8871b3700ac1b9fc7a53a20bc0ba0fed40e4350b03d8b0c9e340321210b259d9a20b19632929b4a219254a4269c11f820c75168c6a91d309f4b134a7d715a5ac408991e1cf9415995053cf8a4e185dae22a06617ac51ebf7d232bc49e567f90be4db815c2b88ca0d9a4ef7a5119c0e592c88dfb96706e6510fb8a657c0f70f6695ea310d24786e6d980e9b33cf2665342b965b2391f6bb982c4c5f6058b9cba58038d32452e07cdee9420a8bd7f514e1")
58
59 testTable := []struct {
60 payload []byte
61 expected []byte
62 hasError bool
63 description string
64 }{
65 {
66 payload: p,
67 expected: expected,
68 hasError: false,
69 description: "expected payload",
70 },
71 {
72 payload: p[:PayloadLength-1],
73 expected: nil,
74 hasError: true,
75 description: "payload invalid length (too small)",
76 },
77 {
78 payload: append(p, byte(1)),
79 expected: nil,
80 hasError: true,
81 description: "payload invalid length (too large)",
82 },
83 }
84
85 for _, test := range testTable {
86 n, err := New(test.payload)
87 if err != nil {
88 if !test.hasError {
89 t.Errorf("test: %v had error: %v", test.description, err)
90 }
91 } else if !bytes.Equal(n.Bytes(), test.expected) {
92 t.Errorf("%v, bytes not equal\n%x\n%x", test.description, n.Bytes(), test.expected)
93 }
94 }
95}
96
97func TestFromBytes(t *testing.T) {
98 t.Parallel()
99
100 validRaw, _ := hex.DecodeString("f1b462c84a0c51dad44293951f0b084a8871b3700ac1b9fc7a53a20bc0ba0fed40e4350b03d8b0c9e340321210b259d9a20b19632929b4a219254a4269c11f820c75168c6a91d309f4b134a7d715a5ac408991e1cf9415995053cf8a4e185dae22a06617ac51ebf7d232bc49e567f90be4db815c2b88ca0d9a4ef7a5119c0e592c88dfb96706e6510fb8a657c0f70f6695ea310d24786e6d980e9b33cf2665342b965b2391f6bb982c4c5f6058b9cba58038d32452e07cdee9420a8bd7f514e1")
101 validExpected, _ := hex.DecodeString("f1b462c84a0c51dad44293951f0b084a8871b3700ac1b9fc7a53a20bc0ba0fed40e4350b03d8b0c9e340321210b259d9a20b19632929b4a219254a4269c11f820c75168c6a91d309f4b134a7d715a5ac408991e1cf9415995053cf8a4e185dae22a06617ac51ebf7d232bc49e567f90be4db815c2b88ca0d9a4ef7a5119c0e592c88dfb96706e6510fb8a657c0f70f6695ea310d24786e6d980e9b33cf2665342b965b2391f6bb982c4c5f6058b9cba58038d32452e07cdee9420a8bd7f514e1")
102 invalidHash, _ := hex.DecodeString("182e0ca0d2fb1da76da6caf36a9d0d2838655632e85891216dc8b545d8f1410940e4350b03d8b0c9e340321210b259d9a20b19632929b4a219254a4269c11f820c75168c6a91d309f4b134a7d715a5ac408991e1cf9415995053cf8a4e185dae22a06617ac51ebf7d232bc49e567f90be4db815c2b88ca0d9a4ef7a5119c0e592c88dfb96706e6510fb8a657c0f70f6695ea310d24786e6d980e9b33cf2665342b965b2391f6bb982c4c5f6058b9cba58038d32452e07cdee9420a8bd7f514e1")
103
104 testTable := []struct {
105 rawBytes []byte
106 expected []byte
107 hasError bool
108 description string
109 }{
110 {
111 rawBytes: validRaw,
112 expected: validExpected,
113 hasError: false,
114 description: "valid raw bytes",
115 },
116 {
117 rawBytes: make([]byte, 0),
118 expected: nil,
119 hasError: true,
120 description: "empty bytes",
121 },
122 {
123 rawBytes: make([]byte, NeedleLength-1),
124 expected: nil,
125 hasError: true,
126 description: "too few bytes, one less than expected",
127 },
128 {
129 rawBytes: make([]byte, 0),
130 expected: nil,
131 hasError: true,
132 description: "too few bytes, no bytes",
133 },
134 {
135 rawBytes: make([]byte, NeedleLength+1),
136 expected: nil,
137 hasError: true,
138 description: "too many bytes",
139 },
140 {
141 rawBytes: invalidHash,
142 expected: nil,
143 hasError: true,
144 description: "invalid hash",
145 },
146 }
147 for _, test := range testTable {
148 n, err := FromBytes(test.rawBytes)
149 if err != nil {
150 if !test.hasError {
151 t.Errorf("test: %v had error: %v", test.description, err)
152 }
153 } else if !bytes.Equal(n.Bytes(), test.expected) {
154 t.Errorf("%v, bytes not equal\n%x\n%x", test.description, n.Bytes(), test.expected)
155 }
156 }
157}