Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: net/cup_utils_unittest.cc

Issue 624713003: Keep only base/extractor.[cc|h]. (Closed) Base URL: https://chromium.googlesource.com/external/omaha.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/cup_utils.cc ('k') | net/detector.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2008-2009 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 // ========================================================================
15
16 #include <windows.h>
17 #include <cstring>
18 #include <vector>
19 #include "omaha/base/security/sha.h"
20 #include "omaha/base/string.h"
21 #include "omaha/base/utils.h"
22 #include "omaha/net/cup_utils.h"
23 #include "omaha/testing/unit_test.h"
24
25 namespace omaha {
26
27 namespace cup_utils {
28
29 namespace {
30
31 const char kPlainText[] = "The quick brown fox jumps over the lazy dog";
32 const int kRsaKeySize = 128; // 128 bytes.
33
34 // Test padding for a random message of num_bytes length.
35 void TestPadding(size_t num_bytes) {
36 std::vector<uint8> data(num_bytes);
37 EXPECT_TRUE(GenRandom(&data.front(), data.size()));
38 std::vector<uint8> m = RsaPad(kRsaKeySize, &data.front(), data.size());
39 EXPECT_EQ(m.size(), kRsaKeySize);
40
41 uint8 hash[SHA_DIGEST_SIZE] = {0};
42 SHA(&m.front(), m.size() - SHA_DIGEST_SIZE, hash);
43 EXPECT_EQ(memcmp(hash, &m[kRsaKeySize - SHA_DIGEST_SIZE], sizeof(hash)), 0);
44
45 EXPECT_FALSE(m[0] & 0x80); // msb is always reset.
46 EXPECT_TRUE(m[0] & 0x60); // bit next to msb is always set.
47 }
48
49 } // namespace
50
51
52 TEST(CupUtilsTest, RsaPad) {
53 EXPECT_GE(kRsaKeySize, SHA_DIGEST_SIZE);
54
55 TestPadding(1); // 1 byte.
56 TestPadding(SHA_DIGEST_SIZE); // 20 bytes.
57 TestPadding(kRsaKeySize); // 128 bytes.
58 TestPadding(1000); // 1000 bytes.
59 }
60
61 // The underlying B64_encode function in common/security does not pad
62 // with '='.
63 TEST(CupUtilsTest, B64Encode) {
64 const char* bytes = "1";
65 EXPECT_STREQ(B64Encode(bytes, strlen(bytes)), "MQ");
66 bytes = "12";
67 EXPECT_STREQ(B64Encode(bytes, strlen(bytes)), "MTI");
68 bytes = "123";
69 EXPECT_STREQ(B64Encode(bytes, strlen(bytes)), "MTIz");
70 bytes = "Google Inc";
71 EXPECT_STREQ(B64Encode(bytes, strlen(bytes)), "R29vZ2xlIEluYw");
72
73 const uint8* first = reinterpret_cast<const uint8*>(bytes);
74 const uint8* last = first + strlen(bytes);
75 EXPECT_STREQ(B64Encode(std::vector<uint8>(first, last)), "R29vZ2xlIEluYw");
76 }
77
78 TEST(CupUtilsTest, Hash) {
79 // Empty vector.
80 std::vector<uint8> data;
81 std::vector<uint8> hash = Hash(data);
82 EXPECT_STREQ(BytesToHex(hash),
83 _T("da39a3ee5e6b4b0d3255bfef95601890afd80709"));
84
85 // Non-empty vector.
86 const uint8* first = reinterpret_cast<const uint8*>(kPlainText);
87 const uint8* last = first + strlen(kPlainText);
88 data.clear();
89 data.insert(data.begin(), first, last);
90 hash = Hash(data);
91 EXPECT_STREQ(BytesToHex(hash),
92 _T("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"));
93
94 // Empty CString.
95 hash = Hash(CStringA());
96 EXPECT_STREQ(BytesToHex(hash),
97 _T("da39a3ee5e6b4b0d3255bfef95601890afd80709"));
98
99 // Non-empty CString.
100 hash = Hash(CStringA(kPlainText));
101 EXPECT_STREQ(BytesToHex(hash),
102 _T("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"));
103
104 // Hash of strings
105 CStringA arg(kPlainText);
106 hash = HashBuffers(arg.GetString(), arg.GetLength(), NULL, 0, NULL, 0);
107 EXPECT_STREQ(BytesToHex(hash),
108 _T("64eb91d899d68d2af394fbfe8f7c3b2884055ddb"));
109
110 hash = HashBuffers(NULL, 0, arg.GetString(), arg.GetLength(), NULL, 0);
111 EXPECT_STREQ(BytesToHex(hash),
112 _T("6c9f398d556ffe64fe441ca5491e25fd67d2ab65"));
113
114 hash = HashBuffers(NULL, 0, NULL, 0, arg.GetString(), arg.GetLength());
115 EXPECT_STREQ(BytesToHex(hash),
116 _T("9a7e591318dc169fc023414d8d25321bcb11121e"));
117
118 hash = HashBuffers(NULL, 0, NULL, 0, NULL, 0);
119 EXPECT_STREQ(BytesToHex(hash),
120 _T("d0dc1cf9bf61884f8e7982e0b1b87954bd9ee9c7"));
121 }
122
123 TEST(CupUtilsTest, SymSign) {
124 // Sign of vectors: ignore NULL vectors.
125 std::vector<uint8> key; // This is the signing key.
126 key.push_back(7);
127
128 CStringA arg(kPlainText);
129 std::vector<uint8> hash = Hash(arg);
130 std::vector<uint8> sym_sign = SymSign(key, 1, &hash, NULL, NULL);
131 EXPECT_STREQ(BytesToHex(sym_sign),
132 _T("1c6fa359c0a7b11421ae4d31a92f9de8a4ef8d2d"));
133
134 sym_sign = SymSign(key, 1, NULL, &hash, NULL);
135 EXPECT_STREQ(BytesToHex(sym_sign),
136 _T("1c6fa359c0a7b11421ae4d31a92f9de8a4ef8d2d"));
137
138 sym_sign = SymSign(key, 1, NULL, NULL, &hash);
139 EXPECT_STREQ(BytesToHex(sym_sign),
140 _T("1c6fa359c0a7b11421ae4d31a92f9de8a4ef8d2d"));
141
142 sym_sign = SymSign(key, 1, &hash, &hash, &hash);
143 EXPECT_STREQ(BytesToHex(sym_sign),
144 _T("fef3e343795946cd47ff4e07eca5f3f09b051d86"));
145 }
146
147 TEST(CupUtilsTest, ParseCupCookie) {
148 EXPECT_STREQ(_T(""), ParseCupCookie(_T("")));
149 EXPECT_STREQ(_T(""), ParseCupCookie(_T("foo; bar;")));
150 EXPECT_STREQ(_T("c="), ParseCupCookie(_T("c=")));
151 EXPECT_STREQ(_T("c="), ParseCupCookie(_T("c= ;")));
152 EXPECT_STREQ(_T("c=foo"), ParseCupCookie(_T("c=foo")));
153 EXPECT_STREQ(_T("c=foo"), ParseCupCookie(_T(";c=foo")));
154 EXPECT_STREQ(_T("c=foo"), ParseCupCookie(_T("c=foo ; bar;")));
155 EXPECT_STREQ(_T("c=foo"), ParseCupCookie(_T("b=bar; c=foo;")));
156 EXPECT_STREQ(_T("c=foo"), ParseCupCookie(_T("b=bar; c=foo ; foobar")));
157 }
158
159 } // namespace cup_utils
160
161 } // namespace omaha
OLDNEW
« no previous file with comments | « net/cup_utils.cc ('k') | net/detector.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698