Program: 4445
Project: 4445
Buggy source:
public void testDownloadFinishes() throws Exception {
  keepAllAlive(testUP);
  drainAll();
  DatagramPacket pack=null;
  Message m=null;
  byte[] guid=RouterService.newQueryGUID();
  RouterService.query(guid,"String_Node_Str");
  ((MyCallback)getCallback()).setGUID(new GUID(guid));
  QueryRequest qr=(QueryRequest)getFirstInstanceOfMessageType(testUP[0],QueryRequest.class);
  assertNotNull(qr);
  assertTrue(qr.desiresOutOfBandReplies());
  for (int i=0; i < testUP.length; i++) {
    Response[] res=new Response[200];
    for (int j=0; j < res.length; j++)     res[j]=new Response(10 + j + i,10 + j + i,"String_Node_Str" + j + i);
    m=new QueryReply(qr.getGUID(),(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();
  }
  final int UPLOADER_PORT=10000;
  TestUploader uploader=new TestUploader("String_Node_Str",UPLOADER_PORT);
  uploader.setBusy(true);
  URN urn=TestFile.hash();
  Set urns=new HashSet();
  urns.add(urn);
  RemoteFileDesc rfd=new RemoteFileDesc("String_Node_Str",UPLOADER_PORT,1,"String_Node_Str",10,GUID.makeGuid(),1,false,3,false,null,urns,false,false,"String_Node_Str",0,new HashSet());
  Thread.sleep(1500);
{
    ReplyNumberVendorMessage vm=new ReplyNumberVendorMessage(new GUID(qr.getGUID()),1);
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    vm.write(baos);
    pack=new DatagramPacket(baos.toByteArray(),baos.toByteArray().length,testUP[0].getInetAddress(),SERVER_PORT);
    UDP_ACCESS[0].send(pack);
  }
  Thread.sleep(500);
{
    Map _bypassedResults=(Map)PrivilegedAccessor.getValue(RouterService.getMessageRouter(),"String_Node_Str");
    assertNotNull(_bypassedResults);
    assertEquals(1,_bypassedResults.size());
    Set endpoints=(Set)_bypassedResults.get(new GUID(qr.getGUID()));
    assertNotNull(endpoints);
    assertEquals(1,endpoints.size());
  }
  long currTime=System.currentTimeMillis();
  Downloader downloader=RouterService.download(new RemoteFileDesc[]{rfd},false,new GUID(guid));
  Thread.sleep(500);
  assertEquals(Downloader.ITERATIVE_GUESSING,downloader.getState());
{
    boolean gotPing=false;
    while (!gotPing) {
      try {
        byte[] datagramBytes=new byte[1000];
        pack=new DatagramPacket(datagramBytes,1000);
        UDP_ACCESS[0].setSoTimeout(10000);
        UDP_ACCESS[0].receive(pack);
        InputStream in=new ByteArrayInputStream(pack.getData());
        m=Message.read(in);
        m.hop();
        if (m instanceof PingRequest)         gotPing=((PingRequest)m).isQueryKeyRequest();
      }
 catch (      InterruptedIOException iioe) {
        assertTrue(false);
      }
    }
  }
  QueryKey qk=QueryKey.getQueryKey(InetAddress.getLocalHost(),SERVER_PORT);
{
    byte[] ip=new byte[]{(byte)127,(byte)0,(byte)0,(byte)1};
    PingReply pr=PingReply.createQueryKeyReply(GUID.makeGuid(),(byte)1,UDP_ACCESS[0].getLocalPort(),ip,10,10,false,qk);
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    pr.write(baos);
    pack=new DatagramPacket(baos.toByteArray(),baos.toByteArray().length,testUP[0].getInetAddress(),SERVER_PORT);
    UDP_ACCESS[0].send(pack);
  }
  Thread.sleep(500);
{
    Map _queryKeys=(Map)PrivilegedAccessor.getValue(OnDemandUnicaster.class,"String_Node_Str");
    assertNotNull(_queryKeys);
    assertEquals(1,_queryKeys.size());
  }
{
    boolean gotQuery=false;
    while (!gotQuery) {
      try {
        byte[] datagramBytes=new byte[1000];
        pack=new DatagramPacket(datagramBytes,1000);
        UDP_ACCESS[0].setSoTimeout(10000);
        UDP_ACCESS[0].receive(pack);
        InputStream in=new ByteArrayInputStream(pack.getData());
        m=Message.read(in);
        if (m instanceof QueryRequest) {
          QueryRequest qReq=(QueryRequest)m;
          Set queryURNs=qReq.getQueryUrns();
          gotQuery=queryURNs.contains(urn);
          if (gotQuery)           gotQuery=qk.equals(qReq.getQueryKey());
        }
      }
 catch (      InterruptedIOException iioe) {
        assertTrue(false);
      }
    }
  }
  long timeoutVal=1000 - (System.currentTimeMillis() - currTime);
  Thread.sleep(timeoutVal > 0 ? timeoutVal : 0);
  assertEquals(Downloader.WAITING_FOR_RETRY,downloader.getState());
  ((MyCallback)getCallback()).clearGUID();
  TestUploader uploader2=new TestUploader("String_Node_Str",UPLOADER_PORT + 1);
  uploader2.setRate(100);
{
    rfd=new RemoteFileDesc("String_Node_Str",UPLOADER_PORT + 1,1,"String_Node_Str",10,GUID.makeGuid(),1,false,3,false,null,urns,false,false,"String_Node_Str",0,new HashSet());
    Response[] res=new Response[]{new Response(10,10,"String_Node_Str")};
    m=new QueryReply(qr.getGUID(),(byte)1,UPLOADER_PORT + 1,myIP(),0,res,GUID.makeGuid(),new byte[0],false,false,true,true,false,false,null);
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    m.write(baos);
    pack=new DatagramPacket(baos.toByteArray(),baos.toByteArray().length,testUP[0].getInetAddress(),SERVER_PORT);
    UDP_ACCESS[0].send(pack);
  }
  Thread.sleep(10000);
  assertEquals(Downloader.COMPLETE,downloader.getState());
{
    Map _bypassedResults=(Map)PrivilegedAccessor.getValue(RouterService.getMessageRouter(),"String_Node_Str");
    assertNotNull(_bypassedResults);
    assertEquals(0,_bypassedResults.size());
    Set endpoints=(Set)_bypassedResults.get(new GUID(qr.getGUID()));
    assertNull(endpoints);
  }
}

Fixed version (ground truth):
public void testDownloadFinishes() throws Exception {
  keepAllAlive(testUP);
  drainAll();
  DatagramPacket pack=null;
  Message m=null;
  byte[] guid=RouterService.newQueryGUID();
  RouterService.query(guid,"String_Node_Str");
  ((MyCallback)getCallback()).setGUID(new GUID(guid));
  QueryRequest qr=(QueryRequest)getFirstInstanceOfMessageType(testUP[0],QueryRequest.class);
  assertNotNull(qr);
  assertTrue(qr.desiresOutOfBandReplies());
  for (int i=0; i < testUP.length; i++) {
    Response[] res=new Response[200];
    for (int j=0; j < res.length; j++)     res[j]=new Response(10 + j + i,10 + j + i,"String_Node_Str" + j + i);
    m=new QueryReply(qr.getGUID(),(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();
  }
  final int UPLOADER_PORT=10000;
  TestUploader uploader=new TestUploader("String_Node_Str",UPLOADER_PORT);
  uploader.setBusy(true);
  URN urn=TestFile.hash();
  Set urns=new HashSet();
  urns.add(urn);
  RemoteFileDesc rfd=new RemoteFileDesc("String_Node_Str",UPLOADER_PORT,1,"String_Node_Str",10,GUID.makeGuid(),1,false,3,false,null,urns,false,false,"String_Node_Str",0,new HashSet());
  Thread.sleep(1500);
{
    ReplyNumberVendorMessage vm=new ReplyNumberVendorMessage(new GUID(qr.getGUID()),1);
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    vm.write(baos);
    pack=new DatagramPacket(baos.toByteArray(),baos.toByteArray().length,testUP[0].getInetAddress(),SERVER_PORT);
    UDP_ACCESS[0].send(pack);
  }
  Thread.sleep(500);
{
    Map _bypassedResults=(Map)PrivilegedAccessor.getValue(RouterService.getMessageRouter(),"String_Node_Str");
    assertNotNull(_bypassedResults);
    assertEquals(1,_bypassedResults.size());
    Set endpoints=(Set)_bypassedResults.get(new GUID(qr.getGUID()));
    assertNotNull(endpoints);
    assertEquals(1,endpoints.size());
  }
  long currTime=System.currentTimeMillis();
  Downloader downloader=RouterService.download(new RemoteFileDesc[]{rfd},false,new GUID(guid));
  final int MAX_TRIES=60;
  for (int i=0; i <= MAX_TRIES; i++) {
    Thread.sleep(500);
    if (downloader.getState() == Downloader.ITERATIVE_GUESSING)     break;
    if (i == MAX_TRIES)     fail("String_Node_Str");
  }
{
    boolean gotPing=false;
    while (!gotPing) {
      try {
        byte[] datagramBytes=new byte[1000];
        pack=new DatagramPacket(datagramBytes,1000);
        UDP_ACCESS[0].setSoTimeout(10000);
        UDP_ACCESS[0].receive(pack);
        InputStream in=new ByteArrayInputStream(pack.getData());
        m=Message.read(in);
        m.hop();
        if (m instanceof PingRequest)         gotPing=((PingRequest)m).isQueryKeyRequest();
      }
 catch (      InterruptedIOException iioe) {
        assertTrue(false);
      }
    }
  }
  QueryKey qk=QueryKey.getQueryKey(InetAddress.getLocalHost(),SERVER_PORT);
{
    byte[] ip=new byte[]{(byte)127,(byte)0,(byte)0,(byte)1};
    PingReply pr=PingReply.createQueryKeyReply(GUID.makeGuid(),(byte)1,UDP_ACCESS[0].getLocalPort(),ip,10,10,false,qk);
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    pr.write(baos);
    pack=new DatagramPacket(baos.toByteArray(),baos.toByteArray().length,testUP[0].getInetAddress(),SERVER_PORT);
    UDP_ACCESS[0].send(pack);
  }
  Thread.sleep(500);
{
    Map _queryKeys=(Map)PrivilegedAccessor.getValue(OnDemandUnicaster.class,"String_Node_Str");
    assertNotNull(_queryKeys);
    assertEquals(1,_queryKeys.size());
  }
{
    boolean gotQuery=false;
    while (!gotQuery) {
      try {
        byte[] datagramBytes=new byte[1000];
        pack=new DatagramPacket(datagramBytes,1000);
        UDP_ACCESS[0].setSoTimeout(10000);
        UDP_ACCESS[0].receive(pack);
        InputStream in=new ByteArrayInputStream(pack.getData());
        m=Message.read(in);
        if (m instanceof QueryRequest) {
          QueryRequest qReq=(QueryRequest)m;
          Set queryURNs=qReq.getQueryUrns();
          gotQuery=queryURNs.contains(urn);
          if (gotQuery)           gotQuery=qk.equals(qReq.getQueryKey());
        }
      }
 catch (      InterruptedIOException iioe) {
        assertTrue(false);
      }
    }
  }
  long timeoutVal=1000 - (System.currentTimeMillis() - currTime);
  Thread.sleep(timeoutVal > 0 ? timeoutVal : 0);
  assertEquals(Downloader.WAITING_FOR_RETRY,downloader.getState());
  ((MyCallback)getCallback()).clearGUID();
  TestUploader uploader2=new TestUploader("String_Node_Str",UPLOADER_PORT + 1);
  uploader2.setRate(100);
{
    rfd=new RemoteFileDesc("String_Node_Str",UPLOADER_PORT + 1,1,"String_Node_Str",10,GUID.makeGuid(),1,false,3,false,null,urns,false,false,"String_Node_Str",0,new HashSet());
    Response[] res=new Response[]{new Response(10,10,"String_Node_Str")};
    m=new QueryReply(qr.getGUID(),(byte)1,UPLOADER_PORT + 1,myIP(),0,res,GUID.makeGuid(),new byte[0],false,false,true,true,false,false,null);
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    m.write(baos);
    pack=new DatagramPacket(baos.toByteArray(),baos.toByteArray().length,testUP[0].getInetAddress(),SERVER_PORT);
    UDP_ACCESS[0].send(pack);
  }
  Thread.sleep(10000);
  assertEquals(Downloader.COMPLETE,downloader.getState());
{
    Map _bypassedResults=(Map)PrivilegedAccessor.getValue(RouterService.getMessageRouter(),"String_Node_Str");
    assertNotNull(_bypassedResults);
    assertEquals(0,_bypassedResults.size());
    Set endpoints=(Set)_bypassedResults.get(new GUID(qr.getGUID()));
    assertNull(endpoints);
  }
}