|
| 1 | +// Copyright 2020-2025 Nestor Neto |
| 2 | + |
| 3 | +#include <gtest/gtest.h> |
| 4 | +#include <cstdlib> |
| 5 | +#include <cstring> |
| 6 | +#include <dirent.h> |
| 7 | +#include <unistd.h> |
| 8 | +#include <memory> |
| 9 | +#include <string> |
| 10 | +#include <iostream> |
| 11 | + |
| 12 | +#include "libserial/ports.hpp" |
| 13 | +#include "libserial/serial_exception.hpp" |
| 14 | + |
| 15 | +// Simple unit tests that don't require actual hardware |
| 16 | +class PortsTest : public ::testing::Test { |
| 17 | +protected: |
| 18 | +void SetUp() override { |
| 19 | + char temp_template[] = "/tmp/fake_serial_XXXXXX"; |
| 20 | + ASSERT_NE(mkdtemp(temp_template), nullptr) << "Failed to create temp directory"; |
| 21 | + temp_dir_ = temp_template; |
| 22 | +} |
| 23 | + |
| 24 | +void TearDown() override { |
| 25 | + // Clean up all symlinks in temp_dir_ |
| 26 | + DIR* dir = opendir(temp_dir_.c_str()); |
| 27 | + if (dir) { |
| 28 | + struct dirent* entry; |
| 29 | + while ((entry = readdir(dir)) != nullptr) { |
| 30 | + if (entry->d_name[0] != '.') { |
| 31 | + std::string path = temp_dir_ + "/" + entry->d_name; |
| 32 | + unlink(path.c_str()); |
| 33 | + } |
| 34 | + } |
| 35 | + closedir(dir); |
| 36 | + } |
| 37 | + rmdir(temp_dir_.c_str()); |
| 38 | +} |
| 39 | + |
| 40 | +std::string temp_dir_; |
| 41 | +}; |
| 42 | + |
| 43 | +TEST_F(PortsTest, DefaultConstructor) { |
| 44 | + EXPECT_NO_THROW({ |
| 45 | + libserial::Ports ports; |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +TEST_F(PortsTest, ScanPortsThrowsWhenPathMissing) { |
| 50 | + // Use an unlikely path to exist to trigger the exception path |
| 51 | + const char* missing_path = "/this/path/should/not/exist/serial/by-id"; |
| 52 | + libserial::Ports ports(missing_path); |
| 53 | + |
| 54 | + |
| 55 | + try { |
| 56 | + (void)ports.scanPorts(); |
| 57 | + FAIL() << "Expected libserial::SerialException to be thrown"; |
| 58 | + } |
| 59 | + catch (const libserial::SerialException& e) { |
| 60 | + std::cout << "Caught SerialException: " << e.what() << std::endl; |
| 61 | + // Optionally assert something about the message: |
| 62 | + EXPECT_NE(std::string(e.what()).find("Error while reading"), std::string::npos); |
| 63 | + } |
| 64 | + catch (...) { |
| 65 | + FAIL() << "Expected libserial::SerialException, but got a different exception"; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +TEST_F(PortsTest, ScanPortsWithFakeDevices) { |
| 70 | + // Create fake device symlinks |
| 71 | + std::string fake_device1 = std::string(temp_dir_) + "/usb-FTDI_FT232R_USB_UART_A1B2C3D4"; |
| 72 | + std::string fake_device2 = std::string(temp_dir_) + "/usb-Arduino_Uno_12345678"; |
| 73 | + |
| 74 | + // Create symlinks pointing to fake /dev/ttyUSB* paths |
| 75 | + // The actual target doesn't need to exist for scanPorts to process it |
| 76 | + ASSERT_EQ(symlink("../../ttyUSB0", fake_device1.c_str()), 0); |
| 77 | + ASSERT_EQ(symlink("../../ttyUSB1", fake_device2.c_str()), 0); |
| 78 | + |
| 79 | + // Test scanPorts with the fake directory |
| 80 | + libserial::Ports ports(temp_dir_.c_str()); |
| 81 | + uint16_t count = 0; |
| 82 | + EXPECT_NO_THROW({ |
| 83 | + count = ports.scanPorts(); |
| 84 | + }); |
| 85 | + |
| 86 | + EXPECT_EQ(count, 2) << "Should find 2 fake devices"; |
| 87 | + |
| 88 | + EXPECT_EQ(ports.findName(1).value(), "usb-FTDI_FT232R_USB_UART_A1B2C3D4"); |
| 89 | + EXPECT_EQ(ports.findName(0).value(), "usb-Arduino_Uno_12345678"); |
| 90 | + EXPECT_EQ(ports.findPortPath(1).value(), "/dev/ttyUSB0"); |
| 91 | + EXPECT_EQ(ports.findPortPath(0).value(), "/dev/ttyUSB1"); |
| 92 | + EXPECT_EQ(ports.findBusPath(1).value(), "/dev/ttyUSB0"); |
| 93 | + EXPECT_EQ(ports.findBusPath(0).value(), "/dev/ttyUSB1"); |
| 94 | +} |
| 95 | + |
| 96 | + |
0 commit comments