Skip to content

Commit

Permalink
Fail on unknown properties (#2837)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone authored Oct 2, 2024
1 parent f35ad49 commit 3babd2e
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 19 deletions.
21 changes: 20 additions & 1 deletion csharp/msbuild/ice.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Microsoft Visual Studio Solution File, Format Version 12.00

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34321.82
MinimumVisualStudioVersion = 15.0.26124.0
Expand Down Expand Up @@ -402,6 +403,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "structure", "structure", "{
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "client", "..\test\Slice\structure\msbuild\client\client.csproj", "{E38F04A6-A338-4B5C-A04D-D80DCADD0BA6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "unknownProperties", "unknownProperties", "{BC295CD8-BE44-46BC-B6B7-5626253DC08B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "client", "..\test\Ice\unknownProperties\msbuild\client\client.csproj", "{EA4F82F9-A851-4275-B4E6-7D16390C1857}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -2008,6 +2013,18 @@ Global
{E38F04A6-A338-4B5C-A04D-D80DCADD0BA6}.Release|x64.Build.0 = Release|Any CPU
{E38F04A6-A338-4B5C-A04D-D80DCADD0BA6}.Release|x86.ActiveCfg = Release|Any CPU
{E38F04A6-A338-4B5C-A04D-D80DCADD0BA6}.Release|x86.Build.0 = Release|Any CPU
{EA4F82F9-A851-4275-B4E6-7D16390C1857}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EA4F82F9-A851-4275-B4E6-7D16390C1857}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA4F82F9-A851-4275-B4E6-7D16390C1857}.Debug|x64.ActiveCfg = Debug|Any CPU
{EA4F82F9-A851-4275-B4E6-7D16390C1857}.Debug|x64.Build.0 = Debug|Any CPU
{EA4F82F9-A851-4275-B4E6-7D16390C1857}.Debug|x86.ActiveCfg = Debug|Any CPU
{EA4F82F9-A851-4275-B4E6-7D16390C1857}.Debug|x86.Build.0 = Debug|Any CPU
{EA4F82F9-A851-4275-B4E6-7D16390C1857}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA4F82F9-A851-4275-B4E6-7D16390C1857}.Release|Any CPU.Build.0 = Release|Any CPU
{EA4F82F9-A851-4275-B4E6-7D16390C1857}.Release|x64.ActiveCfg = Release|Any CPU
{EA4F82F9-A851-4275-B4E6-7D16390C1857}.Release|x64.Build.0 = Release|Any CPU
{EA4F82F9-A851-4275-B4E6-7D16390C1857}.Release|x86.ActiveCfg = Release|Any CPU
{EA4F82F9-A851-4275-B4E6-7D16390C1857}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -2211,6 +2228,8 @@ Global
{80F2E553-D076-400F-9B63-B041F6A06CB0} = {638C9ADB-C42B-4313-B888-A80E4166F785}
{F89683E8-48F2-4374-A606-2ABC73B10492} = {C706F35C-0412-4534-86F6-D8F7D13C7803}
{E38F04A6-A338-4B5C-A04D-D80DCADD0BA6} = {F89683E8-48F2-4374-A606-2ABC73B10492}
{BC295CD8-BE44-46BC-B6B7-5626253DC08B} = {B59F5DBE-A580-4501-95FB-DA2F9E03D003}
{EA4F82F9-A851-4275-B4E6-7D16390C1857} = {BC295CD8-BE44-46BC-B6B7-5626253DC08B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8BB4EF8D-31A9-4DBC-AD9F-164CF6C083B6}
Expand Down
6 changes: 3 additions & 3 deletions csharp/src/Ice/Internal/MetricsAdminI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -690,17 +690,17 @@ public static void validateProperties(string prefix, Ice.Properties properties)
}
}

if (unknownProps.Count != 0 && properties.getIcePropertyAsInt("Ice.Warn.UnknownProperties") > 0)
if (unknownProps.Count != 0)
{
StringBuilder message = new StringBuilder("found unknown IceMX properties for `");
StringBuilder message = new StringBuilder("Found unknown IceMX properties for '");
message.Append(prefix.AsSpan(0, prefix.Length - 1));
message.Append("':");
foreach (string p in unknownProps)
{
message.Append("\n ");
message.Append(p);
}
Ice.Util.getProcessLogger().warning(message.ToString());
throw new UnknownPropertyException(message.ToString());
}
}

Expand Down
12 changes: 3 additions & 9 deletions csharp/src/Ice/Internal/ReferenceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -679,15 +679,15 @@ Dictionary<string, string> props

if (unknownProps.Count != 0)
{
StringBuilder message = new StringBuilder("found unknown properties for proxy '");
StringBuilder message = new StringBuilder("Found unknown properties for proxy '");
message.Append(prefix);
message.Append("':");
foreach (string s in unknownProps)
{
message.Append("\n ");
message.Append(s);
}
_instance.initializationData().logger.warning(message.ToString());
throw new UnknownPropertyException(message.ToString());
}
}

Expand Down Expand Up @@ -736,13 +736,7 @@ private Reference create(
{
Ice.Properties properties = _instance.initializationData().properties;

//
// Warn about unknown properties.
//
if (properties.getIcePropertyAsInt("Ice.Warn.UnknownProperties") > 0)
{
checkForUnknownProperties(propertyPrefix);
}
checkForUnknownProperties(propertyPrefix);

string property;

Expand Down
19 changes: 19 additions & 0 deletions csharp/src/Ice/LocalExceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -855,3 +855,22 @@ public TwowayOnlyException(string operation)

public override string ice_id() => "::Ice::TwowayOnlyException";
}

/// <summary>
/// An unknown property was used in the configuration of an object adapter or a proxy.
/// </summary>
public sealed class UnknownPropertyException : LocalException
{
/// <summary>
/// Initializes a new instance of the <see cref="UnknownPropertyException" /> class.
/// </summary>
/// <param name="message">The exception message.</param>
/// <param name="innerException">The inner exception.</param>
public UnknownPropertyException(string message, System.Exception? innerException = null)
: base(message, innerException)
{
}

/// <inheritdoc/>
public override string ice_id() => "::Ice::UnknownPropertyException";
}
9 changes: 3 additions & 6 deletions csharp/src/Ice/ObjectAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,20 +1097,17 @@ internal ObjectAdapter(
List<string> unknownProps = new List<string>();
bool noProps = filterProperties(unknownProps);

//
// Warn about unknown object adapter properties.
//
if (unknownProps.Count != 0 && properties.getIcePropertyAsInt("Ice.Warn.UnknownProperties") > 0)
if (unknownProps.Count > 0)
{
StringBuilder message = new StringBuilder("found unknown properties for object adapter `");
StringBuilder message = new StringBuilder("Found unknown properties for object adapter '");
message.Append(_name);
message.Append("':");
foreach (string s in unknownProps)
{
message.Append("\n ");
message.Append(s);
}
_instance.initializationData().logger!.warning(message.ToString());
throw new UnknownPropertyException(message.ToString());
}

//
Expand Down
40 changes: 40 additions & 0 deletions csharp/test/Ice/unknownProperties/AllTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) ZeroC, Inc.

namespace Ice.unknownProperties;

public class AllTests : Test.AllTests
{
public static int allTests(Test.TestHelper helper)
{
var communicator = helper.communicator();
var output = helper.getWriter();

output.Write("Creating object adapter with unknown properties fails... ");
communicator.getProperties().setProperty("Foo.Endpoints", "tcp -h 127.0.0.1");
communicator.getProperties().setProperty("Foo.UnknownProperty", "bar");
try
{
communicator.createObjectAdapter("Foo");
test(false);
}
catch (UnknownPropertyException)
{
}
output.WriteLine("ok");

output.Write("Creating proxy with unknown properties fails... ");

communicator.getProperties().setProperty("Greeter", "hello:tcp -h 127.0.0.1 -p 10000");
communicator.getProperties().setProperty("Greeter.UnknownProperty", "bar");
try
{
communicator.propertyToProxy("Greeter");
test(false);
}
catch (UnknownPropertyException)
{
}
output.WriteLine("ok");
return 0;
}
}
17 changes: 17 additions & 0 deletions csharp/test/Ice/unknownProperties/Client.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) ZeroC, Inc.

namespace Ice.unknownProperties;

public class Client : Test.TestHelper
{
public override void run(string[] args)
{
var initData = new InitializationData();
initData.properties = createTestProperties(ref args);
using Communicator communicator = initialize(initData);
AllTests.allTests(this);
}

public static Task<int> Main(string[] args) =>
Test.TestDriver.runTestAsync<Client>(args);
}
29 changes: 29 additions & 0 deletions csharp/test/Ice/unknownProperties/msbuild/client/client.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../../../../../msbuild/ice.common.props" />
<PropertyGroup>
<AssemblyName>client</AssemblyName>
<OutputType>Exe</OutputType>
<TargetFramework>$(AppTargetFramework)</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<OutputPath>.</OutputPath>
</PropertyGroup>
<ItemGroup>
<Compile Include="../../../../TestCommon/TestHelper.cs" />
<Compile Include="../../AllTests.cs" />
<Compile Include="../../Client.cs" />
</ItemGroup>
<Choose>
<When Condition="'$(ICE_BIN_DIST)' == 'all'">
<ItemGroup>
<PackageReference Include="zeroc.ice.net" Version="3.8.0" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<ProjectReference Include="../../../../../src/Ice/Ice.csproj" />
</ItemGroup>
</Otherwise>
</Choose>
</Project>

0 comments on commit 3babd2e

Please sign in to comment.