|
| 1 | +// Copyright (C) 2026 Homer Server Contributors |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + |
| 5 | +package decoder |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +func TestSanitizeHEPTimestamp(t *testing.T) { |
| 13 | + // Fixed "receive" time matching the issue #909 sample window (2026-08-04). |
| 14 | + now := time.Date(2026, 8, 4, 12, 0, 0, 0, time.UTC) |
| 15 | + validUnix := uint32(time.Date(2026, 8, 3, 11, 33, 46, 0, time.UTC).Unix()) // 1785756826 |
| 16 | + ntpAsUnix := uint32(3994745626) // NTP MSW misread as Unix → 2096 |
| 17 | + brokenHeader := uint32(2608623722) // packet 490 tv_sec → 2052 |
| 18 | + brokenUsec := uint32(1059783936) // NTP frac in usec field |
| 19 | + |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + tsec uint32 |
| 23 | + tmsec uint32 |
| 24 | + wantTsec uint32 |
| 25 | + wantTmsec uint32 |
| 26 | + wantNow bool // expect receive time |
| 27 | + checkExact bool |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "normal unix preserved", |
| 31 | + tsec: validUnix, |
| 32 | + tmsec: 123456, |
| 33 | + wantTsec: validUnix, |
| 34 | + wantTmsec: 123456, |
| 35 | + checkExact: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "ntp-as-unix converted", |
| 39 | + tsec: ntpAsUnix, |
| 40 | + tmsec: 0, |
| 41 | + wantTsec: validUnix, |
| 42 | + wantTmsec: 0, |
| 43 | + checkExact: true, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "broken header falls back to receive time", |
| 47 | + tsec: brokenHeader, |
| 48 | + tmsec: brokenUsec, |
| 49 | + wantNow: true, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "zero timestamp uses receive time and syncs Tsec", |
| 53 | + tsec: 0, |
| 54 | + tmsec: 0, |
| 55 | + wantNow: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "valid usec under 1e6 preserved", |
| 59 | + tsec: validUnix, |
| 60 | + tmsec: 999999, |
| 61 | + wantTsec: validUnix, |
| 62 | + wantTmsec: 999999, |
| 63 | + checkExact: true, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "invalid usec clamped then out-of-range falls back", |
| 67 | + tsec: brokenHeader, |
| 68 | + tmsec: brokenUsec, |
| 69 | + wantNow: true, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + for _, tt := range tests { |
| 74 | + t.Run(tt.name, func(t *testing.T) { |
| 75 | + h := &HEP{Tsec: tt.tsec, Tmsec: tt.tmsec, NodeID: 2001} |
| 76 | + h.sanitizeHEPTimestamp(now) |
| 77 | + |
| 78 | + if tt.wantNow { |
| 79 | + if h.Timestamp.UTC() != now { |
| 80 | + t.Fatalf("Timestamp = %v, want receive time %v", h.Timestamp, now) |
| 81 | + } |
| 82 | + if h.Tsec != uint32(now.Unix()) { |
| 83 | + t.Fatalf("Tsec = %d, want %d (synced receive time)", h.Tsec, now.Unix()) |
| 84 | + } |
| 85 | + if h.Tmsec != uint32(now.Nanosecond()/1000) { |
| 86 | + t.Fatalf("Tmsec = %d, want %d", h.Tmsec, now.Nanosecond()/1000) |
| 87 | + } |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + if !tt.checkExact { |
| 92 | + return |
| 93 | + } |
| 94 | + if h.Tsec != tt.wantTsec { |
| 95 | + t.Fatalf("Tsec = %d, want %d", h.Tsec, tt.wantTsec) |
| 96 | + } |
| 97 | + if h.Tmsec != tt.wantTmsec { |
| 98 | + t.Fatalf("Tmsec = %d, want %d", h.Tmsec, tt.wantTmsec) |
| 99 | + } |
| 100 | + if h.Timestamp.Unix() != int64(tt.wantTsec) { |
| 101 | + t.Fatalf("Timestamp.Unix() = %d, want %d", h.Timestamp.Unix(), tt.wantTsec) |
| 102 | + } |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestHepTimestampInWindow(t *testing.T) { |
| 108 | + now := time.Date(2026, 8, 4, 12, 0, 0, 0, time.UTC) |
| 109 | + |
| 110 | + if !hepTimestampInWindow(now, now) { |
| 111 | + t.Fatal("now should be in window") |
| 112 | + } |
| 113 | + if !hepTimestampInWindow(now.Add(23*time.Hour), now) { |
| 114 | + t.Fatal("now+23h should be in window") |
| 115 | + } |
| 116 | + if hepTimestampInWindow(now.Add(25*time.Hour), now) { |
| 117 | + t.Fatal("now+25h should be out of window") |
| 118 | + } |
| 119 | + if hepTimestampInWindow(now.Add(-11*365*24*time.Hour), now) { |
| 120 | + t.Fatal("now-11y should be out of window") |
| 121 | + } |
| 122 | + if !hepTimestampInWindow(now.Add(-9*365*24*time.Hour), now) { |
| 123 | + t.Fatal("now-9y should be in window") |
| 124 | + } |
| 125 | +} |
0 commit comments