-
Notifications
You must be signed in to change notification settings - Fork 6
test(csharp): coverage push: 82.5% → 85.9% (TLS cert + OpenSession fallback + Statement errors) #59
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
Merged
CurtHagenlocher
merged 2 commits into
adbc-drivers:main
from
CurtHagenlocher:FinalMocks2
May 29, 2026
Merged
Changes from all commits
Commits
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 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
164 changes: 164 additions & 0 deletions
164
csharp/test/AdbcDrivers.Tests.HiveServer2/Common/HiveServer2TlsImplCertChainTests.cs
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,164 @@ | ||
| /* | ||
| * Copyright (c) 2026 ADBC Drivers Contributors | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| #if NET5_0_OR_GREATER | ||
| using System; | ||
| using System.IO; | ||
| using System.Net.Security; | ||
| using System.Security.Authentication; | ||
| using System.Security.Cryptography.X509Certificates; | ||
| using AdbcDrivers.HiveServer2.Hive2; | ||
| using Xunit; | ||
|
|
||
| namespace AdbcDrivers.Tests.HiveServer2.Common | ||
| { | ||
| /// <summary> | ||
| /// Drives <see cref="HiveServer2TlsImpl.ValidateCertificate"/> through | ||
| /// every branch and exercises the <c>UntrustedRoot</c> arm of | ||
| /// <c>ThrowDetailedCertificateError</c> (the one ChainStatus flag a | ||
| /// self-signed cert reliably produces). The other ChainStatus arms | ||
| /// (Revoked, PartialChain, NotTimeValid, etc.) need a fabricated chain | ||
| /// with a trusted root, which isn't portable from a unit test — those | ||
| /// branches stay uncovered in this PR. | ||
| /// </summary> | ||
| public class HiveServer2TlsImplCertChainTests | ||
| { | ||
| [Fact] | ||
| public void NoPolicyErrors_ReturnsTrue() | ||
| { | ||
| // Early-return path — never inspects the cert. | ||
| Assert.True(HiveServer2TlsImpl.ValidateCertificate( | ||
| cert: null, | ||
| policyErrors: SslPolicyErrors.None, | ||
| tlsProperties: new TlsProperties())); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DisableServerCertificateValidation_BypassesChecks() | ||
| { | ||
| // The escape hatch — accepts anything, including a null cert. | ||
| Assert.True(HiveServer2TlsImpl.ValidateCertificate( | ||
| cert: null, | ||
| policyErrors: SslPolicyErrors.RemoteCertificateChainErrors, | ||
| tlsProperties: new TlsProperties { DisableServerCertificateValidation = true })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NullCert_WithChainErrors_ReturnsFalse() | ||
| { | ||
| Assert.False(HiveServer2TlsImpl.ValidateCertificate( | ||
| cert: null, | ||
| policyErrors: SslPolicyErrors.RemoteCertificateChainErrors, | ||
| tlsProperties: new TlsProperties())); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NameMismatch_NotAllowed_ReturnsFalse() | ||
| { | ||
| using X509Certificate2 cert = TestCertificates.SelfSigned(); | ||
| Assert.False(HiveServer2TlsImpl.ValidateCertificate( | ||
| cert, | ||
| SslPolicyErrors.RemoteCertificateNameMismatch, | ||
| new TlsProperties { AllowHostnameMismatch = false })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NameMismatch_Allowed_FallsThroughToChainCheck() | ||
| { | ||
| using X509Certificate2 cert = TestCertificates.SelfSigned(); | ||
| // No chain-error flag, no trusted-cert path → returns true at | ||
| // the "no chain errors" branch. | ||
| Assert.True(HiveServer2TlsImpl.ValidateCertificate( | ||
| cert, | ||
| SslPolicyErrors.RemoteCertificateNameMismatch, | ||
| new TlsProperties { AllowHostnameMismatch = true })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SelfSigned_WithChainErrors_AllowSelfSigned_ReturnsTrue() | ||
| { | ||
| using X509Certificate2 cert = TestCertificates.SelfSigned(); | ||
| Assert.True(HiveServer2TlsImpl.ValidateCertificate( | ||
| cert, | ||
| SslPolicyErrors.RemoteCertificateChainErrors, | ||
| new TlsProperties { AllowSelfSigned = true, RevocationMode = X509RevocationMode.NoCheck })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SelfSigned_WithChainErrors_NoAllowSelfSigned_ThrowsAuthentication() | ||
| { | ||
| using X509Certificate2 cert = TestCertificates.SelfSigned(); | ||
| // ThrowDetailedCertificateError fires here — for a self-signed | ||
| // cert the chain reports UntrustedRoot, hitting the first arm of | ||
| // the per-flag switch. | ||
| Assert.Throws<AuthenticationException>(() => HiveServer2TlsImpl.ValidateCertificate( | ||
| cert, | ||
| SslPolicyErrors.RemoteCertificateChainErrors, | ||
| new TlsProperties { AllowSelfSigned = false, RevocationMode = X509RevocationMode.NoCheck })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TrustedCertificatePath_AllowSelfSigned_ReturnsTrue() | ||
| { | ||
| using X509Certificate2 cert = TestCertificates.SelfSigned(); | ||
| string trustedPath = TestCertificates.SelfSignedAsTempFile(); | ||
| try | ||
| { | ||
| Assert.True(HiveServer2TlsImpl.ValidateCertificate( | ||
| cert, | ||
| SslPolicyErrors.RemoteCertificateChainErrors, | ||
| new TlsProperties | ||
| { | ||
| AllowSelfSigned = true, | ||
| TrustedCertificatePath = trustedPath, | ||
| RevocationMode = X509RevocationMode.NoCheck, | ||
| })); | ||
| } | ||
| finally | ||
| { | ||
| File.Delete(trustedPath); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TrustedCertificatePath_ExpiredCert_NoAllowSelfSigned_Throws() | ||
| { | ||
| // ChainPolicy uses AllowUnknownCertificateAuthority so an unknown | ||
| // root doesn't fail the chain — but NotTimeValid on an expired | ||
| // cert does, regardless of flags. That's enough to fail | ||
| // customChain.Build and fire ThrowDetailedCertificateError. | ||
| using X509Certificate2 cert = TestCertificates.SelfSignedExpired("server-cert"); | ||
| string trustedPath = TestCertificates.SelfSignedAsTempFile("root-cert"); | ||
| try | ||
| { | ||
| Assert.Throws<AuthenticationException>(() => HiveServer2TlsImpl.ValidateCertificate( | ||
| cert, | ||
| SslPolicyErrors.RemoteCertificateChainErrors, | ||
| new TlsProperties | ||
| { | ||
| AllowSelfSigned = false, | ||
| TrustedCertificatePath = trustedPath, | ||
| RevocationMode = X509RevocationMode.NoCheck, | ||
| })); | ||
| } | ||
| finally | ||
| { | ||
| File.Delete(trustedPath); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| #endif |
70 changes: 70 additions & 0 deletions
70
csharp/test/AdbcDrivers.Tests.HiveServer2/Common/TestCertificates.cs
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,70 @@ | ||
| /* | ||
| * Copyright (c) 2026 ADBC Drivers Contributors | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| #if NET5_0_OR_GREATER | ||
| using System; | ||
| using System.IO; | ||
| using System.Security.Cryptography; | ||
| using System.Security.Cryptography.X509Certificates; | ||
|
|
||
| namespace AdbcDrivers.Tests.HiveServer2.Common | ||
| { | ||
| /// <summary> | ||
| /// In-memory X.509 cert generation for TLS-validation tests. Builds a | ||
| /// self-signed cert via <see cref="CertificateRequest"/> — public API | ||
| /// from net5.0 onward, so this helper is conditionally compiled. | ||
| /// </summary> | ||
| internal static class TestCertificates | ||
| { | ||
| /// <summary> | ||
| /// Self-signed cert valid for one day starting now, common name | ||
| /// <c>CN=mock-server</c>. The private key isn't persisted because | ||
| /// these tests only ever validate the public cert. | ||
| /// </summary> | ||
| public static X509Certificate2 SelfSigned(string commonName = "mock-server") => | ||
| BuildSelfSigned(commonName, DateTimeOffset.UtcNow.AddMinutes(-1), DateTimeOffset.UtcNow.AddDays(1)); | ||
|
|
||
| /// <summary>Self-signed cert whose validity window ended yesterday.</summary> | ||
| public static X509Certificate2 SelfSignedExpired(string commonName = "mock-server-expired") => | ||
| BuildSelfSigned(commonName, DateTimeOffset.UtcNow.AddDays(-30), DateTimeOffset.UtcNow.AddDays(-1)); | ||
|
|
||
| /// <summary> | ||
| /// Writes a self-signed cert to a temp <c>.cer</c> file (DER-encoded | ||
| /// public cert, no private key) and returns the path; the caller is | ||
| /// responsible for deleting it. Used to drive the | ||
| /// <c>TrustedCertificatePath</c> branch of ValidateCertificate. | ||
| /// </summary> | ||
| public static string SelfSignedAsTempFile(string commonName = "trusted-root") | ||
| { | ||
| using X509Certificate2 cert = SelfSigned(commonName); | ||
| string path = Path.Combine(Path.GetTempPath(), $"adbc-tls-{Guid.NewGuid():N}.cer"); | ||
| File.WriteAllBytes(path, cert.Export(X509ContentType.Cert)); | ||
| return path; | ||
| } | ||
|
|
||
| private static X509Certificate2 BuildSelfSigned(string commonName, DateTimeOffset notBefore, DateTimeOffset notAfter) | ||
| { | ||
| using RSA rsa = RSA.Create(2048); | ||
| var req = new CertificateRequest( | ||
| $"CN={commonName}", | ||
| rsa, | ||
| HashAlgorithmName.SHA256, | ||
| RSASignaturePadding.Pkcs1); | ||
| return req.CreateSelfSigned(notBefore, notAfter); | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
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.