From 717194bdb65410ce70bf78453315206d132b7bd0 Mon Sep 17 00:00:00 2001
From: Anuj Borah <aborah@redhat.com>
Date: Nov 26 2019 00:52:03 +0000
Subject: Issue: 48851 - investigate and port TET matching rules filter tests(cert)


Investigate and port TET matching rules filter tests(cert)

Relates: https://pagure.io/389-ds-base/issue/48851

Author: aborah

Reviewed by: ???

---

diff --git a/dirsrvtests/tests/suites/filter/filter_cert_test.py b/dirsrvtests/tests/suites/filter/filter_cert_test.py
new file mode 100644
index 0000000..b2e80a2
--- /dev/null
+++ b/dirsrvtests/tests/suites/filter/filter_cert_test.py
@@ -0,0 +1,69 @@
+# --- BEGIN COPYRIGHT BLOCK ---
+# Copyright (C) 2019 Red Hat, Inc.
+# All rights reserved.
+#
+# License: GPL (version 3 or any later version).
+# See LICENSE for details.
+# --- END COPYRIGHT BLOCK ----
+
+
+"""
+verify and testing  Filter from a search
+"""
+
+import os
+import pytest
+
+from lib389._constants import DEFAULT_SUFFIX
+from lib389.topologies import topology_st as topo
+from lib389.idm.user import UserAccounts
+from lib389.idm.account import Accounts
+from lib389.nss_ssl import NssSsl
+from lib389.utils import search_filter_scape_bytes
+
+pytestmark = pytest.mark.tier1
+
+
+def test_positive(topo):
+    """Test User certificate field
+        :id: abe3e6de-9ecc-11e8-adf0-8c16451d917
+        :setup: Standalone
+        :steps:
+            1. Create entries with userCertificate field.
+            2. Try to search/filter them with userCertificate field.
+        :expected results:
+            1. Pass
+            2. Pass
+    """
+    # SETUP TLS
+    topo.standalone.stop()
+    NssSsl(topo.standalone).reinit()
+    NssSsl(topo.standalone).create_rsa_ca()
+    NssSsl(topo.standalone).create_rsa_key_and_cert()
+    # Create  user
+    NssSsl(topo.standalone).create_rsa_user('testuser1')
+    NssSsl(topo.standalone).create_rsa_user('testuser2')
+    # Creating cert users
+    topo.standalone.start()
+    users_people = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
+    for count in range(1, 3):
+        user = users_people.create_test_user(uid=count, gid=count)
+        tls_locs = NssSsl(topo.standalone).get_rsa_user(f'testuser{count}')
+        #  {'ca': ca_path, 'key': key_path, 'crt': crt_path}
+        user.enroll_certificate(tls_locs['crt_der_path'])
+
+    assert Accounts(topo.standalone, DEFAULT_SUFFIX).filter("(usercertificate=*)")
+    assert Accounts(topo.standalone, DEFAULT_SUFFIX).filter("(userCertificate;binary=*)")
+    user1_cert = users_people.list()[0].get_attr_val("userCertificate;binary")
+    assert Accounts(topo.standalone, DEFAULT_SUFFIX).filter(
+        f'(userCertificate;binary={search_filter_scape_bytes(user1_cert)})')[0].dn == \
+           'uid=test_user_1,ou=People,dc=example,dc=com'
+    user2_cert = users_people.list()[1].get_attr_val("userCertificate;binary")
+    assert Accounts(topo.standalone, DEFAULT_SUFFIX).filter(
+        f'(userCertificate;binary={search_filter_scape_bytes(user2_cert)})')[0].dn == \
+           'uid=test_user_2,ou=People,dc=example,dc=com'
+
+
+if __name__ == '__main__':
+    CURRENT_FILE = os.path.realpath(__file__)
+    pytest.main("-s -v %s" % CURRENT_FILE)
diff --git a/src/lib389/lib389/tests/utils_test.py b/src/lib389/lib389/tests/utils_test.py
index 5378066..a696eb5 100644
--- a/src/lib389/lib389/tests/utils_test.py
+++ b/src/lib389/lib389/tests/utils_test.py
@@ -145,6 +145,35 @@ def test_get_log_data(data):
     assert display_log_data(before) == after
 
 
+@pytest.mark.parametrize('ds_ver, cmp_ver', [
+    ('1.3.1', '1.3.2'),
+    ('1.3.1', '1.3.10'),
+    ('1.3.2', '1.3.10'),
+    ('1.3.9', ('1.3.10', '1.4.2.0')),
+    ('1.4.0.1', ('1.3.9', '1.4.1.0', '1.4.2.1')),
+    ('1.4.1', '1.4.2.0-20191115gitbadc0ffee' ),
+])
+def test_ds_is_older_versions(ds_ver, cmp_ver):
+    if isinstance(cmp_ver, tuple):
+        assert ds_is_related('older', ds_ver, *cmp_ver)
+    else:
+        assert ds_is_related('older', ds_ver, cmp_ver)
+
+@pytest.mark.parametrize('ds_ver, cmp_ver', [
+    ('1.3.2', '1.3.1'),
+    ('1.3.10', '1.3.1'),
+    ('1.3.10', '1.3.2'),
+    ('1.3.10', ('1.3.9', '1.4.2.0')),
+    ('1.4.2.1', ('1.3.9', '1.4.0.1', '1.4.2.0')),
+    ('1.4.2.0-20191115gitbadc0ffee', '1.4.1' ),
+])
+def test_ds_is_newer_versions(ds_ver, cmp_ver):
+    if isinstance(cmp_ver, tuple):
+        assert ds_is_related('newer', ds_ver, *cmp_ver)
+    else:
+        assert ds_is_related('newer', ds_ver, cmp_ver)
+
+
 if __name__ == "__main__":
     CURRENT_FILE = os.path.realpath(__file__)
     pytest.main("-s -v %s" % CURRENT_FILE)

