Program: 4448
Project: 4448
Buggy source:
public void testUsesCachedQueryKeys() 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=URN.createSHA1Urn("String_Node_Str");
  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);
  for (int i=0; i < UDP_ACCESS.length; i++) {
    ReplyNumberVendorMessage vm=new ReplyNumberVendorMessage(new GUID(qr.getGUID()),i + 1);
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    vm.write(baos);
    pack=new DatagramPacket(baos.toByteArray(),baos.toByteArray().length,testUP[0].getInetAddress(),SERVER_PORT);
    UDP_ACCESS[i].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(UDP_ACCESS.length,endpoints.size());
  }
  QueryKey qk=QueryKey.getQueryKey(InetAddress.getLocalHost(),SERVER_PORT);
  for (int i=0; i < UDP_ACCESS.length; i++) {
    byte[] ip=new byte[]{(byte)127,(byte)0,(byte)0,(byte)1};
    PingReply pr=PingReply.createQueryKeyReply(GUID.makeGuid(),(byte)1,UDP_ACCESS[i].getLocalPort(),ip,10,10,false,qk);
    pr.hop();
    OnDemandUnicaster.handleQueryKeyPong(pr);
  }
  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");
  }
  for (int i=0; i < UDP_ACCESS.length; i++) {
    boolean gotQuery=false;
    while (!gotQuery) {
      try {
        byte[] datagramBytes=new byte[1000];
        pack=new DatagramPacket(datagramBytes,1000);
        UDP_ACCESS[i].setSoTimeout(10000);
        UDP_ACCESS[i].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("String_Node_Str" + i,false);
      }
    }
  }
  Thread.sleep((UDP_ACCESS.length * 1000) - (System.currentTimeMillis() - currTime));
  assertEquals(Downloader.WAITING_FOR_RETRY,downloader.getState());
  ((MyCallback)getCallback()).clearGUID();
  downloader.stop();
  Thread.sleep(1000);
{
    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 testUsesCachedQueryKeys() 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=URN.createSHA1Urn("String_Node_Str");
  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);
  for (int i=0; i < UDP_ACCESS.length; i++) {
    ReplyNumberVendorMessage vm=new ReplyNumberVendorMessage(new GUID(qr.getGUID()),i + 1);
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    vm.write(baos);
    pack=new DatagramPacket(baos.toByteArray(),baos.toByteArray().length,testUP[0].getInetAddress(),SERVER_PORT);
    UDP_ACCESS[i].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(UDP_ACCESS.length,endpoints.size());
  }
  QueryKey qk=QueryKey.getQueryKey(InetAddress.getLocalHost(),SERVER_PORT);
  for (int i=0; i < UDP_ACCESS.length; i++) {
    byte[] ip=new byte[]{(byte)127,(byte)0,(byte)0,(byte)1};
    PingReply pr=PingReply.createQueryKeyReply(GUID.makeGuid(),(byte)1,UDP_ACCESS[i].getLocalPort(),ip,10,10,false,qk);
    pr.hop();
    OnDemandUnicaster.handleQueryKeyPong(pr);
  }
  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");
  }
  for (int i=0; i < UDP_ACCESS.length; i++) {
    boolean gotQuery=false;
    while (!gotQuery) {
      try {
        byte[] datagramBytes=new byte[1000];
        pack=new DatagramPacket(datagramBytes,1000);
        UDP_ACCESS[i].setSoTimeout(10000);
        UDP_ACCESS[i].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("String_Node_Str" + i,false);
      }
    }
  }
  Thread.sleep((UDP_ACCESS.length * 1500) - (System.currentTimeMillis() - currTime));
  assertEquals(Downloader.WAITING_FOR_RETRY,downloader.getState());
  ((MyCallback)getCallback()).clearGUID();
  downloader.stop();
  Thread.sleep(1000);
{
    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);
  }
}