summaryrefslogtreecommitdiff
path: root/src/drivers/test/test_storage.cpp
blob: 627fa6153c4edcdc3288ec103334c782f8e4edcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "storage.hpp"

#include <dirent.h>

#include <cstdio>
#include <fstream>
#include <iostream>

#include "catch2/catch.hpp"

#include "gpio_expander.hpp"
#include "i2c.hpp"
#include "i2c_fixture.hpp"
#include "spi.hpp"
#include "spi_fixture.hpp"

namespace drivers {

static const std::string kTestFilename = "test";
static const std::string kTestFilePath =
    std::string(kStoragePath) + "/" + kTestFilename;

TEST_CASE("sd card storage", "[integration]") {
  I2CFixture i2c;
  SpiFixture spi;
  GpioExpander expander;

  {
    std::unique_ptr<SdStorage> result = SdStorage::create(&expander).value();

    SECTION("write to a file") {
      {
        std::ofstream test_file;
        test_file.open(kTestFilePath.c_str());
        test_file << "hello here is some test";
        test_file.close();
      }

      SECTION("read from a file") {
        std::ifstream test_file;
        test_file.open(kTestFilePath.c_str());

        std::string line;
        REQUIRE(std::getline(test_file, line));
        REQUIRE(line == "hello here is some test");

        test_file.close();
      }

      SECTION("list files") {
        DIR* dir;
        struct dirent* ent;

        dir = opendir(kStoragePath);
        REQUIRE(dir != nullptr);

        bool found_test_file = false;
        while (ent = readdir(dir)) {
          if (ent->d_name == kTestFilename) {
            found_test_file = true;
          }
        }
        closedir(dir);

        REQUIRE(found_test_file);
      }

      REQUIRE(remove(kTestFilePath.c_str()) == 0);
    }
  }
}

// Failing due to hardware issue. Re-enable in R2.
TEST_CASE("sd card mux", "[integration][!mayfail]") {
  I2CFixture i2c;
  SpiFixture spi;
  GpioExpander expander;

  SECTION("accessible when switched on") {
    expander.with([&](auto& gpio) {
      gpio.set_pin(GpioExpander::SD_MUX_SWITCH, GpioExpander::SD_MUX_ESP);
    });

    auto result = SdStorage::create(&expander);
    REQUIRE(result.has_value());
  }

  SECTION("inaccessible when switched off") {
    expander.with([&](auto& gpio) {
      gpio.set_pin(GpioExpander::SD_MUX_SWITCH, GpioExpander::SD_MUX_USB);
    });

    auto result = SdStorage::create(&expander);
    REQUIRE(result.has_error());
    REQUIRE(result.error() == SdStorage::FAILED_TO_READ);
  }
}

}  // namespace drivers