diff --git a/.gitignore b/.gitignore index ac32c68..e54cb53 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ Mnesia.* sasl_err* erl_crash* ebin/openid.app -deps/* \ No newline at end of file +deps/* +.eunit/* \ No newline at end of file diff --git a/rebar.config b/rebar.config index 0fc40b3..76d32fc 100644 --- a/rebar.config +++ b/rebar.config @@ -1 +1,3 @@ +{cover_enabled, true}. +{erl_opts, [fail_on_warning, debug_info]}. {deps, [{ibrowse, ".*", {git, "git://github.com/andrewtj/ibrowse.git", "master"}}]}. \ No newline at end of file diff --git a/src/openid.erl b/src/openid.erl index 9a839d9..225f2b3 100644 --- a/src/openid.erl +++ b/src/openid.erl @@ -7,7 +7,7 @@ %%%------------------------------------------------------------------- -module(openid). --export([discover/1, associate/1, authentication_url/3, test/0]). +-export([discover/1, associate/1, authentication_url/3]). -include("openid.hrl"). @@ -215,65 +215,3 @@ authentication_url(AuthReq, ReturnTo, Realm) -> [URL|_] = AuthReq#openid_authreq.opURLs, list_to_binary([URL, "?", QueryString]). - -%% ------------------------------------------------------------ -%% Tests -%% ------------------------------------------------------------ - -test() -> - - application:start(inets), - application:start(ssl), - - Test = fun(ID) -> - ?DBG({identifier, ID}), - Req = discover(ID), - %?DBG({request, Req}), - [URL,_] = Req#openid_authreq.opURLs, - Assoc = associate(URL), - ?DBG({assoc, Assoc}), - %?DBG({auth, authenticate(Req2, "http://dev.brendonh.org/return", "http://dev.brendonh.org/")}), - ok - end, - - % 2.0 - Test("https://www.google.com/accounts/o8/id"), - %Test("http://flickr.com/exbrend"), - %Test("=brendonh"), - - % 1.0 / 1.1 - %?DBG({"AOL:", discover("http://openid.aol.com/brend")}), - %?DBG({"Myspace:", discover("www.myspace.com")}), - %?DBG({"LiveJournal:", discover("http://exbrend.livejournal.com")}), - %?DBG({"PaulBonser:", discover("blog.paulbonser.com")}), - - application:stop(inets). % Avoid error spam from held-open connections - - - - -%% $ make test -%% erlc -o ebin -Wall -v +debug_info src/openid.erl -%% src/openid.erl:155: Warning: function unroll/1 is unused -%% erl +W w -pa ebin -noshell -pa ../mochiweb/ebin -s openid test -s init stop -%% <0.1.0>: {identifier,"https://www.google.com/accounts/o8/id"} -%% <0.1.0>: {request,{authReq,"https://www.google.com/accounts/o8/ud", -%% {2,0}, -%% none,none}} -%% <0.1.0>: {assoc,{"46800", -%% "AO6d/PJErS+mEYxZNEsr3L/Tz6SvipjoQQW4TN8XxzHXTW8n4POIjk9kUBfl1yQvLF2rEmL4R3OqAKgDVsTIb9WzFF75+QmJXtXq5DqyQ4HRgBqgZk2RmijOHSKsVsZbsA==", -%% "CbImJ5wv7y1jgGCS3RWq5cCvByE="}} -%% <0.1.0>: {identifier,"http://flickr.com/exbrend"} -%% <0.1.0>: {request,{authReq,"https://open.login.yahooapis.com/openid/op/auth", -%% {2,0}, -%% none,none}} -%% <0.1.0>: {assoc,{"14400", -%% "EBXqqs44y4MkuvOIVGF+TSUot+/FGqBtMZJm8KyQXwabUc09iB2AesfVb4J2iM2JaPdvk0VgfYur7ywJY9zCZA5bvSNKEOicFP5SAVBsfdCNyCUEjMRt0tvcDAnygWzo", -%% "wBens28gyUh8NzNKVS3IafvWeYE="}} -%% <0.1.0>: {identifier,"=brendonh"} -%% <0.1.0>: {request,{authReq,"https://authn.fullxri.com/authentication/", -%% {2,0}, -%% "=brendonh",none}} -%% <0.1.0>: {assoc,{"1800", -%% "AKngKTyiIQ0JcX3/vXrnavfyLWCj6hsiOTYypoKPS25DAaprDRKkq5gXL4q0Foc+YAUqrLlTuT63W6PeVSpZEornRfNHs3Trfoxggp3N4uE8BFvlHvyf1XySXNANPbLLFQ==", -%% "KIW/+jlgDASt3Xx8T7vHfb1F0vU="}} diff --git a/src/openid_srv.erl b/src/openid_srv.erl index e899da0..2bb50cb 100644 --- a/src/openid_srv.erl +++ b/src/openid_srv.erl @@ -12,7 +12,7 @@ -include("openid.hrl"). %% API --export([start_link/1, test/0]). +-export([start_link/1]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, @@ -265,14 +265,3 @@ check_return_id(ReturnTo, Fields) -> GivenReturn = ?GV("openid.return_to", Fields), % XXX Todo -- do this properly, for people who put fancy stuff in return URLs ReturnTo == GivenReturn. - - - -%%-------------------------------------------------------------------- -%%% Tests -%%-------------------------------------------------------------------- - -test() -> - {ok, Server} = gen_server:start(?MODULE, start_link, [test_server]), - ?DBG({server, Server}), - ?DBG({one, gen_server:call(Server, {prepare, "foo", "=brendonh", true})}). diff --git a/src/yadis.erl b/src/yadis.erl index 3455e66..dd0ec10 100644 --- a/src/yadis.erl +++ b/src/yadis.erl @@ -7,7 +7,7 @@ %%%------------------------------------------------------------------- -module(yadis). --export([retrieve/1, test/0]). +-export([retrieve/1]). -include("openid.hrl"). -include_lib("xmerl/include/xmerl.hrl"). @@ -155,46 +155,3 @@ get_local_id(Service, [Tag|Rest]) -> [] -> get_local_id(Service, Rest); [#xmlElement{content=[Value|_]}|_] -> Value#xmlText.value end. - - -%% ------------------------------------------------------------ -%% Tests -%% ------------------------------------------------------------ - --define(P(S), io:format("~p~n", [S])). - -test() -> - - ?P({"Google:", retrieve("https://www.google.com/accounts/o8/id")}), % Direct XRDS response - %?P({"AOL:", retrieve("https://api.screenname.aol.com/auth/openidServer")}), % x-xrds-location header - ?P({"Flickr:", retrieve("http://flickr.com/exbrend")}), - ?P({"LiveJournal:", retrieve("http://exbrend.livejournal.com")}), % x-xrds-location meta tag - ?P({"XRI Brend:", retrieve("=brendonh")}), % Direct XRDS via xri.net - - application:stop(inets). % Avoid error spam from held-open connections - - -%% $ make test -%% erlc -o ebin -Wall -v +debug_info src/yadis.erl -%% src/yadis.erl:146: Warning: variable 'Rest' is unused -%% erl +W w -pa ebin -noshell -s yadis test -s init stop -%% {"Google:", -%% [{["http://specs.openid.net/auth/2.0/server","http://openid.net/srv/ax/1.0", -%% "http://specs.openid.net/extensions/ui/1.0/mode/popup", -%% "http://specs.openid.net/extensions/ui/1.0/icon", -%% "http://specs.openid.net/extensions/pape/1.0"], -%% ["https://www.google.com/accounts/o8/ud"]}]} -%% {"AOL:", -%% [{["http://specs.openid.net/auth/2.0/return_to"], -%% ["https://api.screenname.aol.com/auth/oidRet"]}]} -%% {"LiveJournal:", -%% [{["http://openid.net/signon/1.0"], -%% ["http://www.livejournal.com/openid/server.bml"]}]} -%% {"XRI Drummond:", -%% [{[],["skype:drummondreed?chat"]}, -%% {[],["skype:drummondreed?call"]}, -%% {["xri://+i-service*(+forwarding)*($v*1.0)",[]],["http://1id.com/"]}, -%% {["http://openid.net/signon/1.0"], -%% ["http://1id.com/sso/","https://1id.com/sso/"]}, -%% {["xri://+i-service*(+contact)*($v*1.0)",[]],["http://1id.com/contact/"]}]} - diff --git a/test/openid_pm_test.erl b/test/openid_pm_test.erl new file mode 100644 index 0000000..0fb53d1 --- /dev/null +++ b/test/openid_pm_test.erl @@ -0,0 +1,37 @@ +-module(openid_pm_test). +-compile([export_all]). +-include_lib("eunit/include/eunit.hrl"). + +-define(PL, [{"mode", "error"}, {"error", "This is an example message"}]). +-define(KVF, "mode:error\nerror:This is an example message\n"). +-define(URL, "openid.mode=error&openid.error=This%20is%20an%20example%20message"). +-define(URI, "openid.mode=error&openid.error=This+is+an+example+message"). + +url_encode_test_() -> + {"URL Encode", ?_assertEqual(?URL, openid_pm:url_encode(?PL))}. + +uri_encode_test_() -> + {"URI Encode", ?_assertEqual(?URI, openid_pm:uri_encode(?PL))}. + +kvf_encode_test_() -> + {"Key-Value Form Encode", ?_assertEqual(?KVF, openid_pm:kvf_encode(?PL))}. + +kvf_decode_test_() -> + {"Key-Value Form Decode", ?_assertEqual(?PL, openid_pm:kvf_decode(?KVF))}. + +prefix_check_test_() -> + Input = [ {"openid." ++ K, V} || {K, V} <- ?PL ], + {"Prefix Check", [?_assertEqual(?KVF, openid_pm:kvf_encode(Input)), + ?_assertEqual(?URI, openid_pm:uri_encode(Input))] + }. + +types_test_() -> + [ {"Encode " ++ Type, + ?_test(begin + Input = [ {Fun(K), Fun(V)} || {K, V} <- ?PL ], + ?assertEqual(?KVF, openid_pm:kvf_encode(Input)) + end) + } || {Type, Fun} <- [{"Atom", fun erlang:list_to_atom/1}, + {"Binary", fun erlang:list_to_binary/1} + ] ]. + diff --git a/test/openid_srv_test.erl b/test/openid_srv_test.erl new file mode 100644 index 0000000..d885edc --- /dev/null +++ b/test/openid_srv_test.erl @@ -0,0 +1,19 @@ +-module(openid_srv_test). +-compile([export_all]). +-include_lib("eunit/include/eunit.hrl"). +-include("openid.hrl"). + +setup() -> + [ application:start(X) || X <- [ crypto, ssl, sasl, inets, ibrowse ] ]. + +prepare_test_() -> + {setup, fun setup/0, + {timeout, 5000, + ?_test( + begin + {ok, Server} = gen_server:start(openid_srv, start_link, [test_server]), + Result = (catch gen_server:call(Server, {prepare, "foo", "http://exbrend.livejournal.com", true})), + ?_assertEqual(ok, element(1, Result)) + end) + } + }. diff --git a/test/openid_test.erl b/test/openid_test.erl new file mode 100644 index 0000000..a8d09ac --- /dev/null +++ b/test/openid_test.erl @@ -0,0 +1,36 @@ +-module(openid_test). +-compile([export_all]). +-include_lib("eunit/include/eunit.hrl"). +-include("openid.hrl"). + +setup() -> + [ application:start(X) || X <- [ crypto, ssl, sasl, inets, ibrowse ] ]. + +discover2_test_() -> + Cases = [{"https://www.google.com/accounts/o8/id", + #openid_authreq{opURLs = ["https://www.google.com/accounts/o8/ud"], + version = {2,0}, + claimedID = "http://specs.openid.net/auth/2.0/identifier_select", + localID = "http://specs.openid.net/auth/2.0/identifier_select", + assoc = none}}, + {"http://flickr.com/exbrend", + #openid_authreq{opURLs = ["https://open.login.yahooapis.com/openid/op/auth"], + version = {2,0}, + claimedID = "http://specs.openid.net/auth/2.0/identifier_select", + localID = "http://specs.openid.net/auth/2.0/identifier_select", + assoc = none}} + ], + {setup, fun setup/0, [?_assertEqual(Result, openid:discover(URL)) + || {URL, Result} <- Cases ]}. + +discover1_test_() -> + Cases = [{"blog.paulbonser.com", + #openid_authreq{opURLs = ["http://www.livejournal.com/openid/server.bml"], + version = {1,1}, + claimedID = "http://specs.openid.net/auth/2.0/identifier_select", + localID = "http://specs.openid.net/auth/2.0/identifier_select", + assoc = none}} + ], + {setup, fun setup/0, [?_assertEqual(Result, openid:discover(URL)) + || {URL, Result} <- Cases ]}. + diff --git a/test/openid_utils_test.erl b/test/openid_utils_test.erl new file mode 100644 index 0000000..5b4c7ab --- /dev/null +++ b/test/openid_utils_test.erl @@ -0,0 +1,20 @@ +-module(openid_utils_test). +-compile([export_all]). +-include_lib("eunit/include/eunit.hrl"). + +normalization_test_() -> + [?_assertEqual(Result, openid_utils:normalize_id(Id)) + || {Id, Result} <- [{"example.com", "http://example.com/"}, + {"http://example.com", "http://example.com/"}, + {"https://example.com/", "https://example.com/"}, + {"http://example.com/user", "http://example.com/user"}, + {"http://example.com/user/", "http://example.com/user/"}, + {"http://example.com/", "http://example.com/"}, + {"=example", "=example"}, + {"xri://=example", "=example"}, + {"http://ExAmPlE.CoM/foo/../a", "http://example.com/a"}, + {"http://ExAmPlE.CoM/foo/../a/", "http://example.com/a/"}, + {"http://eXAMPLE:80//a/./b/../b/%63/%7bfoo%7d", "http://example/a/b/c/%7Bfoo%7D"}, + {"http://example//a/b/c/%7Bfoo%7D", "http://example/a/b/c/%7Bfoo%7D"} + ] + ]. diff --git a/test/yadis_test.erl b/test/yadis_test.erl new file mode 100644 index 0000000..819e4a7 --- /dev/null +++ b/test/yadis_test.erl @@ -0,0 +1,32 @@ +-module(yadis_test). +-compile([export_all]). +-include_lib("eunit/include/eunit.hrl"). +-include("openid.hrl"). + +setup() -> + [ application:start(X) || X <- [ crypto, ssl, sasl, inets, ibrowse ] ]. + +retrieve_test_() -> + Cases = [{"https://www.google.com/accounts/o8/id", + #openid_xrds{origID = "https://www.google.com/accounts/o8/id", + claimedID = none, canonicalID = none, isXRI = false, + services = [#openid_xrdservice{types = ["http://specs.openid.net/auth/2.0/server", + "http://openid.net/srv/ax/1.0", + "http://specs.openid.net/extensions/ui/1.0/mode/popup", + "http://specs.openid.net/extensions/ui/1.0/icon", + "http://specs.openid.net/extensions/pape/1.0"], + uris = ["https://www.google.com/accounts/o8/ud"], + localID = none}]}}, + {"http://exbrend.livejournal.com", + #openid_xrds{origID = "http://exbrend.livejournal.com", + claimedID = "http://exbrend.livejournal.com/data/yadis", + canonicalID = none,isXRI = false, + services = [#openid_xrdservice{types = ["http://specs.openid.net/auth/2.0/signon"], + uris = ["http://www.livejournal.com/openid/server.bml"], + localID = "http://exbrend.livejournal.com/"}]}}, + {"=brendonh", + #openid_xrds{origID = "=brendonh",claimedID = "=brendonh", + canonicalID = none,isXRI = true,services = []}} + ], + {setup, fun setup/0, [?_assertEqual(Result, yadis:retrieve(URL)) + || {URL, Result} <- Cases ]}.