Ejemplo n.º 1
0
        public void Output_Timing_Test()
        {
            // may fail if implementation is broken
            // see http://alabaxblog.info/2013/06/redirectstandardoutput-beginoutputreadline-pattern-broken/

            int timeout = 1000;
            int runs = 500;

            for (int i = 0; i < runs; ++i)
            {
                string testString = Guid.NewGuid().ToString();

                using (var proc = new RedirectedProcess("cmd.exe", string.Format("/c echo {0}", testString)))
                {
                    if (proc.Start())
                    {
                        proc.WaitForExit(timeout);

                        Assert.AreEqual(0, proc.ExitCode, "Wrong exit code.");

                        Assert.AreEqual(testString, proc.Output.TrimEnd(), "Wrong output."); // may fail here

                        Assert.AreEqual(string.Empty, proc.Error.TrimEnd(), "Wrong error.");
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Processes data with file input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <exception cref="System.ArgumentNullException">input</exception>
        /// <exception cref="PgpException"></exception>
        public void ProcessData(FileDataInput input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            input.Verify();
            using (var proc = new RedirectedProcess(GnuPGConfig.GnuPGExePath, CreateDataCommandLineArgs(input)))
            {
                if (proc.Start())
                {
                    if (input.NeedsPassphrase)
                    {
                        proc.Input.WriteSecret(input.Passphrase);
                        proc.Input.Write(Environment.NewLine);
                        proc.Input.Flush();
                    }
                    proc.WaitForExit();

                    Debug.WriteLine("gpg output: " + proc.Output);
                    Debug.WriteLine("gpg exit code: " + proc.ExitCode);
                    var error = proc.Error;
                    if (!string.IsNullOrEmpty(error) && proc.ExitCode != 0)
                    {
                        Debug.WriteLine("gpg error: " + error);
                        throw new PgpException(error);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void Output_Timing_Test()
        {
            // may fail if implementation is broken
            // see http://alabaxblog.info/2013/06/redirectstandardoutput-beginoutputreadline-pattern-broken/

            int timeout = 1000;
            int runs    = 500;


            for (int i = 0; i < runs; ++i)
            {
                string testString = Guid.NewGuid().ToString();

                using (var proc = new RedirectedProcess("cmd.exe", string.Format("/c echo {0}", testString)))
                {
                    if (proc.Start())
                    {
                        proc.WaitForExit(timeout);

                        Assert.AreEqual(0, proc.ExitCode, "Wrong exit code.");

                        Assert.AreEqual(testString, proc.Output.TrimEnd(), "Wrong output."); // may fail here

                        Assert.AreEqual(string.Empty, proc.Error.TrimEnd(), "Wrong error.");
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Lists the known keys.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <returns></returns>
        public IEnumerable<KeyId> ListKeys(KeyTarget target)
        {
            var args = "--fixed-list-mode --with-colons --with-fingerprint --list-public-keys";
            var keyHead = "pub";
            if (target == KeyTarget.Secret)
            {
                args = "--fixed-list-mode --with-colons --with-fingerprint --list-secret-keys";
                keyHead = "sec";
            }
            if (!string.IsNullOrWhiteSpace(KeyringFolder))
            {
                args += string.Format(" --homedir \"{0}\" ", KeyringFolder);
            }

            using (var proc = new RedirectedProcess(GnuPGConfig.GnuPGExePath, args))
            {
                if (proc.Start())
                {
                    proc.WaitForExit();

                    string[] lines = proc.Output.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < lines.Length; i++)
                    {
                        var fields = lines[i].Split(':');
                        if (fields[0] == keyHead)
                        {
                            var id = fields[4];
                            var allowCap = KeyCapabilities.None;
                            var useCap = KeyCapabilities.None;
                            ParseKeyCapField(fields[11], ref allowCap, ref useCap);
                            i++;

                            // read more lines as part of this key
                            string finger = null;
                            List<string> users = new List<string>();
                            for (; i < lines.Length; i++)
                            {
                                fields = lines[i].Split(':');
                                if (fields[0] == keyHead)
                                {
                                    // another key, exit loop
                                    i--;
                                    break;
                                }
                                switch (fields[0])
                                {
                                    case "uid":
                                        users.Add(IOUtility.DecodeAsciiEscapes(fields[9]));
                                        break;
                                    case "fpr":
                                        finger = fields[9];
                                        break;
                                }
                            }

                            yield return new KeyId(id, finger, allowCap, useCap, users);
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Processes data with file input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <exception cref="System.ArgumentNullException">input</exception>
        /// <exception cref="PgpException"></exception>
        public void ProcessData(FileDataInput input)
        {
            if (input == null) { throw new ArgumentNullException("input"); }
            input.Verify();
            using (var proc = new RedirectedProcess(GnuPGConfig.GnuPGExePath, CreateDataCommandLineArgs(input)))
            {
                if (proc.Start())
                {
                    if (input.NeedsPassphrase)
                    {
                        proc.Input.WriteSecret(input.Passphrase);
                        proc.Input.Write(Environment.NewLine);
                        proc.Input.Flush();
                    }
                    proc.WaitForExit();

                    Debug.WriteLine("gpg output: " + proc.Output);
                    Debug.WriteLine("gpg exit code: " + proc.ExitCode);
                    var error = proc.Error;
                    if (!string.IsNullOrEmpty(error) && proc.ExitCode != 0)
                    {
                        Debug.WriteLine("gpg error: " + error);
                        throw new PgpException(error);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Lists the known keys.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <returns></returns>
        public IEnumerable <KeyId> ListKeys(KeyTarget target)
        {
            var args    = "--fixed-list-mode --with-colons --with-fingerprint --list-public-keys";
            var keyHead = "pub";

            if (target == KeyTarget.Secret)
            {
                args    = "--fixed-list-mode --with-colons --with-fingerprint --list-secret-keys";
                keyHead = "sec";
            }
            if (!string.IsNullOrWhiteSpace(KeyringFolder))
            {
                args += string.Format(" --homedir \"{0}\" ", KeyringFolder);
            }

            using (var proc = new RedirectedProcess(GnuPGConfig.GnuPGExePath, args))
            {
                if (proc.Start())
                {
                    proc.WaitForExit();

                    string[] lines = proc.Output.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < lines.Length; i++)
                    {
                        var fields = lines[i].Split(':');
                        if (fields[0] == keyHead)
                        {
                            var id       = fields[4];
                            var allowCap = KeyCapabilities.None;
                            var useCap   = KeyCapabilities.None;
                            ParseKeyCapField(fields[11], ref allowCap, ref useCap);
                            i++;

                            // read more lines as part of this key
                            string        finger = null;
                            List <string> users  = new List <string>();
                            for (; i < lines.Length; i++)
                            {
                                fields = lines[i].Split(':');
                                if (fields[0] == keyHead)
                                {
                                    // another key, exit loop
                                    i--;
                                    break;
                                }
                                switch (fields[0])
                                {
                                case "uid":
                                    users.Add(IOUtility.DecodeAsciiEscapes(fields[9]));
                                    break;

                                case "fpr":
                                    finger = fields[9];
                                    break;
                                }
                            }

                            yield return(new KeyId(id, finger, allowCap, useCap, users));
                        }
                    }
                }
            }
        }