Search in sources :

Example 96 with ClientCache

use of org.apache.geode.cache.client.ClientCache in project geode by apache.

the class RemoteTransactionDUnitTest method testBug43176.

@Test
public void testBug43176() {
    Host host = Host.getHost(0);
    VM datastore = host.getVM(0);
    VM client = host.getVM(1);
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            AttributesFactory<Integer, String> af = new AttributesFactory<Integer, String>();
            af.setScope(Scope.DISTRIBUTED_ACK);
            af.setDataPolicy(DataPolicy.EMPTY);
            af.setConcurrencyChecksEnabled(getConcurrencyChecksEnabled());
            getCache().createRegionFactory(af.create()).create(EMPTY_REGION);
            af.setDataPolicy(DataPolicy.REPLICATE);
            getCache().createRegionFactory(af.create()).create(D_REFERENCE);
            return null;
        }
    });
    final int port = startServer(datastore);
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            ClientCacheFactory ccf = new ClientCacheFactory();
            ccf.addPoolServer("localhost", /* getServerHostName(Host.getHost(0)) */
            port);
            ccf.setPoolSubscriptionEnabled(true);
            ccf.set(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
            ClientCache cCache = getClientCache(ccf);
            ClientRegionFactory<Integer, String> crf = cCache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY);
            crf.addCacheListener(new ClientListener());
            crf.setConcurrencyChecksEnabled(getConcurrencyChecksEnabled());
            Region r = crf.create(D_REFERENCE);
            Region empty = crf.create(EMPTY_REGION);
            r.registerInterest("ALL_KEYS", InterestResultPolicy.KEYS_VALUES);
            empty.registerInterest("ALL_KEYS", InterestResultPolicy.KEYS_VALUES);
            return null;
        }
    });
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region ref = getCache().getRegion(D_REFERENCE);
            Region empty = getCache().getRegion(EMPTY_REGION);
            getGemfireCache().getCacheTransactionManager().begin();
            ref.put("one", "value1");
            empty.put("eone", "valueOne");
            getCache().getLogger().info("SWAP:callingCommit");
            getGemfireCache().getCacheTransactionManager().commit();
            assertTrue(ref.containsKey("one"));
            assertEquals("value1", ref.get("one"));
            assertFalse(empty.containsKey("eone"));
            assertNull(empty.get("eone"));
            return null;
        }
    });
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region empty = getCache().getRegion(EMPTY_REGION);
            final ClientListener l = (ClientListener) empty.getAttributes().getCacheListeners()[0];
            WaitCriterion wc = new WaitCriterion() {

                public boolean done() {
                    return l.invoked;
                }

                public String description() {
                    return "listener invoked:" + l.invoked;
                }
            };
            Wait.waitForCriterion(wc, 10 * 1000, 200, true);
            return null;
        }
    });
}
Also used : Host(org.apache.geode.test.dunit.Host) ClientCache(org.apache.geode.cache.client.ClientCache) NamingException(javax.naming.NamingException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) TransactionException(org.apache.geode.cache.TransactionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) RollbackException(javax.transaction.RollbackException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) CommitConflictException(org.apache.geode.cache.CommitConflictException) ClientRegionFactory(org.apache.geode.cache.client.ClientRegionFactory) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) TXExpiryJUnitTest(org.apache.geode.TXExpiryJUnitTest) Test(org.junit.Test)

Example 97 with ClientCache

use of org.apache.geode.cache.client.ClientCache in project geode by apache.

the class ClientExecuteRegionFunctionAuthDUnitTest method testExecuteRegionFunction.

@Test
public void testExecuteRegionFunction() {
    FunctionService.registerFunction(function);
    client1.invoke("logging in with dataReader", () -> {
        ClientCache cache = createClientCache("dataReader", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        FunctionService.registerFunction(function);
        assertNotAuthorized(() -> FunctionService.onRegion(region).setArguments(Boolean.TRUE).execute(function.getId()), "DATA:WRITE");
    });
    client2.invoke("logging in with super-user", () -> {
        ClientCache cache = createClientCache("super-user", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        FunctionService.registerFunction(function);
        ResultCollector rc = FunctionService.onRegion(region).setArguments(Boolean.TRUE).execute(function.getId());
        rc.getResult();
    });
}
Also used : SecurityTestUtil.createProxyRegion(org.apache.geode.security.SecurityTestUtil.createProxyRegion) Region(org.apache.geode.cache.Region) ClientCache(org.apache.geode.cache.client.ClientCache) SecurityTestUtil.createClientCache(org.apache.geode.security.SecurityTestUtil.createClientCache) ResultCollector(org.apache.geode.cache.execute.ResultCollector) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 98 with ClientCache

use of org.apache.geode.cache.client.ClientCache in project geode by apache.

the class ClientGetEntryAuthDUnitTest method testGetEntry.

@Test
public void testGetEntry() throws Exception {
    // client1 connects to server as a user not authorized to do any operations
    AsyncInvocation ai1 = client1.invokeAsync(() -> {
        ClientCache cache = createClientCache("stranger", "1234567", server.getPort());
        CacheTransactionManager transactionManager = cache.getCacheTransactionManager();
        transactionManager.begin();
        try {
            Region region = createProxyRegion(cache, REGION_NAME);
            assertNotAuthorized(() -> region.getEntry("key3"), "DATA:READ:AuthRegion:key3");
        } finally {
            transactionManager.commit();
        }
    });
    AsyncInvocation ai2 = client2.invokeAsync(() -> {
        ClientCache cache = createClientCache("authRegionReader", "1234567", server.getPort());
        CacheTransactionManager transactionManager = cache.getCacheTransactionManager();
        transactionManager.begin();
        try {
            Region region = createProxyRegion(cache, REGION_NAME);
            region.getEntry("key3");
        } finally {
            transactionManager.commit();
        }
    });
    ai1.await();
    ai2.await();
}
Also used : SecurityTestUtil.createProxyRegion(org.apache.geode.security.SecurityTestUtil.createProxyRegion) Region(org.apache.geode.cache.Region) ClientCache(org.apache.geode.cache.client.ClientCache) SecurityTestUtil.createClientCache(org.apache.geode.security.SecurityTestUtil.createClientCache) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 99 with ClientCache

use of org.apache.geode.cache.client.ClientCache in project geode by apache.

the class ClientGetPutAuthDUnitTest method testGetPutAuthorization.

@Test
public void testGetPutAuthorization() throws InterruptedException {
    Map<String, String> allValues = new HashMap<String, String>();
    allValues.put("key1", "value1");
    allValues.put("key2", "value2");
    List<String> keys = new ArrayList<>();
    keys.add("key1");
    keys.add("key2");
    // client1 connects to server as a user not authorized to do any operations
    AsyncInvocation ai1 = client1.invokeAsync(() -> {
        ClientCache cache = createClientCache("stranger", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        assertNotAuthorized(() -> region.put("key3", "value3"), "DATA:WRITE:AuthRegion:key3");
        assertNotAuthorized(() -> region.get("key3"), "DATA:READ:AuthRegion:key3");
        // putall
        assertNotAuthorized(() -> region.putAll(allValues), "DATA:WRITE:AuthRegion");
        // not authorized for either keys, get no record back
        Map keyValues = region.getAll(keys);
        assertEquals(0, keyValues.size());
        assertNotAuthorized(() -> region.keySetOnServer(), "DATA:READ:AuthRegion");
    });
    // client2 connects to user as a user authorized to use AuthRegion region
    AsyncInvocation ai2 = client2.invokeAsync(() -> {
        ClientCache cache = createClientCache("authRegionUser", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        region.put("key3", "value3");
        assertEquals("value3", region.get("key3"));
        // put all
        region.putAll(allValues);
        // get all
        Map keyValues = region.getAll(keys);
        assertEquals(2, keyValues.size());
        // keyset
        Set keySet = region.keySetOnServer();
        assertEquals(5, keySet.size());
    });
    // client3 connects to user as a user authorized to use key1 in AuthRegion region
    AsyncInvocation ai3 = client3.invokeAsync(() -> {
        ClientCache cache = createClientCache("key1User", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        assertNotAuthorized(() -> region.put("key2", "value1"), "DATA:WRITE:AuthRegion:key2");
        assertNotAuthorized(() -> region.get("key2"), "DATA:READ:AuthRegion:key2");
        assertNotAuthorized(() -> region.putAll(allValues), "DATA:WRITE:AuthRegion");
        // only authorized for one recrod
        Map keyValues = region.getAll(keys);
        assertEquals(1, keyValues.size());
        // keyset
        assertNotAuthorized(() -> region.keySetOnServer(), "DATA:READ:AuthRegion");
    });
    ai1.join();
    ai2.join();
    ai3.join();
    ai1.checkException();
    ai2.checkException();
    ai3.checkException();
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SecurityTestUtil.createProxyRegion(org.apache.geode.security.SecurityTestUtil.createProxyRegion) Region(org.apache.geode.cache.Region) ClientCache(org.apache.geode.cache.client.ClientCache) SecurityTestUtil.createClientCache(org.apache.geode.security.SecurityTestUtil.createClientCache) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) HashMap(java.util.HashMap) Map(java.util.Map) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 100 with ClientCache

use of org.apache.geode.cache.client.ClientCache in project geode by apache.

the class ClientContainsKeyAuthDUnitTest method testContainsKey.

@Test
public void testContainsKey() throws Exception {
    AsyncInvocation ai1 = client1.invokeAsync(() -> {
        ClientCache cache = createClientCache("key1User", "1234567", server.getPort());
        final Region region = createProxyRegion(cache, REGION_NAME);
        assertTrue(region.containsKeyOnServer("key1"));
        SecurityTestUtil.assertNotAuthorized(() -> region.containsKeyOnServer("key3"), "DATA:READ:AuthRegion:key3");
    });
    AsyncInvocation ai2 = client2.invokeAsync(() -> {
        ClientCache cache = createClientCache("authRegionReader", "1234567", server.getPort());
        final Region region = createProxyRegion(cache, REGION_NAME);
        region.containsKeyOnServer("key3");
        assertTrue(region.containsKeyOnServer("key1"));
    });
    ai1.await();
    ai2.await();
}
Also used : SecurityTestUtil.createProxyRegion(org.apache.geode.security.SecurityTestUtil.createProxyRegion) Region(org.apache.geode.cache.Region) ClientCache(org.apache.geode.cache.client.ClientCache) SecurityTestUtil.createClientCache(org.apache.geode.security.SecurityTestUtil.createClientCache) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

ClientCache (org.apache.geode.cache.client.ClientCache)112 Test (org.junit.Test)74 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)73 ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)65 Region (org.apache.geode.cache.Region)64 VM (org.apache.geode.test.dunit.VM)43 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)42 Host (org.apache.geode.test.dunit.Host)40 SecurityTest (org.apache.geode.test.junit.categories.SecurityTest)31 QueryService (org.apache.geode.cache.query.QueryService)26 SelectResults (org.apache.geode.cache.query.SelectResults)25 SecurityTestUtil.createClientCache (org.apache.geode.security.SecurityTestUtil.createClientCache)25 SecurityTestUtil.createProxyRegion (org.apache.geode.security.SecurityTestUtil.createProxyRegion)23 CacheServer (org.apache.geode.cache.server.CacheServer)22 Cache (org.apache.geode.cache.Cache)20 CacheException (org.apache.geode.cache.CacheException)15 PortfolioPdx (org.apache.geode.cache.query.data.PortfolioPdx)13 IOException (java.io.IOException)12 Properties (java.util.Properties)12 IgnoredException (org.apache.geode.test.dunit.IgnoredException)12