Esempio n. 1
0
        /// <summary>
        /// Creates a file-comp instruction and parses its content.
        /// </summary>
        /// <param name="xn">The XML repersentation of the instruction to be checked</param>
        /// <param name="con">The current context object</param>
        static FileComp CreateFileComp(XmlNode xn, Context con)
        {
            FileComp fc = new FileComp();

            fc.Of = XmlFiler.getAttribute(xn, "of");
            Logger.getOnly().isNotNull(fc.Of, "File-Comp instruction must have an 'of'.");
            Logger.getOnly().isTrue(fc.Of != "", "File-Comp instruction must have a non-empty 'of'.");
            fc.To = XmlFiler.getAttribute(xn, "to");
            Logger.getOnly().isNotNull(fc.To, "File-Comp instruction must have a 'to'.");
            Logger.getOnly().isTrue(fc.To != "", "File-Comp instruction must have a non-empty 'to'.");
            fc.OnPass = XmlFiler.getAttribute(xn, "on-pass");
            fc.OnFail = XmlFiler.getAttribute(xn, "on-fail");
            string rest = XmlFiler.getAttribute(xn, "wait");

            if (rest != null)
            {
                fc.Rest = Convert.ToInt32(rest);
            }
            InterpretMessage(fc, xn.ChildNodes);
            AddInstruction(xn, fc, con);
            return(fc);
        }
Esempio n. 2
0
        public override void Execute()
        {
            base.Execute();
            PassFailInContext(m_onPass, m_onFail, out m_onPass, out m_onFail);
            m_log.isNotNull(m_of, makeNameTag() + " 'of' file name is null");
            m_log.isNotNull(m_to, makeNameTag() + " 'to' file name is null");
            m_log.isFalse(m_of == "", makeNameTag() + " 'of' file name is empty");
            m_log.isFalse(m_to == "", makeNameTag() + " 'to' file name is empty");
            m_baseStr   = Utilities.evalExpr(m_of);
            m_targetStr = Utilities.evalExpr(m_to);
            // m_baseStr and m_targetStr can be null
            MD5 md5Hasher = MD5.Create();

            try{ Application.Process.WaitForInputIdle(); }
            catch (Win32Exception e)
            { m_log.paragraph(makeNameTag() + " WaitForInputIdle: " + e.Message); }

            string[] ext      = m_baseStr.Split('.');
            int      lastSub  = ext.Length;
            string   fileType = ext[lastSub - 1];
            string   content1 = null;
            string   content2 = null;

            try
            {
                content1 = System.IO.File.ReadAllText(m_baseStr);
                content2 = System.IO.File.ReadAllText(m_targetStr);
            }
            catch (FileNotFoundException e) { m_log.fail(makeNameTag() + e.FileName + " not found"); }
            catch (DirectoryNotFoundException e) { m_log.fail(makeNameTag() + e.ToString() + " not found"); }

            if (fileType.ToLower() == "pdf")
            {             // Cut out date and crc file integrity hashes - they always change
                // obj 0a 3c 3c 2f Producer(PrimoPDF)           0a 3e 3e 0a
                // trailer 0a 3c 3c 20            0a 3e 3e 0a
                m_log.paragraph(makeNameTag() + " comparing PDF files: " + m_of + " and " + m_to);
                Regex  rx       = null;
                Regex  rx2      = null;
                string pattern  = "(<</Producer\\(PrimoPDF\\)[^>]*>>)";
                string pattern2 = "(trailer[\\s\\S]<<[\\s\\S]*>>)";
                try { rx  = new Regex(pattern, System.Text.RegularExpressions.RegexOptions.Multiline);
                      rx2 = new Regex(pattern2, System.Text.RegularExpressions.RegexOptions.Multiline); }
                catch (ArgumentException e)
                { m_log.paragraph(makeNameTag() + e.Message); }
                FileComp       fc  = new FileComp();
                MatchEvaluator cut = new MatchEvaluator(fc.cutMatch);
                if (!rx.IsMatch(content1))
                {
                    m_log.paragraph(makeNameTag() + " no match in PDF file for pattern: " + pattern);
                }
                content1 = rx.Replace(content1, cut);
                content2 = rx.Replace(content2, cut);
                if (!rx2.IsMatch(content1))
                {
                    m_log.paragraph(makeNameTag() + " no match in PDF file for pattern2: " + pattern2);
                }
                content1 = rx2.Replace(content1, cut);
                content2 = rx2.Replace(content2, cut);
            }
            System.Text.Encoding encoding = new System.Text.UnicodeEncoding();
            byte[] hash1 = md5Hasher.ComputeHash(encoding.GetBytes(content1));
            byte[] hash2 = md5Hasher.ComputeHash(encoding.GetBytes(content2));

            string shash1 = getString(hash1);
            string shash2 = getString(hash2);

            m_Result  = shash1 == shash2;
            m_message = null;

            m_log.paragraph(makeNameTag() + "hashOf = " + shash1 + " hashTo = " + shash2);
            if (m_message != null)
            {
                m_log.paragraph(makeNameTag() + "Message is not null");
            }
            Finished = true;             // tell do-once it's done

            if ((m_onPass == "assert" && m_Result == true) ||
                (m_onFail == "assert" && m_Result == false))
            {
                if (m_message != null)
                {
                    m_log.fail(makeNameTag() + m_message.Read());
                }
                else
                {
                    m_log.fail(makeNameTag() + "File-Comp Assert: Result = '" + m_Result + "', on-pass='******', on-fail='" + m_onFail + "', hashOf='"
                               + shash1 + "', hashTo='" + shash2 + "'");
                }
            }
            m_log.result(this);
        }
Esempio n. 3
0
		/// <summary>
		/// Creates a file-comp instruction and parses its content.
		/// </summary>
		/// <param name="xn">The XML repersentation of the instruction to be checked</param>
		/// <param name="con">The current context object</param>
		static FileComp CreateFileComp(XmlNode xn, Context con)
		{
			FileComp fc = new FileComp();
			fc.Of = XmlFiler.getAttribute(xn, "of");
			Logger.getOnly().isNotNull(fc.Of, "File-Comp instruction must have an 'of'.");
			Logger.getOnly().isTrue(fc.Of != "", "File-Comp instruction must have a non-empty 'of'.");
			fc.To  = XmlFiler.getAttribute(xn, "to");
			Logger.getOnly().isNotNull(fc.To, "File-Comp instruction must have a 'to'.");
			Logger.getOnly().isTrue(fc.To != "", "File-Comp instruction must have a non-empty 'to'.");
			fc.OnPass = XmlFiler.getAttribute(xn, "on-pass");
			fc.OnFail = XmlFiler.getAttribute(xn, "on-fail");
			string rest = XmlFiler.getAttribute(xn, "wait");
			if (rest != null) fc.Rest = Convert.ToInt32(rest);
			InterpretMessage(fc,xn.ChildNodes);
			AddInstruction(xn, fc, con);
			return fc;
		}
Esempio n. 4
0
        public override void Execute()
        {
            base.Execute();
            PassFailInContext(m_onPass, m_onFail, out m_onPass, out m_onFail);
            m_log.isNotNull(m_of, makeNameTag() + " 'of' file name is null");
            m_log.isNotNull(m_to, makeNameTag() + " 'to' file name is null");
            m_log.isFalse(m_of == "", makeNameTag() + " 'of' file name is empty");
            m_log.isFalse(m_to == "", makeNameTag() + " 'to' file name is empty");
            m_baseStr = Utilities.evalExpr(m_of);
            m_targetStr = Utilities.evalExpr(m_to);
            // m_baseStr and m_targetStr can be null
            MD5 md5Hasher = MD5.Create();

            try{Application.Process.WaitForInputIdle();}
            catch (Win32Exception e)
            { m_log.paragraph(makeNameTag() + " WaitForInputIdle: " + e.Message); }

            string[] ext = m_baseStr.Split('.');
            int lastSub = ext.Length;
            string fileType = ext[lastSub - 1];
            string content1 = null;
            string content2 = null;
            try
            {
                content1 = System.IO.File.ReadAllText(m_baseStr);
                content2 = System.IO.File.ReadAllText(m_targetStr);
            }
            catch (FileNotFoundException e) { m_log.fail(makeNameTag() + e.FileName + " not found"); }
            catch (DirectoryNotFoundException e) { m_log.fail(makeNameTag() + e.ToString() + " not found"); }

            if (fileType.ToLower() == "pdf")
            { // Cut out date and crc file integrity hashes - they always change
                // obj 0a 3c 3c 2f Producer(PrimoPDF)           0a 3e 3e 0a
                // trailer 0a 3c 3c 20            0a 3e 3e 0a
                m_log.paragraph(makeNameTag() + " comparing PDF files: " + m_of + " and " + m_to);
                Regex rx = null;
                Regex rx2 = null;
                string pattern = "(<</Producer\\(PrimoPDF\\)[^>]*>>)";
                string pattern2 = "(trailer[\\s\\S]<<[\\s\\S]*>>)";
                try { rx = new Regex(pattern, System.Text.RegularExpressions.RegexOptions.Multiline);
                      rx2 = new Regex(pattern2, System.Text.RegularExpressions.RegexOptions.Multiline); }
                catch (ArgumentException e)
                { m_log.paragraph(makeNameTag() + e.Message); }
                FileComp fc = new FileComp();
                MatchEvaluator cut = new MatchEvaluator(fc.cutMatch);
                if (!rx.IsMatch(content1))
                    m_log.paragraph(makeNameTag() + " no match in PDF file for pattern: " + pattern);
                content1 = rx.Replace(content1, cut);
                content2 = rx.Replace(content2, cut);
                if (!rx2.IsMatch(content1))
                    m_log.paragraph(makeNameTag() + " no match in PDF file for pattern2: " + pattern2);
                content1 = rx2.Replace(content1, cut);
                content2 = rx2.Replace(content2, cut);
            }
            System.Text.Encoding encoding = new System.Text.UnicodeEncoding();
            byte[] hash1 = md5Hasher.ComputeHash(encoding.GetBytes(content1));
            byte[] hash2 = md5Hasher.ComputeHash(encoding.GetBytes(content2));

            string shash1 = getString(hash1);
            string shash2 = getString(hash2);
            m_Result = shash1 == shash2;
            m_message = null;

            m_log.paragraph(makeNameTag() + "hashOf = " + shash1 + " hashTo = " + shash2);
            if (m_message != null) m_log.paragraph(makeNameTag() + "Message is not null");
            Finished = true; // tell do-once it's done

            if ((m_onPass == "assert" && m_Result == true)
                || (m_onFail == "assert" && m_Result == false))
            {
                if (m_message != null)
                    m_log.fail(makeNameTag() + m_message.Read());
                else
                    m_log.fail(makeNameTag() + "File-Comp Assert: Result = '" + m_Result + "', on-pass='******', on-fail='" + m_onFail + "', hashOf='"
                        + shash1 + "', hashTo='" + shash2 + "'");
            }
            m_log.result(this);
        }