|
| 1 | +package firewall |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + testMetaCaveat = "lnd-custom lit-mac-fw meta:{\"actor_name\":" + |
| 12 | + "\"re-balancer\",\"trigger\":\"channel 7413345453234435345 " + |
| 13 | + "depleted\",\"intent\":\"increase outbound liquidity by " + |
| 14 | + "2000000 sats\"}" |
| 15 | + |
| 16 | + testRulesCaveat = "lnd-custom lit-mac-fw rules:[{\"name\":" + |
| 17 | + "\"re-balance-limits\",\"restrictions\":" + |
| 18 | + "{\"first-hop-ignore-list\":\"03abcd...,02badb01...\"," + |
| 19 | + "\"max-hops\":\"4\",\"off-chain-fees-sats\":\"10\"}}," + |
| 20 | + "{\"name\":\"time-limits\",\"restrictions\":" + |
| 21 | + "{\"re-balance-min-interval-seconds\":\"3600\"}}]" |
| 22 | +) |
| 23 | + |
| 24 | +// TestInterceptMetaInfo makes sure that a meta information struct can be |
| 25 | +// formatted as a caveat and then parsed again successfully. |
| 26 | +func TestInterceptMetaInfo(t *testing.T) { |
| 27 | + info := &InterceptMetaInfo{ |
| 28 | + ActorName: "re-balancer", |
| 29 | + Trigger: "channel 7413345453234435345 depleted", |
| 30 | + Intent: "increase outbound liquidity by 2000000 sats", |
| 31 | + } |
| 32 | + |
| 33 | + caveat, err := info.ToCaveat() |
| 34 | + require.NoError(t, err) |
| 35 | + |
| 36 | + require.Equal(t, testMetaCaveat, caveat) |
| 37 | + |
| 38 | + parsedInfo, err := ParseMetaInfoCaveat(caveat) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + require.Equal(t, info, parsedInfo) |
| 42 | +} |
| 43 | + |
| 44 | +// TestParseMetaInfoCaveat makes sure the meta information caveat parsing works |
| 45 | +// as expected. |
| 46 | +func TestParseMetaInfoCaveat(t *testing.T) { |
| 47 | + testCases := []struct { |
| 48 | + name string |
| 49 | + input string |
| 50 | + err error |
| 51 | + result *InterceptMetaInfo |
| 52 | + }{{ |
| 53 | + name: "empty string", |
| 54 | + input: "", |
| 55 | + err: ErrNoMetaInfoCaveat, |
| 56 | + }, { |
| 57 | + name: "prefix only", |
| 58 | + input: "lnd-custom lit-mac-fw meta:", |
| 59 | + err: ErrNoMetaInfoCaveat, |
| 60 | + }, { |
| 61 | + name: "invalid JSON", |
| 62 | + input: "lnd-custom lit-mac-fw meta:bar", |
| 63 | + err: fmt.Errorf("error unmarshaling JSON: invalid character " + |
| 64 | + "'b' looking for beginning of value"), |
| 65 | + }, { |
| 66 | + name: "empty JSON", |
| 67 | + input: "lnd-custom lit-mac-fw meta:{}", |
| 68 | + result: &InterceptMetaInfo{}, |
| 69 | + }} |
| 70 | + |
| 71 | + for _, tc := range testCases { |
| 72 | + tc := tc |
| 73 | + |
| 74 | + t.Run(tc.name, func(tt *testing.T) { |
| 75 | + i, err := ParseMetaInfoCaveat(tc.input) |
| 76 | + |
| 77 | + if tc.err != nil { |
| 78 | + require.Error(tt, err) |
| 79 | + require.Equal(tt, tc.err, err) |
| 80 | + |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + require.NoError(tt, err) |
| 85 | + require.Equal(tt, tc.result, i) |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// TestInterceptRule makes sure that a rules list struct can be formatted as a |
| 91 | +// caveat and then parsed again successfully. |
| 92 | +func TestInterceptRule(t *testing.T) { |
| 93 | + rules := []*InterceptRule{{ |
| 94 | + Name: "re-balance-limits", |
| 95 | + Restrictions: map[string]string{ |
| 96 | + "off-chain-fees-sats": "10", |
| 97 | + "max-hops": "4", |
| 98 | + "first-hop-ignore-list": "03abcd...,02badb01...", |
| 99 | + }, |
| 100 | + }, { |
| 101 | + Name: "time-limits", |
| 102 | + Restrictions: map[string]string{ |
| 103 | + "re-balance-min-interval-seconds": "3600", |
| 104 | + }, |
| 105 | + }} |
| 106 | + |
| 107 | + caveat, err := RulesToCaveat(rules) |
| 108 | + require.NoError(t, err) |
| 109 | + |
| 110 | + require.Equal(t, testRulesCaveat, caveat) |
| 111 | + |
| 112 | + parsedRules, err := ParseRuleCaveat(caveat) |
| 113 | + require.NoError(t, err) |
| 114 | + |
| 115 | + require.Equal(t, rules, parsedRules) |
| 116 | +} |
| 117 | + |
| 118 | +// TestParseRulesCaveat makes sure the rule list caveat parsing works as |
| 119 | +// expected. |
| 120 | +func TestParseRulesCaveat(t *testing.T) { |
| 121 | + testCases := []struct { |
| 122 | + name string |
| 123 | + input string |
| 124 | + err error |
| 125 | + result []*InterceptRule |
| 126 | + }{{ |
| 127 | + name: "empty string", |
| 128 | + input: "", |
| 129 | + err: ErrNoRulesCaveat, |
| 130 | + }, { |
| 131 | + name: "prefix only", |
| 132 | + input: "lnd-custom lit-mac-fw rules:", |
| 133 | + err: ErrNoRulesCaveat, |
| 134 | + }, { |
| 135 | + name: "invalid JSON", |
| 136 | + input: "lnd-custom lit-mac-fw rules:bar", |
| 137 | + err: fmt.Errorf("error unmarshaling JSON: invalid character " + |
| 138 | + "'b' looking for beginning of value"), |
| 139 | + }, { |
| 140 | + name: "empty JSON", |
| 141 | + input: "lnd-custom lit-mac-fw rules:[]", |
| 142 | + result: []*InterceptRule{}, |
| 143 | + }, { |
| 144 | + name: "empty rules", |
| 145 | + input: "lnd-custom lit-mac-fw rules:[{}, {}]", |
| 146 | + result: []*InterceptRule{{}, {}}, |
| 147 | + }, { |
| 148 | + name: "valid rules", |
| 149 | + input: "lnd-custom lit-mac-fw rules:[{\"name\":\"foo\"}, " + |
| 150 | + "{\"restrictions\":{\"foo\":\"bar\"}}]", |
| 151 | + result: []*InterceptRule{{ |
| 152 | + Name: "foo", |
| 153 | + }, { |
| 154 | + Restrictions: map[string]string{ |
| 155 | + "foo": "bar", |
| 156 | + }, |
| 157 | + }}, |
| 158 | + }} |
| 159 | + |
| 160 | + for _, tc := range testCases { |
| 161 | + tc := tc |
| 162 | + |
| 163 | + t.Run(tc.name, func(tt *testing.T) { |
| 164 | + i, err := ParseRuleCaveat(tc.input) |
| 165 | + |
| 166 | + if tc.err != nil { |
| 167 | + require.Error(tt, err) |
| 168 | + require.Equal(tt, tc.err, err) |
| 169 | + |
| 170 | + return |
| 171 | + } |
| 172 | + |
| 173 | + require.NoError(tt, err) |
| 174 | + require.Equal(tt, tc.result, i) |
| 175 | + }) |
| 176 | + } |
| 177 | +} |
0 commit comments