Skip to content

Commit

Permalink
Fix error at startup when host application is published as a single f…
Browse files Browse the repository at this point in the history
…ile (#3)

* Fix empty Assembly.Location when published as single file

* Throw if measure.Name is null or empty

* Suppress null reference warnings in net462

* Remove property VpaxObfuscator.Version

* Fix nullability warnings
  • Loading branch information
albertospelta authored Feb 13, 2024
1 parent 53face9 commit 62ab62c
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/Dax.Vpax.Obfuscator.TestApp/Form1.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Reflection;
using Dax.Vpax.Obfuscator.Common;

namespace Dax.Vpax.Obfuscator.TestApp
Expand All @@ -7,7 +8,7 @@ public partial class Form1 : Form
public Form1()
{
InitializeComponent();
Text = $"VPAX Obfuscator v{VpaxObfuscator.Version}";
Text = $"VPAX Obfuscator v{typeof(VpaxObfuscator).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}";
}

private void buttonObfuscate_Click(object sender, EventArgs e)
Expand Down
3 changes: 1 addition & 2 deletions src/Dax.Vpax.Obfuscator/Comparers/DaxNameEqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ internal sealed class DaxNameEqualityComparer : EqualityComparer<DaxName>
{
public static readonly DaxNameEqualityComparer Instance = new();

// TODO: (review) null check
public override bool Equals(DaxName x, DaxName y) => string.Equals(x.Name, x.Name, StringComparison.OrdinalIgnoreCase);
public override bool Equals(DaxName? x, DaxName? y) => string.Equals(x?.Name, x?.Name, StringComparison.OrdinalIgnoreCase);
public override int GetHashCode(DaxName obj) => obj.Name.GetHashCode();
}
4 changes: 2 additions & 2 deletions src/Dax.Vpax.Obfuscator/Comparers/DaxTextEqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ internal sealed class DaxTextValueEqualityComparer : EqualityComparer<DaxText>
{
public static readonly DaxTextValueEqualityComparer Instance = new();

public override bool Equals(DaxText x, DaxText y) => string.Equals(x.Value, y.Value, StringComparison.OrdinalIgnoreCase);
public override bool Equals(DaxText? x, DaxText? y) => string.Equals(x?.Value, y?.Value, StringComparison.OrdinalIgnoreCase);
public override int GetHashCode(DaxText obj) => StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Value);
}

internal sealed class DaxTextObfuscatedValueEqualityComparer : EqualityComparer<DaxText>
{
public static readonly DaxTextObfuscatedValueEqualityComparer Instance = new();

public override bool Equals(DaxText x, DaxText y) => string.Equals(x.ObfuscatedValue, y.ObfuscatedValue, StringComparison.OrdinalIgnoreCase);
public override bool Equals(DaxText? x, DaxText? y) => string.Equals(x?.ObfuscatedValue, y?.ObfuscatedValue, StringComparison.OrdinalIgnoreCase);
public override int GetHashCode(DaxText obj) => StringComparer.OrdinalIgnoreCase.GetHashCode(obj.ObfuscatedValue);
}
27 changes: 10 additions & 17 deletions src/Dax.Vpax.Obfuscator/DaxModelObfuscator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics;
using System.Reflection;
using Dax.Metadata;
using Dax.Vpax.Obfuscator.Common;
using Dax.Vpax.Obfuscator.Extensions;
Expand All @@ -17,17 +17,16 @@ public DaxModelObfuscator(Model model, ObfuscationDictionary? dictionary = null)
if (model.IsObfuscated()) throw new InvalidOperationException("The model has already been obfuscated.");

var dictionaryId = Guid.NewGuid().ToString("D");
if (dictionary != null) {
if (dictionary != null)
{
if (!ObfuscationDictionary.IsValidId(dictionary.Id)) throw new InvalidOperationException("The dictionary identifier is not valid.");
dictionaryId = dictionary.Id;
}

var obfuscatorAssembly = GetType().Assembly;

_model = model;
_model.ObfuscatorDictionaryId = dictionaryId;
_model.ObfuscatorLib = obfuscatorAssembly.GetName().Name;
_model.ObfuscatorLibVersion = FileVersionInfo.GetVersionInfo(obfuscatorAssembly.Location).ProductVersion;
_model.ObfuscatorLib = GetType().Assembly.GetName().Name;
_model.ObfuscatorLibVersion = GetType().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? throw new InvalidOperationException("The assembly informational version is not available.");
_obfuscator = new DaxTextObfuscator();
_texts = new DaxTextCollection(dictionary);
}
Expand Down Expand Up @@ -63,7 +62,7 @@ private void ObfuscateIdentifiers(Column column)

private void ObfuscateIdentifiers(Measure measure)
{
var measureText = Obfuscate(measure.MeasureName);
var measureText = Obfuscate(measure.MeasureName) ?? throw new InvalidOperationException($"The measure name is not valid [{measure.MeasureName}].");
CreateKpiMeasure(measure.KpiTargetExpression, "Goal");
CreateKpiMeasure(measure.KpiStatusExpression, "Status");
CreateKpiMeasure(measure.KpiTrendExpression, "Trend");
Expand Down Expand Up @@ -133,7 +132,6 @@ private void Obfuscate(Relationship relationship) { }

private void Obfuscate(CalculationGroup calculationGroup)
{
// TOFIX: should not be null
if (calculationGroup == null)
return;

Expand All @@ -142,11 +140,6 @@ private void Obfuscate(CalculationGroup calculationGroup)
Obfuscate(calculationItem.ItemName);
Obfuscate(calculationItem.ItemExpression);
Obfuscate(calculationItem.Description);

// TODO: (review) do we need to obfuscate these?
//calculationItem.ErrorMessage
//calculationItem.FormatStringDefinition
//calculationItem.FormatStringErrorMessage
}
}

Expand All @@ -161,11 +154,11 @@ private void Obfuscate(TablePermission tablePermission)
Obfuscate(tablePermission.FilterExpression);
}

private DaxText Obfuscate(DaxName name)
private DaxText? Obfuscate(DaxName name)
{
if (string.IsNullOrWhiteSpace(name?.Name)) return null;

var text = ObfuscateText(new DaxText(name.Name));
var text = ObfuscateText(new DaxText(name!.Name));
name.Name = text.ObfuscatedValue;
return text;
}
Expand All @@ -174,14 +167,14 @@ private void Obfuscate(DaxNote note)
{
if (string.IsNullOrWhiteSpace(note?.Note)) return;

var text = ObfuscateText(new DaxText(note.Note));
var text = ObfuscateText(new DaxText(note!.Note));
note.Note = text.ObfuscatedValue;
}

private void Obfuscate(DaxExpression expression)
{
if (string.IsNullOrWhiteSpace(expression?.Expression)) return;

expression.Expression = ObfuscateExpression(expression.Expression);
expression!.Expression = ObfuscateExpression(expression.Expression);
}
}
4 changes: 0 additions & 4 deletions src/Dax.Vpax.Obfuscator/VpaxObfuscator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ namespace Dax.Vpax.Obfuscator;

public sealed class VpaxObfuscator : IVpaxObfuscator
{
#pragma warning disable CS0436 // Type conflicts with imported type
public static string Version = ThisAssembly.AssemblyInformationalVersion;
#pragma warning restore CS0436 // Type conflicts with imported type

/// <inheritdoc/>
public ObfuscationDictionary Obfuscate(Stream stream)
=> ObfuscateImpl(stream, dictionary: null);
Expand Down
4 changes: 2 additions & 2 deletions tests/Dax.Vpax.Obfuscator.Tests/DaxTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ public class DaxTextTests
[Fact]
public void ctor_NullValue_Throws()
{
Assert.Throws<ArgumentNullException>(() => new DaxText(value: null));
Assert.Throws<ArgumentNullException>(() => new DaxText(value: null!));
}

[Fact]
public void ctor_NullObfuscatedValue_Throws()
{
Assert.Throws<ArgumentNullException>(() => new DaxText(value: "abc", obfuscatedValue: null));
Assert.Throws<ArgumentNullException>(() => new DaxText(value: "abc", obfuscatedValue: null!));
}

[Fact]
Expand Down

0 comments on commit 62ab62c

Please sign in to comment.