Skip to content

Commit

Permalink
Moves tests to correct class
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Sep 26, 2023
1 parent 054dc73 commit db6efd8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.security.user.AuthCredentials;

import java.net.InetAddress;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class AddressBasedRateLimiterTest {

private final static byte[] PASSWORD = new byte[] { '1', '2', '3' };

@Test
public void simpleTest() throws Exception {
Settings settings = Settings.builder().put("allowed_tries", 3).build();

UserNameBasedRateLimiter rateLimiter = new UserNameBasedRateLimiter(settings, null);
AddressBasedRateLimiter rateLimiter = new AddressBasedRateLimiter(settings, null);

assertFalse(rateLimiter.isBlocked("a"));
rateLimiter.onAuthFailure(null, new AuthCredentials("a", PASSWORD), null);
assertFalse(rateLimiter.isBlocked("a"));
rateLimiter.onAuthFailure(null, new AuthCredentials("a", PASSWORD), null);
assertFalse(rateLimiter.isBlocked("a"));
rateLimiter.onAuthFailure(null, new AuthCredentials("a", PASSWORD), null);
assertTrue(rateLimiter.isBlocked("a"));
assertFalse(rateLimiter.isBlocked(InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 })));
rateLimiter.onAuthFailure(InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 }), null, null);
assertFalse(rateLimiter.isBlocked(InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 })));
rateLimiter.onAuthFailure(InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 }), null, null);
assertFalse(rateLimiter.isBlocked(InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 })));
rateLimiter.onAuthFailure(InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 }), null, null);
assertTrue(rateLimiter.isBlocked(InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 })));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,28 @@
import org.junit.Test;

import org.opensearch.common.settings.Settings;
import org.opensearch.security.user.AuthCredentials;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class UserNameBasedRateLimiterTest {

private final static byte[] PASSWORD = new byte[] { '1', '2', '3' };

@Test
public void simpleTest() throws Exception {
Settings settings = Settings.builder().put("allowed_tries", 3).build();

AddressBasedRateLimiter rateLimiter = new AddressBasedRateLimiter(settings, null);
UserNameBasedRateLimiter rateLimiter = new UserNameBasedRateLimiter(settings, null);

assertFalse(rateLimiter.isBlocked(InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 })));
rateLimiter.onAuthFailure(InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 }), null, null);
assertFalse(rateLimiter.isBlocked(InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 })));
rateLimiter.onAuthFailure(InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 }), null, null);
assertFalse(rateLimiter.isBlocked(InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 })));
rateLimiter.onAuthFailure(InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 }), null, null);
assertTrue(rateLimiter.isBlocked(InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 })));
assertFalse(rateLimiter.isBlocked("a"));
rateLimiter.onAuthFailure(null, new AuthCredentials("a", PASSWORD), null);
assertFalse(rateLimiter.isBlocked("a"));
rateLimiter.onAuthFailure(null, new AuthCredentials("a", PASSWORD), null);
assertFalse(rateLimiter.isBlocked("a"));
rateLimiter.onAuthFailure(null, new AuthCredentials("a", PASSWORD), null);
assertTrue(rateLimiter.isBlocked("a"));

}
}

0 comments on commit db6efd8

Please sign in to comment.