Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix verify header report false on summary packet #51

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import "github.com/sirupsen/logrus"

var log logrus.FieldLogger

func init() {
// Give a default logger at the start to avoid null pointer error
log = logrus.New()
}

func SetLogger(logger logrus.FieldLogger) {
log = logger
}
2 changes: 0 additions & 2 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import (
"os"
"testing"

"github.com/sirupsen/logrus"
"github.com/spf13/viper"

"github.com/stretchr/testify/assert"
)

func TestSingleIp(t *testing.T) {
log = logrus.New()
ip := net.UDPAddr{IP: net.ParseIP("192.168.0.5"), Port: 514}

// If the map is not set
Expand Down
3 changes: 0 additions & 3 deletions packageudp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import (
"net"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func TestPackageUdp(t *testing.T) {
log = logrus.New()

// No mapping enabled
ip := net.UDPAddr{IP: net.ParseIP("192.168.0.7"), Port: 12345}
config := Config{}
Expand Down
2 changes: 0 additions & 2 deletions queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import (
"testing"
"time"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

// TestQueueInsert tests the good validation
func TestQueueInsert(t *testing.T) {
log = logrus.New()
queuePath := path.Join(t.TempDir(), "shoveler-queue")
config := Config{QueueDir: queuePath}
queue := NewConfirmationQueue(&config)
Expand Down
20 changes: 12 additions & 8 deletions verify.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package shoveler

import (
"bytes"
"encoding/binary"
)

Expand All @@ -11,7 +10,7 @@ type Header struct {
Code byte
Pseq uint8
Plen uint16
ServerStart uint32
ServerStart int32
}

// verifyPacket will verify the packet matches the expected
Expand All @@ -23,13 +22,18 @@ func VerifyPacket(packet []byte) bool {
log.Infoln("Packet not large enough for XRootD header of 8 bytes, dropping.")
return false
}
header := Header{}
buffer := bytes.NewBuffer(packet[:8])
err := binary.Read(buffer, binary.BigEndian, &header)
if err != nil {
log.Warningln("Failed to read the binary packet into header structure:", err)
return false

// XML '<' character indicates a summary packet
if len(packet) > 0 && packet[0] == '<' {
return true
}

header := Header{}
header.Code = packet[0]
header.Pseq = packet[1]
header.Plen = binary.BigEndian.Uint16(packet[2:4])
header.ServerStart = int32(binary.BigEndian.Uint32(packet[4:8]))

// If the beginning of the packet doesn't match some expectations, then continue
if len(packet) != int(header.Plen) {
log.Warningln("Packet length does not match header. Packet:", len(packet), "Header:", int(header.Plen))
Expand Down
10 changes: 10 additions & 0 deletions verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ func TestGoodVerify(t *testing.T) {

}

func TestVerifySummaryPacket(t *testing.T) {
summaryPacket := `<statistics
tod="int64" ver="chars" src=”chars” tos=”int64”
pgm=”chars” ins=”chars” pid=”int” site=”chars”>
</statistics>
`

assert.True(t, VerifyPacket([]byte(summaryPacket)), "Failed to verify packet")
}

// TestBadVerify tests the validation if the packets are not good (random bits)
func TestBadVerify(t *testing.T) {
badHeader := Header{}
Expand Down
Loading