LCOV - code coverage report
Current view: top level - model - Transformation.hpp (source / functions) Coverage Total Hit
Test: final-coverage.info Lines: 100.0 % 53 53
Test Date: 2025-02-14 17:40:40 Functions: 100.0 % 26 26

            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              : #pragma once
      37              : 
      38              : 
      39              : #include <string>
      40              : #include <vector>
      41              : #include <regex>
      42              : #include <cstdint>
      43              : 
      44              : 
      45              : #include <nlohmann/json.hpp>
      46              : 
      47              : 
      48              : namespace h2agent
      49              : {
      50              : namespace model
      51              : {
      52              : 
      53              : class Transformation
      54              : {
      55              : public:
      56              : 
      57          157 :     Transformation(): source_(""), source_i1_(0), source_i2_(0),
      58          157 :         target_(""),
      59          785 :         has_filter_(false), filter_(""), filter_number_type_(0), filter_i_(0), filter_u_(0), filter_f_(0) {;}
      60              : 
      61              :     // Source type
      62              :     enum SourceType { RequestUri = 0, RequestUriPath, RequestUriParam, RequestBody, ResponseBody, RequestHeader, Eraser, Math, Random, RandomSet, Timestamp, Strftime, Recvseq, SVar, SGVar, Value, ServerEvent, InState, STxtFile, SBinFile, Command };
      63           11 :     const char* SourceTypeAsText(const SourceType & type) const
      64              :     {
      65              :         static const char* text [] = { "RequestUri", "RequestUriPath", "RequestUriParam", "RequestBody", "ResponseBody", "RequestHeader", "Eraser", "Math", "Random", "RandomSet", "Timestamp", "Strftime", "Recvseq", "SVar", "SGVar", "Value", "ServerEvent", "InState", "STxtFile", "SBinFile", "Command" };
      66           11 :         return text [type];
      67              :     }
      68              : 
      69              :     // Target type
      70              :     enum TargetType { ResponseBodyString = 0, ResponseBodyHexString, ResponseBodyJson_String, ResponseBodyJson_Integer, ResponseBodyJson_Unsigned, ResponseBodyJson_Float, ResponseBodyJson_Boolean, ResponseBodyJson_Object, ResponseBodyJson_JsonString, ResponseHeader, ResponseStatusCode, ResponseDelayMs, TVar, TGVar, OutState, TTxtFile, TBinFile, UDPSocket, ServerEventToPurge, Break, RequestBodyString, RequestBodyHexString, RequestBodyJson_String, RequestBodyJson_Integer, RequestBodyJson_Unsigned, RequestBodyJson_Float, RequestBodyJson_Boolean, RequestBodyJson_Object, RequestBodyJson_JsonString };
      71           11 :     const char* TargetTypeAsText(const TargetType & type) const
      72              :     {
      73              :         static const char* text [] = { "ResponseBodyString", "ResponseBodyHexString", "ResponseBodyJson_String", "ResponseBodyJson_Integer", "ResponseBodyJson_Unsigned", "ResponseBodyJson_Float", "ResponseBodyJson_Boolean", "ResponseBodyJson_Object", "ResponseBodyJson_JsonString", "ResponseHeader", "ResponseStatusCode", "ResponseDelayMs", "TVar", "TGVar", "OutState", "TTxtFile", "TBinFile", "UDPSocket", "ServerEventToPurge", "Break", "RequestBodyString", "RequestBodyHexString", "RequestBodyJson_String", "RequestBodyJson_Integer", "RequestBodyJson_Unsigned", "RequestBodyJson_Float", "RequestBodyJson_Boolean", "RequestBodyJson_Object", "RequestBodyJson_JsonString" };
      74           11 :         return text [type];
      75              :     }
      76              : 
      77              :     // Filter type
      78              :     enum FilterType { RegexCapture = 0, RegexReplace, Append, Prepend, Sum, Multiply, ConditionVar, EqualTo, DifferentFrom, JsonConstraint, SchemaId };
      79            4 :     const char* FilterTypeAsText(const FilterType & type) const
      80              :     {
      81              :         static const char* text [] = { "RegexCapture", "RegexReplace", "Append", "Prepend", "Sum", "Multiply", "ConditionVar", "EqualTo", "DifferentFrom", "JsonConstraint", "SchemaId" };
      82            4 :         return text [type];
      83              :     }
      84              : 
      85              :     // setters:
      86              : 
      87              :     /**
      88              :      * Load transformation information
      89              :      *
      90              :      * @param j Json transformation object
      91              :      *
      92              :      * @return Operation success
      93              :      */
      94              :     bool load(const nlohmann::json &j);
      95              : 
      96              : 
      97              :     /**
      98              :      * Class string representation
      99              :      *
     100              :      * @return string representation
     101              :      */
     102              :     std::string asString() const;
     103              : 
     104              : 
     105              : private:
     106              : 
     107              :     SourceType source_type_{};
     108              :     std::string source_{}; // RequestUriParam, RequestBody(empty: whole, path: node), ResponseBody(empty: whole, path: node),
     109              :     // RequestHeader, Math, Timestamp, Strftime, SVar, SGVar, Value, STxtFile(path), SBinFile (path), Command(expression)
     110              :     std::vector<std::string> source_tokenized_{}; // RandomSet, ServerEvent
     111              :     int source_i1_{}, source_i2_{}; // Random
     112              : 
     113              :     TargetType target_type_{};
     114              :     std::string target_{}; // ResponseBodyJson_String/Integer/Unsigned/Float/Boolean/Object/JsonString(empty: whole, path: node),
     115              :     // ResponseHeader, TVar, TGVar, OutState (foreign method part), TTxtFile(path), TBinFile (path), UDPSocket (path[.delayMs])
     116              :     std::vector<std::string> target_tokenized_{}; // ServerEventToPurge
     117              :     std::string target2_{}; // OutState (foreign uri part)
     118              : 
     119              :     bool has_filter_{};
     120              :     FilterType filter_type_{};
     121              :     std::string filter_{}; // RegexReplace(fmt), RegexCapture(literal, although not actually needed, but useful to access & print on traces), Append, Prepend, ConditionVar, EqualTo, DifferentFrom, SchemaId
     122              :     nlohmann::json filter_object_{}; // JsonConstraint
     123              :     std::regex filter_rgx_{}; // RegexCapture, RegexReplace
     124              :     int filter_number_type_{}; // Sum, Multiply (0: integer, 1: unsigned, 2: float)
     125              :     std::int64_t filter_i_{}; // Sum, Multiply
     126              :     std::uint64_t filter_u_{}; // Sum, Multiply
     127              :     double filter_f_{}; // Sum, Multiply
     128              : 
     129              :     /**
     130              :      * Builds a map of patterns (pattern, varname) where pattern is @{varname}
     131              :      *
     132              :      * @param str string to analyze
     133              :      * @param patterns map generated by reference
     134              :      */
     135              :     void collectVariablePatterns(const std::string &str, std::map<std::string, std::string> &patterns);
     136              : 
     137              :     std::map<std::string, std::string> source_patterns_;
     138              :     std::map<std::string, std::string> filter_patterns_;
     139              :     std::map<std::string, std::string> target_patterns_;
     140              :     std::map<std::string, std::string> target2_patterns_;
     141              : 
     142              : public:
     143              : 
     144              :     // getters:
     145              : 
     146              :     /** Gets source type */
     147          267 :     SourceType getSourceType() const {
     148          267 :         return source_type_;
     149              :     }
     150              :     /** Gets source */
     151          113 :     const std::string &getSource() const {
     152          113 :         return source_;
     153              :     }
     154              :     /** Gets source tokenized */
     155           14 :     const std::vector<std::string> &getSourceTokenized() const {
     156           14 :         return source_tokenized_;
     157              :     }
     158              :     /** Gets source for random min */
     159            4 :     int getSourceI1() const {
     160            4 :         return source_i1_;
     161              :     }
     162              :     /** Gets source for random max */
     163            2 :     int getSourceI2() const {
     164            2 :         return source_i2_;
     165              :     }
     166              : 
     167              :     /** Gets target type */
     168          865 :     TargetType getTargetType() const {
     169          865 :         return target_type_;
     170              :     }
     171              :     /** Gets target */
     172          117 :     const std::string &getTarget() const {
     173          117 :         return target_;
     174              :     }
     175              :     /** Gets target tokenized */
     176            6 :     const std::vector<std::string> &getTargetTokenized() const {
     177            6 :         return target_tokenized_;
     178              :     }
     179              :     /** Gets target2 */
     180          117 :     const std::string &getTarget2() const {
     181          117 :         return target2_;
     182              :     }
     183              : 
     184              :     /** Gets filter existence */
     185          121 :     bool hasFilter() const {
     186          121 :         return has_filter_;
     187              :     }
     188              :     /** Gets filter type */
     189           85 :     FilterType getFilterType() const {
     190           85 :         return filter_type_;
     191              :     }
     192              :     /** Gets filter */
     193           17 :     const std::string &getFilter() const {
     194           17 :         return filter_;
     195              :     }
     196              :     /** Gets filter regex */
     197            4 :     const std::regex &getFilterRegex() const {
     198            4 :         return filter_rgx_;
     199              :     }
     200              :     /** Gets filter type for sum/multiply */
     201            6 :     int getFilterNumberType() const {
     202            6 :         return filter_number_type_;
     203              :     }
     204              :     /** Integer container for sum/multiply */
     205            2 :     std::int64_t getFilterI() const {
     206            2 :         return filter_i_;
     207              :     }
     208              :     /** Unsigned integer container for sum/multiply */
     209            2 :     std::uint64_t getFilterU() const {
     210            2 :         return filter_u_;
     211              :     }
     212              :     /** Float container for sum/multiply */
     213            2 :     double getFilterF() const {
     214            2 :         return filter_f_;
     215              :     }
     216              :     /** Obecjt container for json constraint */
     217            3 :     const nlohmann::json &getFilterObject() const {
     218            3 :         return filter_object_;
     219              :     }
     220              : 
     221              :     /** Source patterns */
     222          112 :     const std::map<std::string, std::string> &getSourcePatterns() const {
     223          112 :         return source_patterns_;
     224              :     }
     225              :     /** Filter patterns */
     226            6 :     const std::map<std::string, std::string> &getFilterPatterns() const {
     227            6 :         return filter_patterns_;
     228              :     }
     229              :     /** Target patterns */
     230          125 :     const std::map<std::string, std::string> &getTargetPatterns() const {
     231          125 :         return target_patterns_;
     232              :     }
     233              :     /** Target2 patterns */
     234            3 :     const std::map<std::string, std::string> &getTarget2Patterns() const {
     235            3 :         return target2_patterns_;
     236              :     }
     237              : };
     238              : 
     239              : }
     240              : }
        

Generated by: LCOV version 2.0-1