Skip to content

Commit

Permalink
[NetKAT] Temporarily add basic policy constructors to evaluator.h.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 702448376
  • Loading branch information
jonathan-dilorenzo authored and copybara-github committed Dec 3, 2024
1 parent 2850917 commit 4ef2697
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 2 deletions.
2 changes: 2 additions & 0 deletions netkat/netkat.proto
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ message PredicateProto {
}

// The intermediate representation of a NetKAT policy.
// By convention, uninitialized policies must be treated as "DENY" (i.e. the
// false filter). We call the true filter "ACCEPT".
message PolicyProto {
// A policy is defined as some combination of actions and predicates.
oneof policy {
Expand Down
47 changes: 47 additions & 0 deletions netkat/netkat_proto_constructors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,51 @@ PredicateProto NotProto(PredicateProto negand) {
return proto;
}

// -- Basic Policy constructors ------------------------------------------------

PolicyProto FilterProto(PredicateProto filter) {
PolicyProto policy;
*policy.mutable_filter() = std::move(filter);
return policy;
}

PolicyProto ModificationProto(absl::string_view field, int value) {
PolicyProto policy;
policy.mutable_modification()->set_field(field);
policy.mutable_modification()->set_value(value);
return policy;
}

PolicyProto RecordProto() {
PolicyProto policy;
policy.mutable_record();
return policy;
}

PolicyProto SequenceProto(PolicyProto left, PolicyProto right) {
PolicyProto policy;
*policy.mutable_sequence_op()->mutable_left() = std::move(left);
*policy.mutable_sequence_op()->mutable_right() = std::move(right);
return policy;
}

PolicyProto UnionProto(PolicyProto left, PolicyProto right) {
PolicyProto policy;
*policy.mutable_union_op()->mutable_left() = std::move(left);
*policy.mutable_union_op()->mutable_right() = std::move(right);
return policy;
}

PolicyProto IterateProto(PolicyProto iterable) {
PolicyProto policy;
*policy.mutable_iterate_op()->mutable_iterable() = std::move(iterable);
return policy;
}

// -- Derived Policy constructors ----------------------------------------------

PolicyProto DenyProto() { return FilterProto(FalseProto()); }

PolicyProto AcceptProto() { return FilterProto(TrueProto()); }

} // namespace netkat
14 changes: 12 additions & 2 deletions netkat/netkat_proto_constructors.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,19 @@ PredicateProto AndProto(PredicateProto left, PredicateProto right);
PredicateProto OrProto(PredicateProto left, PredicateProto right);
PredicateProto NotProto(PredicateProto negand);

// -- Policy constructors ------------------------------------------------------
// -- Basic Policy constructors ------------------------------------------------

// TODO - smolkaj: Add policy constructors when needed.
PolicyProto FilterProto(PredicateProto filter);
PolicyProto ModificationProto(absl::string_view field, int value);
PolicyProto RecordProto();
PolicyProto SequenceProto(PolicyProto left, PolicyProto right);
PolicyProto UnionProto(PolicyProto left, PolicyProto right);
PolicyProto IterateProto(PolicyProto iterable);

// -- Derived Policy constructors ----------------------------------------------

PolicyProto DenyProto();
PolicyProto AcceptProto();

} // namespace netkat

Expand Down
60 changes: 60 additions & 0 deletions netkat/netkat_proto_constructors_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,65 @@ void NotProtoReturnsNot(PredicateProto negand) {
}
FUZZ_TEST(NotProtoTest, NotProtoReturnsNot);

// -- Basic Policy constructors ------------------------------------------------

void FilterProtoReturnsFilter(PredicateProto filter) {
PolicyProto expected_policy;
*expected_policy.mutable_filter() = filter;
EXPECT_THAT(FilterProto(filter), EqualsProto(expected_policy));
}
FUZZ_TEST(PolicyProtoTest, FilterProtoReturnsFilter);

void ModificationProtoReturnsModification(std::string field, int value) {
PolicyProto expected_policy;
expected_policy.mutable_modification()->set_field(field);
expected_policy.mutable_modification()->set_value(value);

EXPECT_THAT(ModificationProto(field, value), EqualsProto(expected_policy));
}
FUZZ_TEST(PolicyProtoTest, ModificationProtoReturnsModification);

TEST(PolicyProtoTest, RecordProtoReturnsRecordPolicy) {
EXPECT_THAT(RecordProto(), EqualsProto(R"pb(record {})pb"));
}

void SequenceProtoReturnsSequence(PolicyProto left, PolicyProto right) {
PolicyProto expected_policy;
*expected_policy.mutable_sequence_op()->mutable_left() = left;
*expected_policy.mutable_sequence_op()->mutable_right() = right;

EXPECT_THAT(SequenceProto(left, right), EqualsProto(expected_policy));
}
FUZZ_TEST(PolicyProtoTest, SequenceProtoReturnsSequence);

void UnionProtoReturnsUnion(PolicyProto left, PolicyProto right) {
PolicyProto expected_policy;
*expected_policy.mutable_union_op()->mutable_left() = left;
*expected_policy.mutable_union_op()->mutable_right() = right;

EXPECT_THAT(UnionProto(left, right), EqualsProto(expected_policy));
}
FUZZ_TEST(PolicyProtoTest, UnionProtoReturnsUnion);

void IterateProtoReturnsIterate(PolicyProto iterable) {
PolicyProto expected_policy;
*expected_policy.mutable_iterate_op()->mutable_iterable() = iterable;

EXPECT_THAT(IterateProto(iterable), EqualsProto(expected_policy));
}
FUZZ_TEST(PolicyProtoTest, IterateProtoReturnsIterate);

// -- Derived Policy tests -----------------------------------------------------

TEST(PolicyProtoTest, DenyProtoFiltersOnFalse) {
EXPECT_THAT(DenyProto(),
EqualsProto(R"pb(filter { bool_constant { value: false } })pb"));
}

TEST(PolicyProtoTest, AcceptProtoFiltersOnTrue) {
EXPECT_THAT(AcceptProto(),
EqualsProto(R"pb(filter { bool_constant { value: true } })pb"));
}

} // namespace
} // namespace netkat

0 comments on commit 4ef2697

Please sign in to comment.