<commit_before>require 'test_helper'
require 'xml_security'

class XmlSecurityTest < Test::Unit::TestCase
  include XMLSecurity

  context "XmlSecurity" do
    setup do
      @document = XMLSecurity::SignedDocument.new(Base64.decode64(response_document))
      @base64cert = @document.elements["//ds:X509Certificate"].text
    end

    should "should run validate without throwing NS related exceptions" do
      assert !@document.validate_signature(@base64cert, true)
    end

    should "should run validate with throwing NS related exceptions" do
      assert_raise(OneLogin::RubySaml::ValidationError) do
        @document.validate_signature(@base64cert, false)
      end
    end

    should "not raise an error when softly validating the document multiple times" do
      assert_nothing_raised do
        2.times { @document.validate_signature(@base64cert, true) }
      end
    end

    should "should raise Fingerprint mismatch" do
      exception = assert_raise(OneLogin::RubySaml::ValidationError) do
        @document.validate_document("no:fi:ng:er:pr:in:t", false)
      end
      assert_equal("Fingerprint mismatch", exception.message)
    end

    should "should raise Digest mismatch" do
      exception = assert_raise(OneLogin::RubySaml::ValidationError) do
        @document.validate_signature(@base64cert, false)
      end
      assert_equal("Digest mismatch", exception.message)
    end

    should "should raise Key validation error" do
      response = Base64.decode64(response_document)
      response.sub!("<ds:DigestValue>pJQ7MS/ek4KRRWGmv/H43ReHYMs=</ds:DigestValue>",
                    "<ds:DigestValue>b9xsAXLsynugg3Wc1CI3kpWku+0=</ds:DigestValue>")
      document = XMLSecurity::SignedDocument.new(response)
      base64cert = document.elements["//ds:X509Certificate"].text
      exception = assert_raise(OneLogin::RubySaml::ValidationError) do
        document.validate_signature(base64cert, false)
      end
      assert_equal("Key validation error", exception.message)
    end

    should "raise validation error when the X509Certificate is missing" do<commit_msg>add tests for @errors object<commit_after>require 'test_helper'
require 'xml_security'

class XmlSecurityTest < Test::Unit::TestCase
  include XMLSecurity

  context "XmlSecurity" do
    setup do
      @document = XMLSecurity::SignedDocument.new(Base64.decode64(response_document))
      @base64cert = @document.elements["//ds:X509Certificate"].text
    end

    should "should run validate without throwing NS related exceptions" do
      assert !@document.validate_signature(@base64cert, true)
    end

    should "should run validate with throwing NS related exceptions" do
      assert_raise(OneLogin::RubySaml::ValidationError) do
        @document.validate_signature(@base64cert, false)
      end
    end

    should "not raise an error when softly validating the document multiple times" do
      assert_nothing_raised do
        2.times { @document.validate_signature(@base64cert, true) }
      end
    end

    should "should raise Fingerprint mismatch" do
      exception = assert_raise(OneLogin::RubySaml::ValidationError) do
        @document.validate_document("no:fi:ng:er:pr:in:t", false)
      end
      assert_equal("Fingerprint mismatch", exception.message)
      assert @document.errors.include? "Fingerprint mismatch"
    end

    should "should raise Digest mismatch" do
      exception = assert_raise(OneLogin::RubySaml::ValidationError) do
        @document.validate_signature(@base64cert, false)
      end
      assert_equal("Digest mismatch", exception.message)
      assert @document.errors.include? "Digest mismatch"
    end

    should "should raise Key validation error" do
      response = Base64.decode64(response_document)
      response.sub!("<ds:DigestValue>pJQ7MS/ek4KRRWGmv/H43ReHYMs=</ds:DigestValue>",
                    "<ds:DigestValue>b9xsAXLsynugg3Wc1CI3kpWku+0=</ds:DigestValue>")
      document = XMLSecurity::SignedDocument.new(response)
      base64cert = document.elements["//ds:X509Certificate"].text
      exception = assert_raise(OneLogin::RubySaml::ValidationError) do
        document.validate_signature(base64cert, false)
      end
      assert_equal("Key validation error", exception.message)
      assert document.errors.include? "Key validation error"
    end

    should "raise validation error when the X509Certificate is missing" do
{'fraction_non_alphanumeric': 0.08981080465010258, 'fraction_numerical': 0.018691588785046728, 'mean_word_length': 3.8973214285714284, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5687217', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1212, 'n_words': 290}