Skip to content
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

JRE cache: NRE on failed download response #2062

Merged
merged 1 commit into from
Jul 17, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,30 @@ public async Task Download_DownloadFileNew_Failure_Download()
streamAccess.Should().Throw<ObjectDisposedException>("FileStream should be closed after failure.");
}

[TestMethod]
public async Task Download_DownloadFileNew_Download_NullStream()
{
var home = @"C:\Users\user\.sonar";
var cache = $@"{home}\cache";
var sha = $@"{cache}\sha256";
directoryWrapper.Exists(cache).Returns(true);
directoryWrapper.Exists(sha).Returns(true);
directoryWrapper.GetRandomFileName().Returns("xFirst.rnd");
fileWrapper.Exists($@"{sha}\filename.tar.gz").Returns(false);
var fileContentStream = new MemoryStream();
fileWrapper.Create(Path.Combine(sha, "xFirst.rnd")).Returns(fileContentStream);

var sut = CreateSutWithSubstitutes();
var result = await sut.DownloadJreAsync(home, new("filename.tar.gz", "sha256", "javaPath"), () => Task.FromResult<Stream>(null));
result.Should().BeOfType<JreCacheFailure>().Which.Message.Should().Be(
"The download of the Java runtime environment from the server failed with the exception 'The download stream is null. The server likely returned an error status code.'.");
fileWrapper.Received(1).Create(Path.Combine(sha, "xFirst.rnd"));
fileWrapper.Received(1).Delete(Path.Combine(sha, "xFirst.rnd"));
fileWrapper.DidNotReceive().Move(Path.Combine(sha, "xFirst.rnd"), Path.Combine(sha, "filename.tar.gz"));
var streamAccess = () => fileContentStream.Position;
streamAccess.Should().Throw<ObjectDisposedException>("FileStream should be closed after failure.");
}

[DataTestMethod]
[DynamicData(nameof(DirectoryAndFileCreateAndMoveExceptions))]
public async Task Download_DownloadFileNew_Failure_Move(Type exceptionType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ private async Task<Exception> DownloadAndValidateJre(string jreDownloadPath, str
try
{
using var downloadStream = await jreDownload();
if (downloadStream is null)
{
throw new InvalidOperationException(Resources.ERR_JreDownloadStreamNull);
}
await downloadStream.CopyToAsync(fileStream);
fileStream.Close();
if (ValidateChecksum(tempFile, sha256))
Expand Down
9 changes: 9 additions & 0 deletions src/SonarScanner.MSBuild.PreProcessor/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/SonarScanner.MSBuild.PreProcessor/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -508,4 +508,7 @@ If the intention was to connect to the local SonarQube instance, please add the
<data name="MSG_FilePermissionsCopyFailed" xml:space="preserve">
<value>There was an error when trying to set permissions for '{0}'. {1}</value>
</data>
<data name="ERR_JreDownloadStreamNull" xml:space="preserve">
<value>The download stream is null. The server likely returned an error status code.</value>
</data>
</root>
Loading