Skip to content

Commit ceb06cd

Browse files
zdiffzdiff
andauthored
chore: go testifylint (VirusTotal#674)
* Addressed testifylint errors * Replaced deprecated ioutil.TempFile with os.CreateTemp * Formatted with gofumpt --------- Co-authored-by: zdiff <zdiff@localhost>
1 parent b0d3360 commit ceb06cd

6 files changed

Lines changed: 54 additions & 53 deletions

File tree

go/compiler.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package yara_x
22

33
// #include <yara_x.h>
44
import "C"
5+
56
import (
67
"encoding/json"
78
"errors"
@@ -28,7 +29,7 @@ type CompileOption func(c *Compiler) error
2829
//
2930
// Valid value types include: int, int32, int64, bool, string, float32 and
3031
// float64.
31-
func Globals(vars map[string]interface{}) CompileOption {
32+
func Globals(vars map[string]any) CompileOption {
3233
return func(c *Compiler) error {
3334
for ident, value := range vars {
3435
c.vars[ident] = value
@@ -289,7 +290,7 @@ type Compiler struct {
289290
includesEnabled bool
290291
ignoredModules map[string]bool
291292
bannedModules map[string]bannedModule
292-
vars map[string]interface{}
293+
vars map[string]any
293294
features []string
294295
includeDirs []string
295296
maxWarnings *int
@@ -301,7 +302,7 @@ func NewCompiler(opts ...CompileOption) (*Compiler, error) {
301302
includesEnabled: true,
302303
ignoredModules: make(map[string]bool),
303304
bannedModules: make(map[string]bannedModule),
304-
vars: make(map[string]interface{}),
305+
vars: make(map[string]any),
305306
features: make([]string, 0),
306307
includeDirs: make([]string, 0),
307308
}
@@ -558,7 +559,7 @@ func (c *Compiler) DefineGlobal(ident string, value interface{}) error {
558559
ret = C.int(C.yrx_compiler_define_global_float(c.cCompiler, cIdent, C.double(v)))
559560
case float64:
560561
ret = C.int(C.yrx_compiler_define_global_float(c.cCompiler, cIdent, C.double(v)))
561-
case map[string]interface{}, []interface{}:
562+
case map[string]any, []any:
562563
jsonStr, err := json.Marshal(v)
563564
if err != nil {
564565
return fmt.Errorf("failed to marshal '%s' to json: '%v'", ident, err)

go/compiler_test.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ package yara_x
33
import (
44
"bytes"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"testing"
98

109
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
1111
)
1212

1313
func TestNamespaces(t *testing.T) {
1414
c, err := NewCompiler()
15-
assert.NoError(t, err)
15+
require.NoError(t, err)
1616

1717
c.NewNamespace("foo")
1818
c.AddSource("rule test { condition: true }")
@@ -26,7 +26,7 @@ func TestNamespaces(t *testing.T) {
2626

2727
func TestGlobals(t *testing.T) {
2828
c, err := NewCompiler()
29-
assert.NoError(t, err)
29+
require.NoError(t, err)
3030
x := map[string]any{"a": map[string]any{"a": "b"}, "b": "d"}
3131
y := []any{"z"}
3232

@@ -54,7 +54,7 @@ func TestUnsupportedModules(t *testing.T) {
5454
rule test { condition: true }`,
5555
IgnoreModule("unsupported_module"))
5656

57-
assert.NoError(t, err)
57+
require.NoError(t, err)
5858
scanResults, _ := r.Scan([]byte{})
5959
assert.Len(t, scanResults.MatchingRules(), 1)
6060
}
@@ -85,23 +85,23 @@ func TestDisabledIncludes(t *testing.T) {
8585
}
8686

8787
func TestIncludes(t *testing.T) {
88-
file, err := ioutil.TempFile("", "prefix")
89-
assert.NoError(t, err)
88+
file, err := os.CreateTemp(t.TempDir(), "prefix")
89+
require.NoError(t, err)
9090

9191
defer os.Remove(file.Name())
9292

9393
_, err = Compile(
9494
fmt.Sprintf(`include "%s"`, file.Name()),
9595
IncludeDir(os.TempDir()))
9696

97-
assert.NoError(t, err)
97+
require.NoError(t, err)
9898
}
9999

100100
func TestRelaxedReSyntax(t *testing.T) {
101101
r, err := Compile(`
102102
rule test { strings: $a = /\Release/ condition: $a }`,
103103
RelaxedReSyntax(true))
104-
assert.NoError(t, err)
104+
require.NoError(t, err)
105105
scanResults, _ := r.Scan([]byte("Release"))
106106
assert.Len(t, scanResults.MatchingRules(), 1)
107107
}
@@ -110,7 +110,7 @@ func TestConditionOptimization(t *testing.T) {
110110
_, err := Compile(`
111111
rule test { condition: true }`,
112112
ConditionOptimization(true))
113-
assert.NoError(t, err)
113+
require.NoError(t, err)
114114
}
115115

116116
func TestErrorOnSlowPattern(t *testing.T) {
@@ -129,14 +129,14 @@ func TestErrorOnSlowLoop(t *testing.T) {
129129

130130
func TestSerialization(t *testing.T) {
131131
r, err := Compile("rule test { condition: true }")
132-
assert.NoError(t, err)
132+
require.NoError(t, err)
133133

134134
var buf bytes.Buffer
135135

136136
// Write rules into buffer
137137
n, err := r.WriteTo(&buf)
138138

139-
assert.NoError(t, err)
139+
require.NoError(t, err)
140140
assert.Len(t, buf.Bytes(), int(n))
141141

142142
// Read rules from buffer
@@ -157,7 +157,7 @@ func TestVariables(t *testing.T) {
157157
assert.Len(t, scanResults.MatchingRules(), 1)
158158

159159
c, err := NewCompiler()
160-
assert.NoError(t, err)
160+
require.NoError(t, err)
161161

162162
c.DefineGlobal("var", 1234)
163163
c.AddSource("rule test { condition: var == 1234 }")
@@ -177,7 +177,7 @@ func TestVariables(t *testing.T) {
177177
c.DefineGlobal("var", false)
178178
c.AddSource("rule test { condition: var }")
179179
scanResults, _ = NewScanner(c.Build()).Scan([]byte{})
180-
assert.Len(t, scanResults.MatchingRules(), 0)
180+
assert.Empty(t, scanResults.MatchingRules())
181181

182182
c.DefineGlobal("var", "foo")
183183
c.AddSource("rule test { condition: var == \"foo\" }")
@@ -207,26 +207,26 @@ func TestCompilerFeatures(t *testing.T) {
207207
rules := `import "test_proto2" rule test { condition: test_proto2.requires_foo_and_bar }`
208208

209209
_, err := Compile(rules)
210-
assert.EqualError(t, err, `error[E100]: foo is required
210+
require.EqualError(t, err, `error[E100]: foo is required
211211
--> line:1:57
212212
|
213213
1 | import "test_proto2" rule test { condition: test_proto2.requires_foo_and_bar }
214214
| ^^^^^^^^^^^^^^^^^^^^ this field was used without foo`)
215215

216216
_, err = Compile(rules, WithFeature("foo"))
217-
assert.EqualError(t, err, `error[E100]: bar is required
217+
require.EqualError(t, err, `error[E100]: bar is required
218218
--> line:1:57
219219
|
220220
1 | import "test_proto2" rule test { condition: test_proto2.requires_foo_and_bar }
221221
| ^^^^^^^^^^^^^^^^^^^^ this field was used without bar`)
222222

223223
_, err = Compile(rules, WithFeature("foo"), WithFeature("bar"))
224-
assert.NoError(t, err)
224+
require.NoError(t, err)
225225
}
226226

227227
func TestErrors(t *testing.T) {
228228
c, err := NewCompiler()
229-
assert.NoError(t, err)
229+
require.NoError(t, err)
230230

231231
c.AddSource("rule test_1 { condition: true }")
232232
assert.Equal(t, []CompileError{}, c.Errors())
@@ -291,13 +291,13 @@ func TestErrors(t *testing.T) {
291291

292292
func TestRules(t *testing.T) {
293293
c, err := NewCompiler()
294-
assert.NoError(t, err)
294+
require.NoError(t, err)
295295

296296
c.AddSource(`rule test_1 : tag1 tag2 {
297297
condition:
298298
true
299299
}`)
300-
assert.NoError(t, err)
300+
require.NoError(t, err)
301301

302302
c.AddSource(`rule test_2 {
303303
meta:
@@ -308,7 +308,7 @@ func TestRules(t *testing.T) {
308308
condition:
309309
true
310310
}`)
311-
assert.NoError(t, err)
311+
require.NoError(t, err)
312312

313313
rules := c.Build()
314314
assert.Equal(t, 2, rules.Count())
@@ -324,7 +324,7 @@ func TestRules(t *testing.T) {
324324
assert.Equal(t, []string{"tag1", "tag2"}, slice[0].Tags())
325325
assert.Equal(t, []string{}, slice[1].Tags())
326326

327-
assert.Len(t, slice[0].Metadata(), 0)
327+
assert.Empty(t, slice[0].Metadata())
328328
assert.Len(t, slice[1].Metadata(), 4)
329329

330330
assert.Equal(t, "foo", slice[1].Metadata()[0].Identifier())
@@ -342,7 +342,7 @@ func TestRules(t *testing.T) {
342342

343343
func TestImportsIter(t *testing.T) {
344344
c, err := NewCompiler()
345-
assert.NoError(t, err)
345+
require.NoError(t, err)
346346

347347
c.AddSource(`
348348
import "pe"
@@ -351,7 +351,7 @@ func TestImportsIter(t *testing.T) {
351351
condition:
352352
true
353353
}`)
354-
assert.NoError(t, err)
354+
require.NoError(t, err)
355355

356356
rules := c.Build()
357357
imports := rules.Imports()
@@ -363,7 +363,7 @@ func TestImportsIter(t *testing.T) {
363363

364364
func TestWarnings(t *testing.T) {
365365
c, err := NewCompiler()
366-
assert.NoError(t, err)
366+
require.NoError(t, err)
367367

368368
c.AddSource("rule test { strings: $a = {01 [0-1][0-1] 02 } condition: $a }")
369369

go/example_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ func Example_compilerAndScanner() {
5050
condition:
5151
$bar
5252
}`)
53-
5453
if err != nil {
5554
panic(err)
5655
}

go/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ type Pattern struct {
266266
// Metadata represents a metadata in a Rule.
267267
type Metadata struct {
268268
identifier string
269-
value interface{}
269+
value any
270270
}
271271

272272
// Match contains information about the offset where a match occurred and
@@ -358,7 +358,7 @@ func (m *Metadata) Identifier() string {
358358
}
359359

360360
// Value associated to the metadata.
361-
func (m *Metadata) Value() interface{} {
361+
func (m *Metadata) Value() any {
362362
return m.value
363363
}
364364

@@ -474,7 +474,7 @@ func metadataCallback(metadata *C.YRX_METADATA, handle C.uintptr_t) {
474474
panic("matchCallback didn't receive a *[]Metadata")
475475
}
476476

477-
var value interface{}
477+
var value any
478478

479479
switch metadata.value_type {
480480
case C.YRX_I64:

go/scanner.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ import (
3131
"runtime/cgo"
3232
"time"
3333
"unsafe"
34-
)
3534

36-
import (
3735
"google.golang.org/protobuf/proto"
3836
)
3937

@@ -163,7 +161,7 @@ func (s *Scanner) SetGlobal(ident string, value interface{}) error {
163161
ret = C.int(C.yrx_scanner_set_global_str(s.cScanner, cIdent, cValue))
164162
case float64:
165163
ret = C.int(C.yrx_scanner_set_global_float(s.cScanner, cIdent, C.double(v)))
166-
case map[string]interface{}, []interface{}:
164+
case map[string]any, []any:
167165
jsonStr, err := json.Marshal(v)
168166
if err != nil {
169167
return fmt.Errorf("failed to marshal '%s' to json: '%v'", ident, err)
@@ -316,7 +314,8 @@ func slowestRulesCallback(
316314
rule *C.char,
317315
patternMatchingTime C.double,
318316
conditionExecTime C.double,
319-
handle C.uintptr_t) {
317+
handle C.uintptr_t,
318+
) {
320319
h := cgo.Handle(handle)
321320
profilingInfo, ok := h.Value().(*[]ProfilingInfo)
322321
if !ok {
@@ -358,17 +357,17 @@ func (s *Scanner) SlowestRules(n int) []ProfilingInfo {
358357
return profilingInfo
359358
}
360359

361-
/// ClearProfilingData resets the profiling data collected during rule execution
362-
/// across scanned files. Use it to start a new profiling session, ensuring the
363-
/// results reflect only the data gathered after this method is called.
360+
// ClearProfilingData resets the profiling data collected during rule execution
361+
// across scanned files. Use it to start a new profiling session, ensuring the
362+
// results reflect only the data gathered after this method is called.
364363
//
365364
// In order to use this function, the YARA-X C library must be built with
366365
// support for rules profiling by enabling the `rules-profiling` feature.
367366
// Otherwise, calling this function will cause a panic.
368367
func (s *Scanner) ClearProfilingData() {
369-
if C.yrx_scanner_clear_profiling_data(s.cScanner) == C.YRX_NOT_SUPPORTED {
370-
panic("ClearProfilingData requires that the YARA-X C library is built with the `rules-profiling` feature")
371-
}
368+
if C.yrx_scanner_clear_profiling_data(s.cScanner) == C.YRX_NOT_SUPPORTED {
369+
panic("ClearProfilingData requires that the YARA-X C library is built with the `rules-profiling` feature")
370+
}
372371
}
373372

374373
// Destroy destroys the scanner.

0 commit comments

Comments
 (0)