forked from stratisproject/StratisBitcoinFullNode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request stratisproject#45 from quantumagi/master
Remain up to date with Full Node Activation Branch
- Loading branch information
Showing
81 changed files
with
1,589 additions
and
506 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.Security.Cryptography.X509Certificates; | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Hosting; | ||
using NSubstitute; | ||
using Stratis.Bitcoin.Features.Api; | ||
using Xunit; | ||
|
||
namespace Stratis.Bitcoin.Api.Tests | ||
{ | ||
public class ProgramTest | ||
{ | ||
private readonly X509Certificate2 certificateToUse; | ||
private readonly ICertificateStore certificateStore; | ||
private readonly ApiSettings apiSettings; | ||
private readonly IWebHostBuilder webHostBuilder; | ||
|
||
private X509Certificate2 certificateRetrieved; | ||
|
||
public ProgramTest() | ||
{ | ||
this.apiSettings = new ApiSettings { UseHttps = true }; | ||
this.certificateToUse = new X509Certificate2(); | ||
this.certificateStore = Substitute.For<ICertificateStore>(); | ||
this.webHostBuilder = Substitute.For<IWebHostBuilder>(); | ||
} | ||
|
||
[Fact] | ||
public void Initialize_WhenCertificateRetrieved_UsesCertificateOnHttpsWithKestrel() | ||
{ | ||
this.apiSettings.UseHttps = true; | ||
this.SetCertificateInStore(true); | ||
|
||
this.certificateRetrieved.Should().BeNull(); | ||
|
||
Program.Initialize(null, new FullNode(), this.apiSettings, this.certificateStore, this.webHostBuilder); | ||
|
||
this.certificateRetrieved.Should().NotBeNull(); | ||
this.certificateRetrieved.Should().Be(this.certificateToUse); | ||
this.certificateStore.ReceivedWithAnyArgs(1).TryGet(null, out _); | ||
} | ||
|
||
[Fact] | ||
public void Initialize_WhenNotUsing_Https_ShouldNotLookForCertificates() | ||
{ | ||
this.apiSettings.UseHttps = false; | ||
this.SetCertificateInStore(true); | ||
|
||
Program.Initialize(null, new FullNode(), this.apiSettings, this.certificateStore, this.webHostBuilder); | ||
|
||
this.certificateStore.DidNotReceiveWithAnyArgs().TryGet(null, out _); | ||
} | ||
|
||
private void SetCertificateInStore(bool isCertInStore) | ||
{ | ||
this.certificateStore.TryGet(this.apiSettings.HttpsCertificateFilePath, out this.certificateRetrieved) | ||
.Returns(isCertInStore) | ||
.AndDoes(_ => { this.certificateRetrieved = this.certificateToUse; }); | ||
} | ||
} | ||
} |
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
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,34 @@ | ||
using System; | ||
using System.IO; | ||
using System.Security.Cryptography.X509Certificates; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Stratis.Bitcoin.Features.Api | ||
{ | ||
public class CertificateStore : ICertificateStore | ||
{ | ||
private readonly ILogger logger; | ||
|
||
public CertificateStore(ILoggerFactory loggerFactory) | ||
{ | ||
this.logger = loggerFactory.CreateLogger(this.GetType().FullName); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool TryGet(string filePath, out X509Certificate2 certificate) | ||
{ | ||
try | ||
{ | ||
var fileInBytes = File.ReadAllBytes(filePath); | ||
certificate = new X509Certificate2(fileInBytes); | ||
return true; | ||
} | ||
catch (Exception e) | ||
{ | ||
this.logger.LogWarning("Failed to read certificate at {0} : {1}", filePath, e.Message); | ||
certificate = null; | ||
return false; | ||
} | ||
} | ||
} | ||
} |
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,18 @@ | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
namespace Stratis.Bitcoin.Features.Api | ||
{ | ||
/// <summary> | ||
/// An interface providing operations on certificate repositories. | ||
/// </summary> | ||
public interface ICertificateStore | ||
{ | ||
/// <summary> | ||
/// Tries to retrieve a certificate from the file system. | ||
/// </summary> | ||
/// <param name="filePath">The full path of the certificate file.</param> | ||
/// <param name="certificate">The certificate, if found.</param> | ||
/// <returns>A value indicating whether or not the certificate has been found at the specified location.</returns> | ||
bool TryGet(string filePath, out X509Certificate2 certificate); | ||
} | ||
} |
Oops, something went wrong.