Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 52a93bbb authored by Jerry Zhang's avatar Jerry Zhang Committed by android-build-merger
Browse files

Merge "Update MtpFfsHandle tests, add new tests for small and partial files"...

Merge "Update MtpFfsHandle tests, add new tests for small and partial files" am: ddc029af am: 47c772f6 am: 61c034aa
am: 79902b17

Change-Id: I38f9c56c6a0a5b79c38b6ce6ace14ca60bb94237
parents 3775c69f 79902b17
Loading
Loading
Loading
Loading
+76 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include <fcntl.h>
#include <gtest/gtest.h>
#include <memory>
#include <random>
#include <string>
#include <unistd.h>
#include <utils/Log.h>
@@ -28,6 +29,8 @@

namespace android {

constexpr int MAX_FILE_CHUNK_SIZE = 3 * 1024 * 1024;

constexpr int TEST_PACKET_SIZE = 512;
constexpr int SMALL_MULT = 30;
constexpr int MED_MULT = 510;
@@ -42,6 +45,10 @@ static const std::string dummyDataStr =
    "BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o"
    "r implied.\n * Se";

/**
 * Functional tests for the MtpFfsHandle class. Ensures header and data integrity
 * by mocking ffs endpoints as pipes to capture input / output.
 */
class MtpFfsHandleTest : public ::testing::Test {
protected:
    std::unique_ptr<IMtpHandle> handle;
@@ -72,6 +79,9 @@ protected:
        EXPECT_EQ(pipe(fd), 0);
        intr.reset(fd[0]);
        ffs_handle->mIntr.reset(fd[1]);

        ffs_handle->mBuffer1.resize(MAX_FILE_CHUNK_SIZE);
        ffs_handle->mBuffer2.resize(MAX_FILE_CHUNK_SIZE);
    }

    ~MtpFfsHandleTest() {}
@@ -138,6 +148,7 @@ TEST_F(MtpFfsHandleTest, testSendFileSmall) {
    mtp_file_range mfr;
    mfr.command = 42;
    mfr.transaction_id = 1337;
    mfr.offset = 0;
    int size = TEST_PACKET_SIZE * SMALL_MULT;
    char buf[size + sizeof(mtp_data_header) + 1];
    buf[size + sizeof(mtp_data_header)] = '\0';
@@ -166,6 +177,7 @@ TEST_F(MtpFfsHandleTest, testSendFileMed) {
    mtp_file_range mfr;
    mfr.command = 42;
    mfr.transaction_id = 1337;
    mfr.offset = 0;
    int size = TEST_PACKET_SIZE * MED_MULT;
    char buf[size + sizeof(mtp_data_header) + 1];
    buf[size + sizeof(mtp_data_header)] = '\0';
@@ -189,6 +201,70 @@ TEST_F(MtpFfsHandleTest, testSendFileMed) {
    EXPECT_EQ(header->transaction_id, static_cast<unsigned int>(1337));
}

TEST_F(MtpFfsHandleTest, testSendFileMedPartial) {
    std::stringstream ss;
    mtp_file_range mfr;
    mfr.fd = dummy_file.fd;
    mfr.command = 42;
    mfr.transaction_id = 1337;
    int size = TEST_PACKET_SIZE * MED_MULT;
    char buf[size + 1];
    buf[size] = '\0';

    for (int i = 0; i < MED_MULT; i++)
        ss << dummyDataStr;

    EXPECT_EQ(write(dummy_file.fd, ss.str().c_str(), size), size);

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(1, TEST_PACKET_SIZE);
    int offset = 0;
    while (offset != size) {
        mfr.offset = offset;
        int length = std::min(size - offset, dis(gen));
        mfr.length = length;
        char temp_buf[length + sizeof(mtp_data_header)];
        EXPECT_EQ(handle->sendFile(mfr), 0);

        EXPECT_EQ(read(bulk_in, temp_buf, length + sizeof(mtp_data_header)),
                static_cast<long>(length + sizeof(mtp_data_header)));

        struct mtp_data_header *header = reinterpret_cast<struct mtp_data_header*>(temp_buf);
        EXPECT_EQ(header->length, static_cast<unsigned int>(length + sizeof(mtp_data_header)));
        EXPECT_EQ(header->type, static_cast<unsigned int>(2));
        EXPECT_EQ(header->command, static_cast<unsigned int>(42));
        EXPECT_EQ(header->transaction_id, static_cast<unsigned int>(1337));
        memcpy(buf + offset, temp_buf + sizeof(mtp_data_header), length);
        offset += length;
    }
    EXPECT_STREQ(buf, ss.str().c_str());
}

TEST_F(MtpFfsHandleTest, testSendFileEmpty) {
    mtp_file_range mfr;
    mfr.command = 42;
    mfr.transaction_id = 1337;
    mfr.offset = 0;
    int size = 0;
    char buf[size + sizeof(mtp_data_header) + 1];
    buf[size + sizeof(mtp_data_header)] = '\0';

    mfr.length = size;
    mfr.fd = dummy_file.fd;

    EXPECT_EQ(handle->sendFile(mfr), 0);

    EXPECT_EQ(read(bulk_in, buf, size + sizeof(mtp_data_header)),
            static_cast<long>(size + sizeof(mtp_data_header)));

    struct mtp_data_header *header = reinterpret_cast<struct mtp_data_header*>(buf);
    EXPECT_EQ(header->length, static_cast<unsigned int>(size + sizeof(mtp_data_header)));
    EXPECT_EQ(header->type, static_cast<unsigned int>(2));
    EXPECT_EQ(header->command, static_cast<unsigned int>(42));
    EXPECT_EQ(header->transaction_id, static_cast<unsigned int>(1337));
}

TEST_F(MtpFfsHandleTest, testSendEvent) {
    struct mtp_event event;
    event.length = TEST_PACKET_SIZE;