📄 locks-test.c
字号:
/* lock-test.c --- tests for the filesystem locking functions * * ==================================================================== * Copyright (c) 2000-2004 CollabNet. All rights reserved. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms * are also available at http://subversion.tigris.org/license-1.html. * If newer versions of this license are posted there, you may use a * newer version instead, at your option. * * This software consists of voluntary contributions made by many * individuals. For exact contribution history, see the revision * history and logs, available at http://subversion.tigris.org/. * ==================================================================== */#include <string.h>#include <apr_pools.h>#include <apr_time.h>#include "svn_error.h"#include "svn_fs.h"#include "../svn_test_fs.h"/*-----------------------------------------------------------------*//** Helper functions **//* Implementations of the svn_fs_get_locks_callback_t interface and baton, for verifying expected output from svn_fs_get_locks(). */struct get_locks_baton_t{ apr_hash_t *locks;};static svn_error_t *get_locks_callback(void *baton, svn_lock_t *lock, apr_pool_t *pool){ struct get_locks_baton_t *b = baton; apr_pool_t *hash_pool = apr_hash_pool_get(b->locks); svn_string_t *lock_path = svn_string_create(lock->path, hash_pool); apr_hash_set(b->locks, lock_path->data, lock_path->len, svn_lock_dup(lock, hash_pool)); return SVN_NO_ERROR;}/* A factory function. */static struct get_locks_baton_t *make_get_locks_baton(apr_pool_t *pool){ struct get_locks_baton_t *baton = apr_pcalloc(pool, sizeof(*baton)); baton->locks = apr_hash_make(pool); return baton;} /* And verification function(s). */static svn_error_t *verify_matching_lock_paths(struct get_locks_baton_t *baton, const char *expected_paths[], apr_size_t num_expected_paths, apr_pool_t *pool){ apr_size_t i; if (num_expected_paths != apr_hash_count(baton->locks)) return svn_error_create(SVN_ERR_TEST_FAILED, NULL, "Unexpected number of locks."); for (i = 0; i < num_expected_paths; i++) { const char *path = expected_paths[i]; if (! apr_hash_get(baton->locks, path, APR_HASH_KEY_STRING)) return svn_error_createf(SVN_ERR_TEST_FAILED, NULL, "Missing lock for path '%s'", path); } return SVN_NO_ERROR;}/*-----------------------------------------------------------------*//** The actual lock-tests called by `make check` **//* Test that we can create a lock--nothing more. */static svn_error_t *lock_only(const char **msg, svn_boolean_t msg_only, svn_test_opts_t *opts, apr_pool_t *pool){ svn_fs_t *fs; svn_fs_txn_t *txn; svn_fs_root_t *txn_root; const char *conflict; svn_revnum_t newrev; svn_fs_access_t *access; svn_lock_t *mylock; *msg = "lock only"; if (msg_only) return SVN_NO_ERROR; /* Prepare a filesystem and a new txn. */ SVN_ERR(svn_test__create_fs(&fs, "test-repo-lock-only", opts->fs_type, pool)); SVN_ERR(svn_fs_begin_txn2(&txn, fs, 0, SVN_FS_TXN_CHECK_LOCKS, pool)); SVN_ERR(svn_fs_txn_root(&txn_root, txn, pool)); /* Create the greek tree and commit it. */ SVN_ERR(svn_test__create_greek_tree(txn_root, pool)); SVN_ERR(svn_fs_commit_txn(&conflict, &newrev, txn, pool)); /* We are now 'bubba'. */ SVN_ERR(svn_fs_create_access(&access, "bubba", pool)); SVN_ERR(svn_fs_set_access(fs, access)); /* Lock /A/D/G/rho. */ SVN_ERR(svn_fs_lock(&mylock, fs, "/A/D/G/rho", NULL, "", 0, 0, SVN_INVALID_REVNUM, FALSE, pool)); return SVN_NO_ERROR;}/* Test that we can create, fetch, and destroy a lock. It exercises each of the five public fs locking functions. */static svn_error_t *lookup_lock_by_path(const char **msg, svn_boolean_t msg_only, svn_test_opts_t *opts, apr_pool_t *pool){ svn_fs_t *fs; svn_fs_txn_t *txn; svn_fs_root_t *txn_root; const char *conflict; svn_revnum_t newrev; svn_fs_access_t *access; svn_lock_t *mylock, *somelock; *msg = "lookup lock by path"; if (msg_only) return SVN_NO_ERROR; /* Prepare a filesystem and a new txn. */ SVN_ERR(svn_test__create_fs(&fs, "test-repo-lookup-lock-by-path", opts->fs_type, pool)); SVN_ERR(svn_fs_begin_txn2(&txn, fs, 0, SVN_FS_TXN_CHECK_LOCKS, pool)); SVN_ERR(svn_fs_txn_root(&txn_root, txn, pool)); /* Create the greek tree and commit it. */ SVN_ERR(svn_test__create_greek_tree(txn_root, pool)); SVN_ERR(svn_fs_commit_txn(&conflict, &newrev, txn, pool)); /* We are now 'bubba'. */ SVN_ERR(svn_fs_create_access(&access, "bubba", pool)); SVN_ERR(svn_fs_set_access(fs, access)); /* Lock /A/D/G/rho. */ SVN_ERR(svn_fs_lock(&mylock, fs, "/A/D/G/rho", NULL, "", 0, 0, SVN_INVALID_REVNUM, FALSE, pool)); /* Can we look up the lock by path? */ SVN_ERR(svn_fs_get_lock(&somelock, fs, "/A/D/G/rho", pool)); if ((! somelock) || (strcmp(somelock->token, mylock->token) != 0)) return svn_error_create(SVN_ERR_TEST_FAILED, NULL, "Couldn't look up a lock by pathname."); return SVN_NO_ERROR;}/* Test that we can create a lock outside of the fs and attach it to a path. */static svn_error_t *attach_lock(const char **msg, svn_boolean_t msg_only, svn_test_opts_t *opts, apr_pool_t *pool){ svn_fs_t *fs; svn_fs_txn_t *txn; svn_fs_root_t *txn_root; const char *conflict; svn_revnum_t newrev; svn_fs_access_t *access; svn_lock_t *somelock; svn_lock_t *mylock; const char *token; *msg = "attach lock"; if (msg_only) return SVN_NO_ERROR; /* Prepare a filesystem and a new txn. */ SVN_ERR(svn_test__create_fs(&fs, "test-repo-attach-lock", opts->fs_type, pool)); SVN_ERR(svn_fs_begin_txn2(&txn, fs, 0, SVN_FS_TXN_CHECK_LOCKS, pool)); SVN_ERR(svn_fs_txn_root(&txn_root, txn, pool)); /* Create the greek tree and commit it. */ SVN_ERR(svn_test__create_greek_tree(txn_root, pool)); SVN_ERR(svn_fs_commit_txn(&conflict, &newrev, txn, pool)); /* We are now 'bubba'. */ SVN_ERR(svn_fs_create_access(&access, "bubba", pool)); SVN_ERR(svn_fs_set_access(fs, access)); SVN_ERR(svn_fs_generate_lock_token(&token, fs, pool)); SVN_ERR(svn_fs_lock(&mylock, fs, "/A/D/G/rho", token, "This is a comment. Yay comment!", 0, apr_time_now() + apr_time_from_sec(3), SVN_INVALID_REVNUM, FALSE, pool)); /* Can we look up the lock by path? */ SVN_ERR(svn_fs_get_lock(&somelock, fs, "/A/D/G/rho", pool)); if ((! somelock) || (strcmp(somelock->token, mylock->token) != 0)) return svn_error_create(SVN_ERR_TEST_FAILED, NULL, "Couldn't look up a lock by pathname."); /* Unlock /A/D/G/rho, and verify that it's gone. */ SVN_ERR(svn_fs_unlock(fs, mylock->path, mylock->token, 0, pool)); SVN_ERR(svn_fs_get_lock(&somelock, fs, "/A/D/G/rho", pool)); if (somelock) return svn_error_create(SVN_ERR_TEST_FAILED, NULL, "Removed a lock, but it's still there."); return SVN_NO_ERROR;}/* Test that we can get all locks under a directory. */static svn_error_t *get_locks(const char **msg, svn_boolean_t msg_only, svn_test_opts_t *opts, apr_pool_t *pool){ svn_fs_t *fs; svn_fs_txn_t *txn; svn_fs_root_t *txn_root; const char *conflict; svn_revnum_t newrev; svn_fs_access_t *access; svn_lock_t *mylock; struct get_locks_baton_t *get_locks_baton; apr_size_t i, num_expected_paths; *msg = "get locks"; if (msg_only) return SVN_NO_ERROR; /* Prepare a filesystem and a new txn. */ SVN_ERR(svn_test__create_fs(&fs, "test-repo-get-locks", opts->fs_type, pool)); SVN_ERR(svn_fs_begin_txn2(&txn, fs, 0, SVN_FS_TXN_CHECK_LOCKS, pool)); SVN_ERR(svn_fs_txn_root(&txn_root, txn, pool)); /* Create the greek tree and commit it. */ SVN_ERR(svn_test__create_greek_tree(txn_root, pool)); SVN_ERR(svn_fs_commit_txn(&conflict, &newrev, txn, pool)); /* We are now 'bubba'. */ SVN_ERR(svn_fs_create_access(&access, "bubba", pool)); SVN_ERR(svn_fs_set_access(fs, access)); /* Lock our paths; verify from "/". */ { static const char *expected_paths[] = { "/A/D/G/pi", "/A/D/G/rho", "/A/D/G/tau", "/A/D/H/psi", "/A/D/H/chi", "/A/D/H/omega", "/A/B/E/alpha", "/A/B/E/beta", }; num_expected_paths = sizeof(expected_paths) / sizeof(const char *); for (i = 0; i < num_expected_paths; i++) { SVN_ERR(svn_fs_lock(&mylock, fs, expected_paths[i], NULL, "", 0, 0, SVN_INVALID_REVNUM, FALSE, pool)); } get_locks_baton = make_get_locks_baton(pool); SVN_ERR(svn_fs_get_locks(fs, "", get_locks_callback, get_locks_baton, pool)); SVN_ERR(verify_matching_lock_paths(get_locks_baton, expected_paths, num_expected_paths, pool)); } /* Verify from "/A/B". */ { static const char *expected_paths[] = { "/A/B/E/alpha", "/A/B/E/beta", }; num_expected_paths = sizeof(expected_paths) / sizeof(const char *); get_locks_baton = make_get_locks_baton(pool); SVN_ERR(svn_fs_get_locks(fs, "A/B", get_locks_callback, get_locks_baton, pool)); SVN_ERR(verify_matching_lock_paths(get_locks_baton, expected_paths, num_expected_paths, pool)); } /* Verify from "/A/D". */ { static const char *expected_paths[] = { "/A/D/G/pi", "/A/D/G/rho", "/A/D/G/tau", "/A/D/H/psi", "/A/D/H/chi", "/A/D/H/omega", }; num_expected_paths = sizeof(expected_paths) / sizeof(const char *); get_locks_baton = make_get_locks_baton(pool); SVN_ERR(svn_fs_get_locks(fs, "A/D", get_locks_callback, get_locks_baton, pool)); SVN_ERR(verify_matching_lock_paths(get_locks_baton, expected_paths, num_expected_paths, pool)); } /* Verify from "/A/D/G". */ { static const char *expected_paths[] = { "/A/D/G/pi", "/A/D/G/rho", "/A/D/G/tau", }; num_expected_paths = sizeof(expected_paths) / sizeof(const char *); get_locks_baton = make_get_locks_baton(pool); SVN_ERR(svn_fs_get_locks(fs, "A/D/G", get_locks_callback, get_locks_baton, pool)); SVN_ERR(verify_matching_lock_paths(get_locks_baton, expected_paths, num_expected_paths, pool)); } /* Verify from "/A/D/H/omega". */ { static const char *expected_paths[] = {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -