-
Notifications
You must be signed in to change notification settings - Fork 476
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
Change to use snakeyaml with SafeConstructor #1018
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
This file contains 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 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
125 changes: 53 additions & 72 deletions
125
...nder-controller/src/test/java/org/ngrinder/script/service/GitHubFileEntryServiceTest.java
This file contains 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 |
---|---|---|
@@ -1,88 +1,69 @@ | ||
package org.ngrinder.script.service; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.ngrinder.AbstractNGrinderTransactionalTest; | ||
import org.ngrinder.common.util.CompressionUtils; | ||
import org.ngrinder.model.User; | ||
import org.ngrinder.common.exception.NGrinderRuntimeException; | ||
import org.ngrinder.script.model.FileEntry; | ||
import org.ngrinder.script.model.GitHubConfig; | ||
import org.ngrinder.script.repository.MockFileEntityRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.yaml.snakeyaml.constructor.ConstructorException; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Set; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.hasItems; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class GitHubFileEntryServiceTest extends AbstractNGrinderTransactionalTest { | ||
@Autowired | ||
private GitHubFileEntryService gitHubFileEntryService; | ||
|
||
private static final String GITHUB_CONFIG_NAME = ".gitconfig.yml"; | ||
|
||
@Autowired | ||
private GitHubFileEntryService gitHubFileEntryService; | ||
|
||
@Autowired | ||
private MockFileEntityRepository mockFileEntityRepository; | ||
@Test | ||
public void testValidateInvalidConfigNameLength() { | ||
FileEntry fileEntry = new FileEntry(); | ||
fileEntry.setContent( | ||
"name: My Long Long Long Long Long Long Github Config Name\n" + | ||
"owner: naver\n" + | ||
"repo: ngrinder\n" + | ||
"access-token: e1a47e652762b60a...3ddc0713b07g13k\n" | ||
); | ||
fileEntry.setRevision(-1L); | ||
|
||
@Before | ||
public void before() throws IOException { | ||
File file = new File(System.getProperty("java.io.tmpdir"), "repo"); | ||
FileUtils.deleteQuietly(file); | ||
CompressionUtils.unzip(new ClassPathResource("TEST_USER.zip").getFile(), file); | ||
mockFileEntityRepository.setUserRepository(new File(file, getTestUser().getUserId())); | ||
} | ||
try { | ||
gitHubFileEntryService.validate(fileEntry); | ||
} catch (Exception e) { | ||
assertTrue(e instanceof NGrinderRuntimeException); | ||
assertTrue(e.getMessage().contains("Configuration name must be shorter than")); | ||
} | ||
} | ||
|
||
@Test | ||
public void getAllGitHubConfig() throws Exception { | ||
User testUser = getTestUser(); | ||
@Test | ||
public void testValidateInvalidYamlValue() { | ||
FileEntry fileEntry = new FileEntry(); | ||
fileEntry.setContent( | ||
"!!com.sun.rowset.JdbcRowSetImpl\n " + | ||
"dataSourceName: rmi://127.0.0.1:13243/jmxrmi\n " + | ||
"autoCommit: true" | ||
); | ||
fileEntry.setRevision(-1L); | ||
|
||
FileEntry fileEntry = new FileEntry(); | ||
fileEntry.setContent("name: My Github Config\n" + | ||
"owner: naver\n" + | ||
"repo: ngrinder\n" + | ||
"access-token: e1a47e652762b60a...3ddc0713b07g13k\n" + | ||
"---\n" + | ||
"name: Another Config\n" + | ||
"owner: naver\n" + | ||
"repo: pinpoint\n" + | ||
"access-token: t9a47e6ff262b60a...3dac0713b07e82a\n" + | ||
"branch: feature/add-ngrinder-scripts\n" + | ||
"base-url: https://api.mygithub.com\n" + | ||
"script-root: ngrinder-scripts\n" | ||
); | ||
fileEntry.setEncoding("UTF-8"); | ||
fileEntry.setPath(GITHUB_CONFIG_NAME); | ||
fileEntry.setDescription("for unit test"); | ||
mockFileEntityRepository.save(testUser, fileEntry, fileEntry.getEncoding()); | ||
try { | ||
gitHubFileEntryService.validate(fileEntry); | ||
} catch (Exception e) { | ||
assertTrue(e instanceof ConstructorException); | ||
assertTrue(e.getMessage().contains("could not determine a constructor for the tag")); | ||
} | ||
} | ||
|
||
Set<GitHubConfig> gitHubConfigs = gitHubFileEntryService.getAllGitHubConfig(testUser); | ||
@Test | ||
public void testValidateInvalidYamlValue2() { | ||
FileEntry fileEntry = new FileEntry(); | ||
fileEntry.setContent( | ||
"some_var: !!javax.script.ScriptEngineManager " + | ||
"[!!java.net.URLClassLoader [[!!java.net.URL [\"http://localhost:8080\"]]]]" | ||
); | ||
fileEntry.setRevision(-1L); | ||
|
||
assertThat(gitHubConfigs.size(), is(2)); | ||
assertThat( | ||
gitHubConfigs, | ||
hasItems( | ||
GitHubConfig.builder() | ||
.name("My Github Config") | ||
.owner("naver") | ||
.repo("ngrinder") | ||
.accessToken("e1a47e652762b60a...3ddc0713b07g13k") | ||
.build(), | ||
GitHubConfig.builder() | ||
.name("Another Config") | ||
.owner("naver") | ||
.repo("pinpoint") | ||
.accessToken("t9a47e6ff262b60a...3dac0713b07e82a") | ||
.branch("feature/add-ngrinder-scripts") | ||
.baseUrl("https://api.mygithub.com") | ||
.scriptRoot("ngrinder-scripts") | ||
.build() | ||
) | ||
); | ||
} | ||
try { | ||
gitHubFileEntryService.validate(fileEntry); | ||
} catch (Exception e) { | ||
assertTrue(e instanceof ConstructorException); | ||
assertTrue(e.getMessage().contains("could not determine a constructor for the tag")); | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, the one thing that changed is here. Use the SafeConstructor.