Search in sources :

Example 6 with CommandError

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

the class ServerCnxTest method testSubscribeCommandWithAuthorizationNegative.

@Test(timeOut = 30000)
public void testSubscribeCommandWithAuthorizationNegative() throws Exception {
    AuthorizationService authorizationService = mock(AuthorizationService.class);
    doReturn(CompletableFuture.completedFuture(false)).when(authorizationService).canConsumeAsync(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
    doReturn(authorizationService).when(brokerService).getAuthorizationService();
    doReturn(true).when(brokerService).isAuthenticationEnabled();
    doReturn(true).when(brokerService).isAuthorizationEnabled();
    resetChannel();
    setChannelConnected();
    // test SUBSCRIBE on topic and cursor creation success
    ByteBuf clientCommand = // 
    Commands.newSubscribe(// 
    successTopicName, successSubName, 1, /* consumer id */
    1, /* request id */
    SubType.Exclusive, 0, "test");
    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) Test(org.testng.annotations.Test)

Example 7 with CommandError

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

the class ServerCnxTest method testUnsupportedBatchMsgSubscribeCommand.

@Test(timeOut = 30000)
public void testUnsupportedBatchMsgSubscribeCommand() throws Exception {
    final String failSubName = "failSub";
    resetChannel();
    setChannelConnected();
    setConnectionVersion(ProtocolVersion.v3.getNumber());
    doReturn(false).when(brokerService).isAuthenticationEnabled();
    doReturn(false).when(brokerService).isAuthorizationEnabled();
    // test SUBSCRIBE on topic and cursor creation success
    ByteBuf clientCommand = // 
    Commands.newSubscribe(// 
    successTopicName, successSubName, 1, /* consumer id */
    1, /* request id */
    SubType.Exclusive, 0, /* priority */
    "test");
    channel.writeInbound(clientCommand);
    assertTrue(getResponse() instanceof CommandSuccess);
    PersistentTopic topicRef = (PersistentTopic) brokerService.getTopicReference(successTopicName);
    topicRef.markBatchMessagePublished();
    // test SUBSCRIBE on topic and cursor creation success
    clientCommand = Commands.newSubscribe(successTopicName, failSubName, 2, 2, SubType.Exclusive, 0, /* priority */
    "test");
    channel.writeInbound(clientCommand);
    Object response = getResponse();
    assertTrue(response instanceof CommandError);
    assertTrue(((CommandError) response).getError().equals(ServerError.UnsupportedVersionError));
    // Server will not close the connection
    assertTrue(channel.isOpen());
    channel.finish();
}
Also used : CommandSuccess(org.apache.pulsar.common.api.proto.PulsarApi.CommandSuccess) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) 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 8 with CommandError

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

the class ServerCnxTest method testNonExistentTopic.

@Test(timeOut = 30000)
public void testNonExistentTopic() throws Exception {
    ZooKeeperDataCache<Policies> zkDataCache = mock(ZooKeeperDataCache.class);
    ConfigurationCacheService configCacheService = mock(ConfigurationCacheService.class);
    doReturn(configCacheService).when(pulsar).getConfigurationCache();
    doReturn(zkDataCache).when(configCacheService).policiesCache();
    doReturn(CompletableFuture.completedFuture(Optional.empty())).when(zkDataCache).getAsync(matches(".*nonexistent.*"));
    AuthorizationService authorizationService = spy(new AuthorizationService(svcConfig, configCacheService));
    doReturn(authorizationService).when(brokerService).getAuthorizationService();
    doReturn(true).when(brokerService).isAuthorizationEnabled();
    svcConfig.setAuthorizationEnabled(true);
    Field providerField = AuthorizationService.class.getDeclaredField("provider");
    providerField.setAccessible(true);
    PulsarAuthorizationProvider authorizationProvider = spy(new PulsarAuthorizationProvider(svcConfig, configCacheService));
    providerField.set(authorizationService, authorizationProvider);
    doReturn(false).when(authorizationProvider).isSuperUser(Mockito.anyString());
    // Test producer creation
    resetChannel();
    setChannelConnected();
    ByteBuf newProducerCmd = Commands.newProducer(nonExistentTopicName, 1, /* producer id */
    1, /* request id */
    "prod-name", Collections.emptyMap());
    channel.writeInbound(newProducerCmd);
    assertTrue(getResponse() instanceof CommandError);
    channel.finish();
    // Test consumer creation
    resetChannel();
    setChannelConnected();
    ByteBuf newSubscribeCmd = // 
    Commands.newSubscribe(// 
    nonExistentTopicName, successSubName, 1, /* consumer id */
    1, /* request id */
    SubType.Exclusive, 0, "test");
    channel.writeInbound(newSubscribeCmd);
    assertTrue(getResponse() instanceof CommandError);
}
Also used : Field(java.lang.reflect.Field) Policies(org.apache.pulsar.common.policies.data.Policies) AuthorizationService(org.apache.pulsar.broker.authorization.AuthorizationService) ConfigurationCacheService(org.apache.pulsar.broker.cache.ConfigurationCacheService) PulsarAuthorizationProvider(org.apache.pulsar.broker.authorization.PulsarAuthorizationProvider) CommandError(org.apache.pulsar.common.api.proto.PulsarApi.CommandError) ByteBuf(io.netty.buffer.ByteBuf) Test(org.testng.annotations.Test)

Example 9 with CommandError

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

the class ServerCnxTest method testProducerFailureOnEncryptionRequiredTopic.

@Test(timeOut = 30000)
public void testProducerFailureOnEncryptionRequiredTopic() throws Exception {
    resetChannel();
    setChannelConnected();
    // Set encryption_required to true
    ZooKeeperDataCache<Policies> zkDataCache = mock(ZooKeeperDataCache.class);
    Policies policies = mock(Policies.class);
    policies.encryption_required = true;
    policies.clusterDispatchRate = Maps.newHashMap();
    doReturn(Optional.of(policies)).when(zkDataCache).get(AdminResource.path(POLICIES, TopicName.get(encryptionRequiredTopicName).getNamespace()));
    doReturn(CompletableFuture.completedFuture(Optional.of(policies))).when(zkDataCache).getAsync(AdminResource.path(POLICIES, TopicName.get(encryptionRequiredTopicName).getNamespace()));
    doReturn(zkDataCache).when(configCacheService).policiesCache();
    // test failure case: unencrypted producer cannot connect
    ByteBuf clientCommand = Commands.newProducer(encryptionRequiredTopicName, 2, /* producer id */
    2, /* request id */
    "unencrypted-producer", false, null);
    channel.writeInbound(clientCommand);
    Object response = getResponse();
    assertEquals(response.getClass(), CommandError.class);
    CommandError errorResponse = (CommandError) response;
    assertEquals(errorResponse.getError(), ServerError.MetadataError);
    PersistentTopic topicRef = (PersistentTopic) brokerService.getTopicReference(encryptionRequiredTopicName);
    assertNotNull(topicRef);
    assertEquals(topicRef.getProducers().size(), 0);
    channel.finish();
}
Also used : Policies(org.apache.pulsar.common.policies.data.Policies) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) Matchers.anyObject(org.mockito.Matchers.anyObject) CommandError(org.apache.pulsar.common.api.proto.PulsarApi.CommandError) ByteBuf(io.netty.buffer.ByteBuf) Test(org.testng.annotations.Test)

Example 10 with CommandError

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

the class ServerCnxTest method testInvalidTopicOnSubscribe.

@Test(timeOut = 30000)
public void testInvalidTopicOnSubscribe() throws Exception {
    resetChannel();
    setChannelConnected();
    String invalidTopicName = "xx/ass/aa/aaa";
    resetChannel();
    setChannelConnected();
    channel.writeInbound(Commands.newSubscribe(invalidTopicName, "test-subscription", 1, 1, SubType.Exclusive, 0, "consumerName"));
    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) 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