Program: 4505
Project: 4505
Buggy source:
public void testMixedProtocol() throws Exception {
  DatagramPacket pack=null;
  UDP_ACCESS=new DatagramSocket();
  for (int i=0; i < testUP.length; i++) {
    assertTrue("String_Node_Str",testUP[i].isOpen());
    assertTrue("String_Node_Str",testUP[i].isSupernodeClientConnection());
    drain(testUP[i],500);
    if ((i == 2)) {
      testUP[i].send(MessagesSupportedVendorMessage.instance());
      testUP[i].flush();
    }
  }
{
    drainAll();
    PingReply pong=PingReply.create(GUID.makeGuid(),(byte)4,UDP_ACCESS.getLocalPort(),InetAddress.getLocalHost().getAddress(),10,10,true,900,true);
    testUP[0].send(pong);
    testUP[0].flush();
    UDP_ACCESS.setSoTimeout(TIMEOUT * 2);
    pack=new DatagramPacket(new byte[1000],1000);
    try {
      UDP_ACCESS.receive(pack);
    }
 catch (    IOException bad) {
      fail("String_Node_Str",bad);
    }
    InputStream in=new ByteArrayInputStream(pack.getData());
    PingRequest ping=(PingRequest)Message.read(in);
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    pong=PingReply.create(ping.getGUID(),(byte)4,UDP_ACCESS.getLocalPort(),InetAddress.getLocalHost().getAddress(),10,10,true,900,true);
    pong.write(baos);
    pack=new DatagramPacket(baos.toByteArray(),baos.toByteArray().length,pack.getAddress(),pack.getPort());
    UDP_ACCESS.send(pack);
  }
{
    testUP[0].send(MessagesSupportedVendorMessage.instance());
    testUP[0].flush();
    byte[] cbGuid=null;
    int cbPort=-1;
    while (cbGuid == null) {
      try {
        Message m=testUP[0].receive(TIMEOUT);
        if (m instanceof UDPConnectBackVendorMessage) {
          UDPConnectBackVendorMessage udp=(UDPConnectBackVendorMessage)m;
          cbGuid=udp.getConnectBackGUID().bytes();
          cbPort=udp.getConnectBackPort();
        }
      }
 catch (      Exception ie) {
        fail("String_Node_Str",ie);
      }
    }
    PingRequest pr=new PingRequest(cbGuid,(byte)1,(byte)0);
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    pr.write(baos);
    pack=new DatagramPacket(baos.toByteArray(),baos.toByteArray().length,testUP[0].getInetAddress(),cbPort);
    UDP_ACCESS.send(pack);
  }
{
    Socket sock=null;
    OutputStream os=null;
    try {
      sock=Sockets.connect(InetAddress.getLocalHost().getHostAddress(),SERVER_PORT,12);
      os=sock.getOutputStream();
      os.write("String_Node_Str".getBytes());
    }
 catch (    IOException ignored) {
    }
catch (    SecurityException ignored) {
    }
catch (    Throwable t) {
      ErrorService.error(t);
    }
 finally {
      if (sock != null)       try {
        sock.close();
      }
 catch (      IOException ignored) {
      }
      if (os != null)       try {
        os.close();
      }
 catch (      IOException ignored) {
      }
    }
  }
  Thread.sleep(250);
  assertTrue(rs.isGUESSCapable());
  assertTrue(rs.acceptedIncomingConnection());
  drainAll();
  GUID queryGuid=new GUID(rs.newQueryGUID());
  assertTrue(GUID.addressesMatch(queryGuid.bytes(),rs.getAddress(),rs.getPort()));
  rs.query(queryGuid.bytes(),"String_Node_Str");
  Thread.sleep(250);
  for (int i=0; i < testUP.length; i++) {
    QueryRequest qr=getFirstQueryRequest(testUP[i],TIMEOUT);
    assertNotNull("String_Node_Str" + i + "String_Node_Str",qr);
    assertEquals(new GUID(qr.getGUID()),queryGuid);
    if ((i == 0) || (i == 2))     assertTrue(qr.desiresOutOfBandReplies());
 else     assertTrue(!qr.desiresOutOfBandReplies());
  }
  Message m=null;
  assertGreaterThan(SearchResultHandler.REPORT_INTERVAL,4 * testUP.length);
  for (int i=0; i < testUP.length; i++) {
    Response[] res=new Response[4];
    res[0]=new Response(10,10,"String_Node_Str" + i);
    res[1]=new Response(10,10,"String_Node_Str" + i);
    res[2]=new Response(10,10,"String_Node_Str" + i);
    res[3]=new Response(10,10,"String_Node_Str" + i);
    m=new QueryReply(queryGuid.bytes(),(byte)1,6355,myIP(),0,res,GUID.makeGuid(),new byte[0],false,false,true,true,false,false,null);
    testUP[i].send(m);
    testUP[i].flush();
  }
  for (int i=0; i < testUP.length; i++) {
    QueryStatusResponse stat=getFirstQueryStatus(testUP[i]);
    if ((i == 0) || (i == 2)) {
      assertNotNull(stat);
      assertEquals(new GUID(stat.getGUID()),queryGuid);
      assertEquals(4,stat.getNumResults());
    }
 else     assertNull(stat);
  }
  rs.stopQuery(queryGuid);
  for (int i=0; i < testUP.length; i++) {
    QueryStatusResponse stat=getFirstQueryStatus(testUP[i]);
    if ((i == 0) || (i == 2)) {
      assertNotNull(stat);
      assertEquals(new GUID(stat.getGUID()),queryGuid);
      assertEquals(65535,stat.getNumResults());
    }
 else     assertNull(stat);
  }
}

Fixed version (ground truth):
public void testMixedProtocol() throws Exception {
  DatagramPacket pack=null;
  UDP_ACCESS=new DatagramSocket();
  for (int i=0; i < testUP.length; i++) {
    assertTrue("String_Node_Str",testUP[i].isOpen());
    assertTrue("String_Node_Str",testUP[i].isSupernodeClientConnection());
    drain(testUP[i],500);
    if ((i == 2)) {
      testUP[i].send(MessagesSupportedVendorMessage.instance());
      testUP[i].flush();
    }
  }
{
    drainAll();
    PingReply pong=PingReply.create(GUID.makeGuid(),(byte)4,UDP_ACCESS.getLocalPort(),InetAddress.getLocalHost().getAddress(),10,10,true,900,true);
    testUP[0].send(pong);
    testUP[0].flush();
    UDP_ACCESS.setSoTimeout(TIMEOUT * 2);
    pack=new DatagramPacket(new byte[1000],1000);
    try {
      UDP_ACCESS.receive(pack);
    }
 catch (    IOException bad) {
      fail("String_Node_Str",bad);
    }
    InputStream in=new ByteArrayInputStream(pack.getData());
    PingRequest ping=(PingRequest)Message.read(in);
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    pong=PingReply.create(ping.getGUID(),(byte)4,UDP_ACCESS.getLocalPort(),InetAddress.getLocalHost().getAddress(),10,10,true,900,true);
    pong.write(baos);
    pack=new DatagramPacket(baos.toByteArray(),baos.toByteArray().length,pack.getAddress(),pack.getPort());
    UDP_ACCESS.send(pack);
  }
{
    testUP[0].send(MessagesSupportedVendorMessage.instance());
    testUP[0].flush();
    byte[] cbGuid=null;
    int cbPort=-1;
    while (cbGuid == null) {
      try {
        Message m=testUP[0].receive(TIMEOUT);
        if (m instanceof UDPConnectBackVendorMessage) {
          UDPConnectBackVendorMessage udp=(UDPConnectBackVendorMessage)m;
          cbGuid=udp.getConnectBackGUID().bytes();
          cbPort=udp.getConnectBackPort();
        }
      }
 catch (      Exception ie) {
        fail("String_Node_Str",ie);
      }
    }
    PingRequest pr=new PingRequest(cbGuid,(byte)1,(byte)0);
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    pr.write(baos);
    pack=new DatagramPacket(baos.toByteArray(),baos.toByteArray().length,testUP[0].getInetAddress(),cbPort);
    UDP_ACCESS.send(pack);
  }
{
    Socket sock=null;
    OutputStream os=null;
    try {
      sock=Sockets.connect(InetAddress.getLocalHost().getHostAddress(),SERVER_PORT,12);
      os=sock.getOutputStream();
      os.write("String_Node_Str".getBytes());
    }
 catch (    IOException ignored) {
    }
catch (    SecurityException ignored) {
    }
catch (    Throwable t) {
      ErrorService.error(t);
    }
 finally {
      if (sock != null)       try {
        sock.close();
      }
 catch (      IOException ignored) {
      }
      if (os != null)       try {
        os.close();
      }
 catch (      IOException ignored) {
      }
    }
  }
  Thread.sleep(250);
  assertTrue(RouterService.isGUESSCapable());
  assertTrue(RouterService.acceptedIncomingConnection());
  drainAll();
  GUID queryGuid=new GUID(RouterService.newQueryGUID());
  assertTrue(GUID.addressesMatch(queryGuid.bytes(),RouterService.getAddress(),RouterService.getPort()));
  RouterService.query(queryGuid.bytes(),"String_Node_Str");
  Thread.sleep(250);
  for (int i=0; i < testUP.length; i++) {
    QueryRequest qr=getFirstQueryRequest(testUP[i],TIMEOUT);
    assertNotNull("String_Node_Str" + i + "String_Node_Str",qr);
    assertEquals(new GUID(qr.getGUID()),queryGuid);
    if ((i == 0) || (i == 2))     assertTrue(qr.desiresOutOfBandReplies());
 else     assertTrue(!qr.desiresOutOfBandReplies());
  }
  Message m=null;
  assertGreaterThan(SearchResultHandler.REPORT_INTERVAL,4 * testUP.length);
  for (int i=0; i < testUP.length; i++) {
    Response[] res=new Response[4];
    res[0]=new Response(10,10,"String_Node_Str" + i);
    res[1]=new Response(10,10,"String_Node_Str" + i);
    res[2]=new Response(10,10,"String_Node_Str" + i);
    res[3]=new Response(10,10,"String_Node_Str" + i);
    m=new QueryReply(queryGuid.bytes(),(byte)1,6355,myIP(),0,res,GUID.makeGuid(),new byte[0],false,false,true,true,false,false,null);
    testUP[i].send(m);
    testUP[i].flush();
  }
  for (int i=0; i < testUP.length; i++) {
    QueryStatusResponse stat=getFirstQueryStatus(testUP[i]);
    if ((i == 0) || (i == 2)) {
      assertNotNull(stat);
      assertEquals(new GUID(stat.getGUID()),queryGuid);
      assertEquals(4,stat.getNumResults());
    }
 else     assertNull(stat);
  }
  RouterService.stopQuery(queryGuid);
  for (int i=0; i < testUP.length; i++) {
    QueryStatusResponse stat=getFirstQueryStatus(testUP[i]);
    if ((i == 0) || (i == 2)) {
      assertNotNull(stat);
      assertEquals(new GUID(stat.getGUID()),queryGuid);
      assertEquals(65535,stat.getNumResults());
    }
 else     assertNull(stat);
  }
}