Fix profile creation in FakeLoginUtils.
[chromium-blink-merge.git] / google_apis / cup / client_update_protocol_unittest.cc
blob6a2abedce44df18c1e75cfafb5826114cd77f0da
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <limits>
6 #include <vector>
8 #include "base/base64.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "crypto/random.h"
11 #include "crypto/secure_util.h"
12 #include "google_apis/cup/client_update_protocol.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace {
17 std::string GetPublicKeyForTesting() {
18 // How to generate this key:
19 // openssl genpkey -out cr.pem -outform PEM -algorithm RSA
20 // -pkeyopt rsa_keygen_pubexp:3
21 // openssl rsa -in cr.pem -pubout -out cr_pub.pem
23 static const char kCupTestKey1024_Base64[] =
24 "MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQC7ct1JhLSol2DkBcJdNjR3KkEA"
25 "ZfXpF22lDD2WZu5JAZ4NiZqnHsKGJNPUbCH4AhFsXmuW5wEHhUVNhsMP6F9mQ06D"
26 "i+ygwZ8aXlklmW4S0Et+SNg3i73fnYn0KDQzrzJnMu46s/CFPhjr4f0TH9b7oHkU"
27 "XbqNZtG6gwaN1bmzFwIBAw==";
29 std::string result;
30 if (!base::Base64Decode(std::string(kCupTestKey1024_Base64), &result))
31 return "";
33 return result;
36 } // end namespace
38 #if defined(USE_OPENSSL)
40 // Once CUP is implemented for OpenSSL, remove this #if block.
41 TEST(CupTest, OpenSSLStub) {
42 scoped_ptr<ClientUpdateProtocol> cup =
43 ClientUpdateProtocol::Create(8, GetPublicKeyForTesting());
44 ASSERT_FALSE(cup.get());
47 #else
49 class CupTest : public testing::Test {
50 protected:
51 virtual void SetUp() {
52 cup_ = ClientUpdateProtocol::Create(8, GetPublicKeyForTesting());
53 ASSERT_TRUE(cup_.get());
56 void OverrideRAndRebuildKeys() {
57 // This must be the same length as the modulus of the public key being
58 // used in the unit test.
59 static const size_t kPublicKeyLength = 1024 / 8;
60 static const char kFixedR[] =
61 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do "
62 "eiusmod tempor incididunt ut labore et dolore magna aliqua. ";
64 ASSERT_EQ(kPublicKeyLength, strlen(kFixedR));
65 ASSERT_TRUE(cup_->SetSharedKeyForTesting(kFixedR));
68 ClientUpdateProtocol& CUP() {
69 return *cup_.get();
72 private:
73 scoped_ptr<ClientUpdateProtocol> cup_;
76 TEST_F(CupTest, GetVersionedSecret) {
77 // Given a fixed public key set in the test fixture, if the key source |r|
78 // is filled with known data, |w| can be tested against an expected output,
79 // and the signing/verification for an given request becomes fixed.
81 // The expected output can be generated using this command line, where
82 // plaintext.bin is the contents of kFixedR[] in OverrideRAndRebuildKeys():
84 // openssl rsautl -inkey cr2_pub.pem -pubin -encrypt -raw
85 // -in plaintext.bin | base64
87 // Remember to prepend the key version number, and fix up the Base64
88 // afterwards to be URL-safe.
90 static const char kExpectedVW[] =
91 "8:lMmNR3mVbOitbq8ceYGStFBwrJcpvY-sauFSbMVe6VONS9x42xTOLY_KdqsWCy"
92 "KuiJBiQziQLOybPUyA9vk0N5kMnC90LIh2nP2FgFG0M0Z22qjB3drsdJPi7TQZbb"
93 "Xhqm587M8vjc6VlM_eoC0qYwCPaXBqXjsyiHnXetcn5X0";
95 EXPECT_NE(kExpectedVW, CUP().GetVersionedSecret());
96 OverrideRAndRebuildKeys();
97 EXPECT_EQ(kExpectedVW, CUP().GetVersionedSecret());
100 TEST_F(CupTest, SignRequest) {
101 static const char kUrl[] = "//testserver.chromium.org/update";
102 static const char kUrlQuery[] = "?junk=present";
103 static const char kRequest[] = "testbody";
105 static const char kExpectedCP[] = "tfjmVMDAbU0-Kye4PjrCuyQIDCU";
107 OverrideRAndRebuildKeys();
109 // Check the case with no query string other than v|w.
110 std::string url(kUrl);
111 url.append("?w=");
112 url.append(CUP().GetVersionedSecret());
114 std::string cp;
115 ASSERT_TRUE(CUP().SignRequest(url, kRequest, &cp));
117 // Check the case with a pre-existing query string.
118 std::string url2(kUrl);
119 url2.append(kUrlQuery);
120 url2.append("&w=");
121 url2.append(CUP().GetVersionedSecret());
123 std::string cp2;
124 ASSERT_TRUE(CUP().SignRequest(url2, kRequest, &cp2));
126 // Changes in the URL should result in changes in the client proof.
127 EXPECT_EQ(kExpectedCP, cp2);
128 EXPECT_NE(cp, cp2);
131 TEST_F(CupTest, ValidateResponse) {
132 static const char kUrl[] = "//testserver.chromium.orgupdate?junk=present&w=";
133 static const char kRequest[] = "testbody";
135 static const char kGoodResponse[] = "intact_response";
136 static const char kGoodC[] = "c=EncryptedDataFromTheUpdateServer";
137 static const char kGoodSP[] = "5rMFMPL9Hgqb-2J8kL3scsHeNgg";
139 static const char kBadResponse[] = "tampered_response";
140 static const char kBadC[] = "c=TotalJunkThatAnAttackerCouldSend";
141 static const char kBadSP[] = "Base64TamperedShaOneHash";
143 OverrideRAndRebuildKeys();
145 std::string url(kUrl);
146 url.append(CUP().GetVersionedSecret());
148 std::string client_proof;
149 ASSERT_TRUE(CUP().SignRequest(url, kRequest, &client_proof));
151 // Return true on a valid response and server proof.
152 EXPECT_TRUE(CUP().ValidateResponse(kGoodResponse, kGoodC, kGoodSP));
154 // Return false on anything invalid.
155 EXPECT_FALSE(CUP().ValidateResponse(kBadResponse, kGoodC, kGoodSP));
156 EXPECT_FALSE(CUP().ValidateResponse(kGoodResponse, kBadC, kGoodSP));
157 EXPECT_FALSE(CUP().ValidateResponse(kGoodResponse, kGoodC, kBadSP));
158 EXPECT_FALSE(CUP().ValidateResponse(kGoodResponse, kBadC, kBadSP));
159 EXPECT_FALSE(CUP().ValidateResponse(kBadResponse, kGoodC, kBadSP));
160 EXPECT_FALSE(CUP().ValidateResponse(kBadResponse, kBadC, kGoodSP));
161 EXPECT_FALSE(CUP().ValidateResponse(kBadResponse, kBadC, kBadSP));
164 #endif // !defined(USE_OPENSSL)