|
| 1 | +package connectionlimiter |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/ProtonMail/gluon/imap" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func imapID(name string) imap.IMAPID { |
| 12 | + return imap.IMAPID{Name: name} |
| 13 | +} |
| 14 | + |
| 15 | +func TestTryBind_FirstConnectionAllowed(t *testing.T) { |
| 16 | + limits := NewLimits(map[Client]int{ClientAppleMail: 3}, 1) |
| 17 | + l := NewConnectionLimiter(limits) |
| 18 | + allowed, key, current, max := l.TryBind(1, imapID("Apple Mail")) |
| 19 | + require.True(t, allowed) |
| 20 | + assert.Equal(t, ClientAppleMail, key) |
| 21 | + assert.Equal(t, 1, current) |
| 22 | + assert.Equal(t, 3, max) |
| 23 | +} |
| 24 | + |
| 25 | +func TestTryBind_OverLimitDenied(t *testing.T) { |
| 26 | + limits := NewLimits(map[Client]int{ClientAppleMail: 2}, 1) |
| 27 | + l := NewConnectionLimiter(limits) |
| 28 | + l.TryBind(1, imapID("Apple Mail")) |
| 29 | + l.TryBind(2, imapID("Apple Mail")) |
| 30 | + allowed, key, current, max := l.TryBind(3, imapID("Apple Mail")) |
| 31 | + require.False(t, allowed) |
| 32 | + assert.Equal(t, ClientAppleMail, key) |
| 33 | + assert.Equal(t, 2, current) |
| 34 | + assert.Equal(t, 2, max) |
| 35 | +} |
| 36 | + |
| 37 | +func TestUnbind_FreesSlot(t *testing.T) { |
| 38 | + limits := NewLimits(map[Client]int{ClientAppleMail: 2}, 1) |
| 39 | + l := NewConnectionLimiter(limits) |
| 40 | + l.TryBind(1, imapID("Apple Mail")) |
| 41 | + l.TryBind(2, imapID("Apple Mail")) |
| 42 | + l.Unbind(1) |
| 43 | + allowed, _, current, _ := l.TryBind(3, imapID("Apple Mail")) |
| 44 | + require.True(t, allowed) |
| 45 | + assert.Equal(t, 2, current) |
| 46 | +} |
| 47 | + |
| 48 | +func TestUnbind_UnknownSessionNoop(t *testing.T) { |
| 49 | + limits := NewLimits(map[Client]int{ClientAppleMail: 2}, 1) |
| 50 | + l := NewConnectionLimiter(limits) |
| 51 | + l.TryBind(1, imapID("Apple Mail")) |
| 52 | + l.Unbind(999) // never bound |
| 53 | + _, _, current, _ := l.TryBind(2, imapID("Apple Mail")) |
| 54 | + assert.Equal(t, 2, current) |
| 55 | +} |
| 56 | + |
| 57 | +func TestTryBind_SameSessionSameClientNoop(t *testing.T) { |
| 58 | + limits := NewLimits(map[Client]int{ClientAppleMail: 2}, 1) |
| 59 | + l := NewConnectionLimiter(limits) |
| 60 | + l.TryBind(1, imapID("Apple Mail")) |
| 61 | + allowed, key, current, max := l.TryBind(1, imapID("Apple Mail")) |
| 62 | + require.True(t, allowed) |
| 63 | + assert.Equal(t, ClientAppleMail, key) |
| 64 | + assert.Equal(t, 1, current) |
| 65 | + assert.Equal(t, 2, max) |
| 66 | +} |
| 67 | + |
| 68 | +func TestTryBind_RebindToDifferentClient(t *testing.T) { |
| 69 | + limits := NewLimits(map[Client]int{ |
| 70 | + ClientAppleMail: 2, |
| 71 | + ClientOutlook: 2, |
| 72 | + }, 1) |
| 73 | + l := NewConnectionLimiter(limits) |
| 74 | + l.TryBind(1, imapID("Apple Mail")) |
| 75 | + allowed, key, cur, _ := l.TryBind(1, imapID("Microsoft Outlook")) |
| 76 | + require.True(t, allowed) |
| 77 | + assert.Equal(t, ClientOutlook, key) |
| 78 | + assert.Equal(t, 1, cur) |
| 79 | + _, _, appleCur, _ := l.TryBind(2, imapID("Apple Mail")) |
| 80 | + assert.Equal(t, 1, appleCur) |
| 81 | +} |
| 82 | + |
| 83 | +func TestTryBind_UnknownClientUsesUnknownLimit(t *testing.T) { |
| 84 | + limits := NewLimits(map[Client]int{ClientAppleMail: 10}, 2) |
| 85 | + l := NewConnectionLimiter(limits) |
| 86 | + l.TryBind(1, imapID("SomeOtherClient")) |
| 87 | + l.TryBind(2, imapID("Unknown")) |
| 88 | + allowed, key, current, max := l.TryBind(3, imapID("Custom")) |
| 89 | + require.False(t, allowed) |
| 90 | + assert.Equal(t, ClientUnknown, key) |
| 91 | + assert.Equal(t, 2, current) |
| 92 | + assert.Equal(t, 2, max) |
| 93 | +} |
| 94 | + |
| 95 | +func TestTryBind_UnlimitedLimit(t *testing.T) { |
| 96 | + limits := NewLimits(map[Client]int{ClientAppleMail: 0}, 1) |
| 97 | + l := NewConnectionLimiter(limits) |
| 98 | + for i := 1; i <= 5; i++ { |
| 99 | + allowed, key, current, max := l.TryBind(i, imapID("Apple Mail")) |
| 100 | + require.True(t, allowed) |
| 101 | + assert.Equal(t, ClientAppleMail, key) |
| 102 | + assert.Equal(t, i, current) |
| 103 | + assert.Equal(t, 0, max) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestNormalizeClientKey_AppleMail(t *testing.T) { |
| 108 | + assert.Equal(t, ClientAppleMail, normalizeClientKey(imapID("Apple Mail"))) |
| 109 | + assert.Equal(t, ClientAppleMail, normalizeClientKey(imapID("macos mail"))) |
| 110 | + assert.Equal(t, ClientAppleMail, normalizeClientKey(imapID("OS X Mail"))) |
| 111 | +} |
| 112 | +func TestNormalizeClientKey_OutlookAndThunderbird(t *testing.T) { |
| 113 | + assert.Equal(t, ClientOutlook, normalizeClientKey(imapID("Microsoft Outlook"))) |
| 114 | + assert.Equal(t, ClientThunderbird, normalizeClientKey(imapID("Thunderbird"))) |
| 115 | +} |
| 116 | +func TestNormalizeClientKey_Unknown(t *testing.T) { |
| 117 | + assert.Equal(t, ClientUnknown, normalizeClientKey(imapID(""))) |
| 118 | + assert.Equal(t, ClientUnknown, normalizeClientKey(imapID("Custom Client"))) |
| 119 | +} |
0 commit comments