Skip to content

Commit

Permalink
fix: Replace non-ASCII characters in filenames (#42)
Browse files Browse the repository at this point in the history
Fixes #41.
  • Loading branch information
ermshiperete authored Mar 27, 2024
1 parent b4b487d commit 83f80cd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Fixed

- replaced non-ASCII characters in encrypted filename (#41)

## [1.3.3] - 2023-10-17

### Fixed
Expand Down
19 changes: 18 additions & 1 deletion DigitaleBriefwahl/Encryption/EncryptVote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,26 @@ private static byte[] EncryptData(byte[] inputData)
return encOut.ToArray();
}

private const string Umlauts = "äöüÄÖÜß";
private static string[] UmlautReplacements = { "ae", "oe", "ue", "AE", "OE", "UE", "ss" };

private static string GetSanitizedElection(string election)
{
return election.Replace(' ', '_').Replace('.', '_');
var filePath = election.Replace(' ', '_').Replace('.', '_');
var bldr = new StringBuilder();
for (int i = 0; i < filePath.Length; i++)
{
var c = filePath[i];
if (c > 128)
{
var umlautIndex = Umlauts.IndexOf(c);
bldr.Append(umlautIndex >= 0 ? UmlautReplacements[umlautIndex] : "_");
}
else
bldr.Append(c);
}

return bldr.ToString();
}

public string WriteVote(string vote, string filePath = null)
Expand Down
21 changes: 21 additions & 0 deletions DigitaleBriefwahlTests/EncryptVoteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,26 @@ public void GetEncryptedFilePath()
Assert.That(sut.GetEncryptedFilePath(sut.BallotFilePath),
Is.EqualTo(sut.BallotFilePath.Substring(0, sut.BallotFilePath.Length - 4) + ".gpg"));
}

[Test]
public void GetEncryptedFilePath_Umlaut_AllASCII()
{
var sut= new EncryptVote("ÄÖÜäöüß");
var filePath = Path.GetFileName(sut.GetEncryptedFilePath(sut.BallotFilePath));
Assert.That(filePath.StartsWith("AEOEUEaeoeuess"), Is.True,
$"Unexpected FilePath.\n Expected: AEOEUEaeoeuess...\n But was: {filePath}");
}

[Test]
public void GetEncryptedFilePath_Other_AllASCII()
{
var sut= new EncryptVote("ខ្មែរ");
var filePath = sut.GetEncryptedFilePath(sut.BallotFilePath);
foreach (var c in filePath.ToCharArray())
{
Assert.That(c, Is.LessThanOrEqualTo(128),
$"FilePath contains non-ASCII characters: {filePath}");
}
}
}
}

0 comments on commit 83f80cd

Please sign in to comment.