• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 14191488263

01 Apr 2025 07:17AM UTC coverage: 91.364% (+0.002%) from 91.362%
14191488263

push

github

web-flow
Merge pull request #4811 from KaganCanSit/feature-android-NDK-and-XCode-TODO-messages

Fixed the use of C++20 functions with updated Android NDK and XCode versions.

95237 of 104239 relevant lines covered (91.36%)

11724846.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.51
/src/lib/tls/tls_session_manager_memory.cpp
1
/**
2
 * TLS Session Management
3
 * (C) 2011,2012 Jack Lloyd
4
 *     2023 René Meusel - Rohde & Schwarz Cybersecurity
5
 *
6
 * Botan is released under the Simplified BSD License (see license.txt)
7
 */
8

9
#include <botan/tls_session_manager_memory.h>
10

11
#include <botan/rng.h>
12
#include <botan/internal/stl_util.h>
13

14
#include <algorithm>
15

16
namespace Botan::TLS {
17

18
Session_Manager_In_Memory::Session_Manager_In_Memory(const std::shared_ptr<RandomNumberGenerator>& rng,
2,140✔
19
                                                     size_t max_sessions) :
2,140✔
20
      Session_Manager(rng), m_max_sessions(max_sessions) {
2,140✔
21
   if(max_sessions > 0) {
2,140✔
22
      m_fifo.emplace();
2,140✔
23
   }
24
}
2,140✔
25

26
void Session_Manager_In_Memory::store(const Session& session, const Session_Handle& handle) {
1,487✔
27
   // TODO: C++20 allows CTAD for template aliases (read: lock_guard_type), so
28
   //       technically we should be able to omit the explicit mutex type.
29
   //       Unfortuately clang does not agree, yet.
30
   lock_guard_type<recursive_mutex_type> lk(mutex());
1,487✔
31

32
   if(m_fifo.has_value()) {
1,487✔
33
      while(m_sessions.size() >= capacity()) {
1,493✔
34
         BOTAN_ASSERT_NOMSG(m_sessions.size() <= m_fifo->size());
6✔
35
         m_sessions.erase(m_fifo->front());
6✔
36
         m_fifo->pop_front();
6✔
37
      }
38
   }
39

40
   // Generate a random session ID if the peer did not provide one. Note that
41
   // this ID is just for internal use and won't be returned on ::find().
42
   auto id = handle.id().value_or(m_rng->random_vec<Session_ID>(32));
3,154✔
43
   m_sessions.emplace(id, Session_with_Handle{session, handle});
2,974✔
44

45
   if(m_fifo.has_value()) {
1,487✔
46
      m_fifo->emplace_back(std::move(id));
1,487✔
47
   }
48
}
1,487✔
49

50
std::optional<Session> Session_Manager_In_Memory::retrieve_one(const Session_Handle& handle) {
133✔
51
   lock_guard_type<recursive_mutex_type> lk(mutex());
133✔
52

53
   if(auto id = handle.id()) {
133✔
54
      const auto session = m_sessions.find(id.value());
126✔
55
      if(session != m_sessions.end()) {
126✔
56
         return session->second.session;
58✔
57
      }
58
   }
58✔
59

60
   return std::nullopt;
75✔
61
}
133✔
62

63
std::vector<Session_with_Handle> Session_Manager_In_Memory::find_some(const Server_Information& info,
1,553✔
64
                                                                      const size_t max_sessions_hint) {
65
   BOTAN_UNUSED(max_sessions_hint);
1,553✔
66

67
   lock_guard_type<recursive_mutex_type> lk(mutex());
1,553✔
68

69
   std::vector<Session_with_Handle> found_sessions;
1,553✔
70
   // TODO: std::copy_if?
71
   for(const auto& [_, session_and_handle] : m_sessions) {
2,317✔
72
      if(session_and_handle.session.server_info() == info) {
764✔
73
         found_sessions.emplace_back(session_and_handle);
764✔
74
      }
75
   }
76

77
   return found_sessions;
1,553✔
78
}
1,553✔
79

80
size_t Session_Manager_In_Memory::remove(const Session_Handle& handle) {
471✔
81
   lock_guard_type<recursive_mutex_type> lk(mutex());
471✔
82
   return remove_internal(handle);
471✔
83
}
471✔
84

85
size_t Session_Manager_In_Memory::remove_internal(const Session_Handle& handle) {
574✔
86
   return std::visit(overloaded{
1,148✔
87
                        // We deliberately leave the deleted session in m_fifo. Finding it would be
88
                        // another O(n) operation. Instead, purging will run in a loop and skip m_fifo
89
                        // entries that were not found anymore.
90
                        [&](const Session_Ticket& ticket) -> size_t {
454✔
91
                           // TODO: This is an O(n) operation. Typically, the Session_Manager will
92
                           //       not contain a plethora of sessions and this should be fine. If
93
                           //       it's not, we'll need to consider another index on tickets.
94
                           return std::erase_if(m_sessions, [&](const auto& item) {
454✔
95
                              const auto& [_unused1, session_and_handle] = item;
406✔
96
                              const auto& [_unused2, this_handle] = session_and_handle;
406✔
97
                              return this_handle.is_ticket() && this_handle.ticket().value() == ticket;
406✔
98
                           });
99
                        },
100
                        [&](const Session_ID& id) -> size_t { return m_sessions.erase(id); },
18✔
101
                        [&](const Opaque_Session_Handle&) -> size_t {
102✔
102
                           if(auto id = handle.id()) {
102✔
103
                              auto removed = remove_internal(id.value());
12✔
104
                              if(removed > 0) {
4✔
105
                                 return removed;
3✔
106
                              }
107
                           }
3✔
108

109
                           if(auto ticket = handle.ticket()) {
99✔
110
                              return remove_internal(ticket.value());
297✔
111
                           }
99✔
112

113
                           return 0;
×
114
                        },
115
                     },
116
                     handle.get());
1,148✔
117
}
118

119
size_t Session_Manager_In_Memory::remove_all() {
9✔
120
   lock_guard_type<recursive_mutex_type> lk(mutex());
9✔
121

122
   const auto sessions = m_sessions.size();
9✔
123
   m_sessions.clear();
9✔
124
   if(m_fifo.has_value()) {
9✔
125
      m_fifo->clear();
9✔
126
   }
127

128
   return sessions;
9✔
129
}
9✔
130

131
}  // namespace Botan::TLS
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc