<commit_before>require 'spec_helper'

describe Gitlab::LDAP::DN do
  using RSpec::Parameterized::TableSyntax

  describe '#initialize' do
    subject { described_class.new(given).to_s_normalized }

    # Regarding the telephoneNumber test:
    #
    # I am not sure whether a space after the telephoneNumber plus sign is valid,
    # and I am not sure if this is "proper" behavior under these conditions, and
    # I am not sure if it matters to us or anyone else, so rather than dig
    # through RFCs, I am only documenting the behavior here.
    where(:test_description, :given, :expected) do
      'strips extraneous whitespace'                                                                 | 'uid     =<NAME> ,  ou = People, dc=  example,dc =com'                                            | 'uid=<NAME>,ou=people,dc=example,dc=com'
      'strips extraneous whitespace for a DN with a single RDN'                                      | 'uid  =  <NAME>'                                                                                  | 'uid=<NAME>'
      'unescapes non-reserved, non-special Unicode characters'                                       | 'uid   =  Sebasti\\c3\\a1n\\ C.\\20Smith\\   ,   ou=People (aka. \\22humans\\")  ,dc=example, dc=com' | 'uid=sebastián c. smith  \\ ,ou=people (aka. \\"humans\\"),dc=example,dc=com'
      'downcases the whole string'                                                                   | 'UID=<NAME>,ou=People,dc=example,dc=com'                                                          | 'uid=<NAME>,ou=people,dc=example,dc=com'
      'for a null DN (empty string), returns empty string and does not error'                        | ''                                                                                                    | ''
      'does not strip an escaped leading space in an attribute value'                                | 'uid=\\ <NAME>,ou=People,dc=example,dc=com'                                                       | 'uid=\\ <NAME>,ou=people,dc=example,dc=com'
      'does not strip an escaped trailing space in an attribute value'                               | 'uid=<NAME>\\ ,ou=People,dc=example,dc=com'                                                       | 'uid=<NAME>\\ ,ou=people,dc=example,dc=com'
      'does not strip an escaped leading newline in an attribute value'                              | 'uid=\\\n<NAME>,ou=People,dc=example,dc=com'                                                      | 'uid=\\\n<NAME>,ou=people,dc=example,dc=com'
      'does not strip an escaped trailing newline in an attribute value'                             | 'uid=<NAME>\\\n,ou=People,dc=example,dc=com'                                                      | 'uid=<NAME>\\\n,ou=people,dc=example,dc=com'
      'does not strip an unescaped leading newline (actually an invalid DN)'                         | 'uid=\n<NAME>,ou=People,dc=example,dc=com'                                                                               | 'uid=\n<NAME>,ou=people,dc=example,dc=com'
      'does not strip an unescaped trailing newline (actually an invalid DN)'                        | 'uid=<NAME>\n ,ou=People,dc=example,dc=com'                                                                              | 'uid=<NAME>\n,ou=people,dc=example,dc=com'
      'does not strip if no extraneous whitespace'                                                   | 'uid=<NAME>,ou=People,dc=example,dc=com'                                                          | 'uid=<NAME>,ou=people,dc=example,dc=com'
      'does not modify an escaped equal sign in an attribute value'                                  | 'uid= foo  \\=  bar'                                                                                  | 'uid=foo  \\=  bar'
      'converts an escaped hex equal sign to an escaped equal sign in an attribute value'            | 'uid= foo  \\3D  bar'                                                                                 | 'uid=foo  \\=  bar'
      'does not modify an escaped comma in an attribute value'                                       | 'uid= <NAME>, ou=San Francisco\\, CA'                                                          | 'uid=<NAME>,ou=san francisco\\, ca'
      'converts an escaped hex comma to an escaped comma in an attribute value'                      | 'uid= <NAME>, ou=San Francisco\\2C CA'                                                         | 'uid=<NAME>,ou=san francisco\\, ca'
      'does not modify an escaped hex carriage return character in an attribute value'               | 'uid= <NAME>, ou=San Francisco\\,\\0DCA'                                                       | 'uid=<NAME>,ou=san francisco\\,\\0dca'
      'does not modify an escaped hex line feed character in an attribute value'                     | 'uid= <NAME>, ou=San Francisco\\,\\0ACA'                                                       | 'uid=<NAME>,ou=san francisco\\,\\0aca'
      'does not modify an escaped hex CRLF in an attribute value'                                    | 'uid= <NAME>, ou=San Francisco\\,\\0D\\0ACA'                                                   | 'uid=<NAME>,ou=san francisco\\,\\0d\\0aca'
    end

    with_them do
      it 'normalizes the DN' do
        assert_generic_test(test_description, subject, expected)
      end
    end

    context 'when we do not support the given DN format' do
      context 'multivalued RDNs' do
        context 'without extraneous whitespace' do
          let(:given) { 'uid=john smith+telephonenumber=+1 555-555-5555,ou=people,dc=example,dc=com' }

          it 'raises UnsupportedDnFormatError' do
            expect{ subject }.to raise_error(Gitlab::LDAP::UnsupportedDnFormatError)
          end
        end

        context 'with extraneous whitespace' do
          context 'around the phone number plus sign' do
            let(:given) { 'uid = <NAME>  + telephoneNumber  = + 1 555-555-5555 , ou = People,dc=example,dc=com' }

            it 'raises UnsupportedDnFormatError' do
              expect{ subject }.to raise_error(Gitlab::LDAP::UnsupportedDnFormatError)
            end
          end
<commit_msg>Add valid DN tests using OIDs<commit_after>require 'spec_helper'

describe Gitlab::LDAP::DN do
  using RSpec::Parameterized::TableSyntax

  describe '#initialize' do
    subject { described_class.new(given).to_s_normalized }

    # Regarding the telephoneNumber test:
    #
    # I am not sure whether a space after the telephoneNumber plus sign is valid,
    # and I am not sure if this is "proper" behavior under these conditions, and
    # I am not sure if it matters to us or anyone else, so rather than dig
    # through RFCs, I am only documenting the behavior here.
    where(:test_description, :given, :expected) do
      'strips extraneous whitespace'                                                                 | 'uid     =<NAME> ,  ou = People, dc=  example,dc =com'                                            | 'uid=<NAME>,ou=people,dc=example,dc=com'
      'strips extraneous whitespace for a DN with a single RDN'                                      | 'uid  =  <NAME>'                                                                                  | 'uid=<NAME>'
      'unescapes non-reserved, non-special Unicode characters'                                       | 'uid   =  Sebasti\\c3\\a1n\\ C.\\20Smith\\   ,   ou=People (aka. \\22humans\\")  ,dc=example, dc=com' | 'uid=sebastián c. smith  \\ ,ou=people (aka. \\"humans\\"),dc=example,dc=com'
      'downcases the whole string'                                                                   | 'UID=<NAME>,ou=People,dc=example,dc=com'                                                          | 'uid=<NAME>,ou=people,dc=example,dc=com'
      'for a null DN (empty string), returns empty string and does not error'                        | ''                                                                                                    | ''
      'does not strip an escaped leading space in an attribute value'                                | 'uid=\\ <NAME>,ou=People,dc=example,dc=com'                                                       | 'uid=\\ <NAME>,ou=people,dc=example,dc=com'
      'does not strip an escaped trailing space in an attribute value'                               | 'uid=<NAME>\\ ,ou=People,dc=example,dc=com'                                                       | 'uid=<NAME>\\ ,ou=people,dc=example,dc=com'
      'does not strip an escaped leading newline in an attribute value'                              | 'uid=\\\n<NAME>,ou=People,dc=example,dc=com'                                                      | 'uid=\\\n<NAME>,ou=people,dc=example,dc=com'
      'does not strip an escaped trailing newline in an attribute value'                             | 'uid=<NAME>\\\n,ou=People,dc=example,dc=com'                                                      | 'uid=<NAME>\\\n,ou=people,dc=example,dc=com'
      'does not strip an unescaped leading newline (actually an invalid DN)'                         | 'uid=\n<NAME>,ou=People,dc=example,dc=com'                                                                               | 'uid=\n<NAME>,ou=people,dc=example,dc=com'
      'does not strip an unescaped trailing newline (actually an invalid DN)'                        | 'uid=<NAME>\n ,ou=People,dc=example,dc=com'                                                                              | 'uid=<NAME>\n,ou=people,dc=example,dc=com'
      'does not strip if no extraneous whitespace'                                                   | 'uid=<NAME>,ou=People,dc=example,dc=com'                                                          | 'uid=<NAME>,ou=people,dc=example,dc=com'
      'does not modify an escaped equal sign in an attribute value'                                  | 'uid= foo  \\=  bar'                                                                                  | 'uid=foo  \\=  bar'
      'converts an escaped hex equal sign to an escaped equal sign in an attribute value'            | 'uid= foo  \\3D  bar'                                                                                 | 'uid=foo  \\=  bar'
      'does not modify an escaped comma in an attribute value'                                       | 'uid= <NAME>, ou=San Francisco\\, CA'                                                          | 'uid=<NAME>,ou=san francisco\\, ca'
      'converts an escaped hex comma to an escaped comma in an attribute value'                      | 'uid= <NAME>, ou=San Francisco\\2C CA'                                                         | 'uid=john c. smith,ou=san francisco\\, ca'
      'does not modify an escaped hex carriage return character in an attribute value'               | 'uid= <NAME>, ou=San Francisco\\,\\0DCA'                                                       | 'uid=<NAME>,ou=san francisco\\,\\0dca'
      'does not modify an escaped hex line feed character in an attribute value'                     | 'uid= <NAME>, ou=San Francisco\\,\\0ACA'                                                       | 'uid=<NAME>,ou=san francisco\\,\\0aca'
      'does not modify an escaped hex CRLF in an attribute value'                                    | 'uid= <NAME>, ou=San Francisco\\,\\0D\\0ACA'                                                   | 'uid=john c. smith,ou=san francisco\\,\\0d\\0aca'
      'allows attribute type name OIDs'                                                              | '0.9.2342.19200300.100.1.25=Example,0.9.2342.19200300.100.1.25=Com'                                   | '0.9.2342.19200300.100.1.25=example,0.9.2342.19200300.100.1.25=com'
      'strips extraneous whitespace from attribute type name OIDs'                                   | '0.9.2342.19200300.100.1.25 = Example, 0.9.2342.19200300.100.1.25 = Com'                              | '0.9.2342.19200300.100.1.25=example,0.9.2342.19200300.100.1.25=com'
    end

    with_them do
      it 'normalizes the DN' do
        assert_generic_test(test_description, subject, expected)
      end
    end

    context 'when we do not support the given DN format' do
      context 'multivalued RDNs' do
        context 'without extraneous whitespace' do
          let(:given) { 'uid=john smith+telephonenumber=+1 555-555-5555,ou=people,dc=example,dc=com' }

          it 'raises UnsupportedDnFormatError' do
            expect{ subject }.to raise_error(Gitlab::LDAP::UnsupportedDnFormatError)
          end
        end

        context 'with extraneous whitespace' do
          context 'around the phone number plus sign' do
            let(:given) { 'uid = <NAME>  + telephoneNumber  = + 1 555-555-5555 , ou = People,dc=example,dc=com' }

            it 'raises UnsupportedDnFormatError' do
              expect{ subject }.to raise_error(Gitlab::LDAP::UnsupportedDnFormatError)
            end
          end

{'fraction_non_alphanumeric': 0.10325077399380805, 'fraction_numerical': 0.01826625386996904, 'mean_word_length': 1.266842105263158, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 130, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4380046', 'n_tokens_mistral': 3346, 'n_tokens_neox': 3104, 'n_words': 901}