-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathast_construction_says_assertion_test.cc
89 lines (79 loc) · 3.77 KB
/
ast_construction_says_assertion_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//-------------------------------------------------------------------------------
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-------------------------------------------------------------------------------
#include <algorithm>
#include <iostream>
#include <memory>
#include <optional>
#include <stdexcept>
#include "src/common/testing/gtest.h"
#include "src/common/utils/map_iter.h"
#include "src/ir/auth_logic/ast.h"
#include "src/ir/auth_logic/ast_construction.h"
#include "src/ir/auth_logic/ast_string.h"
namespace raksha::ir::auth_logic {
struct SaysAssertionTestData {
std::string input_text;
std::string principal;
std::vector<std::string> assertions;
};
class AstConstructionTest
: public testing::TestWithParam<SaysAssertionTestData> {};
class SaysAssertionAstConstructionTest
: public testing::TestWithParam<SaysAssertionTestData> {};
TEST_P(SaysAssertionAstConstructionTest, SimpleTestWithSaysAssertionProgram) {
const auto& [input_text, principal, assertions] = GetParam();
Program prog_out = ParseProgram(input_text);
ASSERT_EQ(prog_out.says_assertions().size(), 1);
const SaysAssertion& says_assertion = prog_out.says_assertions().front();
EXPECT_EQ(says_assertion.principal().name(), principal);
EXPECT_EQ(says_assertion.assertions().size(), assertions.size());
std::vector<std::string> assertion_string = utils::MapIter<std::string>(
says_assertion.assertions(),
[](Assertion assertion) { return ToString(assertion); });
EXPECT_THAT(assertion_string,
::testing::UnorderedElementsAreArray(assertions));
}
INSTANTIATE_TEST_SUITE_P(
SaysAssertionAstConstructionTest, SaysAssertionAstConstructionTest,
testing::Values(
SaysAssertionTestData({R"("UserA" says { currTimeIs(2022).
mayA(NotifyA) :- currTimeIs > 2021. })",
R"("UserA")",
{"currTimeIs(2022)",
"mayA(NotifyA) :- currTimeIs > 2021"}}),
SaysAssertionTestData({R"("UserB" says { currTimeIs(2020).
mayB(NotifyB) :- currTimeIs < 2021. })",
R"("UserB")",
{"currTimeIs(2020)",
"mayB(NotifyB) :- currTimeIs < 2021"}}),
SaysAssertionTestData({R"("UserC" says { currTimeIs(2021).
mayC(NotifyC) :- currTimeIs = 2021). })",
R"("UserC")",
{"currTimeIs(2021)",
"mayC(NotifyC) :- currTimeIs = 2021"}}),
SaysAssertionTestData({R"("UserC" says { currTimeIs(2021).
mayC(NotifyC) :- currTimeIs >= 2021). })",
R"("UserC")",
{"currTimeIs(2021)",
"mayC(NotifyC) :- currTimeIs >= 2021"}}),
SaysAssertionTestData({R"("UserC" says { currTimeIs(2021).
mayC(NotifyC) :- currTimeIs <= 2021). })",
R"("UserC")",
{"currTimeIs(2021)",
"mayC(NotifyC) :- currTimeIs <= 2021"}})
));
} // namespace raksha::ir::auth_logic