Compare commits

..

2 Commits

Author SHA1 Message Date
TheDiveO 139ffd651e
Merge 41a89e5c2e into 5e242ec578 2024-04-17 17:36:32 +02:00
thediveo 41a89e5c2e feat: add xt.Comment
Signed-off-by: thediveo <thediveo@gmx.eu>
2024-04-17 17:34:43 +02:00
2 changed files with 8 additions and 9 deletions

View File

@ -5,8 +5,7 @@ import (
"fmt"
)
// CommentSize is the fixed size of a comment info xt blob, see:
// https://elixir.bootlin.com/linux/v6.8.7/source/include/uapi/linux/netfilter/xt_comment.h#L5
// CommentSize is the fixed size of a comment info xt blob.
const CommentSize = 256
// Comment gets marshalled and unmarshalled as a fixed-sized char array, filled
@ -16,8 +15,7 @@ type Comment string
func (c *Comment) marshal(fam TableFamily, rev uint32) ([]byte, error) {
if len(*c) >= CommentSize {
return nil, fmt.Errorf("comment must be less than %d bytes, got %d bytes",
CommentSize, len(*c))
return nil, fmt.Errorf("Comment must be less than %d bytes", CommentSize)
}
data := make([]byte, CommentSize)
copy(data, []byte(*c))
@ -26,8 +24,7 @@ func (c *Comment) marshal(fam TableFamily, rev uint32) ([]byte, error) {
func (c *Comment) unmarshal(fam TableFamily, rev uint32, data []byte) error {
if len(data) != CommentSize {
return fmt.Errorf("malformed comment: got %d bytes, expected exactly %d bytes",
len(data), CommentSize)
return fmt.Errorf("Comment takes exactly %d bytes", CommentSize)
}
*c = Comment(bytes.TrimRight(data, "\x00"))
return nil

View File

@ -1,6 +1,7 @@
package xt
import (
"fmt"
"reflect"
"strings"
"testing"
@ -22,7 +23,7 @@ func TestComment(t *testing.T) {
{
name: "marshal oversized Comment",
info: &oversized,
errmsg: "comment must be less than 256 bytes, got 600 bytes",
errmsg: "Comment must be less than 256 bytes",
},
}
@ -56,7 +57,8 @@ func TestComment(t *testing.T) {
oversizeddata := []byte(oversized)
var comment Comment
if err := (&comment).unmarshal(0, 0, oversizeddata); err == nil {
t.Fatalf("unmarshal: expected error, but got nil")
if err := (&comment).unmarshal(0, 0, oversizeddata); err == nil || err.Error() != fmt.Sprintf("Comment takes exactly %d bytes", CommentSize) {
t.Fatalf("unmarshal: expected error: %v", err)
}
}