Skip to content

Commit cb73217

Browse files
Add replacement for accented characters to language class (#333)
* Add replacement for accented characters to language class Add unit test for handling accented characters. Upgraded to version 1.0.0 * Encode the unicode characters as escaped chars
1 parent ff69753 commit cb73217

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

bindings/netstandard/ElectionGuard/ElectionGuard.Encryption.Tests/TestManifest.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,37 @@ public void Test_Can_Create_Manifest_With_Name()
154154
Assert.IsFalse(json.Contains("\"name\":{\"text\":null")); // check to make sure the party name serialized correctly
155155
}
156156

157+
[Test]
158+
public void Test_Unicode_CandidateNames()
159+
{
160+
var candidateName = new Language("Raúl", "en");
161+
var candidate = new Candidate(
162+
"candidate-1",
163+
new InternationalizedText(new[] { candidateName }),
164+
string.Empty,
165+
string.Empty,
166+
false);
167+
168+
List<Candidate> candidates = new List<Candidate>
169+
{
170+
candidate
171+
};
172+
173+
var result = new Manifest(
174+
"test-manifest",
175+
ElectionType.general,
176+
DateTime.Now,
177+
DateTime.Now,
178+
new GeopoliticalUnit[] { },
179+
new Party[] { },
180+
candidates.ToArray(),
181+
new ContestDescription[] { },
182+
new BallotStyle[] { },
183+
new InternationalizedText(new Language[] { }),
184+
new ContactInformation("na"));
185+
186+
var json = result.ToJson();
187+
Assert.IsTrue(json.Contains("\"value\":\"Ra\\\\u00fal\""));
188+
}
157189
}
158190
}

bindings/netstandard/ElectionGuard/ElectionGuard.Encryption/ElectionGuard.Encryption.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<!-- Project -->
88
<RootNamespace>ElectionGuard</RootNamespace>
99
<AssemblyName>ElectionGuard.Encryption</AssemblyName>
10-
<Version>0.1.18</Version>
11-
<AssemblyVersion>0.1.18.0</AssemblyVersion>
12-
<AssemblyFileVersion>0.1.18.0</AssemblyFileVersion>
10+
<Version>1.0.1</Version>
11+
<AssemblyVersion>1.0.1.0</AssemblyVersion>
12+
<AssemblyFileVersion>1.0.1.0</AssemblyFileVersion>
1313
</PropertyGroup>
1414

1515
<PropertyGroup>
@@ -19,7 +19,7 @@
1919
<Title>ElectionGuard Encryption</Title>
2020
<Description>Open source implementation of ElectionGuard's ballot encryption.</Description>
2121
<Authors>Microsoft</Authors>
22-
<PackageVersion>0.1.18</PackageVersion>
22+
<PackageVersion>1.0.1</PackageVersion>
2323
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2424
<PackageProjectUrl>https://github.com/microsoft/electionguard-cpp</PackageProjectUrl>
2525
<RepositoryUrl>https://github.com/microsoft/electionguard-cpp</RepositoryUrl>

bindings/netstandard/ElectionGuard/ElectionGuard.Encryption/Manifest.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text;
23
using System.Runtime.InteropServices;
34

45
namespace ElectionGuard
@@ -177,13 +178,38 @@ unsafe internal Language(
177178
/// <param name="language">string with language info</param>
178179
public unsafe Language(string value, string language)
179180
{
180-
var status = NativeInterface.Language.New(value, language, out Handle);
181+
var data = EncodeNonAsciiCharacters(value);
182+
var status = NativeInterface.Language.New(data, language, out Handle);
181183
if (status != Status.ELECTIONGUARD_STATUS_SUCCESS)
182184
{
183185
throw new ElectionGuardException($"Language Error Status: {status}");
184186
}
185187
}
186188

189+
/// <summary>
190+
/// Temp function for handling accented latin characters for v1.0
191+
/// </summary>
192+
/// <param name="value">string to convert</param>
193+
/// <returns>string with replaced characters</returns>
194+
public static string EncodeNonAsciiCharacters(string value)
195+
{
196+
StringBuilder sb = new StringBuilder();
197+
foreach (char c in value)
198+
{
199+
if (c > 127)
200+
{
201+
// This character is too big for ASCII
202+
string encodedValue = "\\u" + ((int)c).ToString("x4");
203+
sb.Append(encodedValue);
204+
}
205+
else
206+
{
207+
sb.Append(c);
208+
}
209+
}
210+
return sb.ToString();
211+
}
212+
187213
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
188214
protected override unsafe void DisposeUnmanaged()
189215
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
@@ -2169,7 +2195,8 @@ unsafe internal Manifest(NativeManifest handle)
21692195
/// <param name="json">string of json data describing the manifest</param>
21702196
public unsafe Manifest(string json)
21712197
{
2172-
var status = NativeInterface.Manifest.FromJson(json, out Handle);
2198+
var data = Language.EncodeNonAsciiCharacters(json);
2199+
var status = NativeInterface.Manifest.FromJson(data, out Handle);
21732200
if (status != Status.ELECTIONGUARD_STATUS_SUCCESS)
21742201
{
21752202
throw new ElectionGuardException($"Manifest Error Status: {status}");

0 commit comments

Comments
 (0)