53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
package isolation
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestFTPMeterCountsBothDirectionsWithRotationAndPartialLines(t *testing.T) {
|
|
dir := t.TempDir()
|
|
log := filepath.Join(dir, "ftp.log")
|
|
now := time.Date(2026, 10, 25, 10, 0, 0, 0, time.UTC)
|
|
agent := Agent{UsageStatePath: filepath.Join(dir, "traffic.json"), FTPLogPath: log}
|
|
line := func(name, direction string, size int) string {
|
|
return fmt.Sprintf("%d session %s 127.0.0.1 %s %d 0.5 file with spaces.txt\n", now.Unix(), name, direction, size)
|
|
}
|
|
partial := line("sv0123456789", "D", 17)
|
|
data := line("sv0123456789", "U", 101) + line("sv0123456789", "D", 202) + line("sv9876543210", "D", 999) + partial[:len(partial)-1]
|
|
if err := os.WriteFile(log, []byte(data), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check := func(want uint64) {
|
|
t.Helper()
|
|
got, err := agent.measureFTPBandwidth("sv0123456789", now)
|
|
if err != nil || got != want {
|
|
t.Fatalf("bytes=%d want=%d error=%v", got, want, err)
|
|
}
|
|
}
|
|
check(303)
|
|
check(303)
|
|
file, err := os.OpenFile(log, os.O_APPEND|os.O_WRONLY, 0600)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, _ = file.WriteString("\n")
|
|
file.Close()
|
|
check(320)
|
|
if err = os.Rename(log, log+".1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = os.WriteFile(log, []byte(line("sv0123456789", "U", 23)), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check(343)
|
|
next := now.AddDate(0, 1, 0)
|
|
got, err := agent.measureFTPBandwidth("sv0123456789", next)
|
|
if err != nil || got != 0 {
|
|
t.Fatalf("month reset=%d error=%v", got, err)
|
|
}
|
|
}
|