Skip to content

Commit

Permalink
chore: Exclude pre-compiled modules from update_loaded_modules payloa…
Browse files Browse the repository at this point in the history
…d. (#2599)
  • Loading branch information
jaffinito authored Jul 9, 2024
1 parent c31451a commit 181c784
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using System;
Expand All @@ -14,6 +14,8 @@ namespace NewRelic.Agent.Core.WireModels
[JsonConverter(typeof(LoadedModuleWireModelCollectionJsonConverter))]
public class LoadedModuleWireModelCollection
{
private static Version zeroedVersion = new Version("0.0.0.0");

public List<LoadedModuleWireModel> LoadedModules { get; }

private LoadedModuleWireModelCollection()
Expand All @@ -28,8 +30,7 @@ public static LoadedModuleWireModelCollection Build(IList<Assembly> assemblies)
{
if (!TryGetAssemblyName(assembly, out var assemblyName))
{
// no way to properly track this assembly
continue;
continue; // we don't want to report this assembly
}

var assemblyDetails = assembly.GetName();
Expand Down Expand Up @@ -71,11 +72,17 @@ public static LoadedModuleWireModelCollection Build(IList<Assembly> assemblies)

private static bool TryGetAssemblyName(Assembly assembly, out string assemblyName)
{
// We skip any assemblies that cannot be linked to a nuget package.

try
{
if (assembly.IsDynamic)
if (assembly.IsDynamic) // skip dynamic assemblies
{
assemblyName = null;
}
else if (assembly.GetName().Version == zeroedVersion) // skip assemblies that have a version of 0.0.0.0 - these are precompiled assemblies
{
assemblyName = assembly.GetName().Name;
assemblyName = null;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class LoadedModuleWireModelCollectionJsonConverterTests
[Test]
public void LoadedModuleWireModelCollectionIsJsonSerializable()
{
var expected = @"[""Jars"",[[""MyTestAssembly"",""1.0.0"",{""namespace"":""MyTestAssembly"",""publicKeyToken"":""7075626C69636B6579746F6B656E"",""assemblyHashCode"":""42"",""Implementation-Vendor"":""MyCompany"",""copyright"":""Copyright 2008""}]]]";
var expected = @"[""Jars"",[[""MyTestAssembly.dll"",""1.0.0"",{""namespace"":""MyTestAssembly"",""publicKeyToken"":""7075626C69636B6579746F6B656E"",""assemblyHashCode"":""42"",""Implementation-Vendor"":""MyCompany"",""copyright"":""Copyright 2008""}]]]";

var baseAssemblyName = new AssemblyName();
baseAssemblyName.Name = BaseAssemblyName;
Expand All @@ -35,11 +35,10 @@ public void LoadedModuleWireModelCollectionIsJsonSerializable()

var baseTestAssembly = new TestAssembly();
baseTestAssembly.SetAssemblyName = baseAssemblyName;
baseTestAssembly.SetDynamic = true; // false uses on disk assembly and this won'y have one.
baseTestAssembly.SetHashCode = BaseHashCode;
baseTestAssembly.SetLocation = BaseAssemblyPath;
baseTestAssembly.AddCustomAttribute(new AssemblyCompanyAttribute(BaseCompanyName));
baseTestAssembly.AddCustomAttribute(new AssemblyCopyrightAttribute(BaseCopyrightValue));
baseTestAssembly.AddOrReplaceCustomAttribute(new AssemblyCompanyAttribute(BaseCompanyName));
baseTestAssembly.AddOrReplaceCustomAttribute(new AssemblyCopyrightAttribute(BaseCopyrightValue));

var assemblies = new List<Assembly>();
assemblies.Add(baseTestAssembly);
Expand All @@ -52,7 +51,7 @@ public void LoadedModuleWireModelCollectionIsJsonSerializable()
[Test]
public void LoadedModuleWireModelCollectionHandlesNulls()
{
var expected = @"[""Jars"",[[""MyTestAssembly"",""1.0.0"",{""namespace"":""MyTestAssembly"",""publicKeyToken"":""7075626C69636B6579746F6B656E"",""assemblyHashCode"":""42""}]]]";
var expected = @"[""Jars"",[[""MyTestAssembly.dll"",""1.0.0"",{""namespace"":""MyTestAssembly"",""publicKeyToken"":""7075626C69636B6579746F6B656E"",""assemblyHashCode"":""42""}]]]";

var baseAssemblyName = new AssemblyName();
baseAssemblyName.Name = BaseAssemblyName;
Expand All @@ -61,10 +60,10 @@ public void LoadedModuleWireModelCollectionHandlesNulls()

var baseTestAssembly = new TestAssembly();
baseTestAssembly.SetAssemblyName = baseAssemblyName;
baseTestAssembly.SetDynamic = true; // false uses on disk assembly and this won't have one.
baseTestAssembly.SetHashCode = BaseHashCode;
baseTestAssembly.AddCustomAttribute(new AssemblyCompanyAttribute(null));
baseTestAssembly.AddCustomAttribute(new AssemblyCopyrightAttribute(null));
baseTestAssembly.SetLocation = BaseAssemblyPath;
baseTestAssembly.AddOrReplaceCustomAttribute(new AssemblyCompanyAttribute(null));
baseTestAssembly.AddOrReplaceCustomAttribute(new AssemblyCopyrightAttribute(null));

var assemblies = new List<Assembly> { baseTestAssembly };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
using System.Reflection;
using System.Text;
using NUnit.Framework;
using static Google.Protobuf.Reflection.SourceCodeInfo.Types;

namespace NewRelic.Agent.Core.WireModels
{
[TestFixture]
public class LoadedModuleWireModelCollectionTests
{
private const string BaseAssemblyName = "MyTestAssembly";
private const string BaseAssemblyNamespace = "MyTestAssembly";
private const string BaseAssemblyName = "MyTestAssembly.dll";

private string BaseAssemblyVersion;
private string BaseAssemblyPath;
Expand All @@ -39,17 +39,17 @@ public void SetUp()
BasePublicKey = "7075626C69636B6579746F6B656E";

_baseAssemblyName = new AssemblyName();
_baseAssemblyName.Name = BaseAssemblyName;
_baseAssemblyName.Name = BaseAssemblyNamespace;
_baseAssemblyName.Version = new Version(BaseAssemblyVersion);
_baseAssemblyName.SetPublicKeyToken(Encoding.ASCII.GetBytes(BasePublicKeyToken));

_baseTestAssembly = new TestAssembly();
_baseTestAssembly.SetAssemblyName = _baseAssemblyName;
_baseTestAssembly.SetDynamic = true; // false uses on disk assembly and this won'y have one.
_baseTestAssembly.SetDynamic = false; // dynamic assemblies are not included so this must be false for most tests
_baseTestAssembly.SetHashCode = BaseHashCode;
_baseTestAssembly.SetLocation = BaseAssemblyPath;
_baseTestAssembly.AddCustomAttribute(new AssemblyCompanyAttribute(BaseCompanyName));
_baseTestAssembly.AddCustomAttribute(new AssemblyCopyrightAttribute(BaseCopyrightValue));
_baseTestAssembly.AddOrReplaceCustomAttribute(new AssemblyCompanyAttribute(BaseCompanyName));
_baseTestAssembly.AddOrReplaceCustomAttribute(new AssemblyCopyrightAttribute(BaseCopyrightValue));
}

[TearDown] public void TearDown()
Expand All @@ -58,8 +58,8 @@ [TearDown] public void TearDown()
_baseTestAssembly= null;
}

[TestCase(BaseAssemblyName, true, ExpectedResult = 1)]
[TestCase(BaseAssemblyName, false, ExpectedResult = 1)]
[TestCase(BaseAssemblyNamespace, true, ExpectedResult = 0)]
[TestCase(BaseAssemblyNamespace, false, ExpectedResult = 1)]
[TestCase(null, true, ExpectedResult = 0)]
[TestCase(null, false, ExpectedResult = 0)]
public int TryGetAssemblyName_UsingCollectionCount(string assemblyName, bool isDynamic)
Expand All @@ -80,6 +80,36 @@ public int TryGetAssemblyName_UsingCollectionCount(string assemblyName, bool isD
return loadedModules.LoadedModules.Count;
}

[Test]
public void Excludes_ZeroVersioned_Assemblies()
{
var zeroVersioned = new ZeroVersionAssembly();
zeroVersioned.SetAssemblyName = _baseAssemblyName;
zeroVersioned.SetHashCode = BaseHashCode;
zeroVersioned.SetLocation = BaseAssemblyPath;
zeroVersioned.AddOrReplaceCustomAttribute(new AssemblyCompanyAttribute(BaseCompanyName));
zeroVersioned.AddOrReplaceCustomAttribute(new AssemblyCopyrightAttribute(BaseCopyrightValue));

var assemblies = new List<Assembly>();
assemblies.Add(zeroVersioned);

var loadedModules = LoadedModuleWireModelCollection.Build(assemblies);

Assert.That(loadedModules.LoadedModules, Has.Count.EqualTo(0));
}

[Test]
public void Excludes_Dynamic_Assemblies()
{
var assemblies = new List<Assembly>();
_baseTestAssembly.SetDynamic = true;
assemblies.Add(_baseTestAssembly);

var loadedModules = LoadedModuleWireModelCollection.Build(assemblies);

Assert.That(loadedModules.LoadedModules, Has.Count.EqualTo(0));
}

[Test]
public void ValidateAllData()
{
Expand All @@ -96,7 +126,7 @@ public void ValidateAllData()
{
Assert.That(loadedModule.AssemblyName, Is.EqualTo(BaseAssemblyName));
Assert.That(loadedModule.Version, Is.EqualTo(BaseAssemblyVersion));
Assert.That(loadedModule.Data["namespace"], Is.EqualTo(BaseAssemblyName));
Assert.That(loadedModule.Data["namespace"], Is.EqualTo(BaseAssemblyNamespace));
Assert.That(loadedModule.Data["assemblyHashCode"], Is.EqualTo(BaseHashCode.ToString()));
Assert.That(loadedModule.Data["publicKeyToken"], Is.EqualTo(BasePublicKey));
Assert.That(loadedModule.Data["Implementation-Vendor"], Is.EqualTo(BaseCompanyName));
Expand Down Expand Up @@ -156,7 +186,7 @@ public void ErrorsHandled_TryGetAssemblyHashCode()
{
Assert.That(loadedModule.AssemblyName, Is.EqualTo(BaseAssemblyName));
Assert.That(loadedModule.Version, Is.EqualTo(BaseAssemblyVersion));
Assert.That(loadedModule.Data["namespace"], Is.EqualTo(BaseAssemblyName));
Assert.That(loadedModule.Data["namespace"], Is.EqualTo(BaseAssemblyNamespace));
});
Assert.That(loadedModule.Data.ContainsKey("assemblyHashCode"), Is.False);
Assert.Multiple(() =>
Expand All @@ -175,7 +205,6 @@ public void ErrorsHandled_TryGetAssemblyHashCode()
[Test]
public void ErrorsHandled_TryGetAssemblyName_Location()
{
_baseTestAssembly.SetDynamic = false;
var evilAssembly = new EvilTestAssembly(_baseTestAssembly);
evilAssembly.ItemToTest = "Location";

Expand Down Expand Up @@ -206,7 +235,7 @@ public void ErrorsHandled_GetCustomAttributes()
{
Assert.That(loadedModule.AssemblyName, Is.EqualTo(BaseAssemblyName));
Assert.That(loadedModule.Version, Is.EqualTo(BaseAssemblyVersion));
Assert.That(loadedModule.Data["namespace"], Is.EqualTo(BaseAssemblyName));
Assert.That(loadedModule.Data["namespace"], Is.EqualTo(BaseAssemblyNamespace));
Assert.That(loadedModule.Data["assemblyHashCode"], Is.EqualTo(BaseHashCode.ToString()));
Assert.That(loadedModule.Data["publicKeyToken"], Is.EqualTo(BasePublicKey));
});
Expand All @@ -222,15 +251,8 @@ public void ErrorsHandled_GetCustomAttributes()
[Test]
public void ErrorsHandled_GetCustomAttributes_HandlesNulls()
{
_baseTestAssembly = new TestAssembly();
_baseTestAssembly.SetAssemblyName = _baseAssemblyName;
_baseTestAssembly.SetDynamic = true; // false uses on disk assembly and this won't have one.
_baseTestAssembly.SetHashCode = BaseHashCode;


_baseTestAssembly.AddCustomAttribute(new AssemblyCompanyAttribute(null));
_baseTestAssembly.AddCustomAttribute(new AssemblyCopyrightAttribute(null));

_baseTestAssembly.AddOrReplaceCustomAttribute(new AssemblyCompanyAttribute(null));
_baseTestAssembly.AddOrReplaceCustomAttribute(new AssemblyCopyrightAttribute(null));

var evilAssembly = new EvilTestAssembly(_baseTestAssembly);
evilAssembly.ItemToTest = "";
Expand Down Expand Up @@ -270,7 +292,7 @@ public void ErrorsHandled_PublickeyToken()
{
Assert.That(loadedModule.AssemblyName, Is.EqualTo(BaseAssemblyName));
Assert.That(loadedModule.Version, Is.EqualTo(BaseAssemblyVersion));
Assert.That(loadedModule.Data["namespace"], Is.EqualTo(BaseAssemblyName));
Assert.That(loadedModule.Data["namespace"], Is.EqualTo(BaseAssemblyNamespace));
Assert.That(loadedModule.Data["assemblyHashCode"], Is.EqualTo(BaseHashCode.ToString()));
});
Assert.That(loadedModule.Data.ContainsKey("publicKeyToken"), Is.False);
Expand Down Expand Up @@ -332,8 +354,19 @@ public string SetLocation
}

public override string Location => _location;
public void AddCustomAttribute(object attribute)

public void AddOrReplaceCustomAttribute(object attribute)
{
var attributeType = attribute.GetType();
for (int i = 0; i < _customAttributes.Count; i++)
{
if (_customAttributes[i].GetType() == attributeType)
{
_customAttributes[i] = attribute;
return;
}
}

_customAttributes.Add(attribute);
}

Expand Down Expand Up @@ -380,7 +413,7 @@ public override bool IsDynamic
{
if (ItemToTest != "IsDynamic")
{
return _assembly.IsDynamic;
return false;
}

throw new Exception();
Expand Down Expand Up @@ -420,4 +453,16 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
throw new Exception();
}
}

public class ZeroVersionAssembly : TestAssembly
{
public override bool IsDynamic => false;

public override AssemblyName GetName()
{
var assemblyName = base.GetName();
assemblyName.Version = new Version("0.0.0.0");
return assemblyName;
}
}
}

0 comments on commit 181c784

Please sign in to comment.