This class provides a MockDataHandler object that keeps a hash table containing all the data received and an event to notify when an HandleData has been called.
Наследование: IDataHandler
Пример #1
0
        public void test()
        {
            MockDataHandler mdh = new MockDataHandler();

            mdh.HandleDataCallback += Callback;
            ISender sender = new MockSender(null, null, mdh, 0);

            byte[][]   b    = new byte[10][];
            MemBlock[] mb   = new MemBlock[10];
            Random     rand = new Random();

            for (int i = 0; i < 10; i++)
            {
                b[i] = new byte[128];
                rand.NextBytes(b[i]);
                mb[i] = MemBlock.Reference(b[i]);
                sender.Send(mb[i]);
            }

            for (int i = 0; i < 10; i++)
            {
                Assert.AreEqual(i, mdh.Position(mb[i]), "Position " + i);
                Assert.IsTrue(mdh.Contains(mb[i]), "Contains " + i);
            }

            Assert.AreEqual(_count, 10, "Count");
        }
Пример #2
0
    protected void ConnectionHandlerTest(Node node0, Node node1,
        ConnectionHandler ch0, ConnectionHandler ch1)
    {
      Console.WriteLine(node0.Address + " " + node1.Address);
      var mdh0 = new MockDataHandler();
      var mdh1 = new MockDataHandler();
      MemBlock zero = MemBlock.Reference(new byte[] {0});
      EventHandler cb = delegate(object o, EventArgs ea) {
        Assert.AreEqual(o, zero, "Zero");
      };

      mdh0.HandleDataCallback += cb;
      mdh1.HandleDataCallback += cb;
      ch0.Subscribe(mdh0, null);
      ch1.Subscribe(mdh1, null);

      Assert.AreEqual(mdh0.Count, 0, "MDH0 0");
      Assert.AreEqual(mdh1.Count, 0, "MDH1 0");
      ch0.ConnectTo(node1.Address);
      Assert.IsTrue(AreConnected(node0, node1), "ConnectionHandler ConnectTo");
      SimpleTimer.RunSteps(fifteen_mins * 2);
      Assert.IsFalse(Simulator.AreConnected(node0, node1));
      ch0.Send(node1.Address, zero);
      SimpleTimer.RunSteps(fifteen_mins / 60);
      Assert.AreEqual(mdh0.Count, 0, "MDH0 1");
      Assert.AreEqual(mdh1.Count, 0, "MDH1 1");
      Assert.IsTrue(AreConnected(node0, node1), "ConnectionHandler ConnectTo");
      SimpleTimer.RunSteps(fifteen_mins / 3);
      ch0.Send(node1.Address, zero);
      SimpleTimer.RunSteps(fifteen_mins / 60);
      Assert.AreEqual(mdh0.Count, 0, "MDH0 2");
      Assert.AreEqual(mdh1.Count, 1, "MDH1 2");
      Assert.IsTrue(Simulator.AreConnected(node0, node1), "Continuous 0");
      SimpleTimer.RunSteps(fifteen_mins / 3);
      ch0.Send(node1.Address, zero);
      SimpleTimer.RunSteps(fifteen_mins / 60);
      Assert.AreEqual(mdh0.Count, 0, "MDH0 3");
      Assert.AreEqual(mdh1.Count, 2, "MDH1 3");
      Assert.IsTrue(Simulator.AreConnected(node0, node1), "Continuous 1");
      SimpleTimer.RunSteps(fifteen_mins / 3);
      ch0.Send(node1.Address, zero);
      SimpleTimer.RunSteps(fifteen_mins / 60);
      Assert.AreEqual(mdh0.Count, 0, "MDH0 4");
      Assert.AreEqual(mdh1.Count, 3, "MDH1 4");
      Assert.IsTrue(Simulator.AreConnected(node0, node1), "Continuous 2");
      SimpleTimer.RunSteps(fifteen_mins / 3);
      ch1.Send(node0.Address, zero);
      SimpleTimer.RunSteps(fifteen_mins / 60);
      Assert.AreEqual(mdh0.Count, 1, "MDH0 5");
      Assert.AreEqual(mdh1.Count, 3, "MDH1 5");
      Assert.IsTrue(Simulator.AreConnected(node0, node1), "Continuous 3");
      SimpleTimer.RunSteps(fifteen_mins * 2);
      Assert.IsFalse(Simulator.AreConnected(node0, node1), "Dead");
      Assert.AreEqual(mdh0.Count, 1, "MDH0 6");
      Assert.AreEqual(mdh1.Count, 3, "MDH1 6");
    }
Пример #3
0
 public void PType() {
   MockDataHandler idh = new MockDataHandler();
   MockSender mspr = new MockSender(null, null, idh, 1);
   byte[] data = new byte[1024];
   RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
   rng.GetBytes(data);
   MemBlock mdata = MemBlock.Reference(data);
   mspr.Send(new CopyList(new PType("LONG"), mdata));
   Assert.AreEqual(mdata, idh.LastReceived, "PType MockSender");
 }
Пример #4
0
 public void NoPType() {
   MockDataHandler idh = new MockDataHandler();
   MockSender mspr = new ThreadedMockSender(null, null, idh, 0);
   byte[] data = new byte[1024];
   RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
   rng.GetBytes(data);
   MemBlock mdata = MemBlock.Reference(data);
   object lr = idh.LastReceived;
   mspr.Send(mdata);
   while(lr == idh.LastReceived) {
     Thread.Sleep(0);
   }
   Assert.AreEqual(mdata, idh.LastReceived, "No PType MockSender");
 }
Пример #5
0
        public void NoPType()
        {
            MockDataHandler idh  = new MockDataHandler();
            MockSender      mspr = new MockSender(null, null, idh, 0);

            byte[] data = new byte[1024];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

            rng.GetBytes(data);
            MemBlock mdata = MemBlock.Reference(data);

            mspr.Send(mdata);
            Assert.AreEqual(mdata, idh.LastReceived, "No PType MockSender");
        }
Пример #6
0
        public void PType()
        {
            MockDataHandler idh  = new MockDataHandler();
            MockSender      mspr = new ThreadedMockSender(null, null, idh, 1);

            byte[] data = new byte[1024];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

            rng.GetBytes(data);
            MemBlock mdata = MemBlock.Reference(data);
            object   lr    = idh.LastReceived;

            mspr.Send(new CopyList(new PType("LONG"), mdata));
            while (lr == idh.LastReceived)
            {
                Thread.Sleep(0);
            }

            Assert.AreEqual(mdata, idh.LastReceived, "PType MockSender");
        }
Пример #7
0
    public void test() {
      MockDataHandler mdh = new MockDataHandler();
      mdh.HandleDataCallback += Callback;
      ISender sender = new MockSender(null, null, mdh, 0);
      byte[][] b = new byte[10][];
      MemBlock[] mb = new MemBlock[10];
      Random rand = new Random();
      for(int i = 0; i < 10; i++) {
        b[i] = new byte[128];
        rand.NextBytes(b[i]);
        mb[i] = MemBlock.Reference(b[i]);
        sender.Send(mb[i]);
      }

      for(int i = 0; i < 10; i++) {
        Assert.AreEqual(i, mdh.Position(mb[i]), "Position " + i);
        Assert.IsTrue(mdh.Contains(mb[i]), "Contains " + i);
      }

      Assert.AreEqual(_count, 10, "Count");
    }
Пример #8
0
    public void Test() {
      Timer t = new Timer(Timeout, null, 0, 500);
      PeerSecOverlord so0 = CreateValidSO("valid0");
      PeerSecOverlord so1 = CreateValidSO("valid1");

      //Test block one
      {
        MockSender ms0 = new MockSender(null, null, so1, 0);
        MockSender ms1 = new MockSender(ms0, null, so0, 0);
        ms0.ReturnPath = ms1;

        SecurityAssociation sa0 = so0.CreateSecurityAssociation(ms0);
        SecurityAssociation sa1 = so1.CreateSecurityAssociation(ms1);
        Assert.AreEqual(sa0.State, SecurityAssociation.States.Active, "sa0 should be active!");
        Assert.AreEqual(sa1.State, SecurityAssociation.States.Active, "sa1 should be active!");
        Assert.AreEqual(so0.SACount, 1, "so0 should contain just one!");
        Assert.AreEqual(so1.SACount, 1, "so1 should contain just one!");

        Random rand = new Random();
        byte[] b = new byte[128];
        rand.NextBytes(b);
        MemBlock mb = MemBlock.Reference(b);
        sa1.Send(mb);

        new SecurityPolicy(12345, "DES", "MD5");
        sa0 = so0.CreateSecurityAssociation(ms0, 12345);
        Assert.AreEqual(sa0.State, SecurityAssociation.States.Active, "sa0 should be active!");
        Assert.AreEqual(so0.SACount, 2, "so0 should contain just one!");
        Assert.AreEqual(so1.SACount, 2, "so1 should contain just one!");

        b = new byte[128];
        rand.NextBytes(b);
        mb = MemBlock.Reference(b);
        sa0.Send(mb);
      }

      // create ~250 valid SAs for one guy...
      for(int i = 2; i < 250; i++) {
        PeerSecOverlord so = CreateValidSO("valid" + i);
        MockSender msa = new MockSender(null, null, so, 0);
        MockSender msb = new MockSender(msa, null, so0, 0);
        msa.ReturnPath = msb;

        SecurityAssociation sab = so.CreateSecurityAssociation(msb);
        Assert.AreEqual(sab.State, SecurityAssociation.States.Active, "sab should be active! " + i);
        SecurityAssociation saa = so0.CreateSecurityAssociation(msa);
        Assert.AreEqual(saa.State, SecurityAssociation.States.Active, "saa should be active! " + i);

        MockDataHandler mdha = new MockDataHandler();
        saa.Subscribe(mdha, null);
        MockDataHandler mdhb = new MockDataHandler();
        sab.Subscribe(mdhb, null);

        Random rand = new Random();
        byte[] b = new byte[128];
        rand.NextBytes(b);
        MemBlock mb = MemBlock.Reference(b);
        sab.Send(mb);
        Assert.IsTrue(mdha.Contains(mb), "mdhb Contains " + i);

        b = new byte[128];
        rand.NextBytes(b);
        mb = MemBlock.Reference(b);
        sab.Send(mb);
        Assert.IsTrue(mdha.Contains(mb), "mdha Contains " + i);
      }

      for(int i = 250; i < 500; i++) {
        int ij = (250 % 3) + 1;
        PeerSecOverlord so = CreateInvalidSO("valid" + i, ij);
        MockSender msa = new MockSender(null, null, so, 0);
        MockSender msb = new MockSender(msa, null, so0, 0);
        msa.ReturnPath = msb;

        SecurityAssociation sab = so.CreateSecurityAssociation(msb);
        SecurityAssociation saa = so0.CreateSecurityAssociation(msa);
        Assert.AreEqual(sab.State, SecurityAssociation.States.Waiting, "sab should be waiting! " + i);
        Assert.AreEqual(saa.State, SecurityAssociation.States.Waiting, "saa should be waiting! " + i);
      }

      // create ~250 valid SAs for one guy...
      for(int i = 500; i < 750; i++) {
        PeerSecOverlord so = CreateValidSO("valid" + i);
        MockSender msa = new MockSender(null, null, so, 0);
        MockSender msb = new MockSender(msa, null, so0, 0);
        msa.ReturnPath = msb;

        SecurityAssociation sab = so.CreateSecurityAssociation(msb);
        Assert.AreEqual(sab.State, SecurityAssociation.States.Active, "sab should be active! " + i);
        SecurityAssociation saa = so0.CreateSecurityAssociation(msa);
        Assert.AreEqual(saa.State, SecurityAssociation.States.Active, "saa should be active! " + i);

        MockDataHandler mdha = new MockDataHandler();
        saa.Subscribe(mdha, null);
        MockDataHandler mdhb = new MockDataHandler();
        sab.Subscribe(mdhb, null);

        Random rand = new Random();
        byte[] b = new byte[128];
        rand.NextBytes(b);
        MemBlock mb = MemBlock.Reference(b);
        sab.Send(mb);
        Assert.IsTrue(mdha.Contains(mb), "mdhb Contains " + i);

        b = new byte[128];
        rand.NextBytes(b);
        mb = MemBlock.Reference(b);
        sab.Send(mb);
        Assert.IsTrue(mdha.Contains(mb), "mdha Contains " + i);
      }

      Random randr = new Random();
      byte[] br = new byte[128];
      randr.NextBytes(br);
      MemBlock mbr = MemBlock.Reference(br);

      // New logic requires that we call this first, to set all SAs to not
      // running, the following for loop sets all "Active" SAs back to _running
      // Thus keeping the original intent of this test.  The new logic only
      // affects testing paths.
      so0.CheckSAs(DateTime.UtcNow);

      foreach(Dictionary<ISender, PeerSecAssociation> sender_to_sa in so0.SPI.Values) {
        foreach(SecurityAssociation sa in sender_to_sa.Values) {
          if(sa.State == SecurityAssociation.States.Active) {
            sa.Send(mbr);
          }
        }
      }

      so0.CheckSAs(DateTime.UtcNow);
      Assert.AreEqual(500, so0.SACount, "Count!");

      so0.CheckSAs(DateTime.UtcNow);
      Assert.AreEqual(0, so0.SACount, "Count!");

      t.Dispose();
    }
Пример #9
0
    public void SHUpdateTest() {
      callback_count = 0;
      int spi = SecurityPolicy.DefaultSPI;
      MockSender sender1 = new MockSender(null, null, null, 2);
      MockSender sender2 = new MockSender(null, null, null, 2);
      PeerSecAssociation sa1 = new PeerSecAssociation(sender1, null, spi); 
      sa1.StateChangeEvent += StateChange;
      sender2.Receiver = sa1;
      MockDataHandler mdh1 = new MockDataHandler();
      sa1.Subscribe(mdh1, null);
      PeerSecAssociation sa2 = new PeerSecAssociation(sender2, null, spi); 
      sender1.Receiver = sa2;
      MockDataHandler mdh2 = new MockDataHandler();
      sa2.Subscribe(mdh2, null);

      Setup(sa1, sa2);

      sa1.RequestUpdate += Callback;
      sa2.RequestUpdate += Callback;

      byte[] b = null;
      Random rand = new Random();
      MemBlock mb = null;

      int current_epoch = sa1.CurrentEpoch;
      for(int i = 0; i < 80; i++) {
        b = new byte[128];
        rand.NextBytes(b);
        mb = MemBlock.Reference(b);
        sa1.Send(mb);
        Assert.IsTrue(mdh2.Contains(mb), "Contains" + i);
        if(i % 20 == 0 && i != 0) {
          Assert.AreEqual(callback_count, 1, "Callback count " + i);
          callback_count = 0;
          Setup(sa1, sa2);
        } else {
          if(i % 20 == 1 && i != 1) {
            Assert.IsFalse(current_epoch == sa1.CurrentEpoch, "Current epoch " + i);
            current_epoch = sa1.CurrentEpoch;
          }
          Assert.AreEqual(current_epoch, sa1.CurrentEpoch, "Current epoch " + i);
        }
      }
    }
Пример #10
0
    public void Test() {
      int spi = SecurityPolicy.DefaultSPI;
      MockSender sender1 = new MockSender(null, null, null, 2);
      MockSender sender2 = new MockSender(null, null, null, 2);
      PeerSecAssociation sa1 = new PeerSecAssociation(sender1, null, spi); 
      sa1.StateChangeEvent += StateChange;
      sender2.Receiver = sa1;
      MockDataHandler mdh1 = new MockDataHandler();
      sa1.Subscribe(mdh1, null);
      PeerSecAssociation sa2 = new PeerSecAssociation(sender2, null, spi); 
      sender1.Receiver = sa2;
      MockDataHandler mdh2 = new MockDataHandler();
      sa2.Subscribe(mdh2, null);

      byte[] b = null;
      Random rand = new Random();
      MemBlock mb = null;

      int current_epoch = sa1.CurrentEpoch;
      for(int i = 0; i < 5; i++) {
        Setup(sa1, sa2);

        b = new byte[128];
        rand.NextBytes(b);
        mb = MemBlock.Reference(b);
        sa1.Send(mb);

        Assert.IsTrue(mdh2.Contains(mb), "Contains" + i);
        Assert.AreEqual(state, sa1.State, "State == Active" + i);
        Assert.IsFalse(current_epoch == sa1.CurrentEpoch, "Current epoch " + i);
        current_epoch = sa1.CurrentEpoch;
      }

      sa1.CheckState();
      sa1.CheckState();
      b = new byte[128];
      rand.NextBytes(b);
      mb = MemBlock.Reference(b);
      try {
        sa1.Send(mb);
      } catch {}
      Assert.IsTrue(!mdh2.Contains(mb), "Failed!");
      Assert.AreEqual(state, sa1.State, "State == Failed");
    }