| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 #include "net/http/http_mac_signature.h" | 6 #include "net/http/http_mac_signature.h" |
| 7 | 7 |
| 8 namespace net { | 8 namespace net { |
| 9 | 9 |
| 10 TEST(HttpMacSignatureTest, BogusAddStateInfo) { | 10 TEST(HttpMacSignatureTest, BogusAddStateInfo) { |
| 11 HttpMacSignature signature; | 11 HttpMacSignature signature; |
| 12 EXPECT_FALSE(signature.AddStateInfo("exciting-id", | 12 EXPECT_FALSE(signature.AddStateInfo("exciting-id", |
| 13 "the-mac-key", | 13 "the-mac-key", |
| 14 "bogus-hmac-algorithm", | 14 "bogus-hmac-algorithm")); |
| 15 "the-issuer")); | |
| 16 EXPECT_FALSE(signature.AddStateInfo("", | 15 EXPECT_FALSE(signature.AddStateInfo("", |
| 17 "the-mac-key", | 16 "the-mac-key", |
| 18 "hmac-sha-1", | 17 "hmac-sha-1")); |
| 19 "the-issuer")); | |
| 20 EXPECT_FALSE(signature.AddStateInfo("exciting-id", | 18 EXPECT_FALSE(signature.AddStateInfo("exciting-id", |
| 21 "", | 19 "", |
| 22 "hmac-sha-1", | 20 "hmac-sha-1")); |
| 23 "the-issuer")); | |
| 24 EXPECT_FALSE(signature.AddStateInfo("exciting-id", | 21 EXPECT_FALSE(signature.AddStateInfo("exciting-id", |
| 25 "the-mac-key", | 22 "the-mac-key", |
| 26 "", | |
| 27 "the-issuer")); | |
| 28 EXPECT_FALSE(signature.AddStateInfo("exciting-id", | |
| 29 "the-mac-key", | |
| 30 "hmac-sha-1", | |
| 31 "")); | 23 "")); |
| 32 } | 24 } |
| 33 | 25 |
| 34 TEST(HttpMacSignatureTest, BogusAddHttpInfo) { | 26 TEST(HttpMacSignatureTest, BogusAddHttpInfo) { |
| 35 HttpMacSignature signature; | 27 HttpMacSignature signature; |
| 36 EXPECT_FALSE(signature.AddHttpInfo("GET", "/requested", "example.com", 0)); | 28 EXPECT_FALSE(signature.AddHttpInfo("GET", "/requested", "example.com", 0)); |
| 37 EXPECT_FALSE(signature.AddHttpInfo( | 29 EXPECT_FALSE(signature.AddHttpInfo( |
| 38 "GET", "/requested", "example.com", 29088983)); | 30 "GET", "/requested", "example.com", 29088983)); |
| 39 EXPECT_FALSE(signature.AddHttpInfo("", "/requested", "example.com", 80)); | 31 EXPECT_FALSE(signature.AddHttpInfo("", "/requested", "example.com", 80)); |
| 40 EXPECT_FALSE(signature.AddHttpInfo("GET", "", "example.com", 80)); | 32 EXPECT_FALSE(signature.AddHttpInfo("GET", "", "example.com", 80)); |
| 41 EXPECT_FALSE(signature.AddHttpInfo("GET", "/requested", "", 80)); | 33 EXPECT_FALSE(signature.AddHttpInfo("GET", "/requested", "", 80)); |
| 42 } | 34 } |
| 43 | 35 |
| 44 TEST(HttpMacSignatureTest, GenerateHeaderString) { | 36 TEST(HttpMacSignatureTest, GenerateHeaderString) { |
| 45 HttpMacSignature signature; | 37 HttpMacSignature signature; |
| 46 EXPECT_TRUE(signature.AddStateInfo("dfoi30j0qnf", | 38 EXPECT_TRUE(signature.AddStateInfo("dfoi30j0qnf", |
| 47 "adiMf03j0f3nOenc003r", | 39 "adiMf03j0f3nOenc003r", |
| 48 "hmac-sha-1", | 40 "hmac-sha-1")); |
| 49 "login.eXampLe.com:443")); | |
| 50 EXPECT_TRUE(signature.AddHttpInfo("GeT", | 41 EXPECT_TRUE(signature.AddHttpInfo("GeT", |
| 51 "/pAth?to=%22enlightenment%22&dest=magic", | 42 "/pAth?to=%22enlightenment%22&dest=magic", |
| 52 "eXaMple.com", | 43 "eXaMple.com", |
| 53 80)); | 44 80)); |
| 54 | 45 |
| 55 std::string timestamp = "239034"; | 46 std::string timestamp = "239034"; |
| 56 std::string nonce = "mn4302j0n+32r2/f3r="; | 47 std::string nonce = "mn4302j0n+32r2/f3r="; |
| 57 | 48 |
| 58 EXPECT_EQ("MAC id=\"dfoi30j0qnf\", " | 49 EXPECT_EQ("MAC id=\"dfoi30j0qnf\", " |
| 59 "issuer=\"login.eXampLe.com:443\", " | 50 "nonce=\"239034:mn4302j0n+32r2/f3r=\", " |
| 60 "timestamp=\"239034\", " | 51 "mac=\"GrkHtPKzB1m1dCHfa7OCWOw6EQ==\"", |
| 61 "nonce=\"mn4302j0n+32r2/f3r=\", " | |
| 62 "mac=\"zQWLNI5eHOfY5/wCJ6yzZ8bXDw==\"", | |
| 63 signature.GenerateHeaderString(timestamp, nonce)); | 52 signature.GenerateHeaderString(timestamp, nonce)); |
| 64 } | 53 } |
| 65 | 54 |
| 66 | 55 |
| 67 TEST(HttpMacSignatureTest, GenerateNormalizedRequest) { | 56 TEST(HttpMacSignatureTest, GenerateNormalizedRequest) { |
| 68 HttpMacSignature signature; | 57 HttpMacSignature signature; |
| 69 EXPECT_TRUE(signature.AddStateInfo("dfoi30j0qnf", | 58 EXPECT_TRUE(signature.AddStateInfo("dfoi30j0qnf", |
| 70 "adiMf03j0f3nOenc003r", | 59 "adiMf03j0f3nOenc003r", |
| 71 "hmac-sha-1", | 60 "hmac-sha-1")); |
| 72 "login.eXampLe.com:443")); | |
| 73 EXPECT_TRUE(signature.AddHttpInfo("GeT", | 61 EXPECT_TRUE(signature.AddHttpInfo("GeT", |
| 74 "/pAth?to=%22enlightenment%22&dest=magic", | 62 "/pAth?to=%22enlightenment%22&dest=magic", |
| 75 "eXaMple.com", | 63 "eXaMple.com", |
| 76 80)); | 64 80)); |
| 77 | 65 |
| 78 std::string timestamp = "239034"; | 66 std::string timestamp = "239034"; |
| 79 std::string nonce = "mn4302j0n+32r2/f3r="; | 67 std::string nonce = "mn4302j0n+32r2/f3r="; |
| 80 | 68 |
| 81 EXPECT_EQ("dfoi30j0qnf\n" | 69 EXPECT_EQ("239034:mn4302j0n+32r2/f3r=\n" |
| 82 "login.eXampLe.com:443\n" | |
| 83 "239034\n" | |
| 84 "mn4302j0n+32r2/f3r=\n" | |
| 85 "GET\n" | 70 "GET\n" |
| 86 "/pAth?to=%22enlightenment%22&dest=magic\n" | 71 "/pAth?to=%22enlightenment%22&dest=magic\n" |
| 87 "example.com\n" | 72 "example.com\n" |
| 88 "80\n", | 73 "80\n" |
| 74 "\n" |
| 75 "\n", |
| 89 signature.GenerateNormalizedRequest(timestamp, nonce)); | 76 signature.GenerateNormalizedRequest(timestamp, nonce)); |
| 90 } | 77 } |
| 91 | 78 |
| 92 TEST(HttpMacSignatureTest, GenerateMAC) { | 79 TEST(HttpMacSignatureTest, GenerateMAC) { |
| 93 HttpMacSignature signature; | 80 HttpMacSignature signature; |
| 94 EXPECT_TRUE(signature.AddStateInfo("dfoi30j0qnf", | 81 EXPECT_TRUE(signature.AddStateInfo("dfoi30j0qnf", |
| 95 "adiMf03j0f3nOenc003r", | 82 "adiMf03j0f3nOenc003r", |
| 96 "hmac-sha-1", | 83 "hmac-sha-1")); |
| 97 "login.eXampLe.com:443")); | |
| 98 EXPECT_TRUE(signature.AddHttpInfo("GeT", | 84 EXPECT_TRUE(signature.AddHttpInfo("GeT", |
| 99 "/pAth?to=%22enlightenment%22&dest=magic", | 85 "/pAth?to=%22enlightenment%22&dest=magic", |
| 100 "eXaMple.com", | 86 "eXaMple.com", |
| 101 80)); | 87 80)); |
| 102 | 88 |
| 103 std::string timestamp = "239034"; | 89 std::string timestamp = "239034"; |
| 104 std::string nonce = "mn4302j0n+32r2/f3r="; | 90 std::string nonce = "mn4302j0n+32r2/f3r="; |
| 105 | 91 |
| 106 EXPECT_EQ("zQWLNI5eHOfY5/wCJ6yzZ8bXDw==", | 92 EXPECT_EQ("GrkHtPKzB1m1dCHfa7OCWOw6EQ==", |
| 107 signature.GenerateMAC(timestamp, nonce)); | 93 signature.GenerateMAC(timestamp, nonce)); |
| 108 } | 94 } |
| 109 } | 95 } |
| OLD | NEW |