-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Florian Lehner <[email protected]>
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package tc | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type NByteMatch struct { | ||
Check failure on line 7 in ematch_nbyte.go GitHub Actions / code-check (1.20.x)
|
||
Offset uint16 | ||
Layer uint8 | ||
Needle []byte | ||
} | ||
|
||
type tcfEmNByte struct { | ||
off uint16 | ||
len uint16 | ||
layer uint8 | ||
} | ||
|
||
func unmarshalNByteMatch(data []byte, info *NByteMatch) error { | ||
if len(data) < 8 { | ||
return fmt.Errorf("unmarshalNByteMatch: incomplete data") | ||
} | ||
|
||
nbyte := tcfEmNByte{} | ||
if err := unmarshalStruct(data[:5], nbyte); err != nil { | ||
return err | ||
} | ||
info.Offset = nbyte.off | ||
info.Layer = nbyte.layer | ||
info.Needle = data[8:] | ||
|
||
return nil | ||
} | ||
|
||
func marshalNByteMatch(info *NByteMatch) ([]byte, error) { | ||
if info == nil { | ||
return []byte{}, fmt.Errorf("marshalNByteMatch: %w", ErrNoArg) | ||
} | ||
nbyte := tcfEmNByte{ | ||
off: info.Offset, | ||
len: uint16(len(info.Needle)), | ||
layer: info.Layer, | ||
} | ||
|
||
tmp, err := marshalAndAlignStruct(nbyte) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
tmp = append(tmp, info.Needle...) | ||
return tmp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package tc | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestNByteMatch(t *testing.T) { | ||
t.Skip() | ||
tests := map[string]struct { | ||
val NByteMatch | ||
err1 error | ||
err2 error | ||
}{ | ||
"simple": { | ||
val: NByteMatch{Needle: []byte("helloWorld"), | ||
Offset: 42, | ||
Layer: 7}, | ||
}, | ||
} | ||
|
||
for name, testcase := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
data, err1 := marshalNByteMatch(&testcase.val) | ||
if !errors.Is(err1, testcase.err1) { | ||
t.Fatalf("Unexpected error: %v", err1) | ||
} | ||
val := NByteMatch{} | ||
err2 := unmarshalNByteMatch(data, &val) | ||
if !errors.Is(err2, testcase.err2) { | ||
t.Fatalf("Unexpected error: %v", err2) | ||
} | ||
if diff := cmp.Diff(val, testcase.val); diff != "" { | ||
t.Fatalf("NByteMatch missmatch (want +got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
t.Run("nil", func(t *testing.T) { | ||
_, err := marshalNByteMatch(nil) | ||
if !errors.Is(err, ErrNoArg) { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
}) | ||
} |