Search in sources :

Example 11 with CommandError

use of org.apache.pulsar.common.api.proto.PulsarApi.CommandError in project incubator-pulsar by apache.

the class ServerCnxTest method testClusterAccess.

@Test(timeOut = 30000)
public void testClusterAccess() throws Exception {
    svcConfig.setAuthorizationEnabled(true);
    AuthorizationService authorizationService = spy(new AuthorizationService(svcConfig, configCacheService));
    Field providerField = AuthorizationService.class.getDeclaredField("provider");
    providerField.setAccessible(true);
    PulsarAuthorizationProvider authorizationProvider = spy(new PulsarAuthorizationProvider(svcConfig, configCacheService));
    providerField.set(authorizationService, authorizationProvider);
    doReturn(authorizationService).when(brokerService).getAuthorizationService();
    doReturn(true).when(brokerService).isAuthorizationEnabled();
    doReturn(false).when(authorizationProvider).isSuperUser(Mockito.anyString());
    doReturn(CompletableFuture.completedFuture(true)).when(authorizationProvider).checkPermission(any(TopicName.class), Mockito.anyString(), any(AuthAction.class));
    resetChannel();
    setChannelConnected();
    ByteBuf clientCommand = Commands.newProducer(successTopicName, 1, /* producer id */
    1, /* request id */
    "prod-name", Collections.emptyMap());
    channel.writeInbound(clientCommand);
    assertTrue(getResponse() instanceof CommandProducerSuccess);
    resetChannel();
    setChannelConnected();
    clientCommand = Commands.newProducer(topicWithNonLocalCluster, 1, /* producer id */
    1, /* request id */
    "prod-name", Collections.emptyMap());
    channel.writeInbound(clientCommand);
    assertTrue(getResponse() instanceof CommandError);
}
Also used : Field(java.lang.reflect.Field) AuthorizationService(org.apache.pulsar.broker.authorization.AuthorizationService) CommandProducerSuccess(org.apache.pulsar.common.api.proto.PulsarApi.CommandProducerSuccess) PulsarAuthorizationProvider(org.apache.pulsar.broker.authorization.PulsarAuthorizationProvider) CommandError(org.apache.pulsar.common.api.proto.PulsarApi.CommandError) ByteBuf(io.netty.buffer.ByteBuf) TopicName(org.apache.pulsar.common.naming.TopicName) AuthAction(org.apache.pulsar.common.policies.data.AuthAction) Test(org.testng.annotations.Test)

Example 12 with CommandError

use of org.apache.pulsar.common.api.proto.PulsarApi.CommandError in project incubator-pulsar by apache.

the class ServerCnxTest method testProducerCommandWithAuthorizationNegative.

public void testProducerCommandWithAuthorizationNegative() throws Exception {
    AuthorizationService authorizationService = mock(AuthorizationService.class);
    doReturn(CompletableFuture.completedFuture(false)).when(authorizationService).canProduceAsync(Mockito.any(), Mockito.any(), Mockito.any());
    doReturn(authorizationService).when(brokerService).getAuthorizationService();
    doReturn(true).when(brokerService).isAuthenticationEnabled();
    doReturn(true).when(brokerService).isAuthorizationEnabled();
    doReturn("prod1").when(brokerService).generateUniqueProducerName();
    resetChannel();
    setChannelConnected();
    ByteBuf clientCommand = Commands.newProducer(successTopicName, 1, /* producer id */
    1, /* request id */
    null, Collections.emptyMap());
    channel.writeInbound(clientCommand);
    assertTrue(getResponse() instanceof CommandError);
    channel.finish();
}
Also used : AuthorizationService(org.apache.pulsar.broker.authorization.AuthorizationService) CommandError(org.apache.pulsar.common.api.proto.PulsarApi.CommandError) ByteBuf(io.netty.buffer.ByteBuf)

Example 13 with CommandError

use of org.apache.pulsar.common.api.proto.PulsarApi.CommandError in project incubator-pulsar by apache.

the class ServerCnxTest method testInvalidTopicOnProducer.

@Test(timeOut = 30000)
public void testInvalidTopicOnProducer() throws Exception {
    resetChannel();
    setChannelConnected();
    String invalidTopicName = "xx/ass/aa/aaa";
    resetChannel();
    setChannelConnected();
    ByteBuf clientCommand = Commands.newProducer(invalidTopicName, 1, /* producer id */
    1, /* request id */
    "prod-name", Collections.emptyMap());
    channel.writeInbound(clientCommand);
    Object obj = getResponse();
    assertEquals(obj.getClass(), CommandError.class);
    CommandError res = (CommandError) obj;
    assertEquals(res.getError(), ServerError.InvalidTopicName);
    channel.finish();
}
Also used : Matchers.anyObject(org.mockito.Matchers.anyObject) ByteString(com.google.protobuf.ByteString) CommandError(org.apache.pulsar.common.api.proto.PulsarApi.CommandError) ByteBuf(io.netty.buffer.ByteBuf) Test(org.testng.annotations.Test)

Example 14 with CommandError

use of org.apache.pulsar.common.api.proto.PulsarApi.CommandError in project incubator-pulsar by apache.

the class ServerCnxTest method testDuplicateConcurrentProducerCommand.

@Test(timeOut = 5000)
public void testDuplicateConcurrentProducerCommand() throws Exception {
    resetChannel();
    setChannelConnected();
    CompletableFuture<Topic> delayFuture = new CompletableFuture<>();
    doReturn(delayFuture).when(brokerService).getTopic(any(String.class));
    // Create producer first time
    ByteBuf clientCommand = Commands.newProducer(successTopicName, 1, /* producer id */
    1, /* request id */
    "prod-name", Collections.emptyMap());
    channel.writeInbound(clientCommand);
    // Create producer second time
    clientCommand = Commands.newProducer(successTopicName, 1, /* producer id */
    1, /* request id */
    "prod-name", Collections.emptyMap());
    channel.writeInbound(clientCommand);
    Object response = getResponse();
    assertTrue(response instanceof CommandError);
    CommandError error = (CommandError) response;
    assertEquals(error.getError(), ServerError.ServiceNotReady);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Matchers.anyObject(org.mockito.Matchers.anyObject) ByteString(com.google.protobuf.ByteString) CommandError(org.apache.pulsar.common.api.proto.PulsarApi.CommandError) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) ByteBuf(io.netty.buffer.ByteBuf) Test(org.testng.annotations.Test)

Example 15 with CommandError

use of org.apache.pulsar.common.api.proto.PulsarApi.CommandError in project incubator-pulsar by apache.

the class ServerCnxTest method testDuplicateConcurrentSubscribeCommand.

@Test(timeOut = 5000)
public void testDuplicateConcurrentSubscribeCommand() throws Exception {
    resetChannel();
    setChannelConnected();
    CompletableFuture<Topic> delayFuture = new CompletableFuture<>();
    doReturn(delayFuture).when(brokerService).getTopic(any(String.class));
    // Create subscriber first time
    ByteBuf clientCommand = // 
    Commands.newSubscribe(// 
    successTopicName, successSubName, 1, /* consumer id */
    1, /* request id */
    SubType.Exclusive, 0, "test");
    channel.writeInbound(clientCommand);
    // Create producer second time
    clientCommand = // 
    Commands.newSubscribe(// 
    successTopicName, successSubName, 1, /* consumer id */
    1, /* request id */
    SubType.Exclusive, 0, "test");
    channel.writeInbound(clientCommand);
    Object response = getResponse();
    assertTrue(response instanceof CommandError);
    CommandError error = (CommandError) response;
    assertEquals(error.getError(), ServerError.ServiceNotReady);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Matchers.anyObject(org.mockito.Matchers.anyObject) ByteString(com.google.protobuf.ByteString) CommandError(org.apache.pulsar.common.api.proto.PulsarApi.CommandError) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) ByteBuf(io.netty.buffer.ByteBuf) Test(org.testng.annotations.Test)

Aggregations

CommandError (org.apache.pulsar.common.api.proto.PulsarApi.CommandError)16 ByteBuf (io.netty.buffer.ByteBuf)15 Test (org.testng.annotations.Test)14 ByteString (com.google.protobuf.ByteString)7 Matchers.anyObject (org.mockito.Matchers.anyObject)7 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)6 AuthorizationService (org.apache.pulsar.broker.authorization.AuthorizationService)4 CommandProducerSuccess (org.apache.pulsar.common.api.proto.PulsarApi.CommandProducerSuccess)3 Field (java.lang.reflect.Field)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 PulsarAuthorizationProvider (org.apache.pulsar.broker.authorization.PulsarAuthorizationProvider)2 CommandSuccess (org.apache.pulsar.common.api.proto.PulsarApi.CommandSuccess)2 TopicName (org.apache.pulsar.common.naming.TopicName)2 Policies (org.apache.pulsar.common.policies.data.Policies)2 AuthenticationException (javax.naming.AuthenticationException)1 AuthenticationDataCommand (org.apache.pulsar.broker.authentication.AuthenticationDataCommand)1 AuthenticationService (org.apache.pulsar.broker.authentication.AuthenticationService)1 ConfigurationCacheService (org.apache.pulsar.broker.cache.ConfigurationCacheService)1 AuthAction (org.apache.pulsar.common.policies.data.AuthAction)1