-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathAndroidStudioConverterTests.cs
More file actions
427 lines (363 loc) · 19 KB
/
Copy pathAndroidStudioConverterTests.cs
File metadata and controls
427 lines (363 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Xml;
using FluentAssertions;
using Microsoft.CodeAnalysis.Sarif.Writers;
using Xunit;
namespace Microsoft.CodeAnalysis.Sarif.Converters
{
public class AndroidStudioConverterTests : ConverterTestsBase<AndroidStudioConverter>
{
private readonly AndroidStudioConverter _converter = null;
public AndroidStudioConverterTests()
{
_converter = new AndroidStudioConverter();
}
[Fact]
public void AndroidStudioConverter_Convert_Nulls()
{
Assert.Throws<ArgumentNullException>(() => _converter.Convert(null, null, OptionallyEmittedData.None));
}
[Fact]
public void AndroidStudioConverter_Convert_NullInput()
{
Assert.Throws<ArgumentNullException>(() => _converter.Convert(null, new ResultLogObjectWriter(), OptionallyEmittedData.None));
}
[Fact]
public void AndroidStudioConverter_Convert_NullOutput()
{
Assert.Throws<ArgumentNullException>(() => _converter.Convert(new MemoryStream(), null, OptionallyEmittedData.None));
}
[Fact]
public void AndroidStudioConverter_Convert_NoResults()
{
string androidStudioLog = @"<?xml version=""1.0"" encoding=""UTF-8""?><problems></problems>";
string actualJson = Utilities.GetConverterJson(_converter, androidStudioLog);
actualJson.Should().BeCrossPlatformEquivalent<SarifLog>(EmptyResultLogText);
}
[Fact]
public void AndroidStudioConverter_Convert_EmptyResult()
{
string androidStudioLog = @"<?xml version=""1.0"" encoding=""UTF-8""?><problems><problem></problem></problems>";
string actualJson = Utilities.GetConverterJson(_converter, androidStudioLog);
actualJson.Should().BeCrossPlatformEquivalent<SarifLog>(EmptyResultLogText);
}
[Fact]
public void AndroidStudioConverter_Convert_EmptyResultSingleTag()
{
string androidStudioLog = @"<?xml version=""1.0"" encoding=""UTF-8""?><problems><problem /></problems>";
string actualJson = Utilities.GetConverterJson(_converter, androidStudioLog);
actualJson.Should().BeCrossPlatformEquivalent<SarifLog>(EmptyResultLogText);
}
[Fact]
public void AndroidStudioConverter_Convert_InvalidLineNumber()
{
string androidStudioLog = @"<?xml version=""1.0"" encoding=""UTF-8""?><problems><problem><file>file://$PROJECT_DIR$/northwindtraders-droid-2/src/main/java/com/northwindtraders/droid/model/Model.java</file><line>NotALineNumber</line><module>northwindtraders-droid-2</module><package>com.northwindtraders.droid.model</package><entry_point TYPE=""file"" FQNAME=""file://$PROJECT_DIR$/northwindtraders-droid-2/src/main/java/com/northwindtraders/droid/model/Model.java""/><problem_class severity=""WARNING"" attribute_key=""WARNING_ATTRIBUTES"">Assertions</problem_class><hints/><description>Assertions are unreliable. Use BuildConfig.DEBUG conditional checks instead.</description></problem></problems>";
Assert.Throws<XmlException>(() => Utilities.GetConverterJson(_converter, androidStudioLog));
}
[Fact]
public void AndroidStudioConverter_Convert_NonXmlNodeTypeText_ChildElements()
{
string androidStudioLog = @"<?xml version=""1.0"" encoding=""UTF-8""?><problems><problem><file><child_file>file://$PROJECT_DIR$/</child_file></file><line>1</line><module>northwindtraders-droid-2</module><package>com.northwindtraders.droid.model</package><entry_point TYPE=""file"" FQNAME=""file://$PROJECT_DIR$/northwindtraders-droid-2/src/main/java/com/northwindtraders/droid/model/Model.java""/><problem_class severity=""WARNING"" attribute_key=""WARNING_ATTRIBUTES"">Assertions</problem_class><hints/><description>Assertions are unreliable. Use BuildConfig.DEBUG conditional checks instead.</description></problem></problems>";
Assert.Throws<XmlException>(() => Utilities.GetConverterJson(_converter, androidStudioLog));
}
[Fact]
public void AndroidStudioConverter_GetShortDescription_UsesDescriptionIfPresent()
{
AndroidStudioProblem.Builder builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
builder.Description = "Cute fluffy kittens";
var uut = new AndroidStudioProblem(builder);
string result = AndroidStudioConverter.GetShortDescriptionForProblem(uut);
Assert.Equal("Cute fluffy kittens", result);
}
[Fact]
public void AndroidStudioConverter_GetShortDescription_UsesProblemClassIfDescriptionNotPresent()
{
AndroidStudioProblem uut = AndroidStudioProblemTests.GetDefaultProblem();
string result = AndroidStudioConverter.GetShortDescriptionForProblem(uut);
result.Should().Contain("A Problematic Problem");
}
[Fact]
public void AndroidStudioConverter_ConvertToSarifResult_EmptyHintsDoNotAffectDescription()
{
AndroidStudioProblem.Builder builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
builder.Description = "hungry EVIL zombies";
var uut = new AndroidStudioProblem(builder);
Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
Assert.Equal("hungry EVIL zombies", result.Message.Text);
}
[Fact]
public void AndroidStudioConverter_ConvertToSarifResult_HintsAreStapledToDescription()
{
var builder = new AndroidStudioProblem.Builder
{
ProblemClass = "Unused",
File = "Unused",
Description = "hungry EVIL zombies",
Hints = ImmutableArray.Create("comment", "delete")
};
var uut = new AndroidStudioProblem(builder);
Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
string expected = @"hungry EVIL zombies
Possible resolution: comment
Possible resolution: delete";
// We need to normalize the line endings in the multiline string above,
// because otherwise this test passes or fails depending on the value
// of core.autocrlf in git.
expected = expected.Replace("\r", "");
expected = expected.Replace("\n", Environment.NewLine);
Assert.Equal(expected, result.Message.Text);
}
[Fact]
public void AndroidStudioConverter_ConvertToSarifResult_UsesProblemClassForRuleId()
{
AndroidStudioProblem uut = AndroidStudioProblemTests.GetDefaultProblem();
Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
Assert.Equal("A Problematic Problem", result.RuleId);
}
[Fact]
public void AndroidStudioConverter_ConvertToSarifResult_HasNoPropertiesIfAttributeKeyAndSeverity()
{
AndroidStudioProblem uut = AndroidStudioProblemTests.GetDefaultProblem();
Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
Assert.Null(result.Properties);
}
[Fact]
public void AndroidStudioConverter_ConvertToSarifResult_AttributeKeyIsPersistedInProperties()
{
AndroidStudioProblem.Builder builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
builder.AttributeKey = "key";
var uut = new AndroidStudioProblem(builder);
Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
result.PropertyNames.Count.Should().Be(1);
result.GetProperty("attributeKey").Should().Be("key");
}
[Fact]
public void AndroidStudioConverter_ConvertToSarifResult_SeverityIsPersistedInProperties()
{
AndroidStudioProblem.Builder builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
builder.Severity = "warning";
var uut = new AndroidStudioProblem(builder);
Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
result.PropertyNames.Count.Should().Be(1);
result.GetProperty("severity").Should().Be("warning");
}
[Fact]
public void AndroidStudioConverter_ConvertToSarifResult_MultiplePropertiesArePersisted()
{
AndroidStudioProblem.Builder builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
builder.AttributeKey = "key";
builder.Severity = "warning";
var uut = new AndroidStudioProblem(builder);
Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
result.PropertyNames.Count.Should().Be(2);
result.GetProperty("severity").Should().Be("warning");
result.GetProperty("attributeKey").Should().Be("key");
}
[Fact]
public void AndroidStudioConverter_ConvertSarifResult_RecordsTopLevelFileAsSourceFile()
{
AndroidStudioProblem.Builder builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
builder.File = "expected_file.java";
builder.EntryPointType = "file";
builder.EntryPointName = "bad_file.java";
Location loc = GetLocationInfoForBuilder(builder).Location;
loc.PhysicalLocation.ArtifactLocation.Uri.ToString().Should().Be("expected_file.java");
}
[Fact]
public void AndroidStudioConverter_ConvertSarifResult_DoesNotRecordTopLevelEntryPointAsSourceFile()
{
AndroidStudioProblem.Builder builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
builder.File = null;
builder.EntryPointType = "file";
builder.EntryPointName = "expected_file.java";
Location loc = GetLocationInfoForBuilder(builder).Location;
loc.PhysicalLocation.Should().BeNull();
}
internal static void ValidateLogicalLocations(IList<LogicalLocation> expectedLogicalLocations, IList<LogicalLocation> actualLogicalLocations)
{
for (int i = 0; i < expectedLogicalLocations.Count; i++)
{
expectedLogicalLocations[i].ValueEquals(actualLogicalLocations[i]).Should().BeTrue();
}
actualLogicalLocations.Count.Should().Be(expectedLogicalLocations.Count);
}
[Fact]
public void AndroidStudioConverter_ConvertSarifResult_RecordsModuleAsTopLevelIfPresent()
{
AndroidStudioProblem.Builder builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
builder.File = null;
builder.Module = "my_fancy_binary";
builder.EntryPointType = "method";
builder.EntryPointName = "my_method";
var expectedLocation = new Location
{
LogicalLocation = new LogicalLocation
{
FullyQualifiedName = "my_fancy_binary\\my_method",
Index = 1
}
};
var expectedLogicalLocations = new List<LogicalLocation>
{
new LogicalLocation { ParentIndex = -1, FullyQualifiedName = "my_fancy_binary", Kind = LogicalLocationKind.Module },
new LogicalLocation { ParentIndex = 0, Name = "my_method", FullyQualifiedName = @"my_fancy_binary\my_method", Kind = LogicalLocationKind.Member }
};
var converter = new AndroidStudioConverter();
Result result = converter.ConvertProblemToSarifResult(new AndroidStudioProblem(builder));
result.Locations[0].ValueEquals(expectedLocation).Should().BeTrue();
ValidateLogicalLocations(expectedLogicalLocations, converter.LogicalLocations);
}
[Fact]
public void AndroidStudioConverter_ConvertSarifResult_GeneratesLocationWithOnlyMethodEntryPoint()
{
AndroidStudioProblem.Builder builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
builder.File = null;
builder.Package = null;
builder.Module = null;
builder.EntryPointType = "method";
builder.EntryPointName = "my_method";
var expectedLocation = new Location
{
LogicalLocation = new LogicalLocation
{
FullyQualifiedName = "my_method",
Index = 0
}
};
var expectedLogicalLocations = new List<LogicalLocation>
{
new LogicalLocation { ParentIndex = -1, Kind = LogicalLocationKind.Member, FullyQualifiedName = "my_method" }
};
var converter = new AndroidStudioConverter();
Result result = converter.ConvertProblemToSarifResult(new AndroidStudioProblem(builder));
result.Locations[0].ValueEquals(expectedLocation).Should().BeTrue();
ValidateLogicalLocations(expectedLogicalLocations, converter.LogicalLocations);
}
[Fact]
public void AndroidStudioConverter_ConvertSarifResult_GeneratesLocationWithMethodEntryPointAndPackage()
{
AndroidStudioProblem.Builder builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
builder.File = null;
builder.Package = "FancyPackageName";
builder.Module = null;
builder.EntryPointType = "method";
builder.EntryPointName = "my_method";
var expectedLocation = new Location
{
LogicalLocation = new LogicalLocation
{
FullyQualifiedName = "FancyPackageName\\my_method",
Index = 1
}
};
var expectedLogicalLocations = new List<LogicalLocation>
{
new LogicalLocation { ParentIndex = -1, FullyQualifiedName = "FancyPackageName", Kind = LogicalLocationKind.Package },
new LogicalLocation { ParentIndex = 0, Name = "my_method", FullyQualifiedName = @"FancyPackageName\my_method", Kind = LogicalLocationKind.Member }
};
var converter = new AndroidStudioConverter();
Result result = converter.ConvertProblemToSarifResult(new AndroidStudioProblem(builder));
result.Locations[0].ValueEquals(expectedLocation).Should().BeTrue();
ValidateLogicalLocations(expectedLogicalLocations, converter.LogicalLocations);
}
[Fact]
public void AndroidStudioConverter_ConvertSarifResult_GeneratesLocationWithOnlyPackage()
{
AndroidStudioProblem.Builder builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
builder.File = null;
builder.Package = "FancyPackageName";
builder.Module = null;
builder.EntryPointName = null;
var expectedLocation = new Location
{
LogicalLocation = new LogicalLocation
{
FullyQualifiedName = "FancyPackageName",
Index = 0
}
};
var expectedLogicalLocations = new List<LogicalLocation>
{
new LogicalLocation {Kind = LogicalLocationKind.Package, FullyQualifiedName = "FancyPackageName" }
};
var converter = new AndroidStudioConverter();
Result result = converter.ConvertProblemToSarifResult(new AndroidStudioProblem(builder));
result.Locations[0].ValueEquals(expectedLocation).Should().BeTrue();
ValidateLogicalLocations(expectedLogicalLocations, converter.LogicalLocations);
}
[Fact]
public void AndroidStudioConverter_ConvertSarifResult_CanRecordSourceFileAndModule()
{
AndroidStudioProblem.Builder builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
builder.File = "File Goes Here";
builder.Package = null;
builder.Module = "LastResortModule";
builder.EntryPointName = null;
var expectedLocation = new Location
{
PhysicalLocation = new PhysicalLocation
{
ArtifactLocation = new ArtifactLocation
{
Uri = new Uri("File Goes Here", UriKind.RelativeOrAbsolute)
},
},
LogicalLocation = new LogicalLocation
{
FullyQualifiedName = "LastResortModule",
Index = 0
}
};
var expectedLogicalLocations = new List<LogicalLocation>
{
new LogicalLocation { Kind = LogicalLocationKind.Module, FullyQualifiedName = "LastResortModule" }
};
var converter = new AndroidStudioConverter();
Result result = converter.ConvertProblemToSarifResult(new AndroidStudioProblem(builder));
result.Locations[0].ValueEquals(expectedLocation).Should().BeTrue();
ValidateLogicalLocations(expectedLogicalLocations, converter.LogicalLocations);
}
[Fact]
public void AndroidStudioConverter_ConvertSarifResult_RemovesProjectDirPrefix()
{
AndroidStudioProblem.Builder builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
builder.File = "file://$PROJECT_DIR$/mydir/myfile.xml";
LocationInfo locationInfo = GetLocationInfoForBuilder(builder);
locationInfo.Location.PhysicalLocation.ArtifactLocation.Uri.ToString().Should().Be("mydir/myfile.xml");
}
[Fact]
public void AndroidStudioConverter_ConvertSarifResult_PersistsSourceLineInfo()
{
AndroidStudioProblem.Builder builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
builder.Line = 42;
LocationInfo locationInfo = GetLocationInfoForBuilder(builder);
locationInfo.Location.PhysicalLocation.Region.StartLine.Should().Be(42);
}
private struct LocationInfo
{
public Location Location;
public LogicalLocation LogicalLocation;
}
private static LocationInfo GetLocationInfoForBuilder(AndroidStudioProblem.Builder builder)
{
var converter = new AndroidStudioConverter();
Result result = converter.ConvertProblemToSarifResult(new AndroidStudioProblem(builder));
Location location = result.Locations.First();
LogicalLocation logicalLocation =
location.LogicalLocation.Index > -1
? converter.LogicalLocations[location.LogicalLocation.Index]
: null;
return new LocationInfo
{
Location = location,
LogicalLocation = logicalLocation
};
}
}
}