You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ii...@apache.org on 2016/04/07 18:36:15 UTC

[1/2] couch commit: updated refs/heads/master to 05b40a8

Repository: couchdb-couch
Updated Branches:
  refs/heads/master c256ef5d0 -> 05b40a8ef


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/master
Commit: 910770d3faf0a23bd8dfb08abc6906fe45de4a41
Parents: c256ef5
Author: ILYA Khlopotov <ii...@ca.ibm.com>
Authored: Wed Mar 30 15:02:52 2016 -0700
Committer: ILYA Khlopotov <ii...@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.


[2/2] couch commit: updated refs/heads/master to 05b40a8

Posted by ii...@apache.org.
Merge remote branch 'github/pr/158'

This closes #158

Signed-off-by: ILYA Khlopotov <ii...@ca.ibm.com>


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

Branch: refs/heads/master
Commit: 05b40a8ef7e544b1313018d000f0c6d3d6255f1a
Parents: c256ef5 910770d
Author: ILYA Khlopotov <ii...@ca.ibm.com>
Authored: Thu Apr 7 09:35:26 2016 -0700
Committer: ILYA Khlopotov <ii...@ca.ibm.com>
Committed: Thu Apr 7 09:35:26 2016 -0700

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