|
1 | 1 | package lnwire |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
4 | 9 |
|
5 | 10 | // TestNodeAliasValidation tests that the NewNodeAlias method will only accept |
6 | 11 | // valid node announcements. |
@@ -40,3 +45,63 @@ func TestNodeAliasValidation(t *testing.T) { |
40 | 45 | } |
41 | 46 | } |
42 | 47 | } |
| 48 | + |
| 49 | +// TestNodeAnnouncement1Timestamp tests that NodeAnnouncement1 correctly |
| 50 | +// validates timestamps during decoding. Per BOLT 7, the timestamp MUST be |
| 51 | +// greater than any previous node_announcement. |
| 52 | +func TestNodeAnnouncement1Timestamp(t *testing.T) { |
| 53 | + t.Parallel() |
| 54 | + |
| 55 | + testCases := []struct { |
| 56 | + name string |
| 57 | + timestamp uint32 |
| 58 | + valid bool |
| 59 | + }{ |
| 60 | + { |
| 61 | + name: "zero timestamp rejected", |
| 62 | + timestamp: 0, |
| 63 | + valid: false, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "timestamp 1 accepted", |
| 67 | + timestamp: 1, |
| 68 | + valid: true, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "large timestamp accepted", |
| 72 | + timestamp: 1700000000, |
| 73 | + valid: true, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + for _, tc := range testCases { |
| 78 | + t.Run(tc.name, func(t *testing.T) { |
| 79 | + t.Parallel() |
| 80 | + |
| 81 | + na := &NodeAnnouncement1{ |
| 82 | + Timestamp: tc.timestamp, |
| 83 | + Features: NewRawFeatureVector(), |
| 84 | + } |
| 85 | + |
| 86 | + var buf bytes.Buffer |
| 87 | + err := na.Encode(&buf, 0) |
| 88 | + require.NoError(t, err, "failed to encode") |
| 89 | + |
| 90 | + decoded := &NodeAnnouncement1{} |
| 91 | + err = decoded.Decode(&buf, 0) |
| 92 | + |
| 93 | + if tc.valid { |
| 94 | + require.NoError(t, err) |
| 95 | + require.Equal( |
| 96 | + t, tc.timestamp, decoded.Timestamp, |
| 97 | + ) |
| 98 | + } else { |
| 99 | + require.Error(t, err) |
| 100 | + require.Contains( |
| 101 | + t, err.Error(), "timestamp cannot "+ |
| 102 | + "be zero", |
| 103 | + ) |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
0 commit comments