1
- // Copyright 2020 New Relic, Inc. All rights reserved.
1
+ // Copyright 2020 New Relic, Inc. All rights reserved.
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
- using System . Linq ;
7
6
using System . Reflection ;
8
- using System . Runtime . InteropServices ;
9
7
using System . Text ;
10
- using NewRelic . Agent . Core . Metrics ;
11
- using NewRelic . Collections ;
12
8
using NUnit . Framework ;
13
- using Telerik . JustMock ;
14
9
using static Google . Protobuf . Reflection . SourceCodeInfo . Types ;
15
10
16
11
namespace NewRelic . Agent . Core . WireModels
@@ -19,20 +14,30 @@ namespace NewRelic.Agent.Core.WireModels
19
14
public class LoadedModuleWireModelCollectionTests
20
15
{
21
16
private const string BaseAssemblyName = "MyTestAssembly" ;
22
- private const string BaseAssemblyVersion = "1.0.0" ;
23
- private const string BaseAssemblyPath = @"C:\path\to\assembly\MyTestAssembly.dll" ;
24
- private const string BaseCompanyName = "MyCompany" ;
25
- private const string BaseCopyrightValue = "Copyright 2008" ;
26
- private const int BaseHashCode = 42 ;
27
- private const string BasePublicKeyToken = "publickeytoken" ;
28
- private const string BasePublicKey = "7075626C69636B6579746F6B656E" ;
17
+
18
+ private string BaseAssemblyVersion ;
19
+ private string BaseAssemblyPath ;
20
+ private string BaseCompanyName ;
21
+ private string BaseCopyrightValue ;
22
+ private int BaseHashCode ;
23
+ private string BasePublicKeyToken ;
24
+ private string BasePublicKey ;
29
25
30
26
private AssemblyName _baseAssemblyName ;
31
27
private TestAssembly _baseTestAssembly ;
32
28
33
29
[ SetUp ]
34
30
public void SetUp ( )
35
31
{
32
+
33
+ BaseAssemblyVersion = "1.0.0" ;
34
+ BaseAssemblyPath = @"C:\path\to\assembly\MyTestAssembly.dll" ;
35
+ BaseCompanyName = "MyCompany" ;
36
+ BaseCopyrightValue = "Copyright 2008" ;
37
+ BaseHashCode = 42 ;
38
+ BasePublicKeyToken = "publickeytoken" ;
39
+ BasePublicKey = "7075626C69636B6579746F6B656E" ;
40
+
36
41
_baseAssemblyName = new AssemblyName ( ) ;
37
42
_baseAssemblyName . Name = BaseAssemblyName ;
38
43
_baseAssemblyName . Version = new Version ( BaseAssemblyVersion ) ;
@@ -64,7 +69,6 @@ public int TryGetAssemblyName_UsingCollectionCount(string assemblyName, bool isD
64
69
{
65
70
_baseTestAssembly . SetLocation = null ;
66
71
}
67
-
68
72
_baseTestAssembly . SetAssemblyName = _baseAssemblyName ;
69
73
_baseTestAssembly . SetDynamic = isDynamic ;
70
74
@@ -194,6 +198,35 @@ public void ErrorsHandled_GetCustomAttributes()
194
198
Assert . False ( loadedModule . Data . ContainsKey ( "sha512Checksum" ) ) ;
195
199
}
196
200
201
+ [ Test ]
202
+ public void ErrorsHandled_GetCustomAttributes_HandlesNulls ( )
203
+ {
204
+ _baseTestAssembly = new TestAssembly ( ) ;
205
+ _baseTestAssembly . SetAssemblyName = _baseAssemblyName ;
206
+ _baseTestAssembly . SetDynamic = true ; // false uses on disk assembly and this won't have one.
207
+ _baseTestAssembly . SetHashCode = BaseHashCode ;
208
+
209
+
210
+ _baseTestAssembly . AddCustomAttribute ( new AssemblyCompanyAttribute ( null ) ) ;
211
+ _baseTestAssembly . AddCustomAttribute ( new AssemblyCopyrightAttribute ( null ) ) ;
212
+
213
+
214
+ var evilAssembly = new EvilTestAssembly ( _baseTestAssembly ) ;
215
+ evilAssembly . ItemToTest = "" ;
216
+
217
+ var assemblies = new List < Assembly > ( ) ;
218
+ assemblies . Add ( evilAssembly ) ;
219
+
220
+ var loadedModules = LoadedModuleWireModelCollection . Build ( assemblies ) ;
221
+
222
+ Assert . AreEqual ( 1 , loadedModules . LoadedModules . Count ) ;
223
+
224
+ var loadedModule = loadedModules . LoadedModules [ 0 ] ;
225
+
226
+ Assert . False ( loadedModule . Data . ContainsKey ( "Implementation-Vendor" ) ) ;
227
+ Assert . False ( loadedModule . Data . ContainsKey ( "copyright" ) ) ;
228
+ }
229
+
197
230
[ Test ]
198
231
public void ErrorsHandled_PublickeyToken ( )
199
232
{
@@ -266,7 +299,6 @@ public string SetLocation
266
299
}
267
300
268
301
public override string Location => _location ;
269
-
270
302
public void AddCustomAttribute ( object attribute )
271
303
{
272
304
_customAttributes . Add ( attribute ) ;
0 commit comments