You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2020/02/15 18:44:09 UTC

[couchdb] 04/05: Test coverage: validate_dbname, validate_docid

This is an automated email from the ASF dual-hosted git repository.

davisp pushed a commit to branch prototype/fdb-layer-test-coverage
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit d711444f4872639f853fdd99f1425e27351922ba
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Fri Feb 14 12:42:28 2020 -0600

    Test coverage: validate_dbname, validate_docid
---
 src/fabric/test/fabric2_db_misc_tests.erl | 75 +++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)

diff --git a/src/fabric/test/fabric2_db_misc_tests.erl b/src/fabric/test/fabric2_db_misc_tests.erl
index f1ee8a8..c48ee2c 100644
--- a/src/fabric/test/fabric2_db_misc_tests.erl
+++ b/src/fabric/test/fabric2_db_misc_tests.erl
@@ -33,6 +33,8 @@ misc_test_() ->
                 ?TDEF(set_revs_limit),
                 ?TDEF(set_security),
                 ?TDEF(is_system_db),
+                ?TDEF(validate_dbname),
+                ?TDEF(validate_doc_ids),
                 ?TDEF(get_doc_info),
                 ?TDEF(get_doc_info_not_found),
                 ?TDEF(get_full_doc_info),
@@ -54,6 +56,7 @@ setup() ->
 
 
 cleanup({_DbName, Db, Ctx}) ->
+    meck:unload(),
     ok = fabric2_db:delete(fabric2_db:name(Db), []),
     test_util:stop_couch(Ctx).
 
@@ -114,6 +117,78 @@ is_system_db({DbName, Db, _}) ->
     ?assertEqual(false, fabric2_db:is_system_db_name(<<"foo/bar">>)).
 
 
+validate_dbname(_) ->
+    Tests = [
+        {ok, <<"foo">>},
+        {ok, "foo"},
+        {ok, <<"_replicator">>},
+        {error, illegal_database_name, <<"Foo">>},
+        {error, illegal_database_name, <<"foo|bar">>},
+        {error, illegal_database_name, <<"Foo">>},
+        {error, database_name_too_long, <<
+                "0123456789012345678901234567890123456789"
+                "0123456789012345678901234567890123456789"
+                "0123456789012345678901234567890123456789"
+                "0123456789012345678901234567890123456789"
+                "0123456789012345678901234567890123456789"
+                "0123456789012345678901234567890123456789"
+            >>}
+    ],
+    CheckFun = fun
+        ({ok, DbName}) ->
+            ?assertEqual(ok, fabric2_db:validate_dbname(DbName));
+        ({error, Reason, DbName}) ->
+            Expect = {error, {Reason, DbName}},
+            ?assertEqual(Expect, fabric2_db:validate_dbname(DbName))
+    end,
+    lists:foreach(CheckFun, Tests).
+
+
+validate_doc_ids(_) ->
+    % Basic test with default max infinity length
+    ?assertEqual(ok, fabric2_db:validate_docid(<<"foo">>)),
+
+    Tests = [
+        {ok, <<"_local/foo">>},
+        {ok, <<"_design/foo">>},
+        {ok, <<"0123456789012345">>},
+        {illegal_docid, <<"">>},
+        {illegal_docid, <<"_design/">>},
+        {illegal_docid, <<"_local/">>},
+        {illegal_docid, <<"01234567890123456">>},
+        {illegal_docid, <<16#FF>>},
+        {illegal_docid, <<"_bad">>},
+        {illegal_docid, null}
+    ],
+    CheckFun = fun
+        ({ok, DocId}) ->
+            ?assertEqual(ok, fabric2_db:validate_docid(DocId));
+        ({illegal_docid, DocId}) ->
+            ?assertThrow({illegal_docid, _}, fabric2_db:validate_docid(DocId))
+    end,
+
+    try
+        meck:new(config, [passthrough]),
+        meck:expect(
+                config,
+                get,
+                ["couchdb", "max_document_id_length", "infinity"],
+                "16"
+            ),
+        lists:foreach(CheckFun, Tests),
+
+        % Check that fabric2_db_plugin can't allow for
+        % underscore prefixed dbs
+        meck:new(fabric2_db_plugin, [passthrough]),
+        meck:expect(fabric2_db_plugin, validate_docid, ['_'], true),
+        ?assertEqual(ok, fabric2_db:validate_docid(<<"_wheee">>))
+    after
+        % Unloading within the test as the config mock
+        % interferes with the db version bump test.
+        meck:unload()
+    end.
+
+
 get_doc_info({_, Db, _}) ->
     DocId = couch_uuids:random(),
     InsertDoc = #doc{