-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure open handles will not leak on errors (#6326)
- Loading branch information
Showing
4 changed files
with
115 additions
and
38 deletions.
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
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
83 changes: 83 additions & 0 deletions
83
core/src/test/java/org/owasp/dependencycheck/data/update/nvd/api/NvdApiProcessorTest.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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2014 OWASP. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.owasp.dependencycheck.data.update.nvd.api; | ||
|
||
import com.fasterxml.jackson.core.JsonParseException; | ||
import org.junit.Test; | ||
import org.owasp.dependencycheck.BaseTest; | ||
import org.owasp.dependencycheck.data.nvdcve.CveDB; | ||
import org.owasp.dependencycheck.utils.Settings; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.nio.file.NoSuchFileException; | ||
|
||
import static org.junit.Assert.assertThrows; | ||
|
||
/** | ||
* | ||
* @author Jeremy Long | ||
*/ | ||
public class NvdApiProcessorTest extends BaseTest { | ||
|
||
@Test | ||
public void doesNotExistFile() throws Exception { | ||
try (CveDB cve = new CveDB(getSettings())) { | ||
File file = new File("does_not_exist"); | ||
NvdApiProcessor processor = new NvdApiProcessor(null, file); | ||
assertThrows(NoSuchFileException.class, processor::call); | ||
} | ||
} | ||
|
||
@Test | ||
public void unspecifiedFileName() throws Exception { | ||
try (CveDB cve = new CveDB(getSettings())) { | ||
File file = File.createTempFile("test", "test"); | ||
writeFileString(file, ""); | ||
NvdApiProcessor processor = new NvdApiProcessor(null, file); | ||
processor.call(); | ||
} | ||
} | ||
|
||
@Test | ||
public void invalidFileContent() throws Exception { | ||
try (CveDB cve = new CveDB(getSettings())) { | ||
File file = File.createTempFile("test", "test.json"); | ||
// invalid content (broken array) | ||
writeFileString(file, "[}"); | ||
NvdApiProcessor processor = new NvdApiProcessor(null, file); | ||
assertThrows(JsonParseException.class, processor::call); | ||
} | ||
} | ||
|
||
@Test | ||
public void processValidStructure() throws Exception { | ||
try (CveDB cve = new CveDB(getSettings())) { | ||
File file = File.createTempFile("test", "test.json"); | ||
writeFileString(file, "[]"); | ||
NvdApiProcessor processor = new NvdApiProcessor(null, file); | ||
processor.call(); | ||
} | ||
} | ||
|
||
static void writeFileString(File file, String content) throws IOException { | ||
try (FileWriter writer = new FileWriter(file, false)) { | ||
writer.write(content); | ||
writer.flush(); | ||
} | ||
} | ||
} |