Ejemplo n.º 1
0
  /** Adds XML to the responses in a QueryReply. */
  public static boolean addXMLToResponses(
      QueryReply qr, LimeXMLDocumentHelper limeXMLDocumentHelper) {
    // get xml collection string, then get dis-aggregated docs, then
    // in loop
    // you can match up metadata to responses
    String xmlCollectionString = "";
    try {
      LOG.trace("Trying to do uncompress XML.....");
      byte[] xmlCompressed = qr.getXMLBytes();
      if (xmlCompressed.length > 1) {
        byte[] xmlUncompressed = LimeXMLUtils.uncompress(xmlCompressed);
        xmlCollectionString = new String(xmlUncompressed, "UTF-8");
      }
    } catch (UnsupportedEncodingException use) {
      // b/c this should never happen, we will show and error
      // if it ever does for some reason.
      // we won't throw a BadPacketException here but we will show it.
      // the uee will effect the xml part of the reply but we could
      // still show the reply so there shouldn't be any ill effect if
      // xmlCollectionString is ""
      ErrorService.error(use);
    } catch (IOException ignored) {
    }

    // valid response, no XML in EQHD.
    if (xmlCollectionString.equals("")) return true;

    Response[] responses;
    int responsesLength;
    try {
      responses = qr.getResultsArray();
      responsesLength = responses.length;
    } catch (BadPacketException bpe) {
      LOG.trace("Unable to get responses", bpe);
      return false;
    }

    if (LOG.isDebugEnabled()) LOG.debug("xmlCollectionString = " + xmlCollectionString);

    List<LimeXMLDocument[]> allDocsArray =
        limeXMLDocumentHelper.getDocuments(xmlCollectionString, responsesLength);

    for (int i = 0; i < responsesLength; i++) {
      Response response = responses[i];
      LimeXMLDocument[] metaDocs;
      for (int schema = 0; schema < allDocsArray.size(); schema++) {
        metaDocs = allDocsArray.get(schema);
        // If there are no documents in this schema, try another.
        if (metaDocs == null) continue;
        // If this schema had a document for this response, use it.
        if (metaDocs[i] != null) {
          response.setDocument(metaDocs[i]);
          break; // we only need one, so break out.
        }
      }
    }
    return true;
  }
  public void testXMLReturned1() throws Exception {
    drainAll();

    // send a query
    QueryRequest query = queryRequestFactory.createQuery("ID3V24");
    ULTRAPEER[0].send(query);
    ULTRAPEER[0].flush();

    // wait for processing
    Thread.sleep(750);

    // confirm that result has heXML.
    QueryReply reply = BlockingConnectionUtils.getFirstQueryReply(ULTRAPEER[0]);
    assertNotNull(reply);
    assertNotNull(reply.getXMLBytes());
    assertTrue("xml length = " + reply.getXMLBytes().length, reply.getXMLBytes().length > 10);
  }
  public void testXMLReturned2() throws Exception {
    drainAll();

    String richQuery =
        "<?xml version=\"1.0\"?><audios xsi:noNamespaceSchemaLocation=\"http://www.limewire.com/schemas/audio.xsd\"><audio genre=\"Bass\"></audio></audios>";

    // send a query
    QueryRequest query = queryRequestFactory.createQuery("Bass", richQuery);
    ULTRAPEER[0].send(query);
    ULTRAPEER[0].flush();

    // wait for processing
    Thread.sleep(750);

    // confirm that result has heXML.
    QueryReply reply = BlockingConnectionUtils.getFirstQueryReply(ULTRAPEER[0]);
    assertNotNull(reply);
    assertNotNull(reply.getXMLBytes());
    assertTrue("xml length = " + reply.getXMLBytes().length, reply.getXMLBytes().length > 10);
  }
Ejemplo n.º 4
0
  /**
   * Handles a query reply locally.
   *
   * @param address can be null, if not null overrides the address info in <code>reply</code>
   */
  public void handleQueryReply(QueryReply reply, ReplyHandler handler, Address address) {
    // do not allow a faked multicast reply.
    if (reply.isFakeMulticast()) {
      return;
    }

    // Drop if it's a reply to mcast and conditions aren't met ...
    if (reply.isReplyToMulticastQuery()) {
      if (reply.isTCP()) return; // shouldn't be on TCP.
      if (reply.getHops() != 1 || reply.getTTL() != 0) return; // should only have hopped once.
    }

    // XML must be added to the response first, so that
    // whomever calls toRemoteFileDesc on the response
    // will create the cachedRFD with the correct XML.
    boolean validResponses = addXMLToResponses(reply, limeXMLDocumentHelper);
    // responses invalid?  exit.
    if (!validResponses) {
      return;
    }

    // check for unwanted results after xml has been constructed
    if (handler != null && handler.isPersonalSpam(reply)) {
      return;
    }

    if (reply.hasSecureData() && ApplicationSettings.USE_SECURE_RESULTS.getValue()) {
      secureMessageVerifier.verify(reply, this);
    } else {
      routeQueryReplyInternal(reply, address);
    }
  }
Ejemplo n.º 5
0
  public void testResponse() throws Exception {
    QueryRequest qr = queryRequestFactory.createNonFirewalledQuery("badger", (byte) 1);
    testUP[0].send(qr);
    testUP[0].flush();
    Thread.sleep(1000);
    QueryReply r = BlockingConnectionUtils.getFirstQueryReply(testUP[0]);
    assertNotNull(r);
    QueryReply expected = staticMessages.getLimeReply();
    assertTrue(expected.getResultsAsList().containsAll(r.getResultsAsList()));
    assertTrue(r.getResultsAsList().containsAll(expected.getResultsAsList()));

    // change the words to something else
    SearchSettings.LIME_SEARCH_TERMS.set(new String[] {"mushroom"});
    qr = queryRequestFactory.createNonFirewalledQuery("badger", (byte) 1);
    testUP[0].send(qr);
    testUP[0].flush();
    Thread.sleep(1000);
    r = BlockingConnectionUtils.getFirstQueryReply(testUP[0]);
    assertNull(r);

    qr = queryRequestFactory.createNonFirewalledQuery("mushroom", (byte) 1);
    testUP[0].send(qr);
    testUP[0].flush();
    Thread.sleep(1000);
    r = BlockingConnectionUtils.getFirstQueryReply(testUP[0]);
    assertNotNull(r);
    assertTrue(expected.getResultsAsList().containsAll(r.getResultsAsList()));
    assertTrue(r.getResultsAsList().containsAll(expected.getResultsAsList()));

    // turn off responding completely
    SearchSettings.SEND_LIME_RESPONSES.setValue(0);
    qr = queryRequestFactory.createNonFirewalledQuery("mushroom", (byte) 1);
    testUP[0].send(qr);
    testUP[0].flush();
    Thread.sleep(1000);
    r = BlockingConnectionUtils.getFirstQueryReply(testUP[0]);
    assertNull(r);
  }
    private void doQRPCGTest(boolean sendTLS, boolean settingOn, boolean listenTLS) throws Exception {
        if(settingOn)
            networkManagerStub.setOutgoingTLSEnabled(true);
        
    	setAccepted(false);
        BlockingConnectionUtils.drain(testUP[0]);

        // make sure leaf is sharing
        assertEquals(2, gnutellaFileView.size());
        assertEquals(1, connectionManager.getNumConnections());

        // send a query that should be answered
        QueryRequest query = queryRequestFactory.createQueryRequest(GUID.makeGuid(), (byte) 1,
                "berkeley", null, null, null, false, Network.UNKNOWN, false, 0);
        testUP[0].send(query);
        testUP[0].flush();

        // await a response
        Message m;
        do {
            m = testUP[0].receive(TIMEOUT);
        } while (!(m instanceof QueryReply));

        // confirm it has proxy info
        QueryReply reply = (QueryReply) m;
        assertNotNull(reply.getPushProxies());

        // check out PushProxy info
        Set proxies = reply.getPushProxies();
        assertEquals(1, proxies.size());
        Iterator iter = proxies.iterator();
        IpPort ppi = (IpPort) iter.next();
        assertEquals(ppi.getPort(), 6355);
        assertTrue(ppi.getInetAddress().equals(testUP[0].getInetAddress()));

        // set up a ServerSocket to get give on
        ServerSocket ss;
        if(listenTLS) {
            SSLContext context = SSLUtils.getTLSContext();
            SSLServerSocket sslServer = (SSLServerSocket)context.getServerSocketFactory().createServerSocket();
            sslServer.setNeedClientAuth(false);
            sslServer.setWantClientAuth(false);
            sslServer.setEnabledCipherSuites(new String[] {"TLS_DH_anon_WITH_AES_128_CBC_SHA"});
            ss = sslServer;
        } else {
            ss = new ServerSocket();
        }

        try {
            ss.setReuseAddress(true);        
            ss.setSoTimeout(TIMEOUT);
            ss.bind(new InetSocketAddress(9000));
            // test that the client responds to a PushRequest
            PushRequest pr = new PushRequestImpl(GUID.makeGuid(), (byte) 1, 
                                             applicationServices.getMyGUID(),
                                             0, 
                                             InetAddress.getLocalHost().getAddress(),
                                             9000,
                                             Network.TCP,
                                             sendTLS);

            // send the PR off
            testUP[0].send(pr);
            testUP[0].flush();

            // we should get a incoming GIV
            Socket givSock = ss.accept();
            try {
                assertNotNull(givSock);

                // start reading and confirming the HTTP request
                String currLine;
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(givSock.getInputStream()));

                // confirm a GIV
                currLine = reader.readLine();
                GUID guid = new GUID(
                        applicationServices.getMyGUID());
                String givLine = "GIV 0:" + guid.toHexString();
                assertTrue(currLine.startsWith(givLine));
            } finally {
                givSock.close();
            }
        } finally {
            ss.close();
        }
    }
  public void testBitrateExclusion() throws Exception {
    // test that a mismatching artist name doesn't return a result
    {
      drainAll();

      String richQuery =
          "<?xml version=\"1.0\"?><audios xsi:noNamespaceSchemaLocation=\"http://www.limewire.com/schemas/audio.xsd\"><audio bitrate=\"64\" artist=\"junk\"></audio></audios>";

      // send a query
      QueryRequest query = queryRequestFactory.createQuery("junk 64", richQuery);
      ULTRAPEER[0].send(query);
      ULTRAPEER[0].flush();

      // wait for processing
      Thread.sleep(750);

      // confirm that we don't get a result
      QueryReply reply = BlockingConnectionUtils.getFirstQueryReply(ULTRAPEER[0]);
      assertNull(reply);
    }

    // test that a matching artist name does return a result
    {
      drainAll();

      String richQuery =
          "<?xml version=\"1.0\"?><audios xsi:noNamespaceSchemaLocation=\"http://www.limewire.com/schemas/audio.xsd\"><audio bitrate=\"64\" artist=\"artist test\"></audio></audios>";

      // send a query
      QueryRequest query = queryRequestFactory.createQuery("title 16", richQuery);
      ULTRAPEER[0].send(query);
      ULTRAPEER[0].flush();

      // wait for processing
      Thread.sleep(750);

      // confirm that we do get a result
      QueryReply reply = BlockingConnectionUtils.getFirstQueryReply(ULTRAPEER[0]);
      assertNotNull(reply);
      assertNotNull(reply.getXMLBytes());
      assertTrue("xml length = " + reply.getXMLBytes().length, reply.getXMLBytes().length > 10);
    }

    // test that a null price value doesn't return a result
    {
      drainAll();

      String richQuery =
          "<?xml version=\"1.0\"?><audios xsi:noNamespaceSchemaLocation=\"http://www.limewire.com/schemas/audio.xsd\"><audio bitrate=\"64\" price=\"$19.99\"></audio></audios>";

      // send a query
      QueryRequest query = queryRequestFactory.createQuery("$19.99 16", richQuery);
      ULTRAPEER[0].send(query);
      ULTRAPEER[0].flush();

      // wait for processing
      Thread.sleep(750);

      // confirm that we don't get a result
      QueryReply reply = BlockingConnectionUtils.getFirstQueryReply(ULTRAPEER[0]);
      assertNull(reply);
    }

    // 3 fields - bitrate matches, but only one other, so no return
    {
      drainAll();

      String richQuery =
          "<?xml version=\"1.0\"?><audios xsi:noNamespaceSchemaLocation=\"http://www.limewire.com/schemas/audio.xsd\"><audio bitrate=\"64\" artist=\"artist test\" title=\"junk\"></audio></audios>";

      // send a query
      QueryRequest query = queryRequestFactory.createQuery("Test junk 16", richQuery);
      ULTRAPEER[0].send(query);
      ULTRAPEER[0].flush();

      // wait for processing
      Thread.sleep(750);

      // confirm that we don't get a result
      QueryReply reply = BlockingConnectionUtils.getFirstQueryReply(ULTRAPEER[0]);
      assertNull(reply);
    }

    // 3 fields - all match, should return
    {
      drainAll();

      String richQuery =
          "<?xml version=\"1.0\"?><audios xsi:noNamespaceSchemaLocation=\"http://www.limewire.com/schemas/audio.xsd\"><audio bitrate=\"64\" artist=\"artist test\" title=\"title test\"></audio></audios>";

      // send a query
      QueryRequest query = queryRequestFactory.createQuery("Test mpg 16", richQuery);
      ULTRAPEER[0].send(query);
      ULTRAPEER[0].flush();

      // wait for processing
      Thread.sleep(750);

      // confirm that we do get a result
      QueryReply reply = BlockingConnectionUtils.getFirstQueryReply(ULTRAPEER[0]);
      assertNotNull(reply);
      assertNotNull(reply.getXMLBytes());
      assertTrue("xml length = " + reply.getXMLBytes().length, reply.getXMLBytes().length > 10);
    }

    // 3 fields - 1 match, 1 null, should return
    {
      drainAll();

      String richQuery =
          "<?xml version=\"1.0\"?><audios xsi:noNamespaceSchemaLocation=\"http://www.limewire.com/schemas/audio.xsd\"><audio bitrate=\"64\" artist=\"artist test\" type=\"Audiobook\"></audio></audios>";

      // send a query
      QueryRequest query = queryRequestFactory.createQuery("Test Audiobook 16", richQuery);
      ULTRAPEER[0].send(query);
      ULTRAPEER[0].flush();

      // wait for processing
      Thread.sleep(750);

      // confirm that we do get a result
      QueryReply reply = BlockingConnectionUtils.getFirstQueryReply(ULTRAPEER[0]);
      assertNotNull(reply);
      assertNotNull(reply.getXMLBytes());
      assertTrue("xml length = " + reply.getXMLBytes().length, reply.getXMLBytes().length > 10);
    }

    // 3 fields - 2 null, should not return
    {
      drainAll();

      String richQuery =
          "<?xml version=\"1.0\"?><audios xsi:noNamespaceSchemaLocation=\"http://www.limewire.com/schemas/audio.xsd\"><audio bitrate=\"64\" price=\"$19.99\" type=\"Audiobook\"></audio></audios>";

      // send a query
      QueryRequest query = queryRequestFactory.createQuery("$19.99 Audiobook 16", richQuery);
      ULTRAPEER[0].send(query);
      ULTRAPEER[0].flush();

      // wait for processing
      Thread.sleep(750);

      // confirm that we don't get a result
      QueryReply reply = BlockingConnectionUtils.getFirstQueryReply(ULTRAPEER[0]);
      assertNull(reply);
    }

    // 3 fields - 1 null, 1 mismatch, should not return
    {
      drainAll();

      String richQuery =
          "<?xml version=\"1.0\"?><audios xsi:noNamespaceSchemaLocation=\"http://www.limewire.com/schemas/audio.xsd\"><audio bitrate=\"64\" price=\"$19.99\" artist=\"Tester\"></audio></audios>";

      // send a query
      QueryRequest query = queryRequestFactory.createQuery("$19.99 Tester 16", richQuery);
      ULTRAPEER[0].send(query);
      ULTRAPEER[0].flush();

      // wait for processing
      Thread.sleep(750);

      // confirm that we don't get a result
      QueryReply reply = BlockingConnectionUtils.getFirstQueryReply(ULTRAPEER[0]);
      assertNull(reply);
    }
  }
  public void testDownloadProgressQueryDoneNoPurge() throws Exception {

    BlockingConnectionUtils.keepAllAlive(testUP, pingReplyFactory);
    // clear up any messages before we begin the test.
    drainAll();

    // luckily there is hacky little way to go through the download paces -
    // download from yourself :) .

    Message m = null;

    byte[] guid = searchServices.newQueryGUID();
    searchServices.query(guid, "metadata");
    callback.setGUID(new GUID(guid));

    QueryRequest qr =
        BlockingConnectionUtils.getFirstInstanceOfMessageType(testUP[0], QueryRequest.class);
    assertNotNull(qr);
    assertTrue(qr.desiresOutOfBandReplies());

    // just return ONE real result and the rest junk
    Response resp = null;
    QueryReply reply = null;
    {
      // get a correct response object
      QueryRequest qrTemp = queryRequestFactory.createQuery("metadata");
      testUP[0].send(qrTemp);
      testUP[0].flush();

      reply = BlockingConnectionUtils.getFirstInstanceOfMessageType(testUP[0], QueryReply.class);
      assertNotNull(reply);
      resp = (reply.getResultsAsList()).get(0);
    }
    assertNotNull(reply);
    assertNotNull(resp);
    Response[] res = new Response[] {resp};

    // this isn't really needed but just for completeness send it back to
    // the test Leaf
    m =
        queryReplyFactory.createQueryReply(
            guid,
            (byte) 1,
            SERVER_PORT,
            myIP(),
            0,
            res,
            GUID.makeGuid(),
            new byte[0],
            false,
            false,
            true,
            true,
            false,
            false,
            null);
    testUP[0].send(m);
    testUP[0].flush();

    // send back a lot of results via TCP so you konw the UDP one will be
    // bypassed
    for (int i = 0; i < testUP.length; i++) {
      res = new Response[75];
      for (int j = 0; j < res.length; j++)
        res[j] =
            responseFactory.createResponse(
                10 + j + i, 10 + j + i, "metadata " + j + i, UrnHelper.SHA1);
      m =
          queryReplyFactory.createQueryReply(
              guid,
              (byte) 1,
              testUP[0].getPort(),
              myIP(),
              0,
              res,
              GUID.makeGuid(),
              new byte[0],
              false,
              false,
              true,
              true,
              false,
              false,
              null);
      testUP[i].send(m);
      testUP[i].flush();
    }

    // allow for processing
    Thread.sleep(3000);

    {
      // now we should make sure MessageRouter has not bypassed anything
      // yet
      assertByPassedResultsCacheHasSize(qr.getGUID(), 0);
    }

    // send back a UDP response and make sure it was saved in bypassed...
    {
      ReplyNumberVendorMessage vm =
          replyNumberVendorMessageFactory.createV3ReplyNumberVendorMessage(new GUID(guid), 1);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      vm.write(baos);
      DatagramPacket pack =
          new DatagramPacket(
              baos.toByteArray(),
              baos.toByteArray().length,
              testUP[0].getInetAddress(),
              SERVER_PORT);
      UDP_ACCESS[0].send(pack);
    }

    // allow for processing
    Thread.sleep(500);

    {
      // all the UDP ReplyNumberVMs should have been bypassed
      assertByPassedResultsCacheHasSize(guid, 1);
    }

    // now do the download, wait for it to finish, and then bypassed results
    // should be empty again
    RemoteFileDesc rfd =
        resp.toRemoteFileDesc(reply, null, remoteFileDescFactory, pushEndpointFactory);

    assertFalse("file should not be saved yet", new File(_savedDir, "metadata.mp3").exists());

    downloadServices.download(new RemoteFileDesc[] {rfd}, false, new GUID(guid));
    UploadSettings.UPLOAD_SPEED.setValue(5);

    searchServices.stopQuery(new GUID(guid));
    callback.clearGUID();

    // download still in progress, don't purge
    assertByPassedResultsCacheHasSize(guid, 1);

    UploadSettings.UPLOAD_SPEED.setValue(100);

    // sleep to make sure the download starts
    Thread.sleep(20000);

    assertTrue("file should saved", new File(_savedDir, "metadata.mp3").exists());

    // now we should make sure MessageRouter clears the cache
    assertByPassedResultsCacheHasSize(qr.getGUID(), 0);
  }