You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2019/02/23 03:57:43 UTC

[couchdb-b64url] 02/31: Encoder is passing tests.

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

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 795d11cb31e72992432bf2edb381a18886c57f43
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Fri Nov 1 11:24:04 2013 -0500

    Encoder is passing tests.
---
 c_src/couch_seqs_b64.c           |  8 ++------
 rebar.config                     |  3 +++
 test/couch_seqs_b64url_tests.erl | 42 ++++++++++++++++++++++++++++++++++------
 3 files changed, 41 insertions(+), 12 deletions(-)

diff --git a/c_src/couch_seqs_b64.c b/c_src/couch_seqs_b64.c
index 8477508..a48d928 100644
--- a/c_src/couch_seqs_b64.c
+++ b/c_src/couch_seqs_b64.c
@@ -233,11 +233,11 @@ cseq_b64url_encode(ErlNifEnv* env, ErlNifBinary* src, cseq_st* st)
         }
     }
 
-    if(src->size - st->si == 2) {
+    if(src->size % 3 == 1) {
         c1 = src->data[st->si];
         st->tgt->data[st->ti++] = B64URL_B2A[(c1 >> 2) & 0x3F];
         st->tgt->data[st->ti++] = B64URL_B2A[(c1 << 4) & 0x3F];
-    } else if(src->size - st->si == 1) {
+    } else if(src->size % 3 == 2) {
         c1 = src->data[st->si];
         c2 = src->data[st->si+1];
         st->tgt->data[st->ti++] = B64URL_B2A[(c1 >> 2) & 0x3F];
@@ -270,10 +270,6 @@ cseq_b64url_encode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
         return enif_make_badarg(env);
     }
 
-    if(src.size <= 0) {
-        return enif_make_badarg(env);
-    }
-
     st = (cseq_st*) enif_alloc_resource(priv->res_st, sizeof(cseq_st));
     if(st == NULL) {
         ret = make_error(env, priv, priv->atom_nomem);
diff --git a/rebar.config b/rebar.config
index 6f2ca46..5a9da0f 100644
--- a/rebar.config
+++ b/rebar.config
@@ -10,3 +10,6 @@
     {".*", "CFLAGS", "$CFLAGS -Wall -Werror -DNDEBUG -O3"}
 ]}.
 
+{deps, [
+    {proper, ".*", {git, "https://github.com/manopapad/proper.git", "master"}}
+]}.
diff --git a/test/couch_seqs_b64url_tests.erl b/test/couch_seqs_b64url_tests.erl
index 0e1089a..9fabe3e 100644
--- a/test/couch_seqs_b64url_tests.erl
+++ b/test/couch_seqs_b64url_tests.erl
@@ -8,28 +8,58 @@
 proper_test_() ->
     PropErOpts = [
         {to_file, user},
-        {max_size, 524288},
+        {max_size, 6401},
         {numtests, 1000}
     ],
     {timeout, 3600, ?_assertEqual([], proper:module(?MODULE, PropErOpts))}.
 
 
 prop_encode_binary() ->
-    ?FORALL(Bin, binary(),
-        couch_seqs_b64url:encode(Bin) == couch_encode_base64url(Bin)
-    ).
+    ?FORALL(Bin, binary(), begin
+        A = couch_encode_base64url(Bin),
+        B = couch_seqs_b64url:encode(Bin),
+        A == B
+    end).
 
 
-couch_encode_base64url(Data) ->
+prop_encode_iolist() ->
+    ?FORALL(Bin, binary(), begin
+        A = couch_encode_base64url(Bin),
+        B = couch_seqs_b64url:encode(to_iolist(Bin)),
+        A == B
+    end).
+
+
+couch_encode_base64url(Url) ->
     Url1 = iolist_to_binary(re:replace(base64:encode(Url), "=+$", "")),
     Url2 = iolist_to_binary(re:replace(Url1, "/", "_", [global])),
     iolist_to_binary(re:replace(Url2, "\\+", "-", [global])).
 
 
-couch_decode_base64url(Data) ->
+couch_decode_base64url(Url64) ->
     Url1 = re:replace(iolist_to_binary(Url64), "-", "+", [global]),
     Url2 = iolist_to_binary(
         re:replace(iolist_to_binary(Url1), "_", "/", [global])
     ),
     Padding = list_to_binary(lists:duplicate((4 - size(Url2) rem 4) rem 4, $=)),
     base64:decode(<<Url2/binary, Padding/binary>>).
+
+
+to_iolist(<<>>) ->
+    case random:uniform(2) of
+        1 -> <<>>;
+        2 -> [<<>>]
+    end;
+to_iolist(B) when is_binary(B), size(B) > 0 ->
+    S = random:uniform(size(B)),
+    <<First:S/binary, Second/binary>> = B,
+    case random:uniform(3) of
+        1 ->
+            [to_iolist(First), Second];
+        2 ->
+            [First, to_iolist(Second)];
+        3 ->
+            [First, Second]
+    end.
+
+