PageRenderTime 55ms CodeModel.GetById 43ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/mordor/tests/oauth.cpp

http://github.com/mozy/mordor
C++ | 62 lines | 55 code | 6 blank | 1 comment | 0 complexity | d0ce5ce0ab0744161579f2674a42453f MD5 | raw file
 1// Copyright (c) 2009 - Mozy, Inc.
 2
 3#include "mordor/http/oauth.h"
 4#include "mordor/test/test.h"
 5
 6using namespace Mordor::HTTP;
 7
 8MORDOR_UNITTEST(OAuth, oauthExample)
 9{
10    std::pair<std::string, std::string>
11        clientCredentials("dpf43f3p2l4k3l03", "kd94hf93k423kf44"),
12        temporaryCredentials("hh5s93j4hdidpola", "hdhd0244k9j7ao03"),
13        tokenCredentials("nnch734d00sl2jdk", "pfkkdhi9sl3r4s00");
14    std::string verifier = "hfdp7dh39dks9884";
15
16    StringMap oauthParameters;
17    oauthParameters["oauth_consumer_key"] = clientCredentials.first;
18    oauthParameters["oauth_version"] = "1.0";
19    oauthParameters["oauth_callback"] =
20        "http://printer.example.com/request_token_ready";
21    oauthParameters["oauth_timestamp"] = "1191242090";
22    oauthParameters["oauth_nonce"] = "hsu94j3884jdopsl";
23    OAuth::sign("https://photos.example.net/request_token", POST, "PLAINTEXT",
24        clientCredentials.second, std::string(), oauthParameters);
25    MORDOR_ASSERT(oauthParameters.find("oauth_signature") !=
26        oauthParameters.end());
27    MORDOR_TEST_ASSERT_EQUAL(oauthParameters["oauth_signature"],
28        "kd94hf93k423kf44&");
29    MORDOR_ASSERT(OAuth::validate("https://photos.example.net/request_token",
30        POST, clientCredentials.second, std::string(), oauthParameters));
31
32    oauthParameters.erase("oauth_callback");
33    oauthParameters["oauth_token"] = temporaryCredentials.first;
34    oauthParameters["oauth_verifier"] = verifier;
35    oauthParameters["oauth_timestamp"] = "1191242092";
36    oauthParameters["oauth_nonce"] = "dji430splmx33448";
37    OAuth::sign("https://photos.example.net/access_token", POST, "PLAINTEXT",
38        clientCredentials.second, temporaryCredentials.second,
39        oauthParameters);
40    MORDOR_ASSERT(oauthParameters.find("oauth_signature") !=
41        oauthParameters.end());
42    MORDOR_TEST_ASSERT_EQUAL(oauthParameters["oauth_signature"],
43        "kd94hf93k423kf44&hdhd0244k9j7ao03");
44    MORDOR_ASSERT(OAuth::validate("https://photos.example.net/access_token",
45        POST, clientCredentials.second, temporaryCredentials.second,
46        oauthParameters));
47
48    oauthParameters.erase("oauth_verifier");
49    oauthParameters["oauth_token"] = tokenCredentials.first;
50    oauthParameters["oauth_timestamp"] = "1191242096";
51    oauthParameters["oauth_nonce"] = "kllo9940pd9333jh";
52    OAuth::sign("http://photos.example.net/photos?file=vacation.jpg&size=original",
53        GET, "HMAC-SHA1", clientCredentials.second, tokenCredentials.second,
54        oauthParameters);
55    MORDOR_ASSERT(oauthParameters.find("oauth_signature") !=
56        oauthParameters.end());
57    MORDOR_TEST_ASSERT_EQUAL(oauthParameters["oauth_signature"],
58        "tR3+Ty81lMeYAr/Fid0kMTYa/WM=");
59    MORDOR_ASSERT(OAuth::validate("http://photos.example.net/photos?file=vacation.jpg&size=original",
60        GET, clientCredentials.second, tokenCredentials.second,
61        oauthParameters));
62}