protected void DoOp()

in clicache/integration-test/ThinClientSecurityAuthzTestBaseN.cs [80:508]


    protected void DoOp(OperationCode op, int[] indices,
      OpFlags flags, ExpectedResult expectedResult, Properties<string, string> creds, bool isMultiuser)
    {
      IRegion<object, object> region;
      if(isMultiuser)
        region = CacheHelper.GetRegion<object, object>(RegionName, creds);
      else
        region = CacheHelper.GetRegion<object, object>(RegionName);

      if (CheckFlags(flags, OpFlags.UseSubRegion))
      {
        IRegion<object, object> subregion = null;
        if (CheckFlags(flags, OpFlags.NoCreateSubRegion))
        {
          subregion = region.GetSubRegion(SubregionName);
          if (CheckFlags(flags, OpFlags.CheckNoRegion))
          {
            Assert.IsNull(subregion);
            return;
          }
          else
          {
            Assert.IsNotNull(subregion);
          }
        }
        else
        {
          subregion = CreateSubregion(region);
          if (isMultiuser)
            subregion = region.GetSubRegion(SubregionName);
        }
        Assert.IsNotNull(subregion);
        region = subregion;
      }
      else if (CheckFlags(flags, OpFlags.CheckNoRegion))
      {
        Assert.IsNull(region);
        return;
      }
      else
      {
        Assert.IsNotNull(region);
      }
      string valPrefix;
      if (CheckFlags(flags, OpFlags.UseNewVal))
      {
        valPrefix = NValuePrefix;
      }
      else
      {
        valPrefix = ValuePrefix;
      }
      int numOps = indices.Length;
      Util.Log("Got DoOp for op: " + op + ", numOps: " + numOps
              + ", indices: " + IndicesToString(indices));
      bool exceptionOccured = false;
      bool breakLoop = false;
      for (int indexIndex = 0; indexIndex < indices.Length; ++indexIndex)
      {
        if (breakLoop)
        {
          break;
        }
        int index = indices[indexIndex];
        string key = KeyPrefix + index;
        string expectedValue = (valPrefix + index);
        try
        {
          switch (op)
          {
            case OperationCode.Get:
              Object value = null;
              if (CheckFlags(flags, OpFlags.LocalOp))
              {
                int sleepMillis = 100;
                int numTries = 30;
                bool success = false;
                while (!success && numTries-- > 0)
                {
                  if (!isMultiuser && region.ContainsValueForKey(key))
                  {
                    value = region[key];
                    success = expectedValue.Equals(value.ToString());
                    if (CheckFlags(flags, OpFlags.CheckFail))
                    {
                      success = !success;
                    }
                  }
                  else
                  {
                    value = null;
                    success = CheckFlags(flags, OpFlags.CheckFail);
                  }
                  if (!success)
                  {
                    Thread.Sleep(sleepMillis);
                  }
                }
              }
              else
              {
                if (!isMultiuser)
                {
                  if (CheckFlags(flags, OpFlags.CheckNoKey))
                  {
                    Assert.IsFalse(region.GetLocalView().ContainsKey(key));
                  }
                  else
                  {
                    Assert.IsTrue(region.GetLocalView().ContainsKey(key));
                    region.GetLocalView().Invalidate(key);
                  }
                }
                try
                {
                  value = region[key];
                }
                catch (Client.KeyNotFoundException )
                {
                  Util.Log("KeyNotFoundException while getting key. should be ok as we are just testing auth");
                }
              }
              if (!isMultiuser && value != null)
              {
                if (CheckFlags(flags, OpFlags.CheckFail))
                {
                  Assert.AreNotEqual(expectedValue, value.ToString());
                }
                else
                {
                  Assert.AreEqual(expectedValue, value.ToString());
                }
              }
              break;
            case OperationCode.Put:
              region[key] = expectedValue;
              break;
            case OperationCode.Destroy:
              if (!isMultiuser && !region.GetLocalView().ContainsKey(key))
              {
                // Since DESTROY will fail unless the value is present
                // in the local cache, this is a workaround for two cases:
                // 1. When the operation is supposed to succeed then in
                // the current AuthzCredentialGenerators the clients having
                // DESTROY permission also has CREATE/UPDATE permission
                // so that calling region.Put() will work for that case.
                // 2. When the operation is supposed to fail with
                // NotAuthorizedException then in the current
                // AuthzCredentialGenerators the clients not
                // having DESTROY permission are those with reader role that have
                // GET permission.
                //
                // If either of these assumptions fails, then this has to be
                // adjusted or reworked accordingly.
                if (CheckFlags(flags, OpFlags.CheckNotAuthz))
                {
                  value = region[key];
                  Assert.AreEqual(expectedValue, value.ToString());
                }
                else
                {
                  region[key] = expectedValue;
                }
              }
              if ( !isMultiuser && CheckFlags(flags, OpFlags.LocalOp))
              {
                region.GetLocalView().Remove(key); //Destroyed replaced by Remove() API
              }
              else
              {
                region.Remove(key); //Destroyed replaced by Remove API
              }
              break;
            //TODO: Need to fix Stack overflow exception..
            case OperationCode.RegisterInterest:
              if (CheckFlags(flags, OpFlags.UseList))
              {
                breakLoop = true;
                // Register interest list in this case
                List<CacheableKey> keyList = new List<CacheableKey>(numOps);
                for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex)
                {
                  int keyNum = indices[keyNumIndex];
                  keyList.Add(KeyPrefix + keyNum);
                }
                region.GetSubscriptionService().RegisterKeys(keyList.ToArray());
              }
              else if (CheckFlags(flags, OpFlags.UseRegex))
              {
                breakLoop = true;
                region.GetSubscriptionService().RegisterRegex(KeyPrefix + "[0-" + (numOps - 1) + ']');
              }
              else if (CheckFlags(flags, OpFlags.UseAllKeys))
              {
                breakLoop = true;
                region.GetSubscriptionService().RegisterAllKeys();
              }
              break;
            //TODO: Need to fix Stack overflow exception..
            case OperationCode.UnregisterInterest:
              if (CheckFlags(flags, OpFlags.UseList))
              {
                breakLoop = true;
                // Register interest list in this case
                List<CacheableKey> keyList = new List<CacheableKey>(numOps);
                for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex)
                {
                  int keyNum = indices[keyNumIndex];
                  keyList.Add(KeyPrefix + keyNum);
                }
                region.GetSubscriptionService().UnregisterKeys(keyList.ToArray());
              }
              else if (CheckFlags(flags, OpFlags.UseRegex))
              {
                breakLoop = true;
                region.GetSubscriptionService().UnregisterRegex(KeyPrefix + "[0-" + (numOps - 1) + ']');
              }
              else if (CheckFlags(flags, OpFlags.UseAllKeys))
              {
                breakLoop = true;
                region.GetSubscriptionService().UnregisterAllKeys();
              }
              break;
            case OperationCode.Query:
              breakLoop = true;
              ISelectResults<object> queryResults;

              if (!isMultiuser)
              {
                queryResults = (ResultSet<object>)region.Query<object>(
                  "SELECT DISTINCT * FROM " + region.FullPath);
              }
              else
              {
                queryResults = CacheHelper.getMultiuserCache(creds).GetQueryService().NewQuery<object>("SELECT DISTINCT * FROM " + region.FullPath).Execute();
              }
              Assert.IsNotNull(queryResults);
              if (!CheckFlags(flags, OpFlags.CheckFail))
              {
                Assert.AreEqual(numOps, queryResults.Size);
              }

              var querySet = new List<string>((int) queryResults.Size);
              ResultSet<object> rs = queryResults as ResultSet<object>;
              foreach ( object result in  rs)
              {
                querySet.Add(result.ToString());
              }
              for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex)
              {
                int keyNum = indices[keyNumIndex];
                string expectedVal = valPrefix + keyNumIndex;
                if (CheckFlags(flags, OpFlags.CheckFail))
                {
                  Assert.IsFalse(querySet.Contains(expectedVal));
                }
                else
                {
                  Assert.IsTrue(querySet.Contains(expectedVal));
                }
              }
              break;
            case OperationCode.RegionDestroy:
              breakLoop = true;
              if ( !isMultiuser && CheckFlags(flags, OpFlags.LocalOp))
              {
                region.GetLocalView().DestroyRegion();
              }
              else
              {
                region.DestroyRegion();
              }
              break;

            case OperationCode.GetServerKeys:
              breakLoop = true;
              ICollection<object> serverKeys = region.Keys;
              break;

            //TODO: Need to fix System.ArgumentOutOfRangeException: Index was out of range. Know issue with GetAll()
            case OperationCode.GetAll:
              //ICacheableKey[] keymap = new ICacheableKey[5];
              List<object> keymap = new List<object>();
              for (int i = 0; i < 5; i++)
              {
                keymap.Add(i);
                //CacheableInt32 item = CacheableInt32.Create(i);
                //Int32 item = i;
                // NOTE: GetAll should operate right after PutAll
                //keymap[i] = item;
              }
              Dictionary<Object, Object> entrymap = new Dictionary<Object, Object>();
              //CacheableHashMap entrymap = CacheableHashMap.Create();
              region.GetAll(keymap, entrymap, null, false);
              if (entrymap.Count < 5)
              {
                Assert.Fail("DoOp: Got fewer entries for op " + op);
              }
              break;
            case OperationCode.PutAll:
              // NOTE: PutAll should operate right before GetAll
              //CacheableHashMap entrymap2 = CacheableHashMap.Create();
              Dictionary<Object, Object> entrymap2 = new Dictionary<object, object>();
              for (int i = 0; i < 5; i++)
              {
                //CacheableInt32 item = CacheableInt32.Create(i);
                Int32 item = i;
                entrymap2.Add(item, item);
              }
              region.PutAll(entrymap2);
              break;
            case OperationCode.RemoveAll:
              Dictionary<Object, Object> entrymap3 = new Dictionary<object, object>();
              for (int i = 0; i < 5; i++)
              {
                //CacheableInt32 item = CacheableInt32.Create(i);
                Int32 item = i;
                entrymap3.Add(item, item);
              }
              region.PutAll(entrymap3);
              ICollection<object> keys = new LinkedList<object>();
              for (int i = 0; i < 5; i++)
              {
                Int32 item = i;
                keys.Add(item);
              }
              region.RemoveAll(keys);
              break;
            case OperationCode.ExecuteCQ:
              Pool/*<object, object>*/ pool = CacheHelper.DCache.GetPoolManager().Find("__TESTPOOL1_");
              QueryService qs;
              if (pool != null)
              {
                qs = pool.GetQueryService();
              }
              else
              {
                //qs = CacheHelper.DCache.GetQueryService();
                qs = null;

              }
              CqAttributesFactory<object, object> cqattrsfact = new CqAttributesFactory<object, object>();
              CqAttributes<object, object> cqattrs = cqattrsfact.Create();
              CqQuery<object, object> cq = qs.NewCq("cq_security", "SELECT * FROM /" + region.Name, cqattrs, false);
              qs.ExecuteCqs();
              qs.StopCqs();
              qs.CloseCqs();
              break;

            case OperationCode.ExecuteFunction:
              if (!isMultiuser)
              {
                Pool/*<object, object>*/ pool2 = CacheHelper.DCache.GetPoolManager().Find("__TESTPOOL1_");
                if (pool2 != null)
                {
                  Client.FunctionService<object>.OnServer(pool2).Execute("securityTest");
                  Client.FunctionService<object>.OnRegion<object, object>(region).Execute("FireNForget"); 
                }
                else
                {
                  expectedResult = ExpectedResult.Success;
                }
              }
              else
              {
                //FunctionService fs = CacheHelper.getMultiuserCache(creds).GetFunctionService();
                //Execution exe =  fs.OnServer();
                IRegionService userCache = CacheHelper.getMultiuserCache(creds);
                Apache.Geode.Client.Execution<object> exe = Client.FunctionService<object>.OnServer(userCache);
                exe.Execute("securityTest");
                exe = Client.FunctionService<object>.OnServers(userCache);
                Client.FunctionService<object>.OnRegion<object, object>(region);
                Client.FunctionService<object>.OnRegion<object, object>(userCache.GetRegion<object, object>(region.Name)).Execute("FireNForget");
              }
              break;
            default:
              Assert.Fail("DoOp: Unhandled operation " + op);
              break;
          }
          
          if (expectedResult != ExpectedResult.Success)
          {
            Assert.Fail("Expected an exception while performing operation");
          }
        }
        catch (AssertionException ex)
        {
          Util.Log("DoOp: failed assertion: {0}", ex);
          throw;
        }
        catch (NotAuthorizedException ex)
        {
          exceptionOccured = true;
          if (expectedResult == ExpectedResult.NotAuthorizedException)
          {
            Util.Log(
                "DoOp: Got expected NotAuthorizedException when doing operation ["
                    + op + "] with flags [" + flags + "]: " + ex.Message);
            continue;
          }
          else
          {
            Assert.Fail("DoOp: Got unexpected NotAuthorizedException when " +
              "doing operation: " + ex.Message);
          }
        }
        catch (Exception ex)
        {
          exceptionOccured = true;
          if (expectedResult == ExpectedResult.OtherException)
          {
            Util.Log("DoOp: Got expected exception when doing operation: " +
              ex.GetType() + "::" + ex.Message);
            continue;
          }
          else
          {
            Assert.Fail("DoOp: Got unexpected exception when doing operation: " + ex);
          }
        }
      }
      
      if (!exceptionOccured
          && expectedResult != ExpectedResult.Success)
      {
        Assert.Fail("Expected an exception while performing operation");
      }
      Util.Log(" doop done");
    }