Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id CD8E6200B5A for ; Thu, 4 Aug 2016 18:57:48 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id CC136160A6A; Thu, 4 Aug 2016 16:57:48 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 5D222160AAB for ; Thu, 4 Aug 2016 18:57:46 +0200 (CEST) Received: (qmail 12095 invoked by uid 500); 4 Aug 2016 16:57:45 -0000 Mailing-List: contact commits-help@geode.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@geode.incubator.apache.org Delivered-To: mailing list commits@geode.incubator.apache.org Received: (qmail 12086 invoked by uid 99); 4 Aug 2016 16:57:45 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 04 Aug 2016 16:57:45 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id D228918864B for ; Thu, 4 Aug 2016 16:57:44 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.646 X-Spam-Level: X-Spam-Status: No, score=-4.646 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-1.426] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id ibPoKlgQpRj5 for ; Thu, 4 Aug 2016 16:57:32 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with SMTP id 132125FB0F for ; Thu, 4 Aug 2016 16:57:30 +0000 (UTC) Received: (qmail 11454 invoked by uid 99); 4 Aug 2016 16:57:30 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 04 Aug 2016 16:57:30 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 5987AE0B40; Thu, 4 Aug 2016 16:57:30 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: klund@apache.org To: commits@geode.incubator.apache.org Date: Thu, 04 Aug 2016 16:57:31 -0000 Message-Id: <5cf54728bf454d07acfa677c2dcacf5d@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [02/10] incubator-geode git commit: GEODE-1712: introduce SecurityService interface for mocking archived-at: Thu, 04 Aug 2016 16:57:49 -0000 http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f93b7485/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetAll70Test.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetAll70Test.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetAll70Test.java new file mode 100644 index 0000000..b36a0a3 --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetAll70Test.java @@ -0,0 +1,214 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gemstone.gemfire.internal.cache.tier.sockets.command; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.gemstone.gemfire.CancelCriterion; +import com.gemstone.gemfire.cache.Cache; +import com.gemstone.gemfire.cache.RegionAttributes; +import com.gemstone.gemfire.cache.operations.GetOperationContext; +import com.gemstone.gemfire.internal.cache.LocalRegion; +import com.gemstone.gemfire.internal.cache.tier.CachedRegionHelper; +import com.gemstone.gemfire.internal.cache.tier.sockets.CacheServerStats; +import com.gemstone.gemfire.internal.cache.tier.sockets.ChunkedMessage; +import com.gemstone.gemfire.internal.cache.tier.sockets.Message; +import com.gemstone.gemfire.internal.cache.tier.sockets.ObjectPartList; +import com.gemstone.gemfire.internal.cache.tier.sockets.Part; +import com.gemstone.gemfire.internal.cache.tier.sockets.ServerConnection; +import com.gemstone.gemfire.internal.security.AuthorizeRequest; +import com.gemstone.gemfire.internal.security.SecurityService; +import com.gemstone.gemfire.security.NotAuthorizedException; +import com.gemstone.gemfire.test.junit.categories.UnitTest; + +@Category(UnitTest.class ) +public class GetAll70Test { + private static final String REGION_NAME = "region1"; + private static final Object[] KEYS = new Object[] { "key1", "key2", "key3" }; + + @Mock + private SecurityService securityService; + @Mock + private Message message; + @Mock + private ServerConnection serverConnection; + @Mock + private AuthorizeRequest authzRequest; + @Mock + private Cache cache; + @Mock + private LocalRegion region; + @Mock + private Part regionNamePart; + @Mock + private Part keyPart; + @Mock + private Part requestSerializableValuesPart; + @Mock + private RegionAttributes regionAttributes; + @Mock + private ChunkedMessage chunkedResponseMessage; + + @InjectMocks + private GetAll70 getAll70; + + @Before + public void setUp() throws Exception { + this.getAll70 = new GetAll70(); + MockitoAnnotations.initMocks(this); + + when(this.authzRequest.getAuthorize(any(), any(), any())).thenReturn(mock(GetOperationContext.class)); + + when(this.cache.getRegion(isA(String.class))).thenReturn(this.region); + when(this.cache.getCancelCriterion()).thenReturn(mock(CancelCriterion.class)); + + when(this.keyPart.getObject()).thenReturn(KEYS); + + when(this.message.getPart(eq(0))).thenReturn(this.regionNamePart); + when(this.message.getPart(eq(1))).thenReturn(this.keyPart); + when(this.message.getPart(eq(2))).thenReturn(this.requestSerializableValuesPart); + + when(this.region.getAttributes()).thenReturn(this.regionAttributes); + + when(this.regionAttributes.getConcurrencyChecksEnabled()).thenReturn(true); + + when(this.regionNamePart.getString()).thenReturn(REGION_NAME); + + when (this.requestSerializableValuesPart.getInt()).thenReturn(0); + + when(this.serverConnection.getCache()).thenReturn(this.cache); + when(this.serverConnection.getCacheServerStats()).thenReturn(mock(CacheServerStats.class)); + when(this.serverConnection.getAuthzRequest()).thenReturn(this.authzRequest); + when(this.serverConnection.getCachedRegionHelper()).thenReturn(mock(CachedRegionHelper.class)); + when(this.serverConnection.getChunkedResponseMessage()).thenReturn(this.chunkedResponseMessage); + } + + @Test + public void noSecurityShouldSucceed() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(false); + + this.getAll70.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.chunkedResponseMessage).sendChunk(this.serverConnection); + } + + @Test + public void integratedSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + + this.getAll70.cmdExecute(this.message, this.serverConnection, 0); + + ArgumentCaptor argument = ArgumentCaptor.forClass(ObjectPartList.class); + verify(this.chunkedResponseMessage).addObjPartNoCopying(argument.capture()); + + assertThat(argument.getValue().getObjects()).hasSize(KEYS.length); + for(Object key : argument.getValue().getKeys()) { + assertThat(key).isIn(KEYS); + } + for (Object key : KEYS) { + verify(this.securityService).authorizeRegionRead(eq(REGION_NAME), eq(key.toString())); + } + + verify(this.chunkedResponseMessage).sendChunk(this.serverConnection); + } + + @Test + public void integratedSecurityShouldFailIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + + for (Object key : KEYS) { + doThrow(new NotAuthorizedException("")).when(this.securityService).authorizeRegionRead(eq(REGION_NAME), eq(key.toString())); + } + + this.getAll70.cmdExecute(this.message, this.serverConnection, 0); + + for (Object key : KEYS) { + verify(this.securityService).authorizeRegionRead(eq(REGION_NAME), eq(key.toString())); + } + + ArgumentCaptor argument = ArgumentCaptor.forClass(ObjectPartList.class); + verify(this.chunkedResponseMessage).addObjPartNoCopying(argument.capture()); + + assertThat(argument.getValue().getObjects()).hasSize(KEYS.length); + for(Object key : argument.getValue().getObjects()){ + assertThat(key).isExactlyInstanceOf(NotAuthorizedException.class); + } + + verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); + } + + @Test + public void oldSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + + this.getAll70.cmdExecute(this.message, this.serverConnection, 0); + + ArgumentCaptor argument = ArgumentCaptor.forClass(ObjectPartList.class); + verify(this.chunkedResponseMessage).addObjPartNoCopying(argument.capture()); + + assertThat(argument.getValue().getObjects()).hasSize(KEYS.length); + for(Object key : argument.getValue().getKeys()) { + assertThat(key).isIn(KEYS); + } + + for (Object key: KEYS) { + verify(this.authzRequest).getAuthorize(eq(REGION_NAME), eq(key.toString()), eq(null)); + } + + verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); + } + + @Test + public void oldSecurityShouldFailIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + + for (Object key : KEYS) { + doThrow(new NotAuthorizedException("")).when(this.authzRequest).getAuthorize(eq(REGION_NAME), eq(key.toString()), eq(null)); + } + + this.getAll70.cmdExecute(this.message, this.serverConnection, 0); + + ArgumentCaptor argument = ArgumentCaptor.forClass(ObjectPartList.class); + verify(this.chunkedResponseMessage).addObjPartNoCopying(argument.capture()); + + assertThat(argument.getValue().getObjects()).hasSize(KEYS.length); + for(Object o : argument.getValue().getObjects()){ + assertThat(o).isExactlyInstanceOf(NotAuthorizedException.class); + } + for (Object key: KEYS) { + verify(this.authzRequest).getAuthorize(eq(REGION_NAME), eq(key.toString()), eq(null)); + } + verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f93b7485/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetAllTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetAllTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetAllTest.java new file mode 100644 index 0000000..2791d4f --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetAllTest.java @@ -0,0 +1,201 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gemstone.gemfire.internal.cache.tier.sockets.command; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.gemstone.gemfire.CancelCriterion; +import com.gemstone.gemfire.cache.Cache; +import com.gemstone.gemfire.cache.operations.GetOperationContext; +import com.gemstone.gemfire.internal.cache.LocalRegion; +import com.gemstone.gemfire.internal.cache.tier.CachedRegionHelper; +import com.gemstone.gemfire.internal.cache.tier.sockets.CacheServerStats; +import com.gemstone.gemfire.internal.cache.tier.sockets.ChunkedMessage; +import com.gemstone.gemfire.internal.cache.tier.sockets.Message; +import com.gemstone.gemfire.internal.cache.tier.sockets.ObjectPartList; +import com.gemstone.gemfire.internal.cache.tier.sockets.Part; +import com.gemstone.gemfire.internal.cache.tier.sockets.ServerConnection; +import com.gemstone.gemfire.internal.security.AuthorizeRequest; +import com.gemstone.gemfire.internal.security.SecurityService; +import com.gemstone.gemfire.security.NotAuthorizedException; +import com.gemstone.gemfire.test.junit.categories.UnitTest; + +@Category(UnitTest.class) +public class GetAllTest { + + private static final String REGION_NAME = "region1"; + private static final Object[] KEYS = new Object[] { "key1", "key2", "key3" }; + + @Mock + private SecurityService securityService; + @Mock + private Message message; + @Mock + private ServerConnection serverConnection; + @Mock + private AuthorizeRequest authzRequest; + @Mock + private Cache cache; + @Mock + private Part regionNamePart; + @Mock + private Part keyPart; + @Mock + private ChunkedMessage chunkedResponseMessage; + + @InjectMocks + private GetAll getAll; + + @Before + public void setUp() throws Exception { + this.getAll = new GetAll(); + MockitoAnnotations.initMocks(this); + + when(this.authzRequest.getAuthorize(any(), any(), any())).thenReturn(mock(GetOperationContext.class)); + + when(this.cache.getRegion(isA(String.class))).thenReturn(mock(LocalRegion.class)); + when(this.cache.getCancelCriterion()).thenReturn(mock(CancelCriterion.class)); + + when(this.keyPart.getObject()).thenReturn(KEYS); + + when(this.message.getPart(eq(0))).thenReturn(this.regionNamePart); + when(this.message.getPart(eq(1))).thenReturn(this.keyPart); + + when(this.regionNamePart.getString()).thenReturn(REGION_NAME); + + when(this.serverConnection.getCache()).thenReturn(this.cache); + when(this.serverConnection.getCacheServerStats()).thenReturn(mock(CacheServerStats.class)); + when(this.serverConnection.getAuthzRequest()).thenReturn(this.authzRequest); + when(this.serverConnection.getCachedRegionHelper()).thenReturn(mock(CachedRegionHelper.class)); + when(this.serverConnection.getChunkedResponseMessage()).thenReturn(this.chunkedResponseMessage); + } + + @Test + public void noSecurityShouldSucceed() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(false); + + this.getAll.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); + } + + @Test + public void integratedSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + + this.getAll.cmdExecute(this.message, this.serverConnection, 0); + + ArgumentCaptor argument = ArgumentCaptor.forClass(ObjectPartList.class); + verify(this.chunkedResponseMessage).addObjPart(argument.capture(), eq(false)); + + assertThat(argument.getValue().getObjects()).hasSize(KEYS.length); + for(Object key : argument.getValue().getKeys()) { + assertThat(key).isIn(KEYS); + } + for (Object key : KEYS) { + verify(this.securityService).authorizeRegionRead(eq(REGION_NAME), eq(key.toString())); + } + + verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); + } + + @Test + public void integratedSecurityShouldFailIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + + for (Object key : KEYS) { + doThrow(new NotAuthorizedException("")).when(this.securityService).authorizeRegionRead(eq(REGION_NAME), eq(key.toString())); + } + + this.getAll.cmdExecute(this.message, this.serverConnection, 0); + + for (Object key : KEYS) { + verify(this.securityService).authorizeRegionRead(eq(REGION_NAME), eq(key.toString())); + } + + ArgumentCaptor argument = ArgumentCaptor.forClass(ObjectPartList.class); + verify(this.chunkedResponseMessage).addObjPart(argument.capture(), eq(false)); + + assertThat(argument.getValue().getObjects()).hasSize(KEYS.length); + for(Object key : argument.getValue().getObjects()){ + assertThat(key).isExactlyInstanceOf(NotAuthorizedException.class); + } + + verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); + } + + @Test + public void oldSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + + this.getAll.cmdExecute(this.message, this.serverConnection, 0); + + ArgumentCaptor argument = ArgumentCaptor.forClass(ObjectPartList.class); + verify(this.chunkedResponseMessage).addObjPart(argument.capture(), eq(false)); + + assertThat(argument.getValue().getObjects()).hasSize(KEYS.length); + for(Object key : argument.getValue().getKeys()) { + assertThat(key).isIn(KEYS); + } + + for (Object key: KEYS) { + verify(this.authzRequest).getAuthorize(eq(REGION_NAME), eq(key.toString()), eq(null)); + } + + verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); + } + + @Test + public void oldSecurityShouldFailIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + + for (Object key : KEYS) { + doThrow(new NotAuthorizedException("")).when(this.authzRequest).getAuthorize(eq(REGION_NAME), eq(key.toString()), eq(null)); + } + this.getAll.cmdExecute(this.message, this.serverConnection, 0); + + ArgumentCaptor argument = ArgumentCaptor.forClass(ObjectPartList.class); + verify(this.chunkedResponseMessage).addObjPart(argument.capture(), eq(false)); + + assertThat(argument.getValue().getObjects()).hasSize(KEYS.length); + for(Object o : argument.getValue().getObjects()){ + assertThat(o).isExactlyInstanceOf(NotAuthorizedException.class); + } + + for (Object key: KEYS) { + verify(this.authzRequest).getAuthorize(eq(REGION_NAME), eq(key.toString()), eq(null)); + } + verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f93b7485/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetAllWithCallbackTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetAllWithCallbackTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetAllWithCallbackTest.java new file mode 100644 index 0000000..72d4bb5 --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetAllWithCallbackTest.java @@ -0,0 +1,213 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gemstone.gemfire.internal.cache.tier.sockets.command; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.gemstone.gemfire.CancelCriterion; +import com.gemstone.gemfire.cache.Cache; +import com.gemstone.gemfire.cache.RegionAttributes; +import com.gemstone.gemfire.cache.operations.GetOperationContext; +import com.gemstone.gemfire.internal.cache.LocalRegion; +import com.gemstone.gemfire.internal.cache.tier.CachedRegionHelper; +import com.gemstone.gemfire.internal.cache.tier.sockets.CacheServerStats; +import com.gemstone.gemfire.internal.cache.tier.sockets.ChunkedMessage; +import com.gemstone.gemfire.internal.cache.tier.sockets.Message; +import com.gemstone.gemfire.internal.cache.tier.sockets.ObjectPartList; +import com.gemstone.gemfire.internal.cache.tier.sockets.Part; +import com.gemstone.gemfire.internal.cache.tier.sockets.ServerConnection; +import com.gemstone.gemfire.internal.security.AuthorizeRequest; +import com.gemstone.gemfire.internal.security.SecurityService; +import com.gemstone.gemfire.security.NotAuthorizedException; +import com.gemstone.gemfire.test.junit.categories.UnitTest; + +@Category(UnitTest.class) +public class GetAllWithCallbackTest { + private static final String REGION_NAME = "region1"; + private static final Object[] KEYS = new Object[] { "key1", "key2", "key3" }; + + @Mock + private SecurityService securityService; + @Mock + private Message message; + @Mock + private ServerConnection serverConnection; + @Mock + private AuthorizeRequest authzRequest; + @Mock + private Cache cache; + @Mock + private LocalRegion region; + @Mock + private Part regionNamePart; + @Mock + private Part keyPart; + @Mock + private Part requestSerializableValuesPart; + @Mock + private RegionAttributes regionAttributes; + @Mock + private ChunkedMessage chunkedResponseMessage; + + @InjectMocks + private GetAll70 getAll70; + + @Before + public void setUp() throws Exception { + this.getAll70 = new GetAll70(); + MockitoAnnotations.initMocks(this); + + when(this.authzRequest.getAuthorize(any(), any(), any())).thenReturn(mock(GetOperationContext.class)); + + when(this.cache.getRegion(isA(String.class))).thenReturn(this.region); + + when(this.keyPart.getObject()).thenReturn(KEYS); + + when(this.message.getPart(eq(0))).thenReturn(this.regionNamePart); + when(this.message.getPart(eq(1))).thenReturn(this.keyPart); + when(this.message.getPart(eq(2))).thenReturn(this.requestSerializableValuesPart); + + when(this.region.getAttributes()).thenReturn(this.regionAttributes); + + when(this.regionAttributes.getConcurrencyChecksEnabled()).thenReturn(true); + + when(this.regionNamePart.getString()).thenReturn(REGION_NAME); + + when (this.requestSerializableValuesPart.getInt()).thenReturn(0); + + when(this.serverConnection.getCache()).thenReturn(this.cache); + when(this.serverConnection.getAuthzRequest()).thenReturn(this.authzRequest); + when(this.serverConnection.getChunkedResponseMessage()).thenReturn(this.chunkedResponseMessage); + } + + @Test + public void noSecurityShouldSucceed() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(false); + + this.getAll70.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); + } + + @Test + public void integratedSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + + this.getAll70.cmdExecute(this.message, this.serverConnection, 0); + + ArgumentCaptor argument = ArgumentCaptor.forClass(ObjectPartList.class); + verify(this.chunkedResponseMessage).addObjPartNoCopying(argument.capture()); + + assertThat(argument.getValue().getObjects()).hasSize(KEYS.length); + for(Object key : argument.getValue().getKeys()) { + assertThat(key).isIn(KEYS); + } + for (Object key : KEYS) { + verify(this.securityService).authorizeRegionRead(eq(REGION_NAME), eq(key.toString())); + } + + verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); + } + + @Test + public void integratedSecurityShouldFailIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + + for (Object key : KEYS) { + doThrow(new NotAuthorizedException("")).when(this.securityService).authorizeRegionRead(eq(REGION_NAME), eq(key.toString())); + } + + this.getAll70.cmdExecute(this.message, this.serverConnection, 0); + + for (Object key : KEYS) { + verify(this.securityService).authorizeRegionRead(eq(REGION_NAME), eq(key.toString())); + } + + ArgumentCaptor argument = ArgumentCaptor.forClass(ObjectPartList.class); + verify(this.chunkedResponseMessage).addObjPartNoCopying(argument.capture()); + + assertThat(argument.getValue().getObjects()).hasSize(KEYS.length); + for(Object key : argument.getValue().getObjects()){ + assertThat(key).isExactlyInstanceOf(NotAuthorizedException.class); + } + + verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); + } + + @Test + public void oldSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + + this.getAll70.cmdExecute(this.message, this.serverConnection, 0); + + ArgumentCaptor argument = ArgumentCaptor.forClass(ObjectPartList.class); + verify(this.chunkedResponseMessage).addObjPartNoCopying(argument.capture()); + + assertThat(argument.getValue().getObjects()).hasSize(KEYS.length); + for(Object key : argument.getValue().getKeys()) { + assertThat(key).isIn(KEYS); + } + + for (Object key: KEYS) { + verify(this.authzRequest).getAuthorize(eq(REGION_NAME), eq(key.toString()), eq(null)); + } + + verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); + } + + @Test + public void oldSecurityShouldFailIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + + for (Object key : KEYS) { + doThrow(new NotAuthorizedException("")).when(this.authzRequest).getAuthorize(eq(REGION_NAME), eq(key.toString()), eq(null)); + } + + this.getAll70.cmdExecute(this.message, this.serverConnection, 0); + + ArgumentCaptor argument = ArgumentCaptor.forClass(ObjectPartList.class); + verify(this.chunkedResponseMessage).addObjPartNoCopying(argument.capture()); + + assertThat(argument.getValue().getObjects()).hasSize(KEYS.length); + for(Object o : argument.getValue().getObjects()){ + assertThat(o).isExactlyInstanceOf(NotAuthorizedException.class); + } + + for (Object key: KEYS) { + verify(this.authzRequest).getAuthorize(eq(REGION_NAME), eq(key.toString()), eq(null)); + } + + verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f93b7485/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetClientPartitionAttributesCommand66Test.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetClientPartitionAttributesCommand66Test.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetClientPartitionAttributesCommand66Test.java new file mode 100644 index 0000000..90b1e90 --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetClientPartitionAttributesCommand66Test.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gemstone.gemfire.internal.cache.tier.sockets.command; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.isA; +import static org.mockito.Mockito.*; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.gemstone.gemfire.cache.Cache; +import com.gemstone.gemfire.internal.cache.LocalRegion; +import com.gemstone.gemfire.internal.cache.tier.sockets.Message; +import com.gemstone.gemfire.internal.cache.tier.sockets.Part; +import com.gemstone.gemfire.internal.cache.tier.sockets.ServerConnection; +import com.gemstone.gemfire.internal.security.SecurityService; +import com.gemstone.gemfire.test.junit.categories.UnitTest; + +@Category(UnitTest.class) +public class GetClientPartitionAttributesCommand66Test { + + private static final String REGION_NAME = "region1"; + + @Mock + private SecurityService securityService; + @Mock + private Message message; + @Mock + private ServerConnection serverConnection; + @Mock + private Cache cache; + @Mock + private Part regionNamePart; + @Mock + private Message responseMessage; + + @InjectMocks + private GetClientPartitionAttributesCommand66 getClientPartitionAttributesCommand66; + + @Before + public void setUp() throws Exception { + this.getClientPartitionAttributesCommand66 = new GetClientPartitionAttributesCommand66(); + MockitoAnnotations.initMocks(this); + + when(this.cache.getRegion(isA(String.class))).thenReturn(mock(LocalRegion.class)); + + when(this.message.getPart(eq(0))).thenReturn(this.regionNamePart); + + when(this.regionNamePart.getString()).thenReturn(REGION_NAME); + + when(this.serverConnection.getCache()).thenReturn(this.cache); + when(this.serverConnection.getResponseMessage()).thenReturn(this.responseMessage); + } + + @Test + public void noSecuirtyShouldSucceed() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(false); + + this.getClientPartitionAttributesCommand66.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.responseMessage).send(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f93b7485/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetClientPartitionAttributesCommandTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetClientPartitionAttributesCommandTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetClientPartitionAttributesCommandTest.java new file mode 100644 index 0000000..9c7e99d --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/GetClientPartitionAttributesCommandTest.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gemstone.gemfire.internal.cache.tier.sockets.command; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.isA; +import static org.mockito.Mockito.*; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.gemstone.gemfire.cache.Cache; +import com.gemstone.gemfire.internal.cache.PartitionedRegion; +import com.gemstone.gemfire.internal.cache.tier.sockets.Message; +import com.gemstone.gemfire.internal.cache.tier.sockets.Part; +import com.gemstone.gemfire.internal.cache.tier.sockets.ServerConnection; +import com.gemstone.gemfire.internal.security.SecurityService; +import com.gemstone.gemfire.test.junit.categories.UnitTest; + +@Category(UnitTest.class) +public class GetClientPartitionAttributesCommandTest { + + private static final String REGION_NAME = "region1"; + + @Mock + private SecurityService securityService; + @Mock + private Message message; + @Mock + private ServerConnection serverConnection; + @Mock + private Cache cache; + @Mock + private Part regionNamePart; + @Mock + private Message responseMessage; + + @InjectMocks + private GetClientPartitionAttributesCommand getClientPartitionAttributesCommand; + + @Before + public void setUp() throws Exception { + this.getClientPartitionAttributesCommand = new GetClientPartitionAttributesCommand(); + MockitoAnnotations.initMocks(this); + + when(this.cache.getRegion(isA(String.class))).thenReturn(mock(PartitionedRegion.class)); + + when(this.message.getPart(eq(0))).thenReturn(this.regionNamePart); + + when(this.regionNamePart.getString()).thenReturn(REGION_NAME); + + when(this.serverConnection.getCache()).thenReturn(this.cache); + when(this.serverConnection.getResponseMessage()).thenReturn(this.responseMessage); + } + + @Test + public void noSecurityShouldSucceed() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(false); + + this.getClientPartitionAttributesCommand.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.responseMessage).send(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f93b7485/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/InvalidateTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/InvalidateTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/InvalidateTest.java new file mode 100644 index 0000000..8e08f0c --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/InvalidateTest.java @@ -0,0 +1,176 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gemstone.gemfire.internal.cache.tier.sockets.command; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.gemstone.gemfire.CancelCriterion; +import com.gemstone.gemfire.cache.Cache; +import com.gemstone.gemfire.cache.operations.GetOperationContext; +import com.gemstone.gemfire.cache.operations.InvalidateOperationContext; +import com.gemstone.gemfire.internal.cache.LocalRegion; +import com.gemstone.gemfire.internal.cache.tier.CachedRegionHelper; +import com.gemstone.gemfire.internal.cache.tier.sockets.CacheServerStats; +import com.gemstone.gemfire.internal.cache.tier.sockets.ChunkedMessage; +import com.gemstone.gemfire.internal.cache.tier.sockets.Message; +import com.gemstone.gemfire.internal.cache.tier.sockets.ObjectPartList; +import com.gemstone.gemfire.internal.cache.tier.sockets.Part; +import com.gemstone.gemfire.internal.cache.tier.sockets.ServerConnection; +import com.gemstone.gemfire.internal.security.AuthorizeRequest; +import com.gemstone.gemfire.internal.security.SecurityService; +import com.gemstone.gemfire.security.NotAuthorizedException; +import com.gemstone.gemfire.test.junit.categories.UnitTest; + +@Category(UnitTest.class) +public class InvalidateTest { + + private static final String REGION_NAME = "region1"; + private static final String KEY_STRING = "key1"; + private static final byte[] EVENT = new byte[8]; + private static final Object CALLBACK_ARG = "arg"; + + @Mock + private SecurityService securityService; + @Mock + private Message message; + @Mock + private ServerConnection serverConnection; + @Mock + private AuthorizeRequest authzRequest; + @Mock + private Cache cache; + @Mock + private Message errorResponseMessage; + @Mock + private Part regionNamePart; + @Mock + private Part keyPart; + @Mock + private Part eventPart; + @Mock + private Part callbackArgPart; + @Mock + private Message responseMessage; + + @InjectMocks + private Invalidate invalidate; + + @Before + public void setUp() throws Exception { + this.invalidate = new Invalidate(); + MockitoAnnotations.initMocks(this); + + when(this.authzRequest.invalidateAuthorize(any(), any(), any())).thenReturn(mock(InvalidateOperationContext.class)); + + when(this.cache.getRegion(isA(String.class))).thenReturn(mock(LocalRegion.class)); + when(this.cache.getCancelCriterion()).thenReturn(mock(CancelCriterion.class)); + + when(this.callbackArgPart.getObject()).thenReturn(CALLBACK_ARG); + + when(this.eventPart.getSerializedForm()).thenReturn(EVENT); + + when(this.keyPart.getStringOrObject()).thenReturn(KEY_STRING); + + when(this.message.getNumberOfParts()).thenReturn(4); + when(this.message.getPart(eq(0))).thenReturn(this.regionNamePart); + when(this.message.getPart(eq(1))).thenReturn(this.keyPart); + when(this.message.getPart(eq(2))).thenReturn(this.eventPart); + when(this.message.getPart(eq(3))).thenReturn(this.callbackArgPart); + + + when(this.regionNamePart.getString()).thenReturn(REGION_NAME); + + when(this.serverConnection.getCache()).thenReturn(this.cache); + when(this.serverConnection.getCacheServerStats()).thenReturn(mock(CacheServerStats.class)); + when(this.serverConnection.getAuthzRequest()).thenReturn(this.authzRequest); + when(this.serverConnection.getCachedRegionHelper()).thenReturn(mock(CachedRegionHelper.class)); + when(this.serverConnection.getReplyMessage()).thenReturn(this.responseMessage); + when(this.serverConnection.getErrorResponseMessage()).thenReturn(this.errorResponseMessage); + } + + @Test + public void noSecurityShouldSucceed() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(false); + + this.invalidate.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.responseMessage).send(this.serverConnection); + } + + @Test + public void integratedSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + + this.invalidate.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.securityService).authorizeRegionWrite(eq(REGION_NAME), eq(KEY_STRING)); + verify(this.responseMessage).send(this.serverConnection); + } + + @Test + public void integratedSecurityShouldFailIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + doThrow(new NotAuthorizedException("")).when(this.securityService).authorizeRegionWrite(eq(REGION_NAME), eq(KEY_STRING)); + + this.invalidate.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.securityService).authorizeRegionWrite(eq(REGION_NAME), eq(KEY_STRING)); + verify(this.errorResponseMessage).send(this.serverConnection); + } + + @Test + public void oldSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + + this.invalidate.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.authzRequest).invalidateAuthorize(eq(REGION_NAME), eq(KEY_STRING), eq(CALLBACK_ARG)); + verify(this.responseMessage).send(this.serverConnection); + } + + @Test + public void oldSecurityShouldFailIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + doThrow(new NotAuthorizedException("")).when(this.authzRequest).invalidateAuthorize(eq(REGION_NAME), eq(KEY_STRING), eq(CALLBACK_ARG)); + + this.invalidate.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.authzRequest).invalidateAuthorize(eq(REGION_NAME), eq(KEY_STRING), eq(CALLBACK_ARG)); + + ArgumentCaptor argument = ArgumentCaptor.forClass(NotAuthorizedException.class); + verify(this.errorResponseMessage).addObjPart(argument.capture()); + assertThat(argument.getValue()).isExactlyInstanceOf(NotAuthorizedException.class); + verify(this.errorResponseMessage).send(this.serverConnection); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f93b7485/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/KeySetTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/KeySetTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/KeySetTest.java new file mode 100644 index 0000000..90969fc --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/KeySetTest.java @@ -0,0 +1,160 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gemstone.gemfire.internal.cache.tier.sockets.command; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.gemstone.gemfire.CancelCriterion; +import com.gemstone.gemfire.cache.Cache; +import com.gemstone.gemfire.cache.operations.DestroyOperationContext; +import com.gemstone.gemfire.cache.operations.KeySetOperationContext; +import com.gemstone.gemfire.internal.cache.LocalRegion; +import com.gemstone.gemfire.internal.cache.tier.CachedRegionHelper; +import com.gemstone.gemfire.internal.cache.tier.sockets.CacheServerStats; +import com.gemstone.gemfire.internal.cache.tier.sockets.ChunkedMessage; +import com.gemstone.gemfire.internal.cache.tier.sockets.Message; +import com.gemstone.gemfire.internal.cache.tier.sockets.ObjectPartList; +import com.gemstone.gemfire.internal.cache.tier.sockets.Part; +import com.gemstone.gemfire.internal.cache.tier.sockets.ServerConnection; +import com.gemstone.gemfire.internal.security.AuthorizeRequest; +import com.gemstone.gemfire.internal.security.SecurityService; +import com.gemstone.gemfire.security.NotAuthorizedException; +import com.gemstone.gemfire.test.junit.categories.UnitTest; + +@Category(UnitTest.class) +public class KeySetTest { + + private static final String REGION_NAME = "region1"; + private static final String KEY = "key1"; + private static final Object CALLBACK_ARG = "arg"; + private static final byte[] EVENT = new byte[8]; + + @Mock + private SecurityService securityService; + @Mock + private Message message; + @Mock + private ServerConnection serverConnection; + @Mock + private AuthorizeRequest authzRequest; + @Mock + private LocalRegion region; + @Mock + private Cache cache; + @Mock + private ChunkedMessage chunkedResponseMessage; + @Mock + private Part regionNamePart; + @Mock + private KeySetOperationContext keySetOperationContext; + @InjectMocks + private KeySet keySet; + + @Before + public void setUp() throws Exception { + this.keySet = new KeySet(); + MockitoAnnotations.initMocks(this); + + when(this.authzRequest.keySetAuthorize(eq(REGION_NAME))).thenReturn(this.keySetOperationContext); + + when(this.cache.getRegion(isA(String.class))).thenReturn(this.region); + when(this.cache.getCancelCriterion()).thenReturn(mock(CancelCriterion.class)); + + when(this.message.getPart(eq(0))).thenReturn(this.regionNamePart); + + when(this.regionNamePart.getString()).thenReturn(REGION_NAME); + + when(this.serverConnection.getCache()).thenReturn(this.cache); + when(this.serverConnection.getCacheServerStats()).thenReturn(mock(CacheServerStats.class)); + when(this.serverConnection.getAuthzRequest()).thenReturn(this.authzRequest); + when(this.serverConnection.getCachedRegionHelper()).thenReturn(mock(CachedRegionHelper.class)); + when(this.serverConnection.getChunkedResponseMessage()).thenReturn(this.chunkedResponseMessage); + } + + @Test + public void noSecurityShouldSucceed() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(false); + + this.keySet.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.chunkedResponseMessage).sendChunk(this.serverConnection); + } + + @Test + public void integratedSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + + this.keySet.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.securityService).authorizeRegionRead(eq(REGION_NAME)); + verify(this.chunkedResponseMessage).sendChunk(this.serverConnection); + } + + @Test + public void integratedSecurityShouldFailIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + doThrow(new NotAuthorizedException("")).when(this.securityService).authorizeRegionRead(eq(REGION_NAME)); + + this.keySet.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.securityService).authorizeRegionRead(eq(REGION_NAME)); + verify(this.chunkedResponseMessage).sendChunk(this.serverConnection); + } + + @Test + public void oldSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + + this.keySet.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.authzRequest).keySetAuthorize(eq(REGION_NAME)); + verify(this.chunkedResponseMessage).sendChunk(this.serverConnection); + } + + @Test + public void oldSecurityShouldFailIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + doThrow(new NotAuthorizedException("")).when(this.authzRequest).keySetAuthorize(eq(REGION_NAME)); + + this.keySet.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.authzRequest).keySetAuthorize(eq(REGION_NAME)); + + ArgumentCaptor argument = ArgumentCaptor.forClass(NotAuthorizedException.class); + verify(this.chunkedResponseMessage).addObjPart(argument.capture()); + assertThat( argument.getValue()).isExactlyInstanceOf(NotAuthorizedException.class); + verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f93b7485/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/Put61Test.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/Put61Test.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/Put61Test.java new file mode 100644 index 0000000..de27b8e --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/Put61Test.java @@ -0,0 +1,202 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gemstone.gemfire.internal.cache.tier.sockets.command; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.gemstone.gemfire.CancelCriterion; +import com.gemstone.gemfire.cache.Cache; +import com.gemstone.gemfire.cache.operations.PutOperationContext; +import com.gemstone.gemfire.internal.cache.EntryEventImpl; +import com.gemstone.gemfire.internal.cache.LocalRegion; +import com.gemstone.gemfire.internal.cache.tier.CachedRegionHelper; +import com.gemstone.gemfire.internal.cache.tier.sockets.CacheServerStats; +import com.gemstone.gemfire.internal.cache.tier.sockets.ClientProxyMembershipID; +import com.gemstone.gemfire.internal.cache.tier.sockets.Message; +import com.gemstone.gemfire.internal.cache.tier.sockets.Part; +import com.gemstone.gemfire.internal.cache.tier.sockets.ServerConnection; +import com.gemstone.gemfire.internal.security.AuthorizeRequest; +import com.gemstone.gemfire.internal.security.SecurityService; +import com.gemstone.gemfire.security.NotAuthorizedException; +import com.gemstone.gemfire.test.junit.categories.UnitTest; + +@Category(UnitTest.class) +public class Put61Test { + + private static final String REGION_NAME = "region1"; + private static final String KEY = "key1"; + private static final Object CALLBACK_ARG = "arg"; + private static final byte[] EVENT = new byte[8]; + private static final byte[] VALUE = new byte[8]; + private static final byte[] OK_BYTES = new byte[]{0}; + + @Mock + private SecurityService securityService; + @Mock + private Message message; + @Mock + private ServerConnection serverConnection; + @Mock + private AuthorizeRequest authzRequest; + @Mock + private Cache cache; + @Mock + private LocalRegion localRegion; + @Mock + private Part regionNamePart; + @Mock + private Part keyPart; + @Mock + private Part valuePart; + @Mock + private Part deltaPart; + @Mock + private Part eventPart; + @Mock + private Part callbackArgsPart; + @Mock + private PutOperationContext putOperationContext; + @Mock + private Message errorResponseMessage; + @Mock + private Message replyMessage; + @InjectMocks + private Put61 put61; + + @Before + public void setUp() throws Exception { + this.put61 = new Put61(); + MockitoAnnotations.initMocks(this); + + when(this.authzRequest.putAuthorize(eq(REGION_NAME), eq(KEY), eq(null), eq(true), eq(CALLBACK_ARG))).thenReturn(this.putOperationContext); + + when(this.putOperationContext.getCallbackArg()).thenReturn(CALLBACK_ARG); + when(this.putOperationContext.getValue()).thenReturn(VALUE); + when(this.putOperationContext.isObject()).thenReturn(true); + + when(this.cache.getRegion(isA(String.class))).thenReturn(this.localRegion); + when(this.cache.getCancelCriterion()).thenReturn(mock(CancelCriterion.class)); + + when(this.callbackArgsPart.getObject()).thenReturn(CALLBACK_ARG); + + when(this.deltaPart.getObject()).thenReturn(true); + + when(this.eventPart.getSerializedForm()).thenReturn(this.EVENT); + + when(this.valuePart.getSerializedForm()).thenReturn(VALUE); + when(this.valuePart.isObject()).thenReturn(true); + when(this.keyPart.getStringOrObject()).thenReturn(KEY); + + when(this.message.getNumberOfParts()).thenReturn(6); + when(this.message.getPart(eq(0))).thenReturn(this.regionNamePart); + when(this.message.getPart(eq(1))).thenReturn(this.keyPart); + when(this.message.getPart(eq(2))).thenReturn(this.deltaPart); + when(this.message.getPart(eq(3))).thenReturn(this.valuePart); + when(this.message.getPart(eq(4))).thenReturn(this.eventPart); + when(this.message.getPart(eq(5))).thenReturn(this.callbackArgsPart); + + when(this.regionNamePart.getString()).thenReturn(REGION_NAME); + + when(this.serverConnection.getCache()).thenReturn(this.cache); + when(this.serverConnection.getCacheServerStats()).thenReturn(mock(CacheServerStats.class)); + when(this.serverConnection.getAuthzRequest()).thenReturn(this.authzRequest); + when(this.serverConnection.getCachedRegionHelper()).thenReturn(mock(CachedRegionHelper.class)); + when(this.serverConnection.getReplyMessage()).thenReturn(this.replyMessage); + when(this.serverConnection.getErrorResponseMessage()).thenReturn(this.errorResponseMessage); + + when(this.localRegion.basicBridgePut(eq(KEY), eq(VALUE), eq(VALUE), eq(true), eq(CALLBACK_ARG), any(ClientProxyMembershipID.class), eq(true), any(EntryEventImpl.class))).thenReturn(true); + } + + @Test + public void noSecurityShouldSucceed() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(false); + + this.put61.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.replyMessage).send(this.serverConnection); + } + + @Test + public void integratedSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + + this.put61.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.securityService).authorizeRegionWrite(eq(REGION_NAME), eq(KEY)); + verify(this.replyMessage).send(this.serverConnection); + } + + @Test + public void integratedSecurityShouldFailIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + doThrow(new NotAuthorizedException("")).when(this.securityService).authorizeRegionWrite(eq(REGION_NAME), eq(KEY)); + + this.put61.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.securityService).authorizeRegionWrite(eq(REGION_NAME), eq(KEY)); + verify(this.errorResponseMessage).send(this.serverConnection); + } + + @Test + public void oldSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + + this.put61.cmdExecute(this.message, this.serverConnection, 0); + + ArgumentCaptor argument = ArgumentCaptor.forClass(byte[].class); + verify(this.replyMessage).addBytesPart(argument.capture()); + + assertThat(argument.getValue()).isEqualTo(OK_BYTES); + + verify(this.authzRequest).putAuthorize(eq(REGION_NAME), eq(KEY), eq(null), eq(true), eq(CALLBACK_ARG)); + verify(this.replyMessage).send(this.serverConnection); + } + + @Test + public void oldSecurityShouldFailIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + doThrow(new NotAuthorizedException("")).when(this.authzRequest).putAuthorize(eq(REGION_NAME), eq(KEY), eq(null), eq(true), eq(CALLBACK_ARG)); + + this.put61.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.authzRequest).putAuthorize(eq(REGION_NAME), eq(KEY), eq(null), eq(true), eq(CALLBACK_ARG)); + + ArgumentCaptor argument = ArgumentCaptor.forClass(NotAuthorizedException.class); + verify(this.errorResponseMessage).addObjPart(argument.capture()); + assertThat(argument.getValue()).isExactlyInstanceOf(NotAuthorizedException.class); + verify(this.errorResponseMessage).send(this.serverConnection); + } + + + +} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f93b7485/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/Put65Test.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/Put65Test.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/Put65Test.java new file mode 100644 index 0000000..d07b2ab --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/Put65Test.java @@ -0,0 +1,223 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gemstone.gemfire.internal.cache.tier.sockets.command; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.gemstone.gemfire.CancelCriterion; +import com.gemstone.gemfire.cache.Cache; +import com.gemstone.gemfire.cache.CacheTransactionManager; +import com.gemstone.gemfire.cache.Operation; +import com.gemstone.gemfire.cache.operations.PutOperationContext; +import com.gemstone.gemfire.internal.cache.EntryEventImpl; +import com.gemstone.gemfire.internal.cache.LocalRegion; +import com.gemstone.gemfire.internal.cache.TXManagerImpl; +import com.gemstone.gemfire.internal.cache.tier.CachedRegionHelper; +import com.gemstone.gemfire.internal.cache.tier.sockets.CacheServerStats; +import com.gemstone.gemfire.internal.cache.tier.sockets.ClientProxyMembershipID; +import com.gemstone.gemfire.internal.cache.tier.sockets.Message; +import com.gemstone.gemfire.internal.cache.tier.sockets.Part; +import com.gemstone.gemfire.internal.cache.tier.sockets.ServerConnection; +import com.gemstone.gemfire.internal.security.AuthorizeRequest; +import com.gemstone.gemfire.internal.security.SecurityService; +import com.gemstone.gemfire.security.NotAuthorizedException; +import com.gemstone.gemfire.test.junit.categories.UnitTest; + +@Category(UnitTest.class) +public class Put65Test { + + private static final String REGION_NAME = "region1"; + private static final String KEY = "key1"; + private static final Object CALLBACK_ARG = "arg"; + private static final byte[] EVENT = new byte[8]; + private static final byte[] VALUE = new byte[8]; + private static final byte[] OK_BYTES = new byte[]{0}; + + @Mock + private SecurityService securityService; + @Mock + private Message message; + @Mock + private ServerConnection serverConnection; + @Mock + private AuthorizeRequest authzRequest; + @Mock + private Cache cache; + @Mock + private LocalRegion localRegion; + @Mock + private Part regionNamePart; + @Mock + private Part keyPart; + @Mock + private Part valuePart; + @Mock + private Part oldValuePart; + @Mock + private Part deltaPart; + @Mock + private Part operationPart; + @Mock + private Part flagsPart; + @Mock + private Part eventPart; + @Mock + private Part callbackArgsPart; + @Mock + private PutOperationContext putOperationContext; + @Mock + private Object object; + @Mock + private Message errorResponseMessage; + @Mock + private Message replyMessage; + + @InjectMocks + private Put65 put65; + + @Before + public void setUp() throws Exception { + this.put65 = new Put65(); + MockitoAnnotations.initMocks(this); + + when(this.authzRequest.putAuthorize(eq(REGION_NAME), eq(KEY), any(), eq(true), eq(CALLBACK_ARG))).thenReturn(this.putOperationContext); + + when(this.cache.getRegion(isA(String.class))).thenReturn(this.localRegion); + when(this.cache.getCancelCriterion()).thenReturn(mock(CancelCriterion.class)); + when(this.cache.getCacheTransactionManager()).thenReturn(mock(TXManagerImpl.class)); + + when(this.callbackArgsPart.getObject()).thenReturn(CALLBACK_ARG); + + when(this.deltaPart.getObject()).thenReturn(Boolean.FALSE); + + when(this.eventPart.getSerializedForm()).thenReturn(EVENT); + + when(this.flagsPart.getInt()).thenReturn(1); + + when(this.keyPart.getStringOrObject()).thenReturn(KEY); + + when(this.localRegion.basicBridgePut(eq(KEY), eq(VALUE), eq(null), eq(true), eq(CALLBACK_ARG), any(ClientProxyMembershipID.class), eq(true), any(EntryEventImpl.class))).thenReturn(true); + + when(this.message.getNumberOfParts()).thenReturn(8); + when(this.message.getPart(eq(0))).thenReturn(this.regionNamePart); + when(this.message.getPart(eq(1))).thenReturn(operationPart); + when(this.message.getPart(eq(2))).thenReturn(this.flagsPart); + when(this.message.getPart(eq(3))).thenReturn(this.keyPart); + when(this.message.getPart(eq(4))).thenReturn(this.deltaPart); + when(this.message.getPart(eq(5))).thenReturn(this.valuePart); + when(this.message.getPart(eq(6))).thenReturn(this.eventPart); + when(this.message.getPart(eq(7))).thenReturn(this.callbackArgsPart); + + when(this.operationPart.getObject()).thenReturn(null); + + when(this.oldValuePart.getObject()).thenReturn(mock(Object.class)); + + when(this.putOperationContext.getCallbackArg()).thenReturn(CALLBACK_ARG); + when(this.putOperationContext.getValue()).thenReturn(VALUE); + when(this.putOperationContext.isObject()).thenReturn(true); + + when(this.regionNamePart.getString()).thenReturn(REGION_NAME); + + when(this.serverConnection.getCache()).thenReturn(this.cache); + when(this.serverConnection.getCacheServerStats()).thenReturn(mock(CacheServerStats.class)); + when(this.serverConnection.getAuthzRequest()).thenReturn(this.authzRequest); + when(this.serverConnection.getCachedRegionHelper()).thenReturn(mock(CachedRegionHelper.class)); + when(this.serverConnection.getReplyMessage()).thenReturn(this.replyMessage); + when(this.serverConnection.getErrorResponseMessage()).thenReturn(this.errorResponseMessage); + + when(this.valuePart.getSerializedForm()).thenReturn(VALUE); + when(this.valuePart.isObject()).thenReturn(true); + } + + @Test + public void noSecurityShouldSucceed() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(false); + + this.put65.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.replyMessage).send(this.serverConnection); + } + + @Test + public void integratedSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + + this.put65.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.securityService).authorizeRegionWrite(eq(REGION_NAME), eq(KEY)); + verify(this.replyMessage).send(this.serverConnection); + } + + @Test + public void integratedSecurityShouldFailIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + doThrow(new NotAuthorizedException("")).when(this.securityService).authorizeRegionWrite(eq(REGION_NAME), eq(KEY)); + + this.put65.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.securityService).authorizeRegionWrite(eq(REGION_NAME), eq(KEY)); + verify(this.errorResponseMessage).send(this.serverConnection); + } + + @Test + public void oldSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + + this.put65.cmdExecute(this.message, this.serverConnection, 0); + + ArgumentCaptor argument = ArgumentCaptor.forClass(byte[].class); + verify(this.replyMessage).addBytesPart(argument.capture()); + + assertThat(argument.getValue()).isEqualTo(OK_BYTES); + + verify(this.authzRequest).putAuthorize(eq(REGION_NAME), eq(KEY), eq(VALUE), eq(true), eq(CALLBACK_ARG)); + verify(this.replyMessage).send(this.serverConnection); + } + + @Test + public void oldSecurityShouldFailIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + doThrow(new NotAuthorizedException("")).when(this.authzRequest).putAuthorize(eq(REGION_NAME), eq(KEY), eq(VALUE), eq(true), eq(CALLBACK_ARG)); + + this.put65.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.authzRequest).putAuthorize(eq(REGION_NAME), eq(KEY), eq(VALUE), eq(true), eq(CALLBACK_ARG)); + + ArgumentCaptor argument = ArgumentCaptor.forClass(NotAuthorizedException.class); + verify(this.errorResponseMessage).addObjPart(argument.capture()); + + assertThat(argument.getValue()).isExactlyInstanceOf(NotAuthorizedException.class); + verify(this.errorResponseMessage).send(this.serverConnection); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f93b7485/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/PutTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/PutTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/PutTest.java new file mode 100644 index 0000000..da6ffd0 --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/PutTest.java @@ -0,0 +1,197 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gemstone.gemfire.internal.cache.tier.sockets.command; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.gemstone.gemfire.CancelCriterion; +import com.gemstone.gemfire.cache.Cache; +import com.gemstone.gemfire.cache.operations.PutOperationContext; +import com.gemstone.gemfire.internal.cache.EntryEventImpl; +import com.gemstone.gemfire.internal.cache.LocalRegion; +import com.gemstone.gemfire.internal.cache.tier.CachedRegionHelper; +import com.gemstone.gemfire.internal.cache.tier.sockets.CacheServerStats; +import com.gemstone.gemfire.internal.cache.tier.sockets.ClientProxyMembershipID; +import com.gemstone.gemfire.internal.cache.tier.sockets.Message; +import com.gemstone.gemfire.internal.cache.tier.sockets.Part; +import com.gemstone.gemfire.internal.cache.tier.sockets.ServerConnection; +import com.gemstone.gemfire.internal.security.AuthorizeRequest; +import com.gemstone.gemfire.internal.security.SecurityService; +import com.gemstone.gemfire.security.NotAuthorizedException; +import com.gemstone.gemfire.test.junit.categories.UnitTest; + +@Category(UnitTest.class) +public class PutTest { + + private static final String REGION_NAME = "region1"; + private static final String KEY = "key1"; + private static final Object CALLBACK_ARG = "arg"; + private static final byte[] EVENT = new byte[8]; + private static final byte[] VALUE = new byte[8]; + private static final byte[] OK_BYTES = new byte[]{0}; + + @Mock + private SecurityService securityService; + @Mock + private Message message; + @Mock + private ServerConnection serverConnection; + @Mock + private AuthorizeRequest authzRequest; + @Mock + private Cache cache; + @Mock + private LocalRegion localRegion; + @Mock + private Part regionNamePart; + @Mock + private Part keyPart; + @Mock + private Part valuePart; + @Mock + private Part eventPart; + @Mock + private Part callbackArgsPart; + @Mock + private PutOperationContext putOperationContext; + @Mock + private Message errorResponseMessage; + @Mock + private Message replyMessage; + + @InjectMocks + private Put put; + + @Before + public void setUp() throws Exception { + this.put = new Put(); + MockitoAnnotations.initMocks(this); + + when(this.authzRequest.putAuthorize(eq(REGION_NAME), eq(KEY), eq(VALUE), eq(true), eq(CALLBACK_ARG))).thenReturn(this.putOperationContext); + + when(this.putOperationContext.getCallbackArg()).thenReturn(CALLBACK_ARG); + when(this.putOperationContext.getSerializedValue()).thenReturn(VALUE); + when(this.putOperationContext.isObject()).thenReturn(true); + + when(this.cache.getRegion(isA(String.class))).thenReturn(this.localRegion); + when(this.cache.getCancelCriterion()).thenReturn(mock(CancelCriterion.class)); + + when(this.callbackArgsPart.getObject()).thenReturn(CALLBACK_ARG); + + when(this.eventPart.getSerializedForm()).thenReturn(this.EVENT); + + when(this.valuePart.getSerializedForm()).thenReturn(VALUE); + when(this.valuePart.isObject()).thenReturn(true); + when(this.keyPart.getStringOrObject()).thenReturn(KEY); + + when(this.message.getNumberOfParts()).thenReturn(5); + when(this.message.getPart(eq(0))).thenReturn(this.regionNamePart); + when(this.message.getPart(eq(1))).thenReturn(this.keyPart); + when(this.message.getPart(eq(2))).thenReturn(this.valuePart); + when(this.message.getPart(eq(3))).thenReturn(this.eventPart); + when(this.message.getPart(eq(4))).thenReturn(this.callbackArgsPart); + + when(this.regionNamePart.getString()).thenReturn(REGION_NAME); + + when(this.serverConnection.getCache()).thenReturn(this.cache); + when(this.serverConnection.getCacheServerStats()).thenReturn(mock(CacheServerStats.class)); + when(this.serverConnection.getAuthzRequest()).thenReturn(this.authzRequest); + when(this.serverConnection.getCachedRegionHelper()).thenReturn(mock(CachedRegionHelper.class)); + when(this.serverConnection.getReplyMessage()).thenReturn(this.replyMessage); + when(this.serverConnection.getErrorResponseMessage()).thenReturn(this.errorResponseMessage); + + when(this.localRegion.basicBridgePut(eq(KEY), eq(VALUE), eq(null), eq(true), eq(CALLBACK_ARG), any(ClientProxyMembershipID.class), eq(true), any(EntryEventImpl.class))).thenReturn(true); + } + + @Test + public void noSecurityShouldSucceed() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(false); + + this.put.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.replyMessage).send(this.serverConnection); + } + + @Test + public void integratedSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + + this.put.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.securityService).authorizeRegionWrite(eq(REGION_NAME), eq(KEY)); + verify(this.replyMessage).send(this.serverConnection); + } + + @Test + public void integratedSecurityShouldThrowIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + doThrow(new NotAuthorizedException("")).when(this.securityService).authorizeRegionWrite(eq(REGION_NAME), eq(KEY)); + + this.put.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.securityService).authorizeRegionWrite(eq(REGION_NAME), eq(KEY)); + verify(this.errorResponseMessage).send(this.serverConnection); + } + + @Test + public void oldSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + + this.put.cmdExecute(this.message, this.serverConnection, 0); + + ArgumentCaptor argument = ArgumentCaptor.forClass(byte[].class); + verify(this.replyMessage).addBytesPart(argument.capture()); + + assertThat(argument.getValue()).isEqualTo(OK_BYTES); + + verify(this.authzRequest).putAuthorize(eq(REGION_NAME), eq(KEY), eq(VALUE), eq(true), eq(CALLBACK_ARG)); + verify(this.replyMessage).send(this.serverConnection); + } + + @Test + public void oldSecurityShouldFailIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + doThrow(new NotAuthorizedException("")).when(this.authzRequest).putAuthorize(eq(REGION_NAME), eq(KEY), eq(VALUE), eq(true), eq(CALLBACK_ARG)); + + this.put.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.authzRequest).putAuthorize(eq(REGION_NAME), eq(KEY), eq(VALUE), eq(true), eq(CALLBACK_ARG)); + + ArgumentCaptor argument = ArgumentCaptor.forClass(NotAuthorizedException.class); + verify(this.errorResponseMessage).addObjPart(argument.capture()); + + assertThat(argument.getValue()).isExactlyInstanceOf(NotAuthorizedException.class); + verify(this.errorResponseMessage).send(this.serverConnection); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f93b7485/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/RegisterInterest61Test.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/RegisterInterest61Test.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/RegisterInterest61Test.java new file mode 100644 index 0000000..7a1fef8 --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/RegisterInterest61Test.java @@ -0,0 +1,184 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gemstone.gemfire.internal.cache.tier.sockets.command; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.gemstone.gemfire.CancelCriterion; +import com.gemstone.gemfire.cache.Cache; +import com.gemstone.gemfire.cache.operations.RegisterInterestOperationContext; +import com.gemstone.gemfire.internal.Version; +import com.gemstone.gemfire.internal.cache.LocalRegion; +import com.gemstone.gemfire.internal.cache.tier.CachedRegionHelper; +import com.gemstone.gemfire.internal.cache.tier.sockets.AcceptorImpl; +import com.gemstone.gemfire.internal.cache.tier.sockets.ChunkedMessage; +import com.gemstone.gemfire.internal.cache.tier.sockets.Message; +import com.gemstone.gemfire.internal.cache.tier.sockets.Part; +import com.gemstone.gemfire.internal.cache.tier.sockets.ServerConnection; +import com.gemstone.gemfire.internal.security.AuthorizeRequest; +import com.gemstone.gemfire.internal.security.SecurityService; +import com.gemstone.gemfire.security.NotAuthorizedException; +import com.gemstone.gemfire.test.junit.categories.UnitTest; + +@Category(UnitTest.class) +public class RegisterInterest61Test { + private static final String REGION_NAME = "region1"; + private static final String KEY = "key1"; + private static final byte[] DURABLE = new byte[8]; + + + @Mock + private SecurityService securityService; + @Mock + private Message message; + @Mock + private ServerConnection serverConnection; + @Mock + private AuthorizeRequest authzRequest; + @Mock + private Cache cache; + @Mock + private Part regionNamePart; + @Mock + private Part interestTypePart; + @Mock + private Part durablePart; + @Mock + private Part keyPart; + @Mock + private Part notifyPart; + @Mock + private RegisterInterestOperationContext registerInterestOperationContext; + @Mock + private ChunkedMessage chunkedResponseMessage; + + @InjectMocks + private RegisterInterest61 registerInterest61; + + @Before + public void setUp() throws Exception { + this.registerInterest61 = new RegisterInterest61(); + MockitoAnnotations.initMocks(this); + + + when(this.authzRequest.registerInterestAuthorize(eq(REGION_NAME), eq(KEY), anyInt(), any())).thenReturn(this.registerInterestOperationContext); + + when(this.cache.getRegion(isA(String.class))).thenReturn(mock(LocalRegion.class)); + when(this.cache.getCancelCriterion()).thenReturn(mock(CancelCriterion.class)); + + when(this.durablePart.getObject()).thenReturn(DURABLE); + + when(this.interestTypePart.getInt()).thenReturn(0); + + when(this.keyPart.getStringOrObject()).thenReturn(KEY); + + when(this.message.getNumberOfParts()).thenReturn(6); + when(this.message.getPart(eq(0))).thenReturn(this.regionNamePart); + when(this.message.getPart(eq(1))).thenReturn(this.interestTypePart); + when(this.message.getPart(eq(2))).thenReturn(mock(Part.class)); + when(this.message.getPart(eq(3))).thenReturn(this.durablePart); + when(this.message.getPart(eq(4))).thenReturn(this.keyPart); + when(this.message.getPart(eq(5))).thenReturn(this.notifyPart); + + when(this.notifyPart.getObject()).thenReturn(DURABLE); + + when(this.regionNamePart.getString()).thenReturn(REGION_NAME); + + when(this.registerInterestOperationContext.getKey()).thenReturn(KEY); + + when(this.serverConnection.getCache()).thenReturn(this.cache); + when(this.serverConnection.getAuthzRequest()).thenReturn(this.authzRequest); + when(this.serverConnection.getCachedRegionHelper()).thenReturn(mock(CachedRegionHelper.class)); + when(this.serverConnection.getChunkedResponseMessage()).thenReturn(this.chunkedResponseMessage); + when(this.serverConnection.getClientVersion()).thenReturn(Version.GFE_80); + when(this.serverConnection.getAcceptor()).thenReturn(mock(AcceptorImpl.class)); + } + + @Test + public void noSecurityShouldSucceed() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(false); + + this.registerInterest61.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.chunkedResponseMessage).sendChunk(this.serverConnection); + } + + @Test + public void integratedSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + + this.registerInterest61.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.securityService).authorizeRegionRead(eq(REGION_NAME), eq(KEY)); + verify(this.chunkedResponseMessage).sendChunk(this.serverConnection); + } + + @Test + public void integratedSecurityShouldThrowIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(true); + doThrow(new NotAuthorizedException("")).when(this.securityService).authorizeRegionRead(eq(REGION_NAME), eq(KEY)); + + this.registerInterest61.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.securityService).authorizeRegionRead(eq(REGION_NAME), eq(KEY)); + verify(this.chunkedResponseMessage).sendChunk(this.serverConnection); + } + + @Test + public void oldSecurityShouldSucceedIfAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + + this.registerInterest61.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.authzRequest).registerInterestAuthorize(eq(REGION_NAME), eq(KEY), anyInt(), any()); + verify(this.chunkedResponseMessage).sendChunk(this.serverConnection); + } + + @Test + public void oldSecurityShouldFailIfNotAuthorized() throws Exception { + when(this.securityService.isClientSecurityRequired()).thenReturn(true); + when(this.securityService.isIntegratedSecurity()).thenReturn(false); + + doThrow(new NotAuthorizedException("")).when(this.authzRequest).registerInterestAuthorize(eq(REGION_NAME), eq(KEY), anyInt(), any()); + + this.registerInterest61.cmdExecute(this.message, this.serverConnection, 0); + + verify(this.authzRequest).registerInterestAuthorize(eq(REGION_NAME), eq(KEY), anyInt(), any()); + + ArgumentCaptor argument = ArgumentCaptor.forClass(NotAuthorizedException.class); + verify(this.chunkedResponseMessage).addObjPart(argument.capture()); + + assertThat(argument.getValue()).isExactlyInstanceOf(NotAuthorizedException.class); + verify(this.chunkedResponseMessage).sendChunk(this.serverConnection); + } + + + +}