Example #1
0
def test_run_success(mocker):
    mocker.patch('json.loads')
    mocker.patch('subprocess.Popen')
    json.loads.return_value = {
        'end': {
            'streams': [{
                'receiver': {
                    'bits_per_second': 101
                },
                'sender': {
                    'bits_per_second': 101
                }
            }]
        }
    }

    statement = test_helpers.O()
    statement.direction = 'download from'
    statement.comparison = 'least'
    statement.bitrate = '100bps'
    statement.server = '10.0.0.1'

    q = test_helpers.Q()
    ipt = IperfTest.IperfTest(statement)
    ipt.run(q)
    assert q.what_was_put.passed is True
Example #2
0
def test_start(mocker):
    q = test_helpers.Q()
    fake_process = test_helpers.O()
    fake_process.start = lambda: 'run'
    mocker.patch('multiprocessing.Process')
    mp.Process.return_value = fake_process

    pc = PacketCapture.PacketCapture('eth1', q)

    res = pc.start()
    assert res == 'run'
Example #3
0
def test_end(mocker):
    q = test_helpers.Q()
    fake_process = test_helpers.O()
    fake_process.join = lambda: 'joined'
    mocker.patch('multiprocessing.Process')
    mp.Process.return_value = fake_process

    pc = PacketCapture.PacketCapture('eth1', q)

    res = pc.end()
    assert res == 'joined'
Example #4
0
def test_run_fail2(mocker):
    mocker.patch('scapy.all.sr')
    ans = th.O()
    ans.res = [1, 2, 3]
    scapy.all.sr.return_value = [ans, 'unans']
    pst = PingStatement('should not')
    pt = PingTest.PingTest(pst)
    q = th.Q()
    pt.run(q)
    scapy.all.sr.assert_called()
    assert q.what_was_put.passed is False
Example #5
0
def test_run_ftp_success(mocker):
    statement = test_helpers.O()
    statement.protocol = 'ftp'
    statement.target = 'speedtest.tele2.net'
    statement.should = 'should'
    statement.filename = '1KB.zip'
    statement.port = 21
    fft = FileFetchTest.FileFetchTest(statement)
    q = test_helpers.Q()
    fft.run(q)

    assert q.what_was_put.passed is True
Example #6
0
def test_run_ftp_failure(mocker):
    statement = test_helpers.O()
    statement.protocol = 'ftp'
    statement.target = 'fakespeedtest.example.come'
    statement.should = 'should'
    statement.filename = '1KB.zip'
    statement.port = 21
    fft = FileFetchTest.FileFetchTest(statement)
    q = test_helpers.Q()
    fft.run(q)

    assert q.what_was_put.passed is False
Example #7
0
def test_run_http_failure(mocker):
    statement = test_helpers.O()
    statement.protocol = 'http'
    statement.target = 'example.com'
    statement.should = 'should'
    statement.filename = 'index.html'
    statement.port = 80111
    fft = FileFetchTest.FileFetchTest(statement)
    q = test_helpers.Q()
    fft.run(q)

    assert q.what_was_put.passed is False
Example #8
0
def test_run_tftp_success(mocker):
    mocker.patch('tftpy.TftpClient.download')
    statement = test_helpers.O()
    statement.protocol = 'tftp'
    statement.target = 'tftp.example.com'
    statement.should = 'should'
    statement.filename = '1KB.zip'
    statement.port = 0
    fft = FileFetchTest.FileFetchTest(statement)
    q = test_helpers.Q()
    fft.run(q)

    assert q.what_was_put.passed is True
Example #9
0
def test_run_success_tcp(mocker):
    statement = test_helpers.O()
    statement.port = 100
    statement.protocol = 'TCP'
    statement.reachable = test_helpers.O()
    statement.reachable.should = 'should'
    statement.reachable.host = 'example.com'

    mocker.patch('PortOpenTest.PortOpenTest.tcp_scan')
    PortOpenTest.PortOpenTest.tcp_scan.return_value = True
    pot = PortOpenTest.PortOpenTest(statement)
    q = test_helpers.Q()
    pot.run(q)
    assert q.what_was_put.passed is True
Example #10
0
def test_run_failure(mocker):
    statement = test_helpers.O()
    statement.port = 100
    statement.protocol = 'UDP'
    statement.reachable = test_helpers.O()
    statement.reachable.should = 'should'
    statement.reachable.host = 'example.com'

    mocker.patch('PortOpenTest.PortOpenTest.udp_scan')
    PortOpenTest.PortOpenTest.udp_scan.return_value = False
    pot = PortOpenTest.PortOpenTest(statement)
    q = test_helpers.Q()
    pot.run(q)
    assert q.what_was_put.passed is False
Example #11
0
def test_capture(mocker):
    q = test_helpers.Q()

    packets = test_helpers.O()
    packets.res = [1, 2]
    mocker.patch('scapy.all.sniff')
    mocker.patch('PacketCapture.PacketCapture.trim_packet')
    PacketCapture.PacketCapture.trim_packet.return_value = 'abc'
    scapy.all.sniff.return_value = packets

    pc = PacketCapture.PacketCapture('eth1', q)

    pc.capture(q)
    assert q.what_was_put[0] == 'abc'
    assert q.what_was_put[1] == 'abc'
Example #12
0
def test_run_error(mocker):
    mocker.patch('json.loads')
    mocker.patch('subprocess.Popen')
    json.loads.return_value = {'error': 'it failed'}

    statement = test_helpers.O()
    statement.direction = 'download from'
    statement.comparison = 'least'
    statement.bitrate = '100mbps'
    statement.server = '10.0.0.1'

    q = test_helpers.Q()
    ipt = IperfTest.IperfTest(statement)
    ipt.run(q)
    assert q.what_was_put.passed is False
Example #13
0
def test_run_http_success(mocker):
    mocker.patch('requests.get')
    requests.get.return_value = test_helpers.O()
    requests.get.return_value.status_code = 200
    statement = test_helpers.O()
    statement.protocol = 'http'
    statement.target = 'www.google.com'
    statement.should = 'should'
    statement.filename = 'index.html'
    statement.port = 80
    fft = FileFetchTest.FileFetchTest(statement)
    q = test_helpers.Q()
    fft.run(q)

    assert q.what_was_put.passed is True
Example #14
0
def test_run_success(mocker):
    s = test_helpers.O()
    s.host = 'example.com'
    s.routers = ['a', 'b']

    mocker.patch('scapy.all.sr')
    p1 = test_helpers.O()
    p2 = test_helpers.O()
    p1.fields = {'src': 'a'}
    p2.fields = {'src': 'b'}
    scapy.all.sr.return_value = [[(None, p1), (None, p2)], None]

    t = TraceRouteTest.TraceRouteTest(s)
    q = test_helpers.Q()
    t.run(q)
    assert q.what_was_put.passed is True
Example #15
0
def test_run_unknown_host(mocker):
    s = test_helpers.O()
    s.host = 'notarealdomainname.comxyz'
    s.routers = ['a', 'b']

    mocker.patch('scapy.all.sr')
    p1 = test_helpers.O()
    p2 = test_helpers.O()
    p1.fields = {'src': 'b'}
    p2.fields = {'src': 'a'}
    scapy.all.sr.return_value = [[(None, p1), (None, p2)], None]

    t = TraceRouteTest.TraceRouteTest(s)
    q = test_helpers.Q()
    t.run(q)
    assert q.what_was_put.passed is False
Example #16
0
def test_run_successful_fail(mocker):
    mocker.patch('dns.resolver.query')
    ans = th.O()
    ans.response = th.O()
    ans.response.answer = [th.O()]
    ans.response.answer[0].items = ['10.0.0.200']
    dns.resolver.query.return_value = ans

    statement = th.O()
    statement.should = 'should not'
    statement.server = 'myserver'
    statement.resolve_to = '10.0.0.201'
    statement.domain = 'example.com'
    t = DNSTest.DNSTest(statement)
    q = th.Q()
    t.run(q)
    dns.resolver.query.assert_called()
    assert q.what_was_put.passed is True
Example #17
0
def test_ctor():
    q = test_helpers.Q()
    pc = PacketCapture.PacketCapture('eth1', q)

    assert pc.iface == 'eth1'
    assert len(pc.packets) == 0