TFTPServer / python_tests / test_tftp.py
test_tftp.py
Raw
import pytest
import os


"""
This file has been revised to allow for two TFTP clients.
Test clients are "clientGet", one "clientPut".
The default path is generated to support the server.
Server generates paths:
    ~/tftp/write/
    ~/tftp/read/
...where ~ is the home dir.

Port is hardcoded and matches the server.
Localhost must be used as "put" tests do not support
networking due to bitwise comparison on untracked
randomly generated files. Put method works.
Other tests pass over the network.

- MU
"""

host = 'localhost'
port = 4970


# Init get client
@pytest.fixture(scope="module")
def clientGet():
    import tftpclient
    return tftpclient.TFTPClient(
        (host, port),
        (os.path.join(os.path.expanduser('~'), 'tftp', 'read'))
    )


# Init put client
@pytest.fixture(scope="module")
def clientPut():
    import tftpclient
    return tftpclient.TFTPClient(
        (host, port),
        (os.path.join(os.path.expanduser('~'), 'tftp', 'write'))
    )


# Get existing 50 byte file
def test_GSBSmall(clientGet):
    assert clientGet.getFile(b'f50b.bin')


# Get existing 500 byte file
def test_GSBLarge(clientGet):
    assert clientGet.getFile(b'f500b.bin')


# Get existing 1,535 byte file
def test_GMB3(clientGet):
    assert clientGet.getFile(b'f3blks.bin')


# Get existing 262,143 byte file
def test_GMB512(clientGet):
    assert clientGet.getFile(b'f512blks.bin')


# Put 50 byte file
def test_PSB50B(clientPut):
    assert clientPut.putFileBytes(b'f50b.ul', 50)


# Put 500 byte file
def test_PSB500B(clientPut):
    assert clientPut.putFileBytes(b'f500b.ul', 500)


# Put 512 byte file
def test_PMB1Blks(clientPut):
    assert clientPut.putFileBlocks(b'f1blk.ul', 1)


# Put 1,536 byte file
def test_PMB3Blks(clientPut):
    assert clientPut.putFileBlocks(b'f3blks.ul', 3)


# Put 262,144 byte file
def test_PMB512Blks(clientPut):
    assert clientPut.putFileBlocks(b'f512blks.ul', 512)


# Try to get a file that does not exist
def test_GFileNotExists(clientGet):
    assert clientGet.getFileNotExists(b'nosuchfile')


# Send unknown request type
def test_BadOp10(clientGet):
    assert clientGet.sendBadOp(10)


# Send an unknown request type (similar to an existing)
def test_BadOp257(clientGet):
    assert clientGet.sendBadOp(257)


# Get a large file and fail the first ACK every time
def test_GMBFail1stAck(clientGet):
    assert clientGet.getMultiBlockFileFailAck(b'f3blks.bin', 1)


# Get a large file and fail the first two ACKs every time
def test_GMBFail2ndAck(clientGet):
    assert clientGet.getMultiBlockFileFailAck(b'f3blks.bin', 2)