couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gar...@apache.org
Subject [1/4] couch commit: updated refs/heads/local-docs-2978 to 2531c3b [Forced Update!]
Date Mon, 11 Apr 2016 14:27:28 GMT
Repository: couchdb-couch
Updated Branches:
  refs/heads/local-docs-2978 64b29d7ff -> 2531c3b81 (forced update)


Fix normalize_dbname to work with slashes

We shouldn't rely on dbname being the last part of path. Since database
name could include slashes.
normalize_dbname as the name suggests should accept either binary or
list and return only binary.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/910770d3
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/910770d3
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/910770d3

Branch: refs/heads/local-docs-2978
Commit: 910770d3faf0a23bd8dfb08abc6906fe45de4a41
Parents: c256ef5
Author: ILYA Khlopotov <iilyak@ca.ibm.com>
Authored: Wed Mar 30 15:02:52 2016 -0700
Committer: ILYA Khlopotov <iilyak@ca.ibm.com>
Committed: Thu Apr 7 09:34:19 2016 -0700

----------------------------------------------------------------------
 src/couch_db.erl | 80 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 77 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/910770d3/src/couch_db.erl
----------------------------------------------------------------------
diff --git a/src/couch_db.erl b/src/couch_db.erl
index 1a9f669..c34d59a 100644
--- a/src/couch_db.erl
+++ b/src/couch_db.erl
@@ -1513,10 +1513,8 @@ select_gt(V1, _V2) -> V1.
 select_lt(V1, V2) when V1 > V2 -> V2;
 select_lt(V1, _V2) -> V1.
 
-normalize_dbname(<<"shards/", _/binary>> = Path) ->
-    lists:last(binary:split(mem3:dbname(Path), <<"/">>, [global]));
 normalize_dbname(DbName) ->
-    DbName.
+    mem3:dbname(DbName).
 
 validate_dbname(DbName) when is_list(DbName) ->
     validate_dbname(?l2b(DbName));
@@ -1546,3 +1544,79 @@ is_systemdb(<<"shards/", _/binary>> = Path) when is_binary(Path)
->
     is_systemdb(normalize_dbname(Path));
 is_systemdb(DbName) when is_binary(DbName) ->
     lists:member(DbName, ?SYSTEM_DATABASES).
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+
+setup() ->
+    ok = meck:new(couch_db_plugin, [passthrough]),
+    ok = meck:expect(couch_db_plugin, validate_dbname, fun(_, _) -> false end),
+    ok.
+
+teardown(_) ->
+    (catch meck:unload(couch_db_plugin)).
+
+validate_dbname_success_test_() ->
+    Cases =
+        generate_cases_with_shards("long/co$mplex-/path+/_something")
+        ++ generate_cases_with_shards("something")
+        ++ lists:append(
+            [generate_cases_with_shards(?b2l(SystemDb))
+                || SystemDb <- ?SYSTEM_DATABASES]),
+    {
+        foreach, fun setup/0, fun teardown/1,
+        [{test_name(A), fun() -> should_pass_validate_dbname(A) end} || {_, A} <- Cases]
+    }.
+
+validate_dbname_fail_test_() ->
+    Cases = generate_cases("_long/co$mplex-/path+/_something")
+       ++ generate_cases("_something")
+       ++ generate_cases_with_shards("long/co$mplex-/path+/_something#")
+       ++ generate_cases_with_shards("long/co$mplex-/path+/some.thing"),
+    {
+        foreach, fun setup/0, fun teardown/1,
+        [{test_name(A), fun() -> should_fail_validate_dbname(A) end} || {_, A} <- Cases]
+    }.
+
+validate_normalize_dbname_test_() ->
+    Cases = generate_cases_with_shards("long/co$mplex-/path+/_something")
+       ++ generate_cases_with_shards("something"),
+    [{test_name(B), fun() -> should_normalize_dbname(A, B) end} || {A, B} <- Cases].
+
+
+should_pass_validate_dbname(DbName) ->
+    {test_name(DbName), ?_assertEqual(ok, validate_dbname(DbName))}.
+
+should_fail_validate_dbname(DbName) ->
+    {test_name(DbName), ?_test(begin
+        Result = validate_dbname(DbName),
+        ?assertMatch({error, {illegal_database_name, _}}, Result),
+        {error, {illegal_database_name, FailedDbName}} = Result,
+        ?assertEqual(to_binary(DbName), FailedDbName),
+        ok
+    end)}.
+
+should_normalize_dbname(Arg, DbName) ->
+    Result = ?l2b(filename:rootname(Arg)),
+    {test_name(DbName), ?_assertEqual(Result, normalize_dbname(DbName))}.
+
+to_binary(DbName) when is_list(DbName) ->
+    ?l2b(DbName);
+to_binary(DbName) when is_binary(DbName) ->
+    DbName.
+
+test_name(DbName) ->
+    lists:flatten(io_lib:format("~p", [DbName])).
+
+generate_cases_with_shards(DbName) ->
+    DbNameWithShard = add_shard(DbName),
+    Cases = [DbName, ?l2b(DbName), DbNameWithShard, ?l2b(DbNameWithShard)],
+    [{DbName, Case} || Case <- Cases].
+
+add_shard(DbName) ->
+    "shards/00000000-3fffffff/" ++ DbName ++ ".1415960794".
+
+generate_cases(DbName) ->
+    [{DbName, DbName}, {DbName, ?l2b(DbName)}].
+
+-endif.


Mime
View raw message