Compare commits

..

2 Commits

Author SHA1 Message Date
TheDiveO cdca03f2f7
Merge e9249a316d into 5e242ec578 2024-04-18 20:14:14 +02:00
thediveo e9249a316d feat: add xt.Comment
Signed-off-by: thediveo <thediveo@gmx.eu>
2024-04-18 20:13:56 +02:00
2 changed files with 9 additions and 8 deletions

View File

@ -5,7 +5,8 @@ import (
"fmt"
)
// CommentSize is the fixed size of a comment info xt blob.
// 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
const CommentSize = 256
// Comment gets marshalled and unmarshalled as a fixed-sized char array, filled
@ -15,7 +16,8 @@ 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", CommentSize)
return nil, fmt.Errorf("comment must be less than %d bytes, got %d bytes",
CommentSize, len(*c))
}
data := make([]byte, CommentSize)
copy(data, []byte(*c))
@ -24,7 +26,8 @@ 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("Comment takes exactly %d bytes", CommentSize)
return fmt.Errorf("malformed comment: got %d bytes, expected exactly %d bytes",
len(data), CommentSize)
}
*c = Comment(bytes.TrimRight(data, "\x00"))
return nil

View File

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