wirecodec_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package wirecodec
  2. import (
  3. "bytes"
  4. "strings"
  5. "testing"
  6. )
  7. func TestCompressRoundTrip(t *testing.T) {
  8. orig := []byte(strings.Repeat("inbound config payload ", 200))
  9. packed := Compress(orig)
  10. if len(packed) == 0 {
  11. t.Fatal("Compress returned empty output")
  12. }
  13. got, err := Decompress(packed, 1<<20)
  14. if err != nil {
  15. t.Fatalf("Decompress: %v", err)
  16. }
  17. if !bytes.Equal(got, orig) {
  18. t.Fatal("round trip mismatch")
  19. }
  20. }
  21. func TestDecompressRejectsOversize(t *testing.T) {
  22. orig := bytes.Repeat([]byte("A"), 1<<16) // 64 KiB, highly compressible
  23. packed := Compress(orig)
  24. if _, err := Decompress(packed, 1024); err == nil {
  25. t.Fatal("Decompress must reject output that exceeds the cap (bomb guard)")
  26. }
  27. }
  28. func TestDecompressRejectsGarbage(t *testing.T) {
  29. if _, err := Decompress([]byte("not a zstd frame"), 1<<20); err == nil {
  30. t.Fatal("Decompress must reject non-zstd input")
  31. }
  32. }
  33. func TestSha256HexStableAndSensitive(t *testing.T) {
  34. a := Sha256Hex([]byte("config-A"))
  35. b := Sha256Hex([]byte("config-A"))
  36. c := Sha256Hex([]byte("config-B"))
  37. if a != b {
  38. t.Fatal("hash must be stable for identical input")
  39. }
  40. if a == c {
  41. t.Fatal("hash must differ when the body changes")
  42. }
  43. if len(a) != 64 {
  44. t.Fatalf("expected 64 hex chars, got %d", len(a))
  45. }
  46. }