>From 4f23a7c8e4461e1db0574360a00e218fad385ec6 Mon Sep 17 00:00:00 2001
From: Simo Sorce <simo@redhat.com>
Date: Mon, 14 Jan 2013 16:47:51 -0500
Subject: [PATCH 06/12] Remove sysdb_subdom completely

struct sss_domain_info is always used to represent domains now.
Adjust tests accordingly.
---
 src/db/sysdb.h                     |  12 +---
 src/db/sysdb_subdomains.c          |  24 ++------
 src/providers/ipa/ipa_subdomains.c | 110 +++++++++++++++++++------------------
 src/tests/sysdb-tests.c            | 110 ++++++++++++++++++++++---------------
 4 files changed, 130 insertions(+), 126 deletions(-)

diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index ab960cc449200b2796c87b17dd5c01cf8df468af..a32a8140bd4c7432389c3d18b9e456e139c1e5e3 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -235,13 +235,6 @@ struct sysdb_attrs {
 /* sysdb_attrs helper functions */
 struct sysdb_attrs *sysdb_new_attrs(TALLOC_CTX *mem_ctx);
 
-struct sysdb_subdom {
-    const char *realm;
-    const char *name;
-    const char *flat_name;
-    const char *id;
-};
-
 struct range_info {
     char *name;
     uint32_t base_id;
@@ -373,10 +366,7 @@ errno_t sysdb_subdomain_store(struct sysdb_ctx *sysdb,
                               const char *name, const char *realm,
                               const char *flat_name, const char *domain_id);
 
-errno_t sysdb_update_subdomains(struct sss_domain_info *domain,
-                                int num_subdoms,
-                                struct sysdb_subdom *subdoms);
-
+errno_t sysdb_update_subdomains(struct sss_domain_info *domain);
 
 errno_t sysdb_master_domain_update(struct sss_domain_info *domain);
 
diff --git a/src/db/sysdb_subdomains.c b/src/db/sysdb_subdomains.c
index d6692b6decf8a418886bc69508a80e1b0b8fa761..c2719075740294b17b1d906f0ede1bb6c631d5bb 100644
--- a/src/db/sysdb_subdomains.c
+++ b/src/db/sysdb_subdomains.c
@@ -394,7 +394,6 @@ errno_t sysdb_subdomain_store(struct sysdb_ctx *sysdb,
             goto done;
         }
 
-
         ret = ldb_msg_add_string(msg, SYSDB_OBJECTCLASS, SYSDB_SUBDOMAIN_CLASS);
         if (ret != LDB_SUCCESS) {
             ret = sysdb_error_to_errno(ret);
@@ -461,9 +460,7 @@ done:
     return ret;
 }
 
-errno_t sysdb_update_subdomains(struct sss_domain_info *domain,
-                                int num_subdoms,
-                                struct sysdb_subdom *subdoms)
+errno_t sysdb_update_subdomains(struct sss_domain_info *domain)
 {
     int ret;
     int sret;
@@ -508,9 +505,9 @@ errno_t sysdb_update_subdomains(struct sss_domain_info *domain,
      * - if a subdomain already exists in sysdb, mark it for preservation
      * - if the subdomain doesn't exist in sysdb, create its bare structure
      */
-    for (c = 0; c < num_subdoms; c++) {
+    for (c = 0; c < domain->subdomain_count; c++) {
         for (d = 0; d < cur_subdomains_count; d++) {
-            if (strcasecmp(subdoms[c].name,
+            if (strcasecmp(domain->subdomains[c]->name,
                            cur_subdomains[d]->name) == 0) {
                 keep_subdomain[d] = true;
                 /* sub-domain already in cache, nothing to do */
@@ -519,20 +516,9 @@ errno_t sysdb_update_subdomains(struct sss_domain_info *domain,
         }
 
         if (d == cur_subdomains_count) {
-            struct sss_domain_info *ns;
-
             DEBUG(SSSDBG_TRACE_FUNC, ("Adding sub-domain [%s].\n",
-                                      subdoms[c].name));
-
-            ns = new_subdomain(tmp_ctx, domain,
-                               subdoms[c].name, subdoms[c].realm,
-                               subdoms[c].flat_name, subdoms[c].id);
-            if (!ns) {
-                DEBUG(SSSDBG_OP_FAILURE, ("new_subdomain failed.\n"));
-                goto done;
-            }
-
-            ret = sysdb_subdomain_create(ns);
+                                      domain->subdomains[c]->name));
+            ret = sysdb_subdomain_create(domain->subdomains[c]);
             if (ret != EOK) {
                 DEBUG(SSSDBG_OP_FAILURE, ("sysdb_subdomain_create failed.\n"));
                 goto done;
diff --git a/src/providers/ipa/ipa_subdomains.c b/src/providers/ipa/ipa_subdomains.c
index e02d8212a6d840d0a0249088bd4112f351c95353..88fb40f56e4d30908759bbeb5f8e0db8bf84f312 100644
--- a/src/providers/ipa/ipa_subdomains.c
+++ b/src/providers/ipa/ipa_subdomains.c
@@ -77,10 +77,6 @@ struct ipa_subdomains_ctx {
     struct tevent_timer *timer_event;
     bool configured_explicit;
     time_t disabled_until;
-
-    /* subdomain map cache */
-    int num_subdoms;
-    struct sysdb_subdom *subdoms;
 };
 
 const char *get_flat_name_from_subdomain_name(struct be_ctx *be_ctx,
@@ -88,6 +84,7 @@ const char *get_flat_name_from_subdomain_name(struct be_ctx *be_ctx,
 {
     size_t c;
     struct ipa_subdomains_ctx *ctx;
+    struct sss_domain_info *domain;
 
     ctx = talloc_get_type(be_ctx->bet_info[BET_SUBDOMAINS].pvt_bet_data,
                           struct ipa_subdomains_ctx);
@@ -96,11 +93,13 @@ const char *get_flat_name_from_subdomain_name(struct be_ctx *be_ctx,
         return NULL;
     }
 
-    for (c = 0; c < ctx->num_subdoms; c++) {
-        if (strcasecmp(ctx->subdoms[c].name, name) == 0 ||
-            (ctx->subdoms[c].flat_name != NULL &&
-             strcasecmp(ctx->subdoms[c].flat_name, name) == 0)) {
-            return ctx->subdoms[c].flat_name;
+    domain = ctx->be_ctx->domain;
+
+    for (c = 0; c < domain->subdomain_count; c++) {
+        if (strcasecmp(domain->subdomains[c]->name, name) == 0 ||
+            (domain->subdomains[c]->flat_name != NULL &&
+             strcasecmp(domain->subdomains[c]->flat_name, name) == 0)) {
+            return domain->subdomains[c]->flat_name;
         }
     }
 
@@ -199,9 +198,11 @@ done:
 }
 
 static errno_t ipa_subdom_parse(TALLOC_CTX *memctx,
+                                struct sss_domain_info *parent,
                                 struct sysdb_attrs *attrs,
-                                struct sysdb_subdom *subdom)
+                                struct sss_domain_info **_subdom)
 {
+    struct sss_domain_info *subdom = *_subdom;
     const char *value;
     int ret;
 
@@ -210,9 +211,10 @@ static errno_t ipa_subdom_parse(TALLOC_CTX *memctx,
         DEBUG(SSSDBG_OP_FAILURE, ("sysdb_attrs_get_string failed.\n"));
         return ret;
     }
-    if (subdom->name == NULL) {
-        subdom->name = talloc_strdup(memctx, value);
-        if (subdom->name == NULL) {
+
+    if (subdom == NULL) {
+        subdom = new_subdomain(memctx, parent, value, NULL, NULL, NULL);
+        if (subdom == NULL) {
             return ENOMEM;
         }
     } else if (strcmp(subdom->name, value) != 0) {
@@ -223,7 +225,7 @@ static errno_t ipa_subdom_parse(TALLOC_CTX *memctx,
     if (subdom->realm == NULL) {
         /* Add Realm as upper(domain name), this is generally always correct
          * with AD domains */
-        subdom->realm = get_uppercase_realm(memctx, subdom->name);
+        subdom->realm = get_uppercase_realm(subdom, subdom->name);
         if (!subdom->realm) {
             return ENOMEM;
         }
@@ -241,12 +243,11 @@ static errno_t ipa_subdom_parse(TALLOC_CTX *memctx,
         if (strcmp(subdom->flat_name, value) != 0) {
             DEBUG(SSSDBG_TRACE_INTERNAL,
                   ("Flat name for subdomain changed!\n"));
-            talloc_free(discard_const(subdom->flat_name));
-            subdom->flat_name = (const char *)NULL;
+            talloc_zfree(subdom->flat_name);
         }
     }
     if (subdom->flat_name == NULL) {
-        subdom->flat_name = talloc_strdup(memctx, value);
+        subdom->flat_name = talloc_strdup(subdom, value);
         if (subdom->flat_name == NULL) {
             return ENOMEM;
         }
@@ -260,28 +261,26 @@ static errno_t ipa_subdom_parse(TALLOC_CTX *memctx,
 
     /* in theory this may change, it should never happen, so we will log a
      * warning if it does, but we will allow it for now */
-    if (subdom->id != NULL) {
-        if (strcmp(subdom->id, value) != 0) {
+    if (subdom->domain_id != NULL) {
+        if (strcmp(subdom->domain_id, value) != 0) {
             DEBUG(SSSDBG_TRACE_INTERNAL,
                   ("ID for subdomain changed!\n"));
-            talloc_free(discard_const(subdom->id));
-            subdom->flat_name = (const char *)NULL;
+            talloc_zfree(subdom->domain_id);
         }
     }
-    if (subdom->id == NULL) {
-        subdom->id = talloc_strdup(memctx, value);
-        if (subdom->id == NULL) {
+    if (subdom->domain_id == NULL) {
+        subdom->domain_id = talloc_strdup(subdom, value);
+        if (subdom->domain_id == NULL) {
             return ENOMEM;
         }
     }
 
+    *_subdom = subdom;
     return EOK;
 }
 
 static errno_t
-ipa_subdomains_write_mappings(struct sss_domain_info *domain,
-                              size_t num_subdoms,
-                              struct sysdb_subdom *subdoms)
+ipa_subdomains_write_mappings(struct sss_domain_info *domain)
 {
     errno_t ret;
     errno_t err;
@@ -343,10 +342,12 @@ ipa_subdomains_write_mappings(struct sss_domain_info *domain,
         goto done;
     }
 
-    for (i = 0; i < num_subdoms; i++) {
+    for (i = 0; i < domain->subdomain_count; i++) {
         ret = fprintf(fstream, ".%s = %s\n%s = %s\n",
-                               subdoms[i].name, subdoms[i].realm,
-                               subdoms[i].name, subdoms[i].realm);
+                               domain->subdomains[i]->name,
+                               domain->subdomains[i]->realm,
+                               domain->subdomains[i]->name,
+                               domain->subdomains[i]->realm);
         if (ret < 0) {
             DEBUG(SSSDBG_CRIT_FAILURE, ("fprintf failed\n"));
             goto done;
@@ -409,16 +410,18 @@ static errno_t ipa_subdomains_refresh(struct ipa_subdomains_ctx *ctx,
                                       int count, struct sysdb_attrs **reply,
                                       bool *changes)
 {
+    struct sss_domain_info *domain;
     bool handled[count];
     const char *value;
     int c, h;
     int ret;
     int i, j;
 
+    domain = ctx->be_ctx->domain;
     memset(handled, 0, sizeof(bool) * count);
 
-    /* check existing subdoms in cache */
-    for (i = 0, h = 0; i < ctx->num_subdoms; i++) {
+    /* check existing subdomains */
+    for (i = 0, h = 0; i < domain->subdomain_count; i++) {
         for (c = 0; c < count; c++) {
             if (handled[c]) {
                 continue;
@@ -428,21 +431,26 @@ static errno_t ipa_subdomains_refresh(struct ipa_subdomains_ctx *ctx,
                 DEBUG(SSSDBG_OP_FAILURE, ("sysdb_attrs_get_string failed.\n"));
                 goto done;
             }
-            if (strcmp(value, ctx->subdoms[i].name) == 0) {
+            if (strcmp(value, domain->subdomains[i]->name) == 0) {
                 break;
             }
         }
 
         if (c >= count) {
             /* ok this subdomain does not exist anymore, let's clean up */
-            for (j = i; j < ctx->num_subdoms - 1; j++) {
-                ctx->subdoms[j] = ctx->subdoms[j + 1];
+            for (j = i; j < domain->subdomain_count - 1; j++) {
+                talloc_zfree(domain->subdomains[j]);
+                domain->subdomains[j] = domain->subdomains[j + 1];
             }
-            ctx->num_subdoms--;
+            if (i != 0) {
+                domain->subdomains[i - 1]->next = domain->subdomains[i];
+            }
+            domain->subdomain_count--;
             i--;
         } else {
             /* ok let's try to update it */
-            ret = ipa_subdom_parse(ctx->subdoms, reply[c], &ctx->subdoms[i]);
+            ret = ipa_subdom_parse(domain->subdomains, domain,
+                                   reply[c], &domain->subdomains[i]);
             if (ret) {
                 DEBUG(SSSDBG_OP_FAILURE, ("Failed to parse subdom data\n"));
                 goto done;
@@ -463,10 +471,10 @@ static errno_t ipa_subdomains_refresh(struct ipa_subdomains_ctx *ctx,
 
     /* add space for unhandled domains */
     c = count - h;
-    ctx->subdoms = talloc_realloc(ctx, ctx->subdoms,
-                                  struct sysdb_subdom,
-                                  ctx->num_subdoms + c);
-    if (ctx->subdoms == NULL) {
+    domain->subdomains = talloc_realloc(domain, domain->subdomains,
+                                        struct sss_domain_info *,
+                                        domain->subdomain_count + c);
+    if (domain->subdomains == NULL) {
         ret = ENOMEM;
         goto done;
     }
@@ -475,14 +483,15 @@ static errno_t ipa_subdomains_refresh(struct ipa_subdomains_ctx *ctx,
         if (handled[c]) {
             continue;
         }
-        i = ctx->num_subdoms;
-        memset(&ctx->subdoms[i], 0, sizeof(struct sysdb_subdom));
-        ret = ipa_subdom_parse(ctx->subdoms, reply[c], &ctx->subdoms[i]);
+        i = domain->subdomain_count;
+        domain->subdomains[i] = NULL;
+        ret = ipa_subdom_parse(domain->subdomains, domain,
+                               reply[c], &domain->subdomains[i]);
         if (ret) {
             DEBUG(SSSDBG_OP_FAILURE, ("Failed to parse subdom data\n"));
             goto done;
         }
-        ctx->num_subdoms++;
+        domain->subdomain_count++;
     }
 
     ret = EOK;
@@ -490,8 +499,8 @@ static errno_t ipa_subdomains_refresh(struct ipa_subdomains_ctx *ctx,
 done:
     if (ret != EOK) {
         ctx->last_refreshed = 0;
-        ctx->num_subdoms = 0;
-        talloc_zfree(ctx->subdoms);
+        domain->subdomain_count = 0;
+        talloc_zfree(domain->subdomains);
     } else {
         ctx->last_refreshed = time(NULL);
     }
@@ -715,16 +724,13 @@ static void ipa_subdomains_handler_done(struct tevent_req *req)
     }
 
     if (refresh_has_changes) {
-        ret = sysdb_update_subdomains(domain, ctx->sd_ctx->num_subdoms,
-                                      ctx->sd_ctx->subdoms);
+        ret = sysdb_update_subdomains(domain);
         if (ret != EOK) {
             DEBUG(SSSDBG_OP_FAILURE, ("sysdb_update_subdomains failed.\n"));
             goto done;
         }
 
-        ret = ipa_subdomains_write_mappings(domain,
-                                            ctx->sd_ctx->num_subdoms,
-                                            ctx->sd_ctx->subdoms);
+        ret = ipa_subdomains_write_mappings(domain);
         if (ret != EOK) {
             DEBUG(SSSDBG_MINOR_FAILURE,
                   ("ipa_subdomains_write_mappings failed.\n"));
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
index 609271dc364222f4837cedf791b5d4393ad3198c..0747b6bffdb4cfa0498271e2253edf17f88434f4 100644
--- a/src/tests/sysdb-tests.c
+++ b/src/tests/sysdb-tests.c
@@ -4463,20 +4463,14 @@ START_TEST(test_sysdb_original_dn_case_insensitive)
 }
 END_TEST
 
-struct sysdb_subdom dom1 =  { "DOM1.SUB", "dom1.sub", "dom1", "S-1" };
-struct sysdb_subdom dom2 =  { "DOM2.SUB", "dom2.sub", "dom2", "S-2" };
-struct sysdb_subdom dom_t = { "TEST.SUB", "test.sub", "test", "S-3" };
-
 START_TEST(test_sysdb_subdomain_create)
 {
     struct sysdb_test_ctx *test_ctx;
     errno_t ret;
     struct sss_domain_info **cur_subdomains = NULL;
     size_t cur_subdomains_count;
-    struct sysdb_subdom *new_subdom1 = &dom1;
-    struct sysdb_subdom *new_subdom2 = &dom2;
-    int num_subdom1 = 1;
-    int num_subdom2 = 1;
+    struct sss_domain_info *new_subdom1;
+    struct sss_domain_info *new_subdom2;
 
     ret = setup_sysdb_tests(&test_ctx);
     fail_if(ret != EOK, "Could not set up the test");
@@ -4485,10 +4479,25 @@ START_TEST(test_sysdb_subdomain_create)
                                &cur_subdomains_count, &cur_subdomains);
     fail_unless(ret == EOK, "sysdb_get_subdomains failed with [%d][%s]",
                             ret, strerror(ret));
-    fail_unless(cur_subdomains != NULL, "No sub-domains returned.");
+    fail_unless(cur_subdomains != NULL, "Non zero sub-domains returned.");
     fail_unless(cur_subdomains[0] == NULL, "No empty sub-domain list returned.");
 
-    ret = sysdb_update_subdomains(test_ctx->domain, num_subdom1, new_subdom1);
+    test_ctx->domain->subdomains = talloc_array(test_ctx->domain,
+                                                struct sss_domain_info *, 2);
+    fail_unless(test_ctx->domain->subdomains != NULL,
+                "talloc_array_zero failed");
+
+    new_subdom1 = new_subdomain(test_ctx, test_ctx->domain,
+                                "dom1.sub", "DOM1.SUB", "dom1", "S-1");
+    fail_unless(new_subdom1 != NULL, "Failed to create new subdomin.");
+    test_ctx->domain->subdomains[0] = new_subdom1;
+    test_ctx->domain->subdomain_count = 1;
+
+    ret = sysdb_subdomain_store(test_ctx->sysdb,
+                                "dom1.sub", "DOM1.SUB", "dom1", "S-1");
+    fail_if(ret != EOK, "Could not set up the test (dom1)");
+
+    ret = sysdb_update_subdomains(test_ctx->domain);
     fail_unless(ret == EOK, "sysdb_update_subdomains failed with [%d][%s]",
                             ret, strerror(ret));
 
@@ -4496,13 +4505,23 @@ START_TEST(test_sysdb_subdomain_create)
                                &cur_subdomains_count, &cur_subdomains);
     fail_unless(ret == EOK, "sysdb_get_subdomains failed with [%d][%s]",
                             ret, strerror(ret));
-    fail_unless(cur_subdomains != NULL, "No sub-domains returned.");
-    fail_unless(cur_subdomains[0] != NULL, "Empyt sub-domain list returned.");
-    fail_unless(strcmp(cur_subdomains[0]->name, new_subdom1[0].name) == 0,
-                "Unexpected sub-domain found, expected [%s], got [%s]",
-                new_subdom1[0].name, cur_subdomains[0]->name);
+    fail_if(cur_subdomains_count != 1, "Wrong number of sub-domains returned.");
+    fail_if(cur_subdomains[0] == NULL, "Empyt sub-domain list returned.");
+    fail_if(strcmp(cur_subdomains[0]->name, new_subdom1->name) != 0,
+            "Unexpected sub-domain found, expected [%s], got [%s]",
+            new_subdom1->name, cur_subdomains[0]->name);
 
-    ret = sysdb_update_subdomains(test_ctx->domain, num_subdom2, new_subdom2);
+    new_subdom2 = new_subdomain(test_ctx, test_ctx->domain,
+                                "dom2.sub", "DOM2.SUB", "dom2", "S-2");
+    fail_unless(new_subdom2 != NULL, "Failed to create new subdomin.");
+    test_ctx->domain->subdomains[1] = new_subdom2;
+    test_ctx->domain->subdomain_count = 2;
+
+    ret = sysdb_subdomain_store(test_ctx->sysdb,
+                                "dom2.sub", "DOM2.SUB", "dom2", "S-2");
+    fail_if(ret != EOK, "Could not set up the test (dom2)");
+
+    ret = sysdb_update_subdomains(test_ctx->domain);
     fail_unless(ret == EOK, "sysdb_update_subdomains failed with [%d][%s]",
                             ret, strerror(ret));
 
@@ -4510,13 +4529,16 @@ START_TEST(test_sysdb_subdomain_create)
                                &cur_subdomains_count, &cur_subdomains);
     fail_unless(ret == EOK, "sysdb_get_subdomains failed with [%d][%s]",
                             ret, strerror(ret));
-    fail_unless(cur_subdomains != NULL, "No sub-domains returned.");
-    fail_unless(cur_subdomains[0] != NULL, "Empyt sub-domain list returned.");
-    fail_unless(strcmp(cur_subdomains[0]->name, new_subdom2[0].name) == 0,
-                "Unexpected sub-domain found, expected [%s], got [%s]",
-                new_subdom2[0].name, cur_subdomains[0]->name);
+    fail_if(cur_subdomains_count != 2, "Wrong number of sub-domains returned.");
+    fail_if(cur_subdomains[1] == NULL, "Empyt sub-domain list returned.");
+    fail_if(strcmp(cur_subdomains[1]->name, new_subdom2->name) != 0,
+            "Unexpected sub-domain found, expected [%s], got [%s]",
+            new_subdom2->name, cur_subdomains[1]->name);
 
-    ret = sysdb_update_subdomains(test_ctx->domain, 0, NULL);
+    test_ctx->domain->subdomain_count = 0;
+    talloc_zfree(test_ctx->domain->subdomains);
+
+    ret = sysdb_update_subdomains(test_ctx->domain);
     fail_unless(ret == EOK, "sysdb_update_subdomains failed with [%d][%s]",
                             ret, strerror(ret));
 
@@ -4525,7 +4547,7 @@ START_TEST(test_sysdb_subdomain_create)
     fail_unless(ret == EOK, "sysdb_get_subdomains failed with [%d][%s]",
                             ret, strerror(ret));
     fail_unless(cur_subdomains != NULL, "No sub-domains returned.");
-    fail_unless(cur_subdomains[0] == NULL, "No empty sub-domain list returned.");
+    fail_if(cur_subdomains[0] != NULL, "No empty sub-domain list returned.");
 }
 END_TEST
 
@@ -4533,8 +4555,6 @@ START_TEST(test_sysdb_subdomain_store_user)
 {
     struct sysdb_test_ctx *test_ctx;
     errno_t ret;
-    struct sysdb_subdom *test_subdom = &dom_t;
-    int num_subdom = 1;
     struct sss_domain_info *subdomain = NULL;
     struct ldb_result *results = NULL;
     struct ldb_dn *base_dn = NULL;
@@ -4543,14 +4563,16 @@ START_TEST(test_sysdb_subdomain_store_user)
     ret = setup_sysdb_tests(&test_ctx);
     fail_if(ret != EOK, "Could not set up the test");
 
-    ret = sysdb_update_subdomains(test_ctx->domain, num_subdom, test_subdom);
+    subdomain = new_subdomain(test_ctx, test_ctx->domain,
+                              "test.sub", "TEST.SUB", "test", "S-3");
+    fail_unless(subdomain != NULL, "Failed to create new subdomin.");
+    ret = sysdb_subdomain_create(subdomain);
+    fail_if(ret != EOK, "Could not set up the test (test subdom)");
+
+    ret = sysdb_update_subdomains(test_ctx->domain);
     fail_unless(ret == EOK, "sysdb_update_subdomains failed with [%d][%s]",
                             ret, strerror(ret));
 
-    subdomain = new_subdomain(test_ctx, test_ctx->domain, "test.sub",
-                              NULL, NULL, NULL);
-    fail_unless(subdomain != NULL, "new_subdomain failed.");
-
     ret = sysdb_store_user(subdomain->sysdb, subdomain, "subdomuser",
                            NULL, 12345, 0, "Sub Domain User",
                            "/home/subdomuser", "/bin/bash",
@@ -4590,8 +4612,6 @@ START_TEST(test_sysdb_subdomain_user_ops)
 {
     struct sysdb_test_ctx *test_ctx;
     errno_t ret;
-    struct sysdb_subdom *test_subdom = &dom_t;
-    int num_subdom = 1;
     struct sss_domain_info *subdomain = NULL;
     struct ldb_message *msg = NULL;
     struct ldb_dn *check_dn = NULL;
@@ -4599,14 +4619,16 @@ START_TEST(test_sysdb_subdomain_user_ops)
     ret = setup_sysdb_tests(&test_ctx);
     fail_if(ret != EOK, "Could not set up the test");
 
-    ret = sysdb_update_subdomains(test_ctx->domain, num_subdom, test_subdom);
+    subdomain = new_subdomain(test_ctx, test_ctx->domain,
+                                "test.sub", "TEST.SUB", "test", "S-3");
+    fail_unless(subdomain != NULL, "Failed to create new subdomin.");
+    ret = sysdb_subdomain_create(subdomain);
+    fail_if(ret != EOK, "Could not set up the test (test subdom)");
+
+    ret = sysdb_update_subdomains(test_ctx->domain);
     fail_unless(ret == EOK, "sysdb_update_subdomains failed with [%d][%s]",
                             ret, strerror(ret));
 
-    subdomain = new_subdomain(test_ctx, test_ctx->domain, "test.sub",
-                              NULL, NULL, NULL);
-    fail_unless(subdomain != NULL, "new_subdomain failed.");
-
     ret = sysdb_store_user(subdomain->sysdb, subdomain, "subdomuser",
                            NULL, 12345, 0, "Sub Domain User",
                            "/home/subdomuser", "/bin/bash",
@@ -4642,8 +4664,6 @@ START_TEST(test_sysdb_subdomain_group_ops)
 {
     struct sysdb_test_ctx *test_ctx;
     errno_t ret;
-    struct sysdb_subdom *test_subdom = &dom_t;
-    int num_subdom = 1;
     struct sss_domain_info *subdomain = NULL;
     struct ldb_message *msg = NULL;
     struct ldb_dn *check_dn = NULL;
@@ -4651,14 +4671,16 @@ START_TEST(test_sysdb_subdomain_group_ops)
     ret = setup_sysdb_tests(&test_ctx);
     fail_if(ret != EOK, "Could not set up the test");
 
-    ret = sysdb_update_subdomains(test_ctx->domain, num_subdom, test_subdom);
+    subdomain = new_subdomain(test_ctx, test_ctx->domain,
+                                "test.sub", "TEST.SUB", "test", "S-3");
+    fail_unless(subdomain != NULL, "Failed to create new subdomin.");
+    ret = sysdb_subdomain_create(subdomain);
+    fail_if(ret != EOK, "Could not set up the test (test subdom)");
+
+    ret = sysdb_update_subdomains(test_ctx->domain);
     fail_unless(ret == EOK, "sysdb_update_subdomains failed with [%d][%s]",
                             ret, strerror(ret));
 
-    subdomain = new_subdomain(test_ctx, test_ctx->domain, "test.sub",
-                              NULL, NULL, NULL);
-    fail_unless(subdomain != NULL, "new_subdomain failed.");
-
     ret = sysdb_store_group(subdomain->sysdb, subdomain,
                             "subdomgroup", 12345, NULL, -1, 0);
     fail_unless(ret == EOK, "sysdb_store_domgroup failed.");
-- 
1.8.0.1

