1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/quic/crypto/crypto_secret_boxer.h"
#include "base/memory/scoped_ptr.h"
#include "net/quic/crypto/quic_random.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::StringPiece;
using std::string;
namespace net {
namespace test {
TEST(CryptoSecretBoxerTest, BoxAndUnbox) {
StringPiece message("hello world");
const size_t key_size = CryptoSecretBoxer::GetKeySize();
scoped_ptr<uint8[]> key(new uint8[key_size]);
memset(key.get(), 0x11, key_size);
CryptoSecretBoxer boxer;
boxer.SetKey(StringPiece(reinterpret_cast<char*>(key.get()), key_size));
const string box = boxer.Box(QuicRandom::GetInstance(), message);
string storage;
StringPiece result;
EXPECT_TRUE(boxer.Unbox(box, &storage, &result));
EXPECT_EQ(result, message);
EXPECT_FALSE(boxer.Unbox(string(1, 'X') + box, &storage, &result));
EXPECT_FALSE(boxer.Unbox(box.substr(1, string::npos), &storage, &result));
EXPECT_FALSE(boxer.Unbox(string(), &storage, &result));
EXPECT_FALSE(boxer.Unbox(string(1, 'X') + box.substr(1, string::npos),
&storage, &result));
}
} // namespace test
} // namespace net
|