Exemple #1
0
 def test_negotiate_no_json(self):
     ''' Verify that negotiate bails out when the body is not a JSON '''
     server = NegotiateServer(None)
     server.register_module('abc', None)
     self.assertRaises(
                       ValueError,
                       server.process_request,
                       MinimalHttpStream(),
                       Message(uri='/negotiate/abc')
                      )
Exemple #2
0
 def test_collect_no_json(self):
     ''' Verify that collect bails out when the body is not a JSON '''
     server = NegotiateServer(None)
     server.register_module('abc', None)
     self.assertRaises(
                       ValueError,
                       server.process_request,
                       None,
                       Message(uri='/collect/abc')
                      )
Exemple #3
0
    def test_negotiate_delayed(self):
        ''' When a stream is already in queue the response is delayed '''

        server = NegotiateServer(None)
        stream = MinimalHttpStream()
        server.queue.append(stream)
        server.known.add(stream)

        request = Message(uri='/negotiate/')
        server.process_request(stream, request)

        self.assertEqual(stream.opaque, request)
Exemple #4
0
    def test_negotiate_delayed(self):
        ''' When a stream is already in queue the response is delayed '''

        server = NegotiateServer(None)
        stream = MinimalHttpStream()
        server.queue.append(stream)
        server.known.add(stream)

        request = Message(uri='/negotiate/')
        server.process_request(stream, request)

        self.assertEqual(stream.opaque, request)
Exemple #5
0
    def test_collect_successful(self):
        ''' Make sure the response is OK when collect succeeds '''

        server = NegotiateServer(None)
        server.register_module('abc', NegotiateServerModule())

        stream = MinimalHttpStream()
        request = Message(uri='/collect/abc')
        request.body = StringIO.StringIO('{}')

        server.process_request(stream, request)
        response = stream.response

        self.assertEqual(response.code, '200')
        self.assertEqual(response.reason, 'Ok')
        self.assertEqual(response.body, '{}')
        self.assertEqual(response['connection'], 'close')
        self.assertEqual(response['content-type'], 'application/json')
Exemple #6
0
    def test_negotiate_red(self):
        ''' Verify that random early discard works as expected '''

        server = NegotiateServer(None)
        server.register_module('abc', NegotiateServerModule())

        red_accepted, red_rejected, red_discarded = 0, 0, 0
        while True:

            # Create request and stream
            request = Message(uri='/negotiate/abc')
            request.body = StringIO.StringIO('{}')
            stream = MinimalHttpStream()

            # Should ALWAYS accept
            if len(server.queue) < CONFIG['negotiate.min_thresh']:
                server.process_request(stream, request)
                self.assertEquals(server.queue[-1], stream)

            # MAY accept or reject
            elif len(server.queue) < CONFIG['negotiate.max_thresh']:
                server.process_request(stream, request)
                if server.queue[-1] == stream:
                    red_accepted += 1
                else:
                    red_rejected += 1

            # MUST reject
            else:
                server.process_request(stream, request)
                self.assertNotEqual(server.queue[-1], stream)
                red_discarded += 1
                if red_discarded == 64:
                    break

        self.assertTrue(red_accepted > 0 and red_rejected > 0
                        and red_discarded == 64)
Exemple #7
0
    def test_negotiate_successful(self):
        ''' Make sure the response is OK when negotiate succeeds '''

        server = NegotiateServer(None)
        server.register_module('abc', NegotiateServerModule())

        # Want to check authorized and nonauthorized streams
        for position in range(CONFIG['negotiate.parallelism'] + 3):

            stream = MinimalHttpStream()
            request = Message(uri='/negotiate/abc')
            request.body = StringIO.StringIO('{}')

            server.process_request(stream, request)
            response = stream.response

            self.assertEqual(response.code, '200')
            self.assertEqual(response.reason, 'Ok')
            self.assertNotEqual(response['connection'], 'close')
            self.assertEqual(response['content-type'], 'application/json')

            # Note: authorization is empty when you're choked
            body = json.loads(response.body)
            if position < CONFIG['negotiate.parallelism']:
                self.assertEqual(body, {
                                        u'unchoked': 1,
                                        u'queue_pos': position,
                                        u'real_address': u'abc',
                                        u'authorization': unicode(hash(stream))
                                       })
            else:
                self.assertEqual(body, {
                                        u'unchoked': 0,
                                        u'queue_pos': position,
                                        u'real_address': u'abc',
                                        u'authorization': u'',
                                       })
Exemple #8
0
    def test_negotiate_red(self):
        ''' Verify that random early discard works as expected '''

        server = NegotiateServer(None)
        server.register_module('abc', NegotiateServerModule())

        red_accepted, red_rejected, red_discarded = 0, 0, 0
        while True:

            # Create request and stream
            request = Message(uri='/negotiate/abc')
            request.body = StringIO.StringIO('{}')
            stream = MinimalHttpStream()

            # Should ALWAYS accept
            if len(server.queue) < CONFIG['negotiate.min_thresh']:
                server.process_request(stream, request)
                self.assertEquals(server.queue[-1], stream)

            # MAY accept or reject
            elif len(server.queue) < CONFIG['negotiate.max_thresh']:
                server.process_request(stream, request)
                if server.queue[-1] == stream:
                    red_accepted += 1
                else:
                    red_rejected += 1

            # MUST reject
            else:
                server.process_request(stream, request)
                self.assertNotEqual(server.queue[-1], stream)
                red_discarded += 1
                if red_discarded == 64:
                    break

        self.assertTrue(red_accepted > 0 and red_rejected > 0 and
                        red_discarded == 64)
Exemple #9
0
    def test_collect_successful(self):
        ''' Make sure the response is OK when collect succeeds '''

        server = NegotiateServer(None)
        server.register_module('abc', NegotiateServerModule())

        stream = MinimalHttpStream()
        request = Message(uri='/collect/abc')
        request.body = StringIO.StringIO('{}')

        server.process_request(stream, request)
        response = stream.response

        self.assertEqual(response.code, '200')
        self.assertEqual(response.reason, 'Ok')
        self.assertEqual(response.body, '{}')
        self.assertEqual(response['connection'], 'close')
        self.assertEqual(response['content-type'], 'application/json')
Exemple #10
0
    def test_negotiate_successful(self):
        ''' Make sure the response is OK when negotiate succeeds '''

        server = NegotiateServer(None)
        server.register_module('abc', NegotiateServerModule())

        # Want to check authorized and nonauthorized streams
        for position in range(CONFIG['negotiate.parallelism'] + 3):

            stream = MinimalHttpStream()
            request = Message(uri='/negotiate/abc')
            request.body = StringIO.StringIO('{}')

            server.process_request(stream, request)
            response = stream.response

            self.assertEqual(response.code, '200')
            self.assertEqual(response.reason, 'Ok')
            self.assertNotEqual(response['connection'], 'close')
            self.assertEqual(response['content-type'], 'application/json')

            # Note: authorization is empty when you're choked
            body = json.loads(response.body)
            if position < CONFIG['negotiate.parallelism']:
                self.assertEqual(
                    body, {
                        u'unchoked': 1,
                        u'queue_pos': position,
                        u'real_address': u'abc',
                        u'authorization': unicode(hash(stream))
                    })
            else:
                self.assertEqual(
                    body, {
                        u'unchoked': 0,
                        u'queue_pos': position,
                        u'real_address': u'abc',
                        u'authorization': u'',
                    })
Exemple #11
0
def main(args):
    """ Main function """
    try:
        options, arguments = getopt.getopt(args[1:], "6A:b:d:flnp:v")
    except getopt.error:
        sys.exit(USAGE)
    if arguments:
        sys.exit(USAGE)

    prefer_ipv6 = 0
    address = "127.0.0.1"
    backend = "volatile"
    datadir = None  # means: pick the default
    force = 0
    listen = 0
    negotiate = 1
    port = 80
    noisy = 0
    for name, value in options:
        if name == "-6":
            prefer_ipv6 = 1
        elif name == "-A":
            address = value
        elif name == "-b":
            backend = value
        elif name == "-d":
            datadir = value
        elif name == "-f":
            force = 1
        elif name == "-l":
            listen = 1
        elif name == "-n":
            negotiate = 0
        elif name == "-p":
            port = int(value)
        elif name == "-v":
            noisy = 1

    if noisy:
        log.set_verbose()

    conf = CONFIG.copy()

    BACKEND.use_backend(backend)
    BACKEND.datadir_init(None, datadir)

    if listen:
        if not negotiate:
            server = DASHServerSmpl(POLLER)
            server.configure(conf)
            server.listen((address, port))

        else:
            # Code adapted from neubot/server.py

            conf["http.server.rootdir"] = ""
            server = ServerHTTP(POLLER)
            server.configure(conf)
            server.listen((address, port))

            negotiate_server = NegotiateServer(POLLER)
            negotiate_server.configure(conf)
            server.register_child(negotiate_server, "/negotiate")
            server.register_child(negotiate_server, "/collect")

            dash_negotiate_server = DASHNegotiateServer()
            negotiate_server.register_module("dash", dash_negotiate_server)

            dash_server = DASHServerGlue(POLLER, dash_negotiate_server)
            dash_server.configure(conf)
            server.register_child(dash_server, "/dash")

    elif not force:
        result = runner_clnt.runner_client(CONFIG["agent.api.address"],
          CONFIG["agent.api.port"], CONFIG["verbose"], "dash")
        if result:
            sys.exit(0)
        logging.warning("dash: failed to contact Neubot; is Neubot running?")
        sys.exit(1)

    else:
        if negotiate:
            client = DASHNegotiateClient(POLLER)
        else:
            client = DASHClientSmpl(POLLER, None, DASH_RATES)
        client.configure(conf)
        client.connect((address, port))

    POLLER.loop()
Exemple #12
0
 def test_collect_no_json(self):
     ''' Verify that collect bails out when the body is not a JSON '''
     server = NegotiateServer(None)
     server.register_module('abc', None)
     self.assertRaises(ValueError, server.process_request, None,
                       Message(uri='/collect/abc'))
Exemple #13
0
 def __init__(self, poller):
     ''' Initialize negotiate server for UpdateQueue '''
     NegotiateServer.__init__(self, poller)
     self.negotiated = []
Exemple #14
0
 def test_negotiate_no_json(self):
     ''' Verify that negotiate bails out when the body is not a JSON '''
     server = NegotiateServer(None)
     server.register_module('abc', None)
     self.assertRaises(ValueError, server.process_request,
                       MinimalHttpStream(), Message(uri='/negotiate/abc'))
Exemple #15
0
 def __init__(self, poller):
     ''' Initialize negotiate server for UpdateQueue '''
     NegotiateServer.__init__(self, poller)
     self.negotiated = []