Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToString Tests #731

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/ir/auth_logic/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,29 @@ cc_library(

cc_library(
name = "ast_string",
hdrs = [
"ast_string.h",
],
srcs = ["ast_string.cc"],
hdrs = ["ast_string.h"],
deps = [
":ast",
"//src/common/utils:map_iter",
"//src/common/utils:overloaded",
"//src/ir/datalog:program",
"@com_google_absl//absl/strings",
],
)

cc_test(
name = "ast_string_test",
srcs = ["ast_string_test.cc"],
deps = [
":ast",
":ast_construction",
":ast_string",
"//src/common/testing:gtest",
"//src/ir/datalog:program",
],
)

cc_test(
name = "ast_construction_query_test",
srcs = ["ast_construction_query_test.cc"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ INSTANTIATE_TEST_SUITE_P(
RelationDeclarationTestData({R"(.decl someFact(x : CustomType))",
R"(someFact)",
false,
{R"(x : CustomType)"}}),
{R"(x : CustomType::Custom)"}}),
RelationDeclarationTestData(
{R"(.decl someOtherFact(x : CustomType, y : Number))",
R"(someOtherFact)",
false,
{R"(x : CustomType)", R"(y : Number)"}}),
{R"(x : CustomType::Custom)", R"(y : Number)"}}),
RelationDeclarationTestData({R"(.decl thirdFact(p : Principal).)",
R"(thirdFact)",
false,
Expand Down
28 changes: 14 additions & 14 deletions src/ir/auth_logic/ast_construction_says_assertion_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,47 +61,47 @@ INSTANTIATE_TEST_SUITE_P(
SaysAssertionTestData({R"("UserA" says { currTimeIs(2022).
mayA(NotifyA) :- currTimeIs > 2021. })",
R"("UserA")",
{"currTimeIs(2022)",
"mayA(NotifyA) :- currTimeIs > 2021"}}),
{"currTimeIs(2022).",
"mayA(NotifyA) :- currTimeIs > 2021."}}),
SaysAssertionTestData({R"("UserB" says { currTimeIs(2020).
mayB(NotifyB) :- currTimeIs < 2021. })",
R"("UserB")",
{"currTimeIs(2020)",
"mayB(NotifyB) :- currTimeIs < 2021"}}),
{"currTimeIs(2020).",
"mayB(NotifyB) :- currTimeIs < 2021."}}),
SaysAssertionTestData({R"("UserC" says { currTimeIs(2021).
mayC(NotifyC) :- currTimeIs = 2021). })",
R"("UserC")",
{"currTimeIs(2021)",
"mayC(NotifyC) :- currTimeIs = 2021"}}),
{"currTimeIs(2021).",
"mayC(NotifyC) :- currTimeIs = 2021."}}),
SaysAssertionTestData({R"("UserC" says { currTimeIs(2021).
mayC(NotifyC) :- currTimeIs >= 2021). })",
R"("UserC")",
{"currTimeIs(2021)",
"mayC(NotifyC) :- currTimeIs >= 2021"}}),
{"currTimeIs(2021).",
"mayC(NotifyC) :- currTimeIs >= 2021."}}),
SaysAssertionTestData({R"("UserC" says { currTimeIs("""A20
21""").
mayC(NotifyC) :- """currTime
Is""" <= 2021). })",
R"("UserC")",
{R"(currTimeIs("A2021"))",
R"(mayC(NotifyC) :- "currTimeIs" <= 2021)"}}),
{R"(currTimeIs("A2021").)",
R"(mayC(NotifyC) :- "currTimeIs" <= 2021.)"}}),
SaysAssertionTestData({
R"("OakTeam" says { ourFavoritePublicKeyIs("""-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAErnyr7XY6gxXPnqRGYEpv5lDsbBeX
izVFLckaF/cbgBuzx7kfmf1qi2j+3DDOQYgISQOj9/LYt4c1cxbt+XtG4g==
-----END PUBLIC KEY-----""").
ourFavoriteTrustedBuilderIs("https://github.com/project-oak/transparent-release"). })",
R"("OakTeam")",
{R"(ourFavoritePublicKeyIs("-----BEGIN PUBLIC KEY-----MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAErnyr7XY6gxXPnqRGYEpv5lDsbBeXizVFLckaF/cbgBuzx7kfmf1qi2j+3DDOQYgISQOj9/LYt4c1cxbt+XtG4g==-----END PUBLIC KEY-----"))",
R"(ourFavoriteTrustedBuilderIs("https://github.com/project-oak/transparent-release"))"}}),
{R"(ourFavoritePublicKeyIs("-----BEGIN PUBLIC KEY-----MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAErnyr7XY6gxXPnqRGYEpv5lDsbBeXizVFLckaF/cbgBuzx7kfmf1qi2j+3DDOQYgISQOj9/LYt4c1cxbt+XtG4g==-----END PUBLIC KEY-----").)",
R"(ourFavoriteTrustedBuilderIs("https://github.com/project-oak/transparent-release").)"}}),
SaysAssertionTestData({
R"("OakTeam" says { ourFavoritePublicKeyIs("""-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAErnyr7XY6gxXPnqRGYEpv5lDsbBeX
izVFLckaF/cbgBuzx7kfmf1qi2j+3DDOQYgISQOj9/LYt4c1cxbt+XtG4g==
-----END PUBLIC KEY-----""").
ourFavoriteTrustedBuilderIs("https://github.com/Attestations/GitHubHostedActions@v1").})",
R"("OakTeam")",
{R"(ourFavoritePublicKeyIs("-----BEGIN PUBLIC KEY-----MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAErnyr7XY6gxXPnqRGYEpv5lDsbBeXizVFLckaF/cbgBuzx7kfmf1qi2j+3DDOQYgISQOj9/LYt4c1cxbt+XtG4g==-----END PUBLIC KEY-----"))",
R"(ourFavoriteTrustedBuilderIs("https://github.com/Attestations/GitHubHostedActions@v1"))"}})));
{R"(ourFavoritePublicKeyIs("-----BEGIN PUBLIC KEY-----MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAErnyr7XY6gxXPnqRGYEpv5lDsbBeXizVFLckaF/cbgBuzx7kfmf1qi2j+3DDOQYgISQOj9/LYt4c1cxbt+XtG4g==-----END PUBLIC KEY-----").)",
R"(ourFavoriteTrustedBuilderIs("https://github.com/Attestations/GitHubHostedActions@v1").)"}})));

} // namespace raksha::ir::auth_logic
132 changes: 132 additions & 0 deletions src/ir/auth_logic/ast_string.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//-------------------------------------------------------------------------------
// 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 "ast_string.h"

namespace raksha::ir::auth_logic {

namespace {
template <typename T>
std::vector<std::string> ToStrings(const std::vector<T> nodes) {
return utils::MapIter<std::string>(
nodes, [](const T node) { return ToString(node); });
}
} // namespace

std::vector<std::string> binary_operations = {"<", ">", "=", "!=", "<=", ">="};

std::string ToString(AstNodeVariantType Node) {
return std::visit(
raksha::utils::overloaded{
[](datalog::Predicate predicate) {
if (std::find(binary_operations.begin(), binary_operations.end(),
predicate.name()) != binary_operations.end()) {
return absl::StrCat(predicate.args().front(), " ",
predicate.name(), " ",
predicate.args().back());
}
return absl::StrCat(
predicate.sign() == datalog::Sign::kNegated ? "!" : "",
predicate.name(), "(", absl::StrJoin(predicate.args(), ", "),
")");
},
[](datalog::ArgumentType argumentType) {
switch (argumentType.kind()) {
case datalog::ArgumentType::Kind::kCustom:
return absl::StrCat(argumentType.name(), "::Custom");
case datalog::ArgumentType::Kind::kNumber:
return std::string("Number");
case datalog::ArgumentType::Kind::kPrincipal:
return std::string("Principal");
}
},
[](datalog::Argument argument) {
return absl::StrCat(argument.argument_name(), " : ",
ToString(argument.argument_type()));
},
[](datalog::RelationDeclaration relationDeclaration) {
return absl::StrCat(
".decl ",
relationDeclaration.is_attribute() ? "attribute " : "",
relationDeclaration.relation_name(), "(",
absl::StrJoin(ToStrings(relationDeclaration.arguments()), ", "),
")");
},
[](Principal principal) { return principal.name(); },
[](Attribute attribute) {
return absl::StrCat(ToString(attribute.principal()), " ",
ToString(attribute.predicate()));
},
[](CanActAs canActAs) {
return absl::StrCat(ToString(canActAs.left_principal()),
" canActAs ",
ToString(canActAs.right_principal()));
},
[](BaseFact basefact) {
return std::visit(
raksha::utils::overloaded{
[](datalog::Predicate predicate) {
return ToString(predicate);
},
[](Attribute attribute) { return ToString(attribute); },
[](CanActAs can_act_as) { return ToString(can_act_as); }},
basefact.GetValue());
},
[](Fact fact) {
std::string cansay_string = "";
for (const Principal& delegatees : fact.delegation_chain()) {
cansay_string =
absl::StrJoin({delegatees.name(), cansay_string}, " canSay ");
}
return absl::StrCat(cansay_string, ToString(fact.base_fact()));
},
[](ConditionalAssertion conditional_assertion) {
return absl::StrJoin(
{ToString(conditional_assertion.lhs()),
absl::StrJoin(ToStrings(conditional_assertion.rhs()), ", ")},
" :- ");
},
[](Assertion assertion) {
return std::visit(
raksha::utils::overloaded{
[](Fact fact) { return absl::StrCat(ToString(fact), "."); },
[](ConditionalAssertion conditional_assertion) {
return absl::StrCat(ToString(conditional_assertion), ".");
}},
assertion.GetValue());
},
[](SaysAssertion saysAssertion) {
return absl::StrCat(
ToString(saysAssertion.principal()), " says {\n",
absl::StrJoin(ToStrings(saysAssertion.assertions()), "\n"),
"}");
},
[](Query query) {
return absl::StrCat(query.name(), " = ",
ToString(query.principal()), " says ",
ToString(query.fact()), "?");
},
[](Program program) {
return absl::StrJoin(
{absl::StrJoin(ToStrings(program.relation_declarations()), "\n"),
absl::StrJoin(ToStrings(program.says_assertions()), "\n"),
absl::StrJoin(ToStrings(program.queries()), "\n")}, "\n");
}},
Node);
}

} // namespace raksha::ir::auth_logic
80 changes: 6 additions & 74 deletions src/ir/auth_logic/ast_string.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

//-------------------------------------------------------------------------------
// Copyright 2022 Google LLC
//
Expand All @@ -25,83 +24,16 @@
#include "src/common/utils/overloaded.h"
#include "src/ir/auth_logic/ast.h"
#include "src/ir/datalog/program.h"
#include "src/common/utils/map_iter.h"

namespace raksha::ir::auth_logic {
std::vector<std::string> binary_operations = {"<", ">", "=", "!=", "<=", ">="};

using AstNodeVariantType =
std::variant<datalog::Predicate, BaseFact, Fact, ConditionalAssertion,
Assertion, datalog::Argument>;
std::variant<datalog::Predicate, datalog::ArgumentType, datalog::Argument,
datalog::RelationDeclaration, Principal, Attribute, CanActAs,
BaseFact, Fact, ConditionalAssertion, Assertion, SaysAssertion,
Query, Program>;

std::string ToString(AstNodeVariantType Node) {
return std::visit(
raksha::utils::overloaded{
[](datalog::Predicate predicate) {
if (std::find(binary_operations.begin(), binary_operations.end(),
predicate.name()) != binary_operations.end()) {
return absl::StrCat(predicate.args().front(), " ",
predicate.name(), " ",
predicate.args().back());
}
return absl::StrCat(
predicate.sign() == datalog::Sign::kNegated ? "!" : "",
predicate.name(), "(", absl::StrJoin(predicate.args(), ", "),
")");
},
[](BaseFact basefact) {
return std::visit(raksha::utils::overloaded{
[](datalog::Predicate predicate) {
return ToString(predicate);
},
[](Attribute attribute) {
return absl::StrJoin(
{attribute.principal().name(),
ToString(attribute.predicate())},
" ");
},
[](CanActAs can_act_as) {
return absl::StrJoin(
{can_act_as.left_principal().name(),
can_act_as.right_principal().name()},
" canActAs ");
}},
basefact.GetValue());
},
[](Fact fact) {
if (fact.delegation_chain().empty()) {
return ToString(fact.base_fact());
}
std::string cansay_string = "";
for (const Principal& delegatees : fact.delegation_chain()) {
cansay_string =
absl::StrJoin({delegatees.name(), cansay_string}, " canSay ");
}
return absl::StrJoin({cansay_string, ToString(fact.base_fact())},
" ");
},
[](ConditionalAssertion conditional_assertion) {
std::string lhs = ToString(conditional_assertion.lhs());
std::vector<std::string> rhs;
for (const BaseFact& base_fact : conditional_assertion.rhs()) {
rhs.push_back(ToString(base_fact));
}
return absl::StrJoin({lhs, absl::StrJoin(rhs, ",")}, " :- ");
},
[](Assertion assertion) {
return std::visit(
raksha::utils::overloaded{
[](Fact fact) { return ToString(fact); },
[](ConditionalAssertion conditional_assertion) {
return ToString(conditional_assertion);
}},
assertion.GetValue());
},
[](datalog::Argument argument) {
return absl::StrJoin(
{argument.argument_name(), argument.argument_type().name()},
" : ");
}},
Node);
}
std::string ToString(AstNodeVariantType Node);

} // namespace raksha::ir::auth_logic
Loading