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 2014/08/04 20:06:40 UTC
[02/15] git commit: Encoder is passing tests.
Encoder is passing tests.
Project: http://git-wip-us.apache.org/repos/asf/couchdb-b64url/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-b64url/commit/795d11cb
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-b64url/tree/795d11cb
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-b64url/diff/795d11cb
Branch: refs/heads/windsor-merge
Commit: 795d11cb31e72992432bf2edb381a18886c57f43
Parents: 13e21f4
Author: Paul J. Davis <pa...@gmail.com>
Authored: Fri Nov 1 11:24:04 2013 -0500
Committer: Paul J. Davis <pa...@gmail.com>
Committed: Fri Nov 1 11:24:04 2013 -0500
----------------------------------------------------------------------
c_src/couch_seqs_b64.c | 8 ++-----
rebar.config | 3 +++
test/couch_seqs_b64url_tests.erl | 42 ++++++++++++++++++++++++++++++-----
3 files changed, 41 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-b64url/blob/795d11cb/c_src/couch_seqs_b64.c
----------------------------------------------------------------------
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);
http://git-wip-us.apache.org/repos/asf/couchdb-b64url/blob/795d11cb/rebar.config
----------------------------------------------------------------------
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"}}
+]}.
http://git-wip-us.apache.org/repos/asf/couchdb-b64url/blob/795d11cb/test/couch_seqs_b64url_tests.erl
----------------------------------------------------------------------
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.
+
+