Coverage Report

Created: 2026-05-30 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/usbguard/src/Library/RuleEvaluatedCondition.cpp
Line
Count
Source
1
//
2
// Copyright (C) 2016 Red Hat, Inc.
3
//
4
// This program is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17
// Authors: Daniel Kopecek <dkopecek@redhat.com>
18
//
19
#ifdef HAVE_BUILD_CONFIG_H
20
  #include <build-config.h>
21
#endif
22
23
#include "RuleEvaluatedCondition.hpp"
24
#include "RulePrivate.hpp"
25
26
#include "usbguard/RuleParser.hpp"
27
28
#include <string>
29
30
#ifndef _XOPEN_SOURCE
31
  #define _XOPEN_SOURCE
32
  #include <ctime>
33
#endif
34
35
namespace usbguard
36
{
37
  RuleEvaluatedCondition::RuleEvaluatedCondition(const std::string& elapsed_time, bool negated)
38
2.03k
    : RuleConditionBase("rule-applied", elapsed_time, negated)
39
2.03k
  {
40
2.03k
    _elapsed_time = std::chrono::steady_clock::duration(stringToSeconds(elapsed_time));
41
2.03k
  }
42
43
  RuleEvaluatedCondition::RuleEvaluatedCondition(const RuleEvaluatedCondition& rhs)
44
3.17k
    : RuleConditionBase(rhs),
45
3.17k
      _elapsed_time(rhs._elapsed_time)
46
3.17k
  {
47
3.17k
  }
48
49
  bool RuleEvaluatedCondition::update(const Rule& rule)
50
0
  {
51
0
    if (rule.internal()->metadata().counter_evaluated > 0) {
52
0
      if (_elapsed_time == std::chrono::steady_clock::duration::zero()) {
53
0
        return true;
54
0
      }
55
0
      else {
56
0
        const auto last_evaluated_duration = std::chrono::steady_clock::now() \
57
0
          - rule.internal()->metadata().tp_last_evaluated;
58
59
0
        if (last_evaluated_duration <= _elapsed_time) {
60
0
          return true;
61
0
        }
62
0
      }
63
0
    }
64
65
0
    return false;
66
0
  }
67
68
  RuleConditionBase* RuleEvaluatedCondition::clone() const
69
3.17k
  {
70
3.17k
    return new RuleEvaluatedCondition(*this);
71
3.17k
  }
72
73
  uint64_t RuleEvaluatedCondition::stringToSeconds(const std::string& string)
74
2.03k
  {
75
2.03k
    struct ::tm tm = { };
76
77
2.03k
    if (string.empty() || string == "") {
78
507
      return 0;
79
507
    }
80
81
1.53k
    if (::strptime(string.c_str(), "%H:%M:%s", &tm) == nullptr) {
82
1.33k
      if (::strptime(string.c_str(), "%H:%M", &tm) == nullptr) {
83
1.13k
        if (::strptime(string.c_str(), "%s", &tm) == nullptr) {
84
9
          throw std::runtime_error("Invalid time string. Expecing either HH:MM, HH:MM:SS or SS format.");
85
9
        }
86
1.13k
      }
87
1.33k
    }
88
89
1.52k
    return tm.tm_sec + 60*tm.tm_min + 60*60*tm.tm_hour;
90
1.53k
  }
91
} /* namespace usbguard */
92
93
/* vim: set ts=2 sw=2 et */