|
14 | 14 | import java.nio.charset.StandardCharsets; |
15 | 15 | import java.security.SecureRandom; |
16 | 16 | import java.time.Instant; |
| 17 | +import java.util.ArrayList; |
17 | 18 | import java.util.Arrays; |
18 | 19 | import java.util.Date; |
19 | 20 | import java.util.HashSet; |
| 21 | +import java.util.List; |
20 | 22 | import java.util.Set; |
21 | 23 | import org.eclipse.biscuit.crypto.KeyPair; |
22 | 24 | import org.eclipse.biscuit.datalog.SymbolTable; |
23 | 25 | import org.eclipse.biscuit.error.Error; |
24 | 26 | import org.eclipse.biscuit.token.Biscuit; |
25 | 27 | import org.eclipse.biscuit.token.builder.Block; |
| 28 | +import org.eclipse.biscuit.token.builder.Check; |
26 | 29 | import org.eclipse.biscuit.token.builder.Expression; |
| 30 | +import org.eclipse.biscuit.token.builder.Rule; |
27 | 31 | import org.eclipse.biscuit.token.builder.Term; |
28 | 32 | import org.eclipse.biscuit.token.builder.Utils; |
| 33 | +import org.eclipse.biscuit.token.builder.parser.Parser; |
29 | 34 | import org.junit.jupiter.api.Test; |
30 | 35 |
|
31 | 36 | public class BuilderTest { |
@@ -143,4 +148,48 @@ public void testArrayValueIsCopy() { |
143 | 148 | System.identityHashCode(term.getValue()), |
144 | 149 | "different objects"); |
145 | 150 | } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testCheckOnlyIncludesQuery() { |
| 154 | + // Built `not_before` check: |
| 155 | + var head = Utils.pred("nbf", List.of(Utils.var("0"), Utils.var("1"))); |
| 156 | + var body = |
| 157 | + List.of( |
| 158 | + Utils.pred("time", List.of(Utils.var("0"))), |
| 159 | + Utils.pred("nbf", List.of(Utils.var("1")))); |
| 160 | + List<Expression> expressions = |
| 161 | + List.of( |
| 162 | + new Expression.Binary( |
| 163 | + Expression.Op.LessOrEqual, |
| 164 | + new Expression.Value(Utils.var("1")), |
| 165 | + new Expression.Value(Utils.var("0")))); |
| 166 | + List<org.eclipse.biscuit.token.builder.Scope> scopes = new ArrayList<>(); |
| 167 | + var nbfRule = new Rule(head, body, expressions, scopes); |
| 168 | + Check builtCheck = Utils.check(nbfRule); |
| 169 | + |
| 170 | + // Parsed `not_before` check: |
| 171 | + var res = Parser.check("check if time($0), nbf($1), $1 <= $0"); |
| 172 | + |
| 173 | + assertEquals(builtCheck, res.getOk()._2); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + public void testInvalidRuleFails() { |
| 178 | + // Head must not include variables that are not in the body |
| 179 | + var head = Utils.pred("nbf", List.of(Utils.var("x"))); |
| 180 | + var body = |
| 181 | + List.of( |
| 182 | + Utils.pred("time", List.of(Utils.var("0"))), |
| 183 | + Utils.pred("nbf", List.of(Utils.var("1")))); |
| 184 | + List<Expression> expressions = |
| 185 | + List.of( |
| 186 | + new Expression.Binary( |
| 187 | + Expression.Op.LessOrEqual, |
| 188 | + new Expression.Value(Utils.var("1")), |
| 189 | + new Expression.Value(Utils.var("0")))); |
| 190 | + List<org.eclipse.biscuit.token.builder.Scope> scopes = new ArrayList<>(); |
| 191 | + var nbfRule = new Rule(head, body, expressions, scopes); |
| 192 | + Block authorityBuilder = new Block(); |
| 193 | + assertTrue(authorityBuilder.addRule(nbfRule, true).isErr()); |
| 194 | + } |
146 | 195 | } |
0 commit comments