-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(behavior): reject null children in behavior tree JSON at load time #5366
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
Open
soloturn
wants to merge
6
commits into
develop
Choose a base branch
from
fix/behavior-tree-null-child-validation
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
851d0b9
fix(behavior): reject null children in behavior tree JSON at load time
soloturn 5682fe6
fix(behavior): isolate a malformed tree's load failure to that asset
soloturn 2323f1d
test(behavior): cover a decorator with a missing child entry
soloturn bfa8239
feat(behavior): add a repair tool for the trailing-comma bug
soloturn 78ae9ff
fix(behavior): make the repair tool crash-safe and backup-safe
soloturn 002e2cf
fix(checkstyle): move BehaviorTreeRepairTool.Result to the end of the…
soloturn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...ests/src/test/java/org/terasology/engine/logic/behavior/asset/BehaviorTreeFormatTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright 2021 The Terasology Foundation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package org.terasology.engine.logic.behavior.asset; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.terasology.gestalt.assets.ResourceUrn; | ||
| import org.terasology.gestalt.assets.format.AssetDataFile; | ||
| import org.terasology.gestalt.module.resources.FileReference; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * Regression coverage for https://github.com/MovingBlocks/Terasology/issues/5099. A malformed behavior tree must | ||
| * fail this single asset's load with the checked {@link IOException} the {@code AssetFileFormat} contract expects | ||
| * - that's what lets gestalt's asset-loading machinery isolate the failure to just this asset (log it, move on) | ||
| * instead of an unchecked exception blowing past that safety net and crashing whatever triggered the load. | ||
| */ | ||
| public class BehaviorTreeFormatTest { | ||
|
|
||
| @Test | ||
| public void malformedTreeFailsWithCheckedIOExceptionNotAnUncheckedOne() { | ||
| BehaviorTreeFormat format = new BehaviorTreeFormat(); | ||
| ResourceUrn urn = new ResourceUrn("engine:malformed"); | ||
| List<AssetDataFile> source = Collections.singletonList(new AssetDataFile(jsonFile("{ selector: [success, null, success] }"))); | ||
|
|
||
| IOException exception = assertThrows(IOException.class, () -> format.load(urn, source)); | ||
|
|
||
| assertTrue(exception.getMessage().contains("engine:malformed")); | ||
| assertTrue(exception.getMessage().contains("selector")); | ||
| } | ||
|
|
||
| private FileReference jsonFile(String json) { | ||
| return new FileReference() { | ||
| @Override | ||
| public String getName() { | ||
| return "malformed.behavior"; | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> getPath() { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| @Override | ||
| public InputStream open() { | ||
| return new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); | ||
| } | ||
| }; | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
...ests/src/test/java/org/terasology/engine/logic/behavior/core/BehaviorTreeBuilderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright 2021 The Terasology Foundation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package org.terasology.engine.logic.behavior.core; | ||
|
|
||
| import com.google.gson.JsonParseException; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.terasology.engine.logic.behavior.actions.InvertAction; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * Regression coverage for https://github.com/MovingBlocks/Terasology/issues/5099: a malformed behavior tree | ||
| * (e.g. a stray/trailing comma producing a null array entry) used to parse "successfully" with a silent | ||
| * {@code null} child, only to crash later with an unrelated NPE deep in tree execution/copying | ||
| * ({@link org.terasology.engine.logic.behavior.core.SelectorNode#deepCopy()} or | ||
| * {@link org.terasology.engine.logic.behavior.DefaultBehaviorTreeRunner}). It should instead fail fast, at | ||
| * load time, with a message that points at the malformed tree. | ||
| */ | ||
| public class BehaviorTreeBuilderTest { | ||
|
|
||
| private final BehaviorTreeBuilder builder = new BehaviorTreeBuilder(); | ||
|
|
||
| @Test | ||
| public void validTreeStillParses() { | ||
| BehaviorNode node = builder.fromJson("{ selector: [success, failure, success] }"); | ||
|
|
||
| assertEquals(3, node.getChildrenCount()); | ||
| } | ||
|
|
||
| @Test | ||
| public void nullChildInCompositeArrayFailsLoudlyInsteadOfLater() { | ||
| JsonParseException exception = assertThrows(JsonParseException.class, | ||
| () -> builder.fromJson("{ selector: [success, null, success] }")); | ||
|
|
||
| assertTrue(exception.getMessage().contains("selector")); | ||
| assertTrue(exception.getMessage().contains("index 1")); | ||
| } | ||
|
|
||
| @Test | ||
| public void nullChildInDecoratorFailsLoudlyInsteadOfLater() { | ||
| builder.registerDecorator("invert", InvertAction.class); | ||
|
|
||
| JsonParseException exception = assertThrows(JsonParseException.class, | ||
| () -> builder.fromJson("{ invert: { child: null } }")); | ||
|
|
||
| assertTrue(exception.getMessage().contains("invert")); | ||
| } | ||
|
|
||
| @Test | ||
| public void missingChildInDecoratorFailsLoudlyInsteadOfLater() { | ||
| builder.registerDecorator("invert", InvertAction.class); | ||
|
|
||
| JsonParseException exception = assertThrows(JsonParseException.class, | ||
| () -> builder.fromJson("{ invert: {} }")); | ||
|
|
||
| assertTrue(exception.getMessage().contains("invert")); | ||
| } | ||
| } | ||
95 changes: 95 additions & 0 deletions
95
...s/src/test/java/org/terasology/engine/logic/behavior/core/BehaviorTreeRepairToolTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // Copyright 2021 The Terasology Foundation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package org.terasology.engine.logic.behavior.core; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * See https://github.com/MovingBlocks/Terasology/issues/5099. The tool exists so a content author who hits the | ||
| * load-time rejection from {@link BehaviorTreeBuilder} doesn't have to hand-edit the file's JSON to find and | ||
| * remove the offending comma - it does the same fix mechanically. | ||
| */ | ||
| public class BehaviorTreeRepairToolTest { | ||
|
|
||
| @Test | ||
| public void trailingCommaInCompositeArrayIsRemoved() { | ||
| BehaviorTreeRepairTool.Result result = BehaviorTreeRepairTool.clean( | ||
| "{ \"selector\": [\"success\", \"success\",] }"); | ||
|
|
||
| assertTrue(result.changed); | ||
| assertEquals(1, result.nullArrayEntriesRemoved); | ||
| assertTrue(result.unfixableIssues.isEmpty()); | ||
| // The cleaned JSON should load through the real builder with no custom actions/decorators needed. | ||
| BehaviorNode node = new BehaviorTreeBuilder().fromJson(result.cleanedJson); | ||
| assertEquals(2, node.getChildrenCount()); | ||
| } | ||
|
|
||
| @Test | ||
| public void explicitNullInCompositeArrayIsRemoved() { | ||
| BehaviorTreeRepairTool.Result result = BehaviorTreeRepairTool.clean( | ||
| "{ \"selector\": [\"success\", null, \"success\"] }"); | ||
|
|
||
| assertTrue(result.changed); | ||
| assertEquals(1, result.nullArrayEntriesRemoved); | ||
| BehaviorNode node = new BehaviorTreeBuilder().fromJson(result.cleanedJson); | ||
| assertEquals(2, node.getChildrenCount()); | ||
| } | ||
|
|
||
| @Test | ||
| public void nothingToFixIsReportedAsUnchanged() { | ||
| BehaviorTreeRepairTool.Result result = BehaviorTreeRepairTool.clean( | ||
| "{ \"selector\": [\"success\", \"success\"] }"); | ||
|
|
||
| assertFalse(result.changed); | ||
| assertEquals(0, result.nullArrayEntriesRemoved); | ||
| assertTrue(result.unfixableIssues.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| public void explicitNullDecoratorChildIsReportedNotSilentlyDropped() { | ||
| BehaviorTreeRepairTool.Result result = BehaviorTreeRepairTool.clean( | ||
| "{ \"invert\": { \"child\": null } }"); | ||
|
|
||
| assertFalse(result.unfixableIssues.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| public void repairWritesTheFileAndKeepsABackup(@TempDir Path dir) throws IOException { | ||
| Path file = dir.resolve("broken.behavior"); | ||
| String original = "{ \"selector\": [\"success\", \"success\",] }"; | ||
| Files.write(file, original.getBytes(StandardCharsets.UTF_8)); | ||
|
|
||
| BehaviorTreeRepairTool.Result result = BehaviorTreeRepairTool.repair(file); | ||
|
|
||
| assertTrue(result.changed); | ||
| Path backup = dir.resolve("broken.behavior.bak"); | ||
| assertTrue(Files.exists(backup)); | ||
| assertEquals(original, new String(Files.readAllBytes(backup), StandardCharsets.UTF_8)); | ||
| BehaviorNode node = new BehaviorTreeBuilder().fromJson( | ||
| new String(Files.readAllBytes(file), StandardCharsets.UTF_8)); | ||
| assertEquals(2, node.getChildrenCount()); | ||
| } | ||
|
|
||
| @Test | ||
| public void repairLeavesUnfixableFileUntouched(@TempDir Path dir) throws IOException { | ||
| Path file = dir.resolve("broken.behavior"); | ||
| String original = "{ \"invert\": { \"child\": null } }"; | ||
| Files.write(file, original.getBytes(StandardCharsets.UTF_8)); | ||
|
|
||
| BehaviorTreeRepairTool.Result result = BehaviorTreeRepairTool.repair(file); | ||
|
|
||
| assertFalse(result.changed); | ||
| assertFalse(Files.exists(dir.resolve("broken.behavior.bak"))); | ||
| assertEquals(original, new String(Files.readAllBytes(file), StandardCharsets.UTF_8)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.