summaryrefslogtreecommitdiffstats
path: root/remoting/base/rsa_key_pair_unittest.cc
blob: cb3c426fdd0bcabf379757cf6bfc822cf77a8654 (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) 2012 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 <string>

#include "remoting/base/rsa_key_pair.h"
#include "remoting/base/test_rsa_key_pair.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace remoting {

namespace {
const char kTestMessage[] = "Test Message";

// |kTestMessage| signed with the key from |kTestRsaKeyPair|.
const char kExpectedSignature[] =
"LfUyXU2AiKM4rpWivUR3bLiQiRt1W3iIenNfJEB8RWyoEfnvSBoD52x8q9yFvtLFDEMPWyIrwM+N2"
"LuaWBKG1c0R7h+twBgvpExzZneJl+lbGMRx9ba8m/KAFrUWA/NRzOen2NHCuPybOEasgrPgGWBrmf"
"gDcvyW8QiGuKLopGj/4c5CQT4yE8JjsyU3Qqo2ZPK4neJYQhOmAlg+Q5dAPLpzWMj5HQyOVHJaSXZ"
"Y8vl/LiKvbdofYLeYNVKAE4q5mfpQMrsysPYpbxBV60AhFyrvtC040MFGcflKQRZNiZwMXVb7DclC"
"BPgvK7rI5Y0ERtVm+yNmH7vCivfyAnDUYA==";

// Another RSA key pair, different from |kTestRsaKeyPair|
const char kTestRsaKeyPair2[] =
"MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDLNd9bNNBFxXSasqWHv8ydItmoi"
"NhiWV/1Z6JI6TpKTnTm9yQSxIoCLHT8fB8QA0wzzgYEuZpDH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9z0Q3YQXJRaOAJ9tblv9mEvvDNV1VQr/Lx+TejYTl2xGEjwdz2CXMxohvE5W7Lc5NSrkxae8Jm"
"XZK2k4sLx2mlQMfErBuy0VvZOzs4fN5/CnviFquPAoGBAI8NAI5ztPDW1L2kvSCGmxT2FTnFYSwUJ"
"ZiEZa/Y5AcWAUtm49fp0oW1OYuraWgTxqCVeMGlbPn2Ga3IdxhjXwdG0uV0a2V7JPEcRiiPjzUsDw"
"yunroXwIVzuU3saacVnPURkDynGDh6XC6u9UOLuUHb3ZURZ7rxcS6by/HdZ3FRAoGBAOhjtjyfFEn"
"bZtjcQd+bNtoTPV/L71+K8AYPwV0td5Qy5VbBrTIlv7pNFxE4bYNuEe6cI2cxTua4i5IoKYXyUm5u"
"SvUVkkz7CpoiFwMnnLsNrZmazVS2zq0Y2a2ai8C3mPgLOdroS2fBBAcuFApeq1PvISmT6ZnJJ8Yah"
"HQCfClh";

}  // namespace

class RsaKeyPairTest : public testing::Test {
};

TEST_F(RsaKeyPairTest, ImportExportImport) {
  // Load a key from a string, export to a string, load again, and verify that
  // we generate the same signature with both keys.
  scoped_refptr<RsaKeyPair> exported_key = RsaKeyPair::FromString(
      kTestRsaKeyPair);
  scoped_refptr<RsaKeyPair> imported_key = RsaKeyPair::FromString(
      exported_key->ToString());

  ASSERT_EQ(exported_key->SignMessage(kTestMessage),
            imported_key->SignMessage(kTestMessage));
}

TEST_F(RsaKeyPairTest, Signatures) {
  // Sign a message and check that we get the expected signature.
  scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::FromString(kTestRsaKeyPair);

  std::string signature_base64 = key_pair->SignMessage(kTestMessage);
  ASSERT_EQ(signature_base64, std::string(kExpectedSignature));
}

TEST_F(RsaKeyPairTest, GenerateKey) {
  // Test that we can generate a valid key.
  scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::Generate();
  ASSERT_TRUE(key_pair.get());
  ASSERT_NE(key_pair->ToString(), "");
  ASSERT_NE(key_pair->GetPublicKey(), "");
  ASSERT_NE(key_pair->SignMessage(kTestMessage), "");
}

TEST_F(RsaKeyPairTest, SignaturesDiffer) {
  // Sign using different keys/messages and check that signatures are different.
  scoped_refptr<RsaKeyPair> key_pair1 = RsaKeyPair::FromString(kTestRsaKeyPair);
  scoped_refptr<RsaKeyPair> key_pair2 = RsaKeyPair::FromString(
      kTestRsaKeyPair2);

  std::string signature_kp1_msg1_base64 = key_pair1->SignMessage(kTestMessage);
  std::string signature_kp2_msg1_base64 = key_pair2->SignMessage(kTestMessage);
  std::string signature_kp1_msg2_base64 = key_pair1->SignMessage("Different");
  ASSERT_NE(signature_kp1_msg1_base64, signature_kp2_msg1_base64);
  ASSERT_NE(signature_kp1_msg1_base64, signature_kp1_msg2_base64);
  ASSERT_NE(key_pair1->GetPublicKey(), key_pair2->GetPublicKey());
}

}  // namespace remoting