LCOV - code coverage report
Current view: top level - model - AdminServerProvisionData.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 73 73
Test Date: 2026-04-17 17:21:26 Functions: 100.0 % 8 8

            Line data    Source code
       1              : /*
       2              :  ___________________________________________
       3              : |    _     ___                        _     |
       4              : |   | |   |__ \                      | |    |
       5              : |   | |__    ) |__ _  __ _  ___ _ __ | |_   |
       6              : |   | '_ \  / // _` |/ _` |/ _ \ '_ \| __|  |  HTTP/2 AGENT FOR MOCK TESTING
       7              : |   | | | |/ /| (_| | (_| |  __/ | | | |_   |  Version 0.0.z
       8              : |   |_| |_|____\__,_|\__, |\___|_| |_|\__|  |  https://github.com/testillano/h2agent
       9              : |                     __/ |                 |
      10              : |                    |___/                  |
      11              : |___________________________________________|
      12              : 
      13              : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
      14              : SPDX-License-Identifier: MIT
      15              : Copyright (c) 2021 Eduardo Ramos
      16              : 
      17              : Permission is hereby  granted, free of charge, to any  person obtaining a copy
      18              : of this software and associated  documentation files (the "Software"), to deal
      19              : in the Software  without restriction, including without  limitation the rights
      20              : to  use, copy,  modify, merge,  publish, distribute,  sublicense, and/or  sell
      21              : copies  of  the Software,  and  to  permit persons  to  whom  the Software  is
      22              : furnished to do so, subject to the following conditions:
      23              : 
      24              : The above copyright notice and this permission notice shall be included in all
      25              : copies or substantial portions of the Software.
      26              : 
      27              : THE SOFTWARE  IS PROVIDED "AS  IS", WITHOUT WARRANTY  OF ANY KIND,  EXPRESS OR
      28              : IMPLIED,  INCLUDING BUT  NOT  LIMITED TO  THE  WARRANTIES OF  MERCHANTABILITY,
      29              : FITNESS FOR  A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT  SHALL THE
      30              : AUTHORS  OR COPYRIGHT  HOLDERS  BE  LIABLE FOR  ANY  CLAIM,  DAMAGES OR  OTHER
      31              : LIABILITY, WHETHER IN AN ACTION OF  CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      32              : OUT OF OR IN CONNECTION WITH THE SOFTWARE  OR THE USE OR OTHER DEALINGS IN THE
      33              : SOFTWARE.
      34              : */
      35              : 
      36              : #include <string>
      37              : #include <regex>
      38              : 
      39              : #include <nlohmann/json.hpp>
      40              : 
      41              : #include <ert/tracing/Logger.hpp>
      42              : 
      43              : #include <AdminServerProvisionData.hpp>
      44              : #include <functions.hpp>
      45              : 
      46              : 
      47              : namespace h2agent
      48              : {
      49              : namespace model
      50              : {
      51              : 
      52          307 : AdminServerProvisionData::AdminServerProvisionData() {
      53          307 :     server_provision_schema_.setJson(h2agent::adminSchemas::server_provision); // won't fail
      54          307 : }
      55              : 
      56            3 : std::string AdminServerProvisionData::asJsonString(bool ordered, bool getUnused) const {
      57              : 
      58            3 :     nlohmann::json result = nlohmann::json::array();
      59              : 
      60            3 :     read_guard_t guard(rw_mutex_);
      61            3 :     if (ordered) {
      62              :         bool aux;
      63            3 :         for (auto it = ordered_keys_.begin(); it != ordered_keys_.end(); it++) {
      64            2 :             auto element =  get(*it, aux);
      65            2 :             if (getUnused && element->employed()) continue;
      66            2 :             result.push_back(element->getJson());
      67            2 :         };
      68              :     }
      69              :     else {
      70            2 :         this->forEach([&](const KeyType& k, const ValueType& value) {
      71            1 :             if (!(getUnused && value->employed())) {
      72            1 :                 result.push_back(value->getJson());
      73              :             }
      74            1 :         });
      75              :     }
      76              : 
      77              :     // Provision is shown as an array regardless if there is 1 item, N items or none ([]):
      78            6 :     return (result.dump());
      79            3 : }
      80              : 
      81          173 : AdminServerProvisionData::LoadResult AdminServerProvisionData::loadSingle(const nlohmann::json &j, bool regexMatchingConfigured, const common_resources_t &cr) {
      82              : 
      83          173 :     std::string error{};
      84          173 :     if (!server_provision_schema_.validate(j, error)) {
      85            2 :         return BadSchema;
      86              :     }
      87              : 
      88              :     // Provision object to fill:
      89          171 :     auto provision = std::make_shared<AdminServerProvision>();
      90              : 
      91          171 :     if (provision->load(j, regexMatchingConfigured)) {
      92              : 
      93              :         // Push the key in the map:
      94          167 :         admin_server_provision_key_t key = provision->getKey();
      95              : 
      96              :         // Push the key just in case we configure ordered algorithm 'RegexMatching'.
      97              :         // So, we always have both lists available; as each algorithm finds within the proper
      98              :         // list, we don't need to drop provisions when swaping the matching mode on the fly:
      99          167 :         write_guard_t guard(rw_mutex_);
     100              : 
     101          167 :         if (!exists(key)) {
     102          167 :             ordered_keys_.push_back(key);
     103              :         }
     104              : 
     105              :         // Set common resources BEFORE adding to map, so traffic threads
     106              :         // never see a provision with null resource pointers:
     107          167 :         provision->setAdminData(cr.AdminDataPtr);
     108          167 :         provision->setConfiguration(cr.ConfigurationPtr);
     109          167 :         provision->setVault(cr.VaultPtr);
     110          167 :         provision->setFileManager(cr.FileManagerPtr);
     111          167 :         provision->setSocketManager(cr.SocketManagerPtr);
     112          167 :         provision->setMockServerData(cr.MockServerDataPtr);
     113          167 :         provision->setMockClientData(cr.MockClientDataPtr);
     114              : 
     115          167 :         add(key, provision);
     116              : 
     117          167 :         return Success;
     118          167 :     }
     119              : 
     120            4 :     return BadContent;
     121          173 : }
     122              : 
     123          171 : AdminServerProvisionData::LoadResult AdminServerProvisionData::load(const nlohmann::json &j, bool regexMatchingConfigured, const common_resources_t &cr) {
     124              : 
     125          171 :     if (j.is_array()) {
     126            8 :         for (auto it : j) // "it" is of type json::reference and has no key() member
     127              :         {
     128            4 :             LoadResult result = loadSingle(it, regexMatchingConfigured, cr);
     129            4 :             if (result != Success)
     130            1 :                 return result;
     131            4 :         }
     132              : 
     133            1 :         return Success;
     134              :     }
     135              : 
     136          169 :     return loadSingle(j, regexMatchingConfigured, cr);
     137              : }
     138              : 
     139           10 : bool AdminServerProvisionData::clear()
     140              : {
     141           10 :     write_guard_t guard(rw_mutex_);
     142           10 :     ordered_keys_.clear();
     143           20 :     return Map::clear();
     144           10 : }
     145              : 
     146          160 : std::shared_ptr<AdminServerProvision> AdminServerProvisionData::find(const std::string &inState, const std::string &method, const std::string &uri) const {
     147          160 :     admin_server_provision_key_t key{};
     148          160 :     h2agent::model::calculateStringKey(key, inState, method, uri);
     149              : 
     150          160 :     bool exists{};
     151          160 :     auto result = get(key, exists);
     152          320 :     return (exists ? result:nullptr);
     153          160 : }
     154              : 
     155            4 : std::shared_ptr<AdminServerProvision> AdminServerProvisionData::findRegexMatching(const std::string &inState, const std::string &method, const std::string &uri) const {
     156            4 :     admin_server_provision_key_t key{};
     157            4 :     h2agent::model::calculateStringKey(key, inState, method, uri);
     158              : 
     159            4 :     read_guard_t guard(rw_mutex_);
     160            4 :     bool aux{};
     161            7 :     for (auto it = ordered_keys_.begin(); it != ordered_keys_.end(); it++) {
     162            4 :         auto provision = get(*it, aux);
     163            4 :         if (std::regex_match(key, provision->getRegex()))
     164            1 :             return provision;
     165            4 :     };
     166              : 
     167            3 :     return nullptr;
     168            4 : }
     169              : 
     170              : }
     171              : }
     172              : 
        

Generated by: LCOV version 2.0-1