From 6ba0bcab88e5d5e1f811f64a06f93af9d0424247 Mon Sep 17 00:00:00 2001 From: Steve Maier <82616727+SteveMaier-IRT@users.noreply.github.com> Date: Tue, 14 Jun 2022 17:18:02 -0400 Subject: [PATCH] - Added checks for the election type and defaulting the VotesAllowed value to 1 if it is an n of m election (#305) - Added C++ and C# unit tests to make sure the value gets set to 1 - Increased the build number for a new release of nuget package --- .../ElectionGuard.Encryption.Tests/TestManifest.cs | 13 +++++++++++++ .../ElectionGuard.Encryption.csproj | 8 ++++---- src/electionguard/manifest.cpp | 4 ++-- src/electionguard/serialize.hpp | 3 ++- test/electionguard/test_manifest.cpp | 13 +++++++++++++ 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/bindings/netstandard/ElectionGuard/ElectionGuard.Encryption.Tests/TestManifest.cs b/bindings/netstandard/ElectionGuard/ElectionGuard.Encryption.Tests/TestManifest.cs index 3accd4d8..9f28a238 100644 --- a/bindings/netstandard/ElectionGuard/ElectionGuard.Encryption.Tests/TestManifest.cs +++ b/bindings/netstandard/ElectionGuard/ElectionGuard.Encryption.Tests/TestManifest.cs @@ -11,6 +11,19 @@ namespace ElectionGuard.Encrypt.Tests public class TestManifest { + [Test] + public void Test_Can_Create_Contest() + { + List selections = new List(); + + var contest = new ContestDescription("contest-id", "district-id", 1, VoteVariationType.n_of_m, 1, + "test election", selections.ToArray()); + + // Assert + Assert.AreEqual(contest.VotesAllowed, 1); + } + + [Test] public void Test_Can_Serialize_Sample_manifest() { diff --git a/bindings/netstandard/ElectionGuard/ElectionGuard.Encryption/ElectionGuard.Encryption.csproj b/bindings/netstandard/ElectionGuard/ElectionGuard.Encryption/ElectionGuard.Encryption.csproj index 3f5e9142..17103b80 100644 --- a/bindings/netstandard/ElectionGuard/ElectionGuard.Encryption/ElectionGuard.Encryption.csproj +++ b/bindings/netstandard/ElectionGuard/ElectionGuard.Encryption/ElectionGuard.Encryption.csproj @@ -7,9 +7,9 @@ ElectionGuard ElectionGuard.Encryption - 0.1.13 - 0.1.13.0 - 0.1.13.0 + 0.1.14 + 0.1.14.0 + 0.1.14.0 @@ -19,7 +19,7 @@ ElectionGuard Encryption Open source implementation of ElectionGuard's ballot encryption. Microsoft - 0.1.13 + 0.1.14 MIT https://github.com/microsoft/electionguard-cpp https://github.com/microsoft/electionguard-cpp diff --git a/src/electionguard/manifest.cpp b/src/electionguard/manifest.cpp index 4b03fc2b..2374211e 100644 --- a/src/electionguard/manifest.cpp +++ b/src/electionguard/manifest.cpp @@ -1027,7 +1027,7 @@ namespace electionguard this->sequenceOrder = sequenceOrder; this->voteVariation = voteVariation; this->numberElected = numberElected; - this->votesAllowed = 0UL; + this->votesAllowed = voteVariation == VoteVariationType::n_of_m ? 1UL : 0UL; this->primaryPartyIds = {}; } @@ -1042,7 +1042,7 @@ namespace electionguard this->sequenceOrder = sequenceOrder; this->voteVariation = voteVariation; this->numberElected = numberElected; - this->votesAllowed = 0UL; + this->votesAllowed = voteVariation == VoteVariationType::n_of_m ? 1UL : 0UL; } Impl(const string &objectId, const string &electoralDistrictId, diff --git a/src/electionguard/serialize.hpp b/src/electionguard/serialize.hpp index 1981fdbe..e63859a1 100644 --- a/src/electionguard/serialize.hpp +++ b/src/electionguard/serialize.hpp @@ -452,7 +452,8 @@ namespace electionguard auto elected = j["number_elected"].get(); auto allowed = j.contains("votes_allowed") && !j["votes_allowed"].is_null() ? j["votes_allowed"].get() - : 0; + : variation == VoteVariationType::n_of_m ? 1 + : 0; auto name = j["name"].get(); auto title = j.contains("ballot_title") && !j["ballot_title"].is_null() ? internationalizedTextFromJson(j["ballot_title"]) diff --git a/test/electionguard/test_manifest.cpp b/test/electionguard/test_manifest.cpp index c000a377..a16626a0 100644 --- a/test/electionguard/test_manifest.cpp +++ b/test/electionguard/test_manifest.cpp @@ -65,3 +65,16 @@ TEST_CASE("Can serialize InternalManifest") // Assert CHECK(internal->getManifestHash()->toHex() == fromJson->getManifestHash()->toHex()); } + +TEST_CASE("Can construct Contest from Parameters") +{ + vector> selections; + + // Arrange + auto data = + make_unique("contest-id", "district-id", 1, VoteVariationType::n_of_m, 1, + "test election", move(selections)); + + // Assert + CHECK_EQ(data->getVotesAllowed(), 1); +}