From 71d28ef947d3437a85075ab6297f9d9a9bdd2f7c Mon Sep 17 00:00:00 2001 From: Abhijeet Shah Date: Sun, 29 Jul 2018 17:25:49 -0700 Subject: [PATCH] Adding null check for innerException. Adding tests --- .../Az.Auth.Net452.Test.csproj | 37 ++++++++++- .../Az.Auth.Net452.Test/CertAuthTests.cs | 2 +- .../Net452ExceptionsTests.cs | 64 +++++++++++++++++++ .../Az.Auth.Net452.Test/TestBase.cs | 21 +++--- .../Az.Auth.Net452.Test/UserLogin.cs | 7 +- .../Az.Auth.NetCore.Test.csproj | 9 ++- .../Az.Auth.FullDesktop.Test.csproj | 35 +++++++++- .../FullDesktop.Tests/ExceptionsTests.cs | 61 ++++++++++++++++++ .../AuthenticationException.cs | 2 +- ....ClientRuntime.Azure.Authentication.csproj | 2 +- ...est.ClientRuntime.Azure.Authentication.sln | 37 ----------- src/SdkCommon/ClientRuntime.sln | 24 +++++++ 12 files changed, 243 insertions(+), 58 deletions(-) create mode 100644 src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/Net452ExceptionsTests.cs create mode 100644 src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/FullDesktop.Tests/ExceptionsTests.cs delete mode 100644 src/SdkCommon/Auth/Az.Auth/Microsoft.Rest.ClientRuntime.Azure.Authentication.sln diff --git a/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/Az.Auth.Net452.Test.csproj b/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/Az.Auth.Net452.Test.csproj index 691a626a96681..45992ae866dee 100644 --- a/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/Az.Auth.Net452.Test.csproj +++ b/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/Az.Auth.Net452.Test.csproj @@ -1,4 +1,36 @@ - + + + Test Project for Microsoft.Rest.ClientRuntime.Azure.Authentication + 1.0.0 + Az.Auth.Net452.Tests + Az.Auth.Net452.Tests + Net452Tests AzAuthNet452 Test $(NugetCommonTags) $(NugetCommonProfileTags) + + + + net452 + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/CertAuthTests.cs b/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/CertAuthTests.cs index 12a3370daae78..81ffecbd9845b 100644 --- a/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/CertAuthTests.cs +++ b/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/CertAuthTests.cs @@ -11,7 +11,7 @@ namespace Az.Auth.Net452.Test { - public class CertAuthTests : TestBase + public class CertAuthTests : AuthFullDesktopTestBase { public CertAuthTests() { diff --git a/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/Net452ExceptionsTests.cs b/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/Net452ExceptionsTests.cs new file mode 100644 index 0000000000000..27e3d3acc3c81 --- /dev/null +++ b/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/Net452ExceptionsTests.cs @@ -0,0 +1,64 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +namespace Az.Auth.Net452.Test +{ + using Microsoft.IdentityModel.Clients.ActiveDirectory; + using Microsoft.Rest.Azure.Authentication; + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using Xunit; + + public class Net452ExceptionsTests + { + [Fact] + public void AuthExceptionWithInnerException() + { + AuthenticationException authExToBeThrown = null; + string authError = "Error Occured"; + try + { + var token = ApplicationTokenProvider.LoginSilentAsync("SomeDomain", "clientId", "someText").GetAwaiter().GetResult(); + } + catch(AdalException adalEx) + { + authExToBeThrown = new AuthenticationException(authError, adalEx); + } + + Assert.NotNull(authExToBeThrown); + Assert.Equal(authError, authExToBeThrown.Message); + } + +#if FullNetFx + [Fact] + public void AuthExceptionWithNullInnerException() + { + AuthenticationException authExToBeThrown = null; + string authError = "Error Occured"; + try + { + ClientCredential cc = new ClientCredential("SomeClientId", "SomethingSomething"); + ActiveDirectoryServiceSettings adSvcSettings = new ActiveDirectoryServiceSettings() + { + AuthenticationEndpoint = new Uri("https://randomEndPoint"), + TokenAudience = new Uri("https://SomeUri"), + ValidateAuthority = true + }; + + var token = ApplicationTokenProvider.LoginSilentAsync( "SomeDomain", cc, adSvcSettings).GetAwaiter().GetResult(); + } + catch (AdalException adalEx) + { + authExToBeThrown = new AuthenticationException(authError); + } + + Assert.NotNull(authExToBeThrown); + Assert.Equal(authError, authExToBeThrown.Message); + } +#endif + + } +} diff --git a/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/TestBase.cs b/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/TestBase.cs index 7bb47113d88ec..16865b9984a38 100644 --- a/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/TestBase.cs +++ b/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/TestBase.cs @@ -1,19 +1,18 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. namespace Az.Auth.Net452.Test { using Microsoft.Rest.ClientRuntime.Azure.TestFramework; + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading.Tasks; - - public class TestBase + public class AuthFullDesktopTestBase { - //private string LiteralCnnString = string.Empty; - private bool IsLiteralDirty = false; private string _literalCnnString; private ConnectionString _cs; @@ -86,11 +85,11 @@ public TestEndpoints CloudEndPoints } - public TestBase(string connectionString) + public AuthFullDesktopTestBase(string connectionString) { LiteralCnnString = connectionString; } - public TestBase() { LiteralCnnString = string.Empty; } + public AuthFullDesktopTestBase() { LiteralCnnString = string.Empty; } } } diff --git a/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/UserLogin.cs b/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/UserLogin.cs index 90ece309cdd1d..bfef5fc518384 100644 --- a/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/UserLogin.cs +++ b/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.Net452.Test/UserLogin.cs @@ -1,4 +1,7 @@ -namespace Az.Auth.Net452.Test +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +namespace Az.Auth.Net452.Test { using Microsoft.IdentityModel.Clients.ActiveDirectory; using Microsoft.Rest; @@ -9,7 +12,7 @@ using System.Threading; using System.Threading.Tasks; - public class UserLogin: TestBase + public class UserLogin: AuthFullDesktopTestBase { public UserLogin() : base() { diff --git a/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.NetCore.Test/Az.Auth.NetCore.Test.csproj b/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.NetCore.Test/Az.Auth.NetCore.Test.csproj index d4939c4701272..43c8d53fd8b4d 100644 --- a/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.NetCore.Test/Az.Auth.NetCore.Test.csproj +++ b/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/Az.Auth.NetCore.Test/Az.Auth.NetCore.Test.csproj @@ -1,5 +1,12 @@  - + + Test Project for Microsoft.Rest.ClientRuntime.Azure.Authentication + 1.0.0 + Az.Auth.NetCore.Tests + Az.Auth.NetCore.Tests + NetCoreTests AzAuthNetCore Test $(NugetCommonTags) $(NugetCommonProfileTags) + + netcoreapp1.1 diff --git a/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/FullDesktop.Tests/Az.Auth.FullDesktop.Test.csproj b/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/FullDesktop.Tests/Az.Auth.FullDesktop.Test.csproj index 9029ebff202ba..d7bef9c9b8ee0 100644 --- a/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/FullDesktop.Tests/Az.Auth.FullDesktop.Test.csproj +++ b/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/FullDesktop.Tests/Az.Auth.FullDesktop.Test.csproj @@ -1,4 +1,34 @@ - + + + Test Project for Microsoft.Rest.ClientRuntime.Azure.Authentication + 1.0.0 + Az.Auth.FullDesktop.Tests + Az.Auth.FullDesktop.Tests + FullDesktopTests AzAuthFullDesktop Test $(NugetCommonTags) $(NugetCommonProfileTags) + + + + net461 + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/FullDesktop.Tests/ExceptionsTests.cs b/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/FullDesktop.Tests/ExceptionsTests.cs new file mode 100644 index 0000000000000..d7c0d79e84f00 --- /dev/null +++ b/src/SdkCommon/Auth/Az.Auth/Az.Auth.Tests/FullDesktop.Tests/ExceptionsTests.cs @@ -0,0 +1,61 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +namespace Az.Auth.FullDesktop.Test +{ + using Microsoft.IdentityModel.Clients.ActiveDirectory; + using Microsoft.Rest.Azure.Authentication; + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using Xunit; + + public class ExceptionsTests + { + [Fact] + public void AuthExceptionWithInnerException() + { + AuthenticationException authExToBeThrown = null; + string authError = "Error Occured"; + try + { + var token = ApplicationTokenProvider.LoginSilentAsync("SomeDomain", "clientId", "someText").GetAwaiter().GetResult(); + } + catch(AdalException adalEx) + { + authExToBeThrown = new AuthenticationException(authError, adalEx); + } + + Assert.NotNull(authExToBeThrown); + Assert.Equal(authError, authExToBeThrown.Message); + } + + [Fact] + public void AuthExceptionWithNullInnerException() + { + AuthenticationException authExToBeThrown = null; + string authError = "Error Occured"; + try + { + ClientCredential cc = new ClientCredential("SomeClientId", "SomethingSomething"); + ActiveDirectoryServiceSettings adSvcSettings = new ActiveDirectoryServiceSettings() + { + AuthenticationEndpoint = new Uri("https://randomEndPoint"), + TokenAudience = new Uri("https://SomeUri"), + ValidateAuthority = true + }; + + var token = ApplicationTokenProvider.LoginSilentAsync( "SomeDomain", cc, adSvcSettings).GetAwaiter().GetResult(); + } + catch (AdalException adalEx) + { + authExToBeThrown = new AuthenticationException(authError); + } + + Assert.NotNull(authExToBeThrown); + Assert.Equal(authError, authExToBeThrown.Message); + } + } +} diff --git a/src/SdkCommon/Auth/Az.Auth/Az.Authentication/AuthenticationException.cs b/src/SdkCommon/Auth/Az.Auth/Az.Authentication/AuthenticationException.cs index 65aef01df37e0..4036ea77bcd29 100644 --- a/src/SdkCommon/Auth/Az.Auth/Az.Authentication/AuthenticationException.cs +++ b/src/SdkCommon/Auth/Az.Auth/Az.Authentication/AuthenticationException.cs @@ -55,7 +55,7 @@ public AuthenticationException(string message, Exception innerException) /// The exception message /// The inner AdalException with additional details internal AuthenticationException(string message, AdalException innerException) : - base(string.Format(CultureInfo.CurrentCulture, message, innerException.Message), innerException) + base(string.Format(CultureInfo.CurrentCulture, message, (innerException == null)? string.Empty : innerException?.Message ), innerException) { } diff --git a/src/SdkCommon/Auth/Az.Auth/Az.Authentication/Microsoft.Rest.ClientRuntime.Azure.Authentication.csproj b/src/SdkCommon/Auth/Az.Auth/Az.Authentication/Microsoft.Rest.ClientRuntime.Azure.Authentication.csproj index 0b0cae9c9ef90..06e9d6668d31d 100644 --- a/src/SdkCommon/Auth/Az.Auth/Az.Authentication/Microsoft.Rest.ClientRuntime.Azure.Authentication.csproj +++ b/src/SdkCommon/Auth/Az.Auth/Az.Authentication/Microsoft.Rest.ClientRuntime.Azure.Authentication.csproj @@ -21,7 +21,7 @@ - net461;FullNetFx + net461 bin\$(Configuration)\ bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml diff --git a/src/SdkCommon/Auth/Az.Auth/Microsoft.Rest.ClientRuntime.Azure.Authentication.sln b/src/SdkCommon/Auth/Az.Auth/Microsoft.Rest.ClientRuntime.Azure.Authentication.sln deleted file mode 100644 index ee220b313c4d3..0000000000000 --- a/src/SdkCommon/Auth/Az.Auth/Microsoft.Rest.ClientRuntime.Azure.Authentication.sln +++ /dev/null @@ -1,37 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27703.2026 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Rest.ClientRuntime.Azure.Authentication", "Az.Authentication\Microsoft.Rest.ClientRuntime.Azure.Authentication.csproj", "{EE15BED1-9C29-4A8F-9FAC-99EEAEEF6F31}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Az.Auth.NetCore.Test", "Az.Auth.Tests\Az.Auth.NetCore.Test\Az.Auth.NetCore.Test.csproj", "{6CFB9119-B192-44D1-8249-DD85172ABB00}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Az.Auth.Net452.Test", "Az.Auth.Tests\Az.Auth.Net452.Test\Az.Auth.Net452.Test.csproj", "{ACACDA67-5693-40B4-BA2B-6099A38C1562}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {EE15BED1-9C29-4A8F-9FAC-99EEAEEF6F31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {EE15BED1-9C29-4A8F-9FAC-99EEAEEF6F31}.Debug|Any CPU.Build.0 = Debug|Any CPU - {EE15BED1-9C29-4A8F-9FAC-99EEAEEF6F31}.Release|Any CPU.ActiveCfg = Release|Any CPU - {EE15BED1-9C29-4A8F-9FAC-99EEAEEF6F31}.Release|Any CPU.Build.0 = Release|Any CPU - {6CFB9119-B192-44D1-8249-DD85172ABB00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6CFB9119-B192-44D1-8249-DD85172ABB00}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6CFB9119-B192-44D1-8249-DD85172ABB00}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6CFB9119-B192-44D1-8249-DD85172ABB00}.Release|Any CPU.Build.0 = Release|Any CPU - {ACACDA67-5693-40B4-BA2B-6099A38C1562}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {ACACDA67-5693-40B4-BA2B-6099A38C1562}.Debug|Any CPU.Build.0 = Debug|Any CPU - {ACACDA67-5693-40B4-BA2B-6099A38C1562}.Release|Any CPU.ActiveCfg = Release|Any CPU - {ACACDA67-5693-40B4-BA2B-6099A38C1562}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {B8405D09-BF11-4B8C-87CB-E79B47CFA938} - EndGlobalSection -EndGlobal diff --git a/src/SdkCommon/ClientRuntime.sln b/src/SdkCommon/ClientRuntime.sln index 983db0297d3e1..aaf022cf5fd13 100644 --- a/src/SdkCommon/ClientRuntime.sln +++ b/src/SdkCommon/ClientRuntime.sln @@ -26,6 +26,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CR.Azure.FullDesktop.Tests" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Rest.ClientRuntime.Azure.Authentication", "Auth\Az.Auth\Az.Authentication\Microsoft.Rest.ClientRuntime.Azure.Authentication.csproj", "{6D0F8DBE-9B7C-498A-9480-99D7761B852E}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AzAuthTests", "AzAuthTests", "{EB26693C-8683-4AC5-AE23-C85AD07596EB}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Az.Auth.Net452.Test", "Auth\Az.Auth\Az.Auth.Tests\Az.Auth.Net452.Test\Az.Auth.Net452.Test.csproj", "{46DA5ED4-2AA4-46F8-82F0-7770A407A95D}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Az.Auth.NetCore.Test", "Auth\Az.Auth\Az.Auth.Tests\Az.Auth.NetCore.Test\Az.Auth.NetCore.Test.csproj", "{517AF1EC-6328-4AEE-AE1D-EC99540C83D1}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Az.Auth.FullDesktop.Test", "Auth\Az.Auth\Az.Auth.Tests\FullDesktop.Tests\Az.Auth.FullDesktop.Test.csproj", "{6C9628F6-940D-4221-B0C7-9BFB4697BFEF}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -68,6 +76,18 @@ Global {6D0F8DBE-9B7C-498A-9480-99D7761B852E}.Debug|Any CPU.Build.0 = Debug|Any CPU {6D0F8DBE-9B7C-498A-9480-99D7761B852E}.Release|Any CPU.ActiveCfg = Release|Any CPU {6D0F8DBE-9B7C-498A-9480-99D7761B852E}.Release|Any CPU.Build.0 = Release|Any CPU + {46DA5ED4-2AA4-46F8-82F0-7770A407A95D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {46DA5ED4-2AA4-46F8-82F0-7770A407A95D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {46DA5ED4-2AA4-46F8-82F0-7770A407A95D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {46DA5ED4-2AA4-46F8-82F0-7770A407A95D}.Release|Any CPU.Build.0 = Release|Any CPU + {517AF1EC-6328-4AEE-AE1D-EC99540C83D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {517AF1EC-6328-4AEE-AE1D-EC99540C83D1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {517AF1EC-6328-4AEE-AE1D-EC99540C83D1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {517AF1EC-6328-4AEE-AE1D-EC99540C83D1}.Release|Any CPU.Build.0 = Release|Any CPU + {6C9628F6-940D-4221-B0C7-9BFB4697BFEF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6C9628F6-940D-4221-B0C7-9BFB4697BFEF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6C9628F6-940D-4221-B0C7-9BFB4697BFEF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6C9628F6-940D-4221-B0C7-9BFB4697BFEF}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -77,6 +97,10 @@ Global {582890B6-B962-4852-8420-1D7BB68E41E8} = {FC756A84-660A-4917-85D0-0BBFB19FF71C} {99E868D7-7046-4E3C-8B4F-AFF7D9F603AD} = {FC756A84-660A-4917-85D0-0BBFB19FF71C} {CCE671C2-5FCC-4A5B-8D80-0636A04D2E2D} = {FC756A84-660A-4917-85D0-0BBFB19FF71C} + {EB26693C-8683-4AC5-AE23-C85AD07596EB} = {FC756A84-660A-4917-85D0-0BBFB19FF71C} + {46DA5ED4-2AA4-46F8-82F0-7770A407A95D} = {EB26693C-8683-4AC5-AE23-C85AD07596EB} + {517AF1EC-6328-4AEE-AE1D-EC99540C83D1} = {EB26693C-8683-4AC5-AE23-C85AD07596EB} + {6C9628F6-940D-4221-B0C7-9BFB4697BFEF} = {EB26693C-8683-4AC5-AE23-C85AD07596EB} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {097433E2-EDF9-4691-B22B-B66C43F19B1A}