|
|
@@ -1293,7 +1293,7 @@ func TestServer_MatrixGateway_Push_Success(t *testing.T) {
|
|
|
|
|
|
func TestServer_MatrixGateway_Push_Failure_NoSubscriber(t *testing.T) {
|
|
|
c := newTestConfig(t)
|
|
|
- c.EnableRateVisitor = true
|
|
|
+ c.VisitorSubscriberRateLimiting = true
|
|
|
s := newTestServer(t, c)
|
|
|
notification := `{"notification":{"devices":[{"pushkey":"http://127.0.0.1:12345/mytopic?up=1"}]}}`
|
|
|
response := request(t, s, "POST", "/_matrix/push/v1/notify", notification, nil)
|
|
|
@@ -2032,7 +2032,7 @@ func TestServer_AnonymousUser_And_NonTierUser_Are_Same_Visitor(t *testing.T) {
|
|
|
func TestServer_SubscriberRateLimiting_Success(t *testing.T) {
|
|
|
c := newTestConfigWithAuthFile(t)
|
|
|
c.VisitorRequestLimitBurst = 3
|
|
|
- c.EnableRateVisitor = true
|
|
|
+ c.VisitorSubscriberRateLimiting = true
|
|
|
s := newTestServer(t, c)
|
|
|
|
|
|
// "Register" visitor 1.2.3.4 to topic "subscriber1topic" as a rate limit visitor
|
|
|
@@ -2044,6 +2044,7 @@ func TestServer_SubscriberRateLimiting_Success(t *testing.T) {
|
|
|
}, subscriber1Fn)
|
|
|
require.Equal(t, 200, rr.Code)
|
|
|
require.Equal(t, "", rr.Body.String())
|
|
|
+ require.Equal(t, "1.2.3.4", s.topics["subscriber1topic"].rateVisitor.ip.String())
|
|
|
|
|
|
// "Register" visitor 8.7.7.1 to topic "up012345678912" as a rate limit visitor (implicitly via topic name)
|
|
|
subscriber2Fn := func(r *http.Request) {
|
|
|
@@ -2052,6 +2053,7 @@ func TestServer_SubscriberRateLimiting_Success(t *testing.T) {
|
|
|
rr = request(t, s, "GET", "/up012345678912/json?poll=1", "", nil, subscriber2Fn)
|
|
|
require.Equal(t, 200, rr.Code)
|
|
|
require.Equal(t, "", rr.Body.String())
|
|
|
+ require.Equal(t, "8.7.7.1", s.topics["up012345678912"].rateVisitor.ip.String())
|
|
|
|
|
|
// Publish 2 messages to "subscriber1topic" as visitor 9.9.9.9. It'd be 3 normally, but the
|
|
|
// GET request before is also counted towards the request limiter.
|
|
|
@@ -2083,10 +2085,47 @@ func TestServer_SubscriberRateLimiting_Success(t *testing.T) {
|
|
|
require.Equal(t, 429, rr.Code)
|
|
|
}
|
|
|
|
|
|
+func TestServer_SubscriberRateLimiting_NotEnabled_Failed(t *testing.T) {
|
|
|
+ c := newTestConfigWithAuthFile(t)
|
|
|
+ c.VisitorRequestLimitBurst = 3
|
|
|
+ c.VisitorSubscriberRateLimiting = false
|
|
|
+ s := newTestServer(t, c)
|
|
|
+
|
|
|
+ // Subscriber rate limiting is disabled!
|
|
|
+
|
|
|
+ // Registering visitor 1.2.3.4 to topic has no effect
|
|
|
+ rr := request(t, s, "GET", "/subscriber1topic/json?poll=1", "", map[string]string{
|
|
|
+ "Rate-Topics": "subscriber1topic",
|
|
|
+ }, func(r *http.Request) {
|
|
|
+ r.RemoteAddr = "1.2.3.4"
|
|
|
+ })
|
|
|
+ require.Equal(t, 200, rr.Code)
|
|
|
+ require.Equal(t, "", rr.Body.String())
|
|
|
+ require.Nil(t, s.topics["subscriber1topic"].rateVisitor)
|
|
|
+
|
|
|
+ // Registering visitor 8.7.7.1 to topic has no effect
|
|
|
+ rr = request(t, s, "GET", "/up012345678912/json?poll=1", "", nil, func(r *http.Request) {
|
|
|
+ r.RemoteAddr = "8.7.7.1"
|
|
|
+ })
|
|
|
+ require.Equal(t, 200, rr.Code)
|
|
|
+ require.Equal(t, "", rr.Body.String())
|
|
|
+ require.Nil(t, s.topics["up012345678912"].rateVisitor)
|
|
|
+
|
|
|
+ // Publish 3 messages to "subscriber1topic" as visitor 9.9.9.9
|
|
|
+ for i := 0; i < 3; i++ {
|
|
|
+ rr := request(t, s, "PUT", "/subscriber1topic", "some message", nil)
|
|
|
+ require.Equal(t, 200, rr.Code)
|
|
|
+ }
|
|
|
+ rr = request(t, s, "PUT", "/subscriber1topic", "some message", nil)
|
|
|
+ require.Equal(t, 429, rr.Code)
|
|
|
+ rr = request(t, s, "PUT", "/up012345678912", "some message", nil)
|
|
|
+ require.Equal(t, 429, rr.Code)
|
|
|
+}
|
|
|
+
|
|
|
func TestServer_SubscriberRateLimiting_UP_Only(t *testing.T) {
|
|
|
c := newTestConfigWithAuthFile(t)
|
|
|
c.VisitorRequestLimitBurst = 3
|
|
|
- c.EnableRateVisitor = true
|
|
|
+ c.VisitorSubscriberRateLimiting = true
|
|
|
s := newTestServer(t, c)
|
|
|
|
|
|
// "Register" 5 different UnifiedPush visitors
|
|
|
@@ -2110,7 +2149,7 @@ func TestServer_SubscriberRateLimiting_UP_Only(t *testing.T) {
|
|
|
func TestServer_Matrix_SubscriberRateLimiting_UP_Only(t *testing.T) {
|
|
|
c := newTestConfig(t)
|
|
|
c.VisitorRequestLimitBurst = 3
|
|
|
- c.EnableRateVisitor = true
|
|
|
+ c.VisitorSubscriberRateLimiting = true
|
|
|
s := newTestServer(t, c)
|
|
|
|
|
|
// "Register" 5 different UnifiedPush visitors
|
|
|
@@ -2138,7 +2177,7 @@ func TestServer_Matrix_SubscriberRateLimiting_UP_Only(t *testing.T) {
|
|
|
func TestServer_SubscriberRateLimiting_VisitorExpiration(t *testing.T) {
|
|
|
c := newTestConfig(t)
|
|
|
c.VisitorRequestLimitBurst = 3
|
|
|
- c.EnableRateVisitor = true
|
|
|
+ c.VisitorSubscriberRateLimiting = true
|
|
|
s := newTestServer(t, c)
|
|
|
|
|
|
// "Register" rate visitor
|
|
|
@@ -2174,7 +2213,7 @@ func TestServer_SubscriberRateLimiting_VisitorExpiration(t *testing.T) {
|
|
|
func TestServer_SubscriberRateLimiting_ProtectedTopics(t *testing.T) {
|
|
|
c := newTestConfigWithAuthFile(t)
|
|
|
c.AuthDefault = user.PermissionDenyAll
|
|
|
- c.EnableRateVisitor = true
|
|
|
+ c.VisitorSubscriberRateLimiting = true
|
|
|
s := newTestServer(t, c)
|
|
|
|
|
|
// Create some ACLs
|
|
|
@@ -2222,7 +2261,7 @@ func TestServer_SubscriberRateLimiting_ProtectedTopics(t *testing.T) {
|
|
|
func TestServer_SubscriberRateLimiting_ProtectedTopics_WithDefaultReadWrite(t *testing.T) {
|
|
|
c := newTestConfigWithAuthFile(t)
|
|
|
c.AuthDefault = user.PermissionReadWrite
|
|
|
- c.EnableRateVisitor = true
|
|
|
+ c.VisitorSubscriberRateLimiting = true
|
|
|
s := newTestServer(t, c)
|
|
|
|
|
|
// Create some ACLs
|
|
|
@@ -2342,5 +2381,5 @@ func waitForWithMaxWait(t *testing.T, maxWait time.Duration, f func() bool) {
|
|
|
}
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
}
|
|
|
- t.Fatalf("Function f did not succeed after %v: %s", maxWait, debug.Stack())
|
|
|
+ t.Fatalf("Function f did not succeed after %v: %v", maxWait, string(debug.Stack()))
|
|
|
}
|