Ejemplo n.º 1
0
        /// <inheritdoc />
        public virtual void SaveAsset(byte[] data, string name, bool backup)
        {
            string filePath = EnginePathToFilePath(name);

            filePath = filePath.Replace(_folderFs.ToLower(), _folderFs);

            if (!File.Exists(filePath))
            {
                // If new - add to the internal manifest.
                InternalManifest.TryAdd(FilePathToEnginePath(filePath, FolderInPath), filePath);
            }
            else if (backup)
            {
                // Backup old - if any.
                File.Copy(filePath, filePath + ".backup", true);
            }

            // Create missing directories.
            string directoryName = Path.GetDirectoryName(filePath);

            if (!string.IsNullOrEmpty(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }

            FileStream stream = File.Open(filePath, FileMode.Create);

            stream.Write(data, 0, data.Length);
            stream.Dispose();
        }
        /// <inheritdoc />
        public override byte[] GetAsset(string enginePath)
        {
            // Convert to embedded path.
            bool found = InternalManifest.TryGetValue(enginePath, out string embeddedPath);

            // Check if found.
            if (!found)
            {
                return(new byte[0]);
            }

            byte[] data;

            // Read the asset from the embedded file.
            using (Stream stream = Assembly.GetManifestResourceStream(embeddedPath))
            {
                // Not found.
                if (stream == null)
                {
                    Engine.Log.Error($"Couldn't read asset [{enginePath}] with embedded path [{embeddedPath}].", MessageSource.AssetLoader);
                    return(new byte[0]);
                }

                // Read from stream.
                data = new byte[stream.Length];
                stream.Read(data, 0, (int)stream.Length);
            }

            return(data);
        }
Ejemplo n.º 3
0
        public void Test_Encrypt_Ballot_Simple_Succeeds()
        {
            // Configure the election context
            var keypair = ElGamalKeyPair.FromSecret(Constants.TWO_MOD_Q);
            var description = new Manifest(manifest_json);
            var manifest = new InternalManifest(description);
            var context = new CiphertextElectionContext(
                1UL, 1UL, keypair.PublicKey, Constants.TWO_MOD_Q, manifest.ManifestHash);
            var device = new EncryptionDevice(12345UL, 23456UL, 34567UL, "Location");
            var mediator = new EncryptionMediator(manifest, context, device);

            var ballot = new PlaintextBallot(plaintext_json);

            // Act
            var ciphertext = mediator.Encrypt(ballot);

            // Assert
            
            // a property
            Assert.That(ciphertext.ObjectId == ballot.ObjectId);

            // json serialization
            var json = ciphertext.Json;
            var fromJson = new CiphertextBallot(json);
            Assert.That(ciphertext.ObjectId == fromJson.ObjectId);

            // binary serialization
            var bson = ciphertext.Bson;
            var fromBson = CiphertextBallot.FromBson(bson);
            Assert.That(ciphertext.ObjectId == fromBson.ObjectId);

            // submitted ballot
            var submitted = SubmittedBallot.From(ciphertext, BallotBoxState.cast);
            Assert.That(ciphertext.ObjectId == submitted.ObjectId);
        }
Ejemplo n.º 4
0
        public void Test_Can_Deserlialze_Internal_Election_Description()
        {
            string data = "{\"ballot_styles\":[{\"geopolitical_unit_ids\":[\"geopolitical-unit-1\"],\"image_uri\":\"some-uri\",\"object_id\":\"some-ballot-style-id\",\"party_ids\":[\"party-1\"]}],\"contests\":[{\"ballot_placeholders\":[{\"candidate_id\":\"candidate-4-id\",\"object_id\":\"contest-1-placeholder-selection-4-id\",\"sequence_order\":4}],\"ballot_selections\":[{\"candidate_id\":\"candidate-1-id\",\"object_id\":\"contest-1-selection-1-id\",\"sequence_order\":1},{\"candidate_id\":\"candidate-2-id\",\"object_id\":\"contest-1-selection-2-id\",\"sequence_order\":2},{\"candidate_id\":\"candidate-3-id\",\"object_id\":\"contest-1-selection-3-id\",\"sequence_order\":3}],\"ballot_subtitle\":{\"text\":[{\"language\":\"en\",\"value\":\"some-title-string\"},{\"language\":\"es\",\"value\":\"some-title-string-es\"}]},\"ballot_title\":{\"text\":[{\"language\":\"en\",\"value\":\"some-title-string\"},{\"language\":\"es\",\"value\":\"some-title-string-es\"}]},\"electoral_district_id\":\"geopolitical-unit-1\",\"name\":\"contest-1-name\",\"number_elected\":2,\"object_id\":\"contest-1-id\",\"sequence_order\":1,\"vote_variation\":\"n_of_m\",\"votes_allowed\":2},{\"ballot_placeholders\":[{\"candidate_id\":\"candidate-3-id\",\"object_id\":\"contest-2-placeholder-selection-3-id\",\"sequence_order\":3}],\"ballot_selections\":[{\"candidate_id\":\"candidate-1-id\",\"object_id\":\"contest-2-selection-1-id\",\"sequence_order\":1},{\"candidate_id\":\"candidate-2-id\",\"object_id\":\"contest-2-selection-2-id\",\"sequence_order\":2}],\"ballot_subtitle\":{\"text\":[{\"language\":\"en\",\"value\":\"some-title-string\"},{\"language\":\"es\",\"value\":\"some-title-string-es\"}]},\"ballot_title\":{\"text\":[{\"language\":\"en\",\"value\":\"some-title-string\"},{\"language\":\"es\",\"value\":\"some-title-string-es\"}]},\"electoral_district_id\":\"geopolitical-unit-1\",\"name\":\"contest-2-name\",\"number_elected\":2,\"object_id\":\"contest-2-id\",\"sequence_order\":2,\"vote_variation\":\"n_of_m\",\"votes_allowed\":2}],\"manifest_hash\":\"02\",\"geopolitical_units\":[{\"contact_information\":{\"address_line\":[\"some-street\",\"some-city\",\"some-whatever\"],\"name\":\"gp-unit-contact-info\"},\"name\":\"district-1-id\",\"object_id\":\"geopolitical-unit-1\",\"type\":\"city\"}]}";

            var result = new InternalManifest(data);

            // Assert

            Assert.That(result.ManifestHash.ToHex() == "02");
        }
Ejemplo n.º 5
0
        /// <inheritdoc />
        /// <summary>
        /// The folder relative to the executable where file assets will be loaded from.
        /// </summary>
        /// <param name="folder">The folder to load assets from.</param>
        public FileAssetSource(string folder)
        {
            Folder = folder;

            // Check if folder exists.
            if (!Directory.Exists(Folder))
            {
                Directory.CreateDirectory(Folder);
            }

            // Populate internal manifest.
            string[] files = Directory.GetFiles(Folder, "*", SearchOption.AllDirectories);
            files.AsParallel().ForAll(x => InternalManifest.TryAdd(FilePathToCrossPlatform(x), x));
        }
        /// <inheritdoc />
        public override DateTime GetAssetModified(string enginePath)
        {
            // Convert to file path.
            bool found = InternalManifest.TryGetValue(enginePath, out string filePath);

            // Check if found.
            if (!found)
            {
                return(base.GetAssetModified(enginePath));
            }

            // The API actually allows for a file to be modified before it was created. lol
            DateTime lastWrite    = File.GetLastWriteTimeUtc(filePath);
            DateTime creationTime = File.GetCreationTimeUtc(filePath);
            int      later        = DateTime.Compare(lastWrite, creationTime);

            return(later > 0 ? lastWrite : creationTime);
        }
        /// <summary>
        /// Create a new source which loads assets embedded into the assembly.
        /// </summary>
        /// <param name="assembly">The assembly to load assets from.</param>
        /// <param name="folder">The root embedded folder.</param>
        public EmbeddedAssetSource(Assembly assembly, string folder)
        {
            Folder   = folder.Replace("/", "$").Replace("\\", "$").Replace("$", ".");
            Assembly = assembly;

            string assemblyFullName = assembly.FullName;

            Name = assemblyFullName.Substring(0, assemblyFullName.IndexOf(",", StringComparison.Ordinal));

            // Populate internal manifest.
            Assembly.GetManifestResourceNames().AsParallel().ForAll(x =>
            {
                string enginePath = EmbeddedPathToEnginePath(x);

                if (enginePath != null)
                {
                    InternalManifest.TryAdd(enginePath, x);
                }
            });
        }
        /// <inheritdoc />
        public override byte[] GetAsset(string enginePath)
        {
            // Convert to file path.
            bool found = InternalManifest.TryGetValue(enginePath, out string filePath);

            // Check if found.
            if (!found)
            {
                return(new byte[0]);
            }

            // Check if exists.
            if (File.Exists(filePath))
            {
                return(File.ReadAllBytes(filePath));
            }

            Engine.Log.Error($"Couldn't read asset {enginePath} with file path {filePath}.", MessageSource.AssetLoader);
            return(new byte[0]);
        }
Ejemplo n.º 9
0
        public void Test_Compact_Encrypt_Ballot_Simple_Succeeds()
        {
            var keypair     = ElGamalKeyPair.FromSecret(Constants.TWO_MOD_Q);
            var description = new Manifest(jsonManifest);
            var manifest    = new InternalManifest(description);
            var context     = new CiphertextElectionContext(1UL, 1UL, keypair.PublicKey, Constants.TWO_MOD_Q, manifest.ManifestHash);
            var device      = new EncryptionDevice(12345UL, 23456UL, 34567UL, "Location");
            var mediator    = new EncryptionMediator(manifest, context, device);

            var ballot = new PlaintextBallot(plaintext);

            // Act
            var compact = mediator.CompactEncrypt(ballot);

            // Assert
            Assert.That(compact.ObjectId == ballot.ObjectId);

            var msgpack     = compact.ToMsgPack();
            var fromMsgpack = new CompactCiphertextBallot(msgpack);

            Assert.That(compact.ObjectId == fromMsgpack.ObjectId);
        }
 protected void PopulateInternalManifest()
 {
     InternalManifest.Clear();
     string[] files = Directory.GetFiles(Folder, "*", SearchOption.AllDirectories);
     files.AsParallel().ForAll(x => InternalManifest.TryAdd(FilePathToEnginePath(x), x));
 }