Skip to content
Open
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
12 changes: 12 additions & 0 deletions src/test/fixtures/syntax-error/deny-expecting-allow/Main.hp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Syntax;

main =
DENY
EXCEPT {
ALLOW {
Actors: Analyst
Resources
Actions
}
DENY Syntax::alice
};
21 changes: 21 additions & 0 deletions src/test/fixtures/syntax-error/deny-expecting-allow/Syntax.hp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export Syntax where

data Actors =
Looker(Analyst),
Analyst(Alice, Bob),
Intern(Bob, Jeff),
Alice, Bob, Jeff;

data Actions = Reads, Deletes, Updates;

data Resources =
Claims(Finance),
Finance(Customers, Companies),
Customers(CCN), Companies(EMAIL, SSN),
CCN, EMAIL, SSN;

alice = DENY {
Actors: Alice
Resources: CCN
Actions: Reads
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export ImportedVariableError where

data Actors =
Looker(Analyst),
Analyst(Alice, Bob),
Intern(Bob, Jeff),
Alice, Bob, Jeff;

data Actions = Reads, Deletes, Updates;

data Resources =
Claims(Finance),
Finance(Customers, Companies),
Customers(CCN), Companies(EMAIL, SSN),
CCN, EMAIL, SSN;

alice = DENY {
Actors: Alice
Resources: CCN
Actions: Reads
};
19 changes: 19 additions & 0 deletions src/test/fixtures/syntax-error/imported-variable/Main.hp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import ImportedVariableError;

main =
DENY
EXCEPT {
ALLOW {
Actors: Analyst
Resources
Actions
}
EXCEPT {
DENY {
Actors: Bob
Resources: EMAIL
Actions: Deletes, Updates, Reads
}
}
ALLOW ImportedVariableError::alice
};
18 changes: 18 additions & 0 deletions src/test/fixtures/visitor/nested-excepts/Main.hp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import NestedExcepts;

main =
DENY
EXCEPT {
ALLOW {
Actors: Analyst
Resources
Actions
}
EXCEPT {
DENY {
Actors: Bob
Resources: EMAIL
Actions: Deletes, Updates, Reads
}
}
};
15 changes: 15 additions & 0 deletions src/test/fixtures/visitor/nested-excepts/NestedExcepts.hp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export NestedExcepts where

data Actors =
Looker(Analyst),
Analyst(Alice, Bob),
Intern(Bob, Jeff),
Alice, Bob, Jeff;

data Actions = Reads, Deletes, Updates;

data Resources =
Claims(Finance),
Finance(Customers, Companies),
Customers(CCN), Companies(EMAIL, SSN),
CCN, EMAIL, SSN;
12 changes: 12 additions & 0 deletions src/test/fixtures/wrong-name/undefined-attribute/Main.hp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import UndefinedAttribute;

main =
DENY
EXCEPT {
ALLOW {
Actors: Analyst
Resources
Actions
}
ALLOW Syntax::jeff
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export UndefinedAttribute where

data Actors =
Looker(Analyst),
Analyst(Alice, Bob),
Intern(Bob, Jeff),
Alice, Bob, Jeff;

data Actions = Reads, Deletes, Updates;

data Resources =
Claims(Finance),
Finance(Customers, Companies),
Customers(CCN), Companies(EMAIL, SSN),
CCN, EMAIL, SSN;

jeff = ALLOW {
Actors: Jefa // Undefined attribute error
Resources: CCN
Actions: Reads
};
34 changes: 34 additions & 0 deletions src/test/kotlin/unit/IRVisitorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,39 @@ class IRVisitorTest {
val exception = assertFailsWith<Exception>{ irFromString(programs.get(i), listOf("Prop")) }
assertEquals(messages.get(i), exception.message)
}

@Test
@DisplayName("Should receive an undefined attribute error")
fun shouldReceiveUndefinedAttributeError() {
val file = "src/test/fixtures/wrong-name/undefined-attribute/Main.hp"
val priority = listOf("Actors", "Actions", "Resources")
val exception = assertFailsWith<Exception> { irFromFile(file, priority) }
assertEquals("undefined attribute: Syntax::jeff::Actors = Jefa", exception.message)
}

@Test
@DisplayName("Should return a syntax error on imported variable: DENY after an ALLOW")
fun shouldReturnSyntaxErrorImportedVariable() {
val file = "src/test/fixtures/syntax-error/imported-variable/Main.hp"
val priority = listOf("Actors", "Actions", "Resources")

val exception = assertFailsWith<Exception> {
irFromFile(file, priority)
}

assertEquals("line 29:10 syntax error 'DENY' after an 'ALLOW'", exception.message)

}

@Test
@DisplayName("Should generate the correct IR even with multiple 'EXCEPT' nested")
fun shouldDenyBobActionsOnEmail() {
val file = "src/test/fixtures/visitor/nested-excepts/Main.hp"
val priority = listOf("Actors", "Actions", "Resources")
val castelo = "teste"
val ir = irFromFile(file, priority)
val expectedIRString = "{Bob={Updates=[SSN, CCN], Deletes=[SSN, CCN], Reads=[SSN, CCN]}, Alice={Updates=[SSN, EMAIL, CCN], Deletes=[SSN, EMAIL, CCN], Reads=[SSN, EMAIL, CCN]}}"
assertThat(ir.toString()).isEqualTo(expectedIRString)
}
}
}
11 changes: 10 additions & 1 deletion src/test/kotlin/unit/ParsingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ParsingTest {
}

@Test
@DisplayName("Should throw ParseCancellationException on syntax rrror")
@DisplayName("Should throw ParseCancellationException on syntax error")
fun shouldThrowParseCancellationExceptionOnSyntaxError(){
val program =
"""
Expand All @@ -55,6 +55,15 @@ class ParsingTest {
val error = "line 6:10 mismatched input 'INVALID' expecting 'ALLOW'"
assertFailsWith<ParseCancellationException>(error) { parse(tokenize(program)) }
}

@Test
@DisplayName("Should return 'mismatched input' on syntax error: input 'DENY' expecting 'ALLOW' or 'EXCEPT'")
fun shouldReturnSyntaxErrorDenyExpectingAllow() {
val file = "src/test/fixtures/syntax-error/deny-expecting-allow/Main.hp"
val error = "line 24:4 mismatched input 'DENY' expecting 'ALLOW'"
assertFailsWith<ParseCancellationException>(error) { parse(tokenize(file)) }
}

@Test
@DisplayName("Should parse valid attribute expressions")
fun shouldParseValidAttributeExpression(){
Expand Down