Skip to content

Commit

Permalink
updating test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
PBrunot committed May 15, 2024
1 parent 76298c5 commit 0d030af
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
15 changes: 15 additions & 0 deletions include/CachedCards.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ namespace fabomatic
return {cards[i], levels[i]};
}

const std::optional<CachedCard> find_uid(const card::uid_t &search_uid) const
{
if (search_uid == card::INVALID)
{
return std::nullopt;
}
const auto pos = std::find(cards.cbegin(), cards.cend(), search_uid);
if (pos != cards.cend())
{
auto idx = std::distance(cards.cbegin(), pos);
return CachedCard{*pos, levels[idx]};
}
return std::nullopt;
}

constexpr void set_at(int idx, const card::uid_t &uid, const FabUser::UserLevel &level)
{
cards[idx] = uid;
Expand Down
18 changes: 2 additions & 16 deletions src/AuthProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ namespace fabomatic
cache_idx = 0;

// Add into list
cache.cards.at(cache_idx) = uid;
cache.levels.at(cache_idx) = level;
cache.set_at(cache_idx, uid, level);

cache_idx = (cache_idx + 1) % conf::rfid_tags::CACHE_LEN;
}
Expand Down Expand Up @@ -157,20 +156,7 @@ namespace fabomatic
/// @return a whitelistentry object if the card is found in whitelist
auto AuthProvider::uidInCache(card::uid_t candidate_uid) const -> std::optional<CachedCard>
{
if (candidate_uid == card::INVALID)
{
return std::nullopt;
}

const auto elem = std::find(cache.cards.cbegin(), cache.cards.cend(), candidate_uid);

if (elem == cache.cards.cend())
{
ESP_LOGD(TAG, "%s not found in cache", card::uid_str(candidate_uid).c_str());
return std::nullopt;
}

return cache[std::distance(cache.cards.cbegin(), elem)];
return cache.find_uid(candidate_uid);
}

/// @brief Loads the cache from EEPROM
Expand Down
9 changes: 3 additions & 6 deletions test/test_mqtt/test_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,8 @@ namespace fabomatic::tests
auto cached_entries = SavedConfig::LoadFromEEPROM().value().cachedRfid;
for (const auto &[uid, level, name] : secrets::cards::whitelist)
{
auto found = std::find_if(cached_entries.begin(), cached_entries.end(),
[uid, level](const auto &entry)
{ return entry.uid == uid && entry.level == level; });
TEST_ASSERT_TRUE_MESSAGE(level == FabUser::UserLevel::Unknown ||
found != cached_entries.end(),
const auto &cached_card = cached_entries.find_uid(uid);
TEST_ASSERT_TRUE_MESSAGE(cached_card || level == FabUser::UserLevel::Unknown,
"AuthProvider saveCache failed to save all whitelist entries");
}
}
Expand Down Expand Up @@ -297,7 +294,7 @@ namespace fabomatic::tests
}
} // namespace fabomatic::Tests

void tearDown(void) {};
void tearDown(void){};

void setUp(void)
{
Expand Down
6 changes: 4 additions & 2 deletions test/test_savedconfig/test_savedconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ namespace fabomatic::tests
TEST_ASSERT_TRUE_MESSAGE(defaults.cachedRfid.size() == conf::rfid_tags::CACHE_LEN, "Default config cachedRfid size mismatch");

// Test that default config has empty cache
for (const auto &tag : defaults.cachedRfid)
for (auto i = 0; i < defaults.cachedRfid.size(); i++)
{
const auto &tag = defaults.cachedRfid[i];
TEST_ASSERT_TRUE_MESSAGE(tag.uid == 0, "Default config cachedRfid not empty");
TEST_ASSERT_TRUE_MESSAGE(tag.level == FabUser::UserLevel::Unknown, "Default config cachedRfid not empty");
}
Expand All @@ -109,8 +110,9 @@ namespace fabomatic::tests
defaults = SavedConfig::LoadFromEEPROM().value_or(SavedConfig::DefaultConfig());

// Test that default config is still empty
for (const auto &tag : defaults.cachedRfid)
for (auto i = 0; i < defaults.cachedRfid.size(); i++)
{
const auto &tag = defaults.cachedRfid[i];
TEST_ASSERT_TRUE_MESSAGE(tag.uid == 0, "Default config cachedRfid not empty after AuthProvider saveCache");
TEST_ASSERT_TRUE_MESSAGE(tag.level == FabUser::UserLevel::Unknown, "Default config cachedRfid not empty after AuthProvider saveCache");
}
Expand Down

0 comments on commit 0d030af

Please sign in to comment.