LCOV - code coverage report
Current view: top level - source3/torture - test_dbwrap_watch.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 0 245 0.0 %
Date: 2024-06-13 04:01:37 Functions: 0 8 0.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    Test dbwrap_watch API
       4             :    Copyright (C) Volker Lendecke 2012
       5             : 
       6             :    This program is free software; you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             : 
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #include "includes.h"
      21             : #include "torture/proto.h"
      22             : #include "system/filesys.h"
      23             : #include "lib/dbwrap/dbwrap.h"
      24             : #include "lib/dbwrap/dbwrap_open.h"
      25             : #include "lib/dbwrap/dbwrap_watch.h"
      26             : #include "lib/util/util_tdb.h"
      27             : 
      28           0 : static bool test_dbwrap_watch_init(
      29             :         TALLOC_CTX *mem_ctx,
      30             :         const char *dbname,
      31             :         struct tevent_context **pev,
      32             :         struct messaging_context **pmsg,
      33             :         struct db_context **pbackend,
      34             :         struct db_context **pdb)
      35             : {
      36           0 :         struct tevent_context *ev = NULL;
      37           0 :         struct messaging_context *msg = NULL;
      38           0 :         struct db_context *backend = NULL;
      39           0 :         struct db_context *db = NULL;
      40             : 
      41           0 :         ev = samba_tevent_context_init(mem_ctx);
      42           0 :         if (ev == NULL) {
      43           0 :                 fprintf(stderr, "tevent_context_init failed\n");
      44           0 :                 goto fail;
      45             :         }
      46             : 
      47           0 :         msg = messaging_init(ev, ev);
      48           0 :         if (msg == NULL) {
      49           0 :                 fprintf(stderr, "messaging_init failed\n");
      50           0 :                 goto fail;
      51             :         }
      52             : 
      53           0 :         backend = db_open(
      54             :                 msg,
      55             :                 dbname,
      56             :                 0,
      57             :                 TDB_CLEAR_IF_FIRST,
      58             :                 O_CREAT|O_RDWR,
      59             :                 0644,
      60             :                 DBWRAP_LOCK_ORDER_1,
      61             :                 DBWRAP_FLAG_NONE);
      62           0 :         if (backend == NULL) {
      63           0 :                 fprintf(stderr, "db_open failed: %s\n", strerror(errno));
      64           0 :                 goto fail;
      65             :         }
      66             : 
      67             :         {
      68           0 :                 struct db_context *backend_copy = backend;
      69             : 
      70           0 :                 db = db_open_watched(ev, &backend_copy, msg);
      71           0 :                 if (db == NULL) {
      72           0 :                         fprintf(stderr, "db_open_watched failed\n");
      73           0 :                         goto fail;
      74             :                 }
      75             :         }
      76             : 
      77           0 :         if (pev != NULL) {
      78           0 :                 *pev = ev;
      79             :         }
      80           0 :         if (pmsg != NULL) {
      81           0 :                 *pmsg = msg;
      82             :         }
      83           0 :         if (pbackend != NULL) {
      84           0 :                 *pbackend = backend;
      85             :         }
      86           0 :         if (pdb != NULL) {
      87           0 :                 *pdb = db;
      88             :         }
      89           0 :         return true;
      90             : 
      91           0 : fail:
      92           0 :         TALLOC_FREE(backend);
      93           0 :         TALLOC_FREE(msg);
      94           0 :         TALLOC_FREE(ev);
      95           0 :         return false;
      96             : }
      97             : 
      98           0 : bool run_dbwrap_watch1(int dummy)
      99             : {
     100           0 :         struct tevent_context *ev = NULL;
     101           0 :         struct messaging_context *msg = NULL;
     102           0 :         struct db_context *backend = NULL;
     103           0 :         struct db_context *db = NULL;
     104           0 :         const char *keystr = "key";
     105           0 :         TDB_DATA key = string_term_tdb_data(keystr);
     106           0 :         struct db_record *rec = NULL;
     107           0 :         struct tevent_req *req = NULL;
     108             :         NTSTATUS status;
     109           0 :         bool ret = false;
     110             : 
     111           0 :         ret = test_dbwrap_watch_init(
     112             :                 talloc_tos(), "test_watch.tdb", &ev, &msg, &backend, &db);
     113           0 :         if (!ret) {
     114           0 :                 goto fail;
     115             :         }
     116             : 
     117           0 :         rec = dbwrap_fetch_locked(db, db, key);
     118           0 :         if (rec == NULL) {
     119           0 :                 fprintf(stderr, "dbwrap_fetch_locked failed\n");
     120           0 :                 goto fail;
     121             :         }
     122           0 :         req = dbwrap_watched_watch_send(talloc_tos(), ev, rec,
     123             :                                         0, /* resume_instance */
     124           0 :                                         (struct server_id){0});
     125           0 :         if (req == NULL) {
     126           0 :                 fprintf(stderr, "dbwrap_record_watch_send failed\n");
     127           0 :                 goto fail;
     128             :         }
     129           0 :         TALLOC_FREE(rec);
     130             : 
     131           0 :         status = dbwrap_store_int32_bystring(db, "different_key", 1);
     132           0 :         if (!NT_STATUS_IS_OK(status)) {
     133           0 :                 fprintf(stderr, "dbwrap_store_int32 failed: %s\n",
     134             :                         nt_errstr(status));
     135           0 :                 goto fail;
     136             :         }
     137             : 
     138           0 :         status = dbwrap_store_int32_bystring(db, keystr, 1);
     139           0 :         if (!NT_STATUS_IS_OK(status)) {
     140           0 :                 fprintf(stderr, "dbwrap_store_int32 failed: %s\n",
     141             :                         nt_errstr(status));
     142           0 :                 goto fail;
     143             :         }
     144             : 
     145           0 :         if (!tevent_req_poll(req, ev)) {
     146           0 :                 fprintf(stderr, "tevent_req_poll failed\n");
     147           0 :                 goto fail;
     148             :         }
     149             : 
     150           0 :         status = dbwrap_watched_watch_recv(req, NULL, NULL, NULL);
     151           0 :         if (!NT_STATUS_IS_OK(status)) {
     152           0 :                 fprintf(stderr, "dbwrap_record_watch_recv failed: %s\n",
     153             :                         nt_errstr(status));
     154           0 :                 goto fail;
     155             :         }
     156             : 
     157           0 :         (void)unlink("test_watch.tdb");
     158           0 :         ret = true;
     159           0 : fail:
     160           0 :         TALLOC_FREE(req);
     161           0 :         TALLOC_FREE(rec);
     162           0 :         TALLOC_FREE(db);
     163           0 :         TALLOC_FREE(msg);
     164           0 :         TALLOC_FREE(ev);
     165           0 :         return ret;
     166             : }
     167             : 
     168             : /*
     169             :  * Make sure dbwrap_parse_record does not return NT_STATUS_OK on
     170             :  * invalid data
     171             :  */
     172             : 
     173           0 : bool run_dbwrap_watch2(int dummy)
     174             : {
     175           0 :         struct tevent_context *ev = NULL;
     176           0 :         struct messaging_context *msg = NULL;
     177           0 :         struct db_context *backend = NULL;
     178           0 :         struct db_context *db = NULL;
     179           0 :         const char *keystr = "key";
     180           0 :         TDB_DATA key = string_term_tdb_data(keystr);
     181             :         NTSTATUS status;
     182           0 :         bool ret = false;
     183             : 
     184           0 :         ret = test_dbwrap_watch_init(
     185             :                 talloc_tos(), "test_watch.tdb", &ev, &msg, &backend, &db);
     186           0 :         if (!ret) {
     187           0 :                 goto fail;
     188             :         }
     189             : 
     190             :         /*
     191             :          * Store invalid data (from the dbwrap_watch point of view)
     192             :          * directly into the backend database
     193             :          */
     194           0 :         status = dbwrap_store_uint32_bystring(backend, keystr, UINT32_MAX);
     195           0 :         if (!NT_STATUS_IS_OK(status)) {
     196           0 :                 fprintf(stderr, "dbwrap_store_uint32_bystring failed: %s\n",
     197             :                         nt_errstr(status));
     198           0 :                 goto fail;
     199             :         }
     200             : 
     201           0 :         status = dbwrap_parse_record(db, key, NULL, NULL);
     202           0 :         if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
     203           0 :                 fprintf(stderr, "dbwrap_parse_record returned %s, expected "
     204             :                         "NT_STATUS_NOT_FOUND\n", nt_errstr(status));
     205           0 :                 goto fail;
     206             :         }
     207             : 
     208           0 :         (void)unlink("test_watch.tdb");
     209           0 :         ret = true;
     210           0 : fail:
     211           0 :         TALLOC_FREE(db);
     212           0 :         TALLOC_FREE(msg);
     213           0 :         TALLOC_FREE(ev);
     214           0 :         return ret;
     215             : }
     216             : 
     217             : /*
     218             :  * Test autocleanup of dead watchers
     219             :  */
     220             : 
     221           0 : bool run_dbwrap_watch3(int dummy)
     222             : {
     223           0 :         struct tevent_context *ev = NULL;
     224           0 :         struct messaging_context *msg = NULL;
     225           0 :         struct db_context *backend = NULL;
     226           0 :         struct db_context *db = NULL;
     227           0 :         const char *keystr = "key";
     228           0 :         TDB_DATA key = string_term_tdb_data(keystr);
     229             :         NTSTATUS status;
     230           0 :         bool ret = false;
     231             :         pid_t child, waited;
     232             :         int wstatus, exit_status;
     233             : 
     234           0 :         BlockSignals(true, SIGCHLD);
     235             : 
     236           0 :         child = fork();
     237           0 :         if (child == -1) {
     238           0 :                 fprintf(stderr,
     239             :                         "fork failed: %s\n",
     240           0 :                         strerror(errno));
     241           0 :                 goto fail;
     242             :         }
     243             : 
     244           0 :         ret = test_dbwrap_watch_init(
     245             :                 talloc_tos(), "test_watch.tdb", &ev, &msg, &backend, &db);
     246           0 :         if (!ret) {
     247           0 :                 goto fail;
     248             :         }
     249             : 
     250           0 :         if (child == 0) {
     251           0 :                 struct db_record *rec = dbwrap_fetch_locked(db, db, key);
     252           0 :                 struct tevent_req *req = NULL;
     253             : 
     254           0 :                 if (rec == NULL) {
     255           0 :                         fprintf(stderr, "dbwrap_fetch_locked failed\n");
     256           0 :                         exit(1);
     257             :                 }
     258             : 
     259           0 :                 req = dbwrap_watched_watch_send(
     260           0 :                         db, ev, rec, 0, (struct server_id) { 0 });
     261           0 :                 if (req == NULL) {
     262           0 :                         fprintf(stderr, "dbwrap_watched_watch_send failed\n");
     263           0 :                         exit(2);
     264             :                 }
     265             : 
     266           0 :                 exit(0);
     267             :         }
     268             : 
     269           0 :         waited = waitpid(child, &wstatus, 0);
     270           0 :         if (waited == -1) {
     271           0 :                 fprintf(stderr, "waitpid failed: %s\n", strerror(errno));
     272           0 :                 goto fail;
     273             :         }
     274           0 :         if (!WIFEXITED(wstatus)) {
     275           0 :                 fprintf(stderr, "child did not exit normally\n");
     276           0 :                 goto fail;
     277             :         }
     278             : 
     279           0 :         exit_status = WEXITSTATUS(wstatus);
     280           0 :         if (exit_status != 0) {
     281           0 :                 fprintf(stderr, "exit status is %d\n", exit_status);
     282           0 :                 goto fail;
     283             :         }
     284             : 
     285           0 :         status = dbwrap_store_uint32_bystring(db, keystr, 1);
     286           0 :         if (!NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
     287           0 :                 fprintf(stderr,
     288             :                         "dbwrap_store_uint32 returned %s\n",
     289             :                         nt_errstr(status));
     290           0 :                 goto fail;
     291             :         }
     292             : 
     293           0 :         (void)unlink("test_watch.tdb");
     294           0 :         ret = true;
     295           0 : fail:
     296           0 :         TALLOC_FREE(db);
     297           0 :         TALLOC_FREE(msg);
     298           0 :         TALLOC_FREE(ev);
     299           0 :         return ret;
     300             : }
     301             : 
     302             : /*
     303             :  * Test that we can't add two watchers in the same
     304             :  * fetch_lock/do_locked round
     305             :  */
     306             : 
     307             : struct dbwrap_watch4_state {
     308             :         TALLOC_CTX *mem_ctx;
     309             :         struct tevent_context *ev;
     310             :         struct db_context *db;
     311             :         TDB_DATA key;
     312             : 
     313             :         NTSTATUS status;
     314             : 
     315             :         struct tevent_req *req1;
     316             :         NTSTATUS status1;
     317             : 
     318             :         struct tevent_req *req2;
     319             :         NTSTATUS status2;
     320             : };
     321             : 
     322             : static void dbwrap_watch4_done1(struct tevent_req *subreq);
     323             : static void dbwrap_watch4_done2(struct tevent_req *subreq);
     324             : 
     325           0 : static void dbwrap_watch4_fn(struct db_record *rec,
     326             :                              TDB_DATA value,
     327             :                              void *private_data)
     328             : {
     329           0 :         struct dbwrap_watch4_state *state = private_data;
     330             :         bool ok;
     331             : 
     332           0 :         state->req1 = dbwrap_watched_watch_send(
     333           0 :                 state->mem_ctx, state->ev, rec, 0, (struct server_id) { .pid=0 });
     334           0 :         if (state->req1 == NULL) {
     335           0 :                 goto nomem;
     336             :         }
     337           0 :         tevent_req_set_callback(state->req1, dbwrap_watch4_done1, state);
     338           0 :         state->status1 = NT_STATUS_EVENT_PENDING;
     339             : 
     340           0 :         ok = tevent_req_set_endtime(
     341             :                 state->req1, state->ev, timeval_current_ofs(1, 0));
     342           0 :         if (!ok) {
     343           0 :                 goto nomem;
     344             :         }
     345             : 
     346           0 :         state->req2 = dbwrap_watched_watch_send(
     347           0 :                 state->mem_ctx, state->ev, rec, 0, (struct server_id) { .pid=0 });
     348           0 :         if (state->req2 == NULL) {
     349           0 :                 goto nomem;
     350             :         }
     351           0 :         tevent_req_set_callback(state->req2, dbwrap_watch4_done2, state);
     352           0 :         state->status2 = NT_STATUS_EVENT_PENDING;
     353             : 
     354           0 :         ok = tevent_req_set_endtime(
     355             :                 state->req2, state->ev, timeval_current_ofs(1, 0));
     356           0 :         if (!ok) {
     357           0 :                 goto nomem;
     358             :         }
     359             : 
     360           0 :         state->status = NT_STATUS_OK;
     361           0 :         return;
     362             : 
     363           0 :         nomem:
     364           0 :         state->status = NT_STATUS_NO_MEMORY;
     365             : }
     366             : 
     367           0 : static void dbwrap_watch4_done1(struct tevent_req *subreq)
     368             : {
     369           0 :         struct dbwrap_watch4_state *state = tevent_req_callback_data_void(subreq);
     370           0 :         state->status1 = dbwrap_watched_watch_recv(subreq, NULL, NULL, NULL);
     371           0 :         TALLOC_FREE(subreq);
     372           0 :         printf("req1 finished: %s\n", nt_errstr(state->status1));
     373           0 :         state->req1 = NULL;
     374           0 : }
     375             : 
     376           0 : static void dbwrap_watch4_done2(struct tevent_req *subreq)
     377             : {
     378           0 :         struct dbwrap_watch4_state *state = tevent_req_callback_data_void(subreq);
     379           0 :         state->status2 = dbwrap_watched_watch_recv(subreq, NULL, NULL, NULL);
     380           0 :         TALLOC_FREE(subreq);
     381           0 :         printf("req2 finished: %s\n", nt_errstr(state->status2));
     382           0 :         state->req2 = NULL;
     383           0 : }
     384             : 
     385           0 : bool run_dbwrap_watch4(int dummy)
     386             : {
     387           0 :         struct tevent_context *ev = NULL;
     388           0 :         struct messaging_context *msg = NULL;
     389           0 :         struct db_context *backend = NULL;
     390           0 :         struct db_context *db = NULL;
     391           0 :         const char *keystr = "key";
     392           0 :         TDB_DATA key = string_term_tdb_data(keystr);
     393           0 :         struct dbwrap_watch4_state state = { 0 };
     394             :         NTSTATUS status;
     395           0 :         bool ret = false;
     396             :         bool ok;
     397             : 
     398           0 :         ok = test_dbwrap_watch_init(
     399             :                 talloc_tos(), "test_watch.tdb", &ev, &msg, &backend, &db);
     400           0 :         if (!ok) {
     401           0 :                 goto fail;
     402             :         }
     403             : 
     404           0 :         state = (struct dbwrap_watch4_state) {
     405           0 :                 .mem_ctx = talloc_tos(),
     406             :                 .ev = ev,
     407             :                 .db = db,
     408             :                 .key = key,
     409             :         };
     410             : 
     411           0 :         status = dbwrap_do_locked(db, key, dbwrap_watch4_fn, &state);
     412           0 :         if (!NT_STATUS_IS_OK(status)) {
     413           0 :                 fprintf(stderr,
     414             :                         "dbwrap_do_locked failed: %s\n",
     415             :                         nt_errstr(status));
     416           0 :                 goto fail;
     417             :         }
     418           0 :         if (!NT_STATUS_IS_OK(state.status)) {
     419           0 :                 fprintf(stderr,
     420             :                         "dbwrap_watch4_fn failed: %s\n",
     421             :                         nt_errstr(status));
     422           0 :                 goto fail;
     423             :         }
     424             : 
     425           0 :         status = dbwrap_store(db, key, key, 0);
     426           0 :         if (!NT_STATUS_IS_OK(status)) {
     427           0 :                 fprintf(stderr,
     428             :                         "dbwrap_store failed: %s\n",
     429             :                         nt_errstr(status));
     430           0 :                 goto fail;
     431             :         }
     432             : 
     433           0 :         while (NT_STATUS_EQUAL(state.status1, NT_STATUS_EVENT_PENDING) ||
     434           0 :                NT_STATUS_EQUAL(state.status2, NT_STATUS_EVENT_PENDING)) {
     435           0 :                 int res = tevent_loop_once(ev);
     436           0 :                 if (res != 0) {
     437           0 :                         fprintf(stderr,
     438             :                                 "tevent_loop_once failed: %s\n",
     439           0 :                                 strerror(errno));
     440           0 :                         goto fail;
     441             :                 }
     442             :         }
     443             : 
     444           0 :         if (!NT_STATUS_IS_OK(state.status1)) {
     445           0 :                 fprintf(stderr,
     446             :                         "req1 returned %s\n",
     447             :                         nt_errstr(state.status1));
     448           0 :                 goto fail;
     449             :         }
     450             : 
     451           0 :         if (!NT_STATUS_EQUAL(state.status2, NT_STATUS_REQUEST_NOT_ACCEPTED)) {
     452           0 :                 fprintf(stderr,
     453             :                         "req2 returned %s\n",
     454             :                         nt_errstr(state.status2));
     455           0 :                 goto fail;
     456             :         }
     457             : 
     458           0 :         (void)unlink("test_watch.tdb");
     459           0 :         ret = true;
     460           0 : fail:
     461           0 :         TALLOC_FREE(state.req2);
     462           0 :         TALLOC_FREE(state.req1);
     463           0 :         TALLOC_FREE(db);
     464           0 :         TALLOC_FREE(msg);
     465           0 :         TALLOC_FREE(ev);
     466           0 :         return ret;
     467             : }

Generated by: LCOV version 1.13