wakeonlan_test.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package wakeonlan
  2. import (
  3. "testing"
  4. )
  5. // TestMagicPacketSize verifies the magic packet type is 102 bytes as per the WOL spec.
  6. func TestMagicPacketSize(t *testing.T) {
  7. var pkt magicPacket
  8. if len(pkt) != 102 {
  9. t.Errorf("expected magic packet length 102, got %d", len(pkt))
  10. }
  11. }
  12. // TestMagicPacketConstruction verifies a correctly built magic packet:
  13. // - bytes 0-5 are 0xFF (sync stream)
  14. // - bytes 6-101 are the MAC address repeated 16 times
  15. func TestMagicPacketConstruction(t *testing.T) {
  16. mac := [6]byte{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}
  17. var pkt magicPacket
  18. // Build sync stream
  19. copy(pkt[0:], []byte{255, 255, 255, 255, 255, 255})
  20. // Repeat MAC 16 times
  21. offset := 6
  22. for i := 0; i < 16; i++ {
  23. copy(pkt[offset:], mac[:])
  24. offset += 6
  25. }
  26. // Check sync stream
  27. for i := 0; i < 6; i++ {
  28. if pkt[i] != 0xFF {
  29. t.Errorf("byte %d of sync stream: expected 0xFF, got 0x%02X", i, pkt[i])
  30. }
  31. }
  32. // Check each MAC repetition
  33. for rep := 0; rep < 16; rep++ {
  34. base := 6 + rep*6
  35. for b := 0; b < 6; b++ {
  36. if pkt[base+b] != mac[b] {
  37. t.Errorf("rep %d byte %d: expected 0x%02X, got 0x%02X",
  38. rep, b, mac[b], pkt[base+b])
  39. }
  40. }
  41. }
  42. }
  43. // TestWakeTargetInvalidMAC verifies WakeTarget returns an error for a bad
  44. // MAC address without sending any network packet.
  45. func TestWakeTargetInvalidMAC(t *testing.T) {
  46. err := WakeTarget("not-a-mac")
  47. if err == nil {
  48. t.Error("expected error for invalid MAC address, got nil")
  49. }
  50. }
  51. // TestWakeTargetEmptyMAC verifies WakeTarget rejects an empty MAC string.
  52. func TestWakeTargetEmptyMAC(t *testing.T) {
  53. err := WakeTarget("")
  54. if err == nil {
  55. t.Error("expected error for empty MAC address, got nil")
  56. }
  57. }
  58. // TestWakeTargetTooShortMAC verifies WakeTarget rejects a malformed MAC.
  59. func TestWakeTargetTooShortMAC(t *testing.T) {
  60. err := WakeTarget("AA:BB:CC")
  61. if err == nil {
  62. t.Error("expected error for too-short MAC address, got nil")
  63. }
  64. }
  65. // TestWakeTargetValidMAC verifies WakeTarget does not return an error for a
  66. // well-formed MAC address. The broadcast UDP send may fail in sandboxed
  67. // environments, so we allow either success or a network-level error.
  68. func TestWakeTargetValidMAC(t *testing.T) {
  69. // This test sends a real UDP broadcast; skip in environments where that
  70. // is not possible (the test itself doesn't assert success to avoid
  71. // flakiness, but it must not panic).
  72. err := WakeTarget("AA:BB:CC:DD:EE:FF")
  73. // A network error (permission denied, unreachable, etc.) is acceptable.
  74. // We only care that the function doesn't panic or return a MAC-parse error.
  75. if err != nil {
  76. t.Logf("WakeTarget returned (non-fatal) network error: %v", err)
  77. }
  78. }
  79. // TestSendPacketInvalidAddr verifies sendPacket returns an error for an
  80. // unparseable address.
  81. func TestSendPacketInvalidAddr(t *testing.T) {
  82. var pkt magicPacket
  83. err := sendPacket("not-an-address", pkt)
  84. if err == nil {
  85. t.Error("expected error for invalid address, got nil")
  86. }
  87. }