Commit e8e3bc0b authored by Michael Ershov's avatar Michael Ershov Committed by Commit Bot

Add unit tests for TpmChallengeKey[Subtle,Result]

Add separate unit tests for TpmChallengeKeySubtle
and TpmChallengeKeyResult.

Bug: 1090723
Test: TpmChallenge*
Change-Id: I267e029410539070435cacbb98409bef1c40a8cc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2228725
Commit-Queue: Michael Ershov <miersh@google.com>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Cr-Commit-Position: refs/heads/master@{#797225}
parent bbf64453
...@@ -3040,6 +3040,8 @@ source_set("unit_tests") { ...@@ -3040,6 +3040,8 @@ source_set("unit_tests") {
"attestation/mock_tpm_challenge_key_subtle.cc", "attestation/mock_tpm_challenge_key_subtle.cc",
"attestation/mock_tpm_challenge_key_subtle.h", "attestation/mock_tpm_challenge_key_subtle.h",
"attestation/platform_verification_flow_unittest.cc", "attestation/platform_verification_flow_unittest.cc",
"attestation/tpm_challenge_key_result_unittest.cc",
"attestation/tpm_challenge_key_subtle_unittest.cc",
"attestation/tpm_challenge_key_unittest.cc", "attestation/tpm_challenge_key_unittest.cc",
"authpolicy/authpolicy_credentials_manager_unittest.cc", "authpolicy/authpolicy_credentials_manager_unittest.cc",
"authpolicy/authpolicy_helper.unittest.cc", "authpolicy/authpolicy_helper.unittest.cc",
......
...@@ -6,11 +6,20 @@ ...@@ -6,11 +6,20 @@
#include <ostream> #include <ostream>
#include "base/base64.h"
#include "base/check_op.h" #include "base/check_op.h"
#include "base/notreached.h" #include "base/notreached.h"
#include "base/values.h"
namespace chromeos { namespace chromeos {
namespace attestation { namespace attestation {
namespace {
std::string Base64EncodeStr(const std::string& str) {
std::string result;
base::Base64Encode(str, &result);
return result;
}
} // namespace
// These messages are exposed to the extensions that using // These messages are exposed to the extensions that using
// chrome.enterprise.platformKeys API. Someone can rely on exectly these // chrome.enterprise.platformKeys API. Someone can rely on exectly these
...@@ -139,5 +148,33 @@ bool TpmChallengeKeyResult::IsSuccess() const { ...@@ -139,5 +148,33 @@ bool TpmChallengeKeyResult::IsSuccess() const {
return result_code == TpmChallengeKeyResultCode::kSuccess; return result_code == TpmChallengeKeyResultCode::kSuccess;
} }
bool TpmChallengeKeyResult::operator==(
const TpmChallengeKeyResult& other) const {
return ((result_code == other.result_code) &&
(public_key == other.public_key) &&
(challenge_response == other.challenge_response));
}
bool TpmChallengeKeyResult::operator!=(
const TpmChallengeKeyResult& other) const {
return !(*this == other);
}
std::ostream& operator<<(std::ostream& os,
const TpmChallengeKeyResult& result) {
base::Value value(base::Value::Type::DICTIONARY);
value.SetIntKey("result_code", static_cast<int>(result.result_code));
if (!result.IsSuccess()) {
value.SetStringKey("error_message", result.GetErrorMessage());
}
value.SetStringKey("public_key", Base64EncodeStr(result.public_key));
value.SetStringKey("challenge_response",
Base64EncodeStr(result.challenge_response));
os << value;
return os;
}
} // namespace attestation } // namespace attestation
} // namespace chromeos } // namespace chromeos
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_CHROMEOS_ATTESTATION_TPM_CHALLENGE_KEY_RESULT_H_ #ifndef CHROME_BROWSER_CHROMEOS_ATTESTATION_TPM_CHALLENGE_KEY_RESULT_H_
#define CHROME_BROWSER_CHROMEOS_ATTESTATION_TPM_CHALLENGE_KEY_RESULT_H_ #define CHROME_BROWSER_CHROMEOS_ATTESTATION_TPM_CHALLENGE_KEY_RESULT_H_
#include <ostream>
#include <string> #include <string>
namespace chromeos { namespace chromeos {
...@@ -71,11 +72,17 @@ struct TpmChallengeKeyResult { ...@@ -71,11 +72,17 @@ struct TpmChallengeKeyResult {
const char* GetErrorMessage() const; const char* GetErrorMessage() const;
bool IsSuccess() const; bool IsSuccess() const;
bool operator==(const TpmChallengeKeyResult& other) const;
bool operator!=(const TpmChallengeKeyResult& other) const;
TpmChallengeKeyResultCode result_code = TpmChallengeKeyResultCode::kSuccess; TpmChallengeKeyResultCode result_code = TpmChallengeKeyResultCode::kSuccess;
std::string public_key; std::string public_key;
std::string challenge_response; std::string challenge_response;
}; };
// For unit tests and debugging.
std::ostream& operator<<(std::ostream& os, const TpmChallengeKeyResult& result);
} // namespace attestation } // namespace attestation
} // namespace chromeos } // namespace chromeos
......
// Copyright 2020 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 "chrome/browser/chromeos/attestation/tpm_challenge_key_result.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace attestation {
namespace {
std::string GetChallengeResponse() {
constexpr uint8_t kBuffer[] = {0x0, 0x1, 0x2, 'r', 'e',
's', 'p', 0xfd, 0xfe, 0xff};
return std::string(reinterpret_cast<const char*>(kBuffer), sizeof(kBuffer));
}
std::string GetPublicKey() {
constexpr uint8_t kBuffer[] = {0x0, 0x1, 0x2, 'p', 'u',
'b', 'k', 0xfd, 0xfe, 0xff};
return std::string(reinterpret_cast<const char*>(kBuffer), sizeof(kBuffer));
}
std::string GetPublicKey2() {
constexpr uint8_t kBuffer[] = {0x0, 0x1, 0x2, 'p', 'u', 'b',
'k', '2', 0xfd, 0xfe, 0xff};
return std::string(reinterpret_cast<const char*>(kBuffer), sizeof(kBuffer));
}
TEST(TpmChallengeKeyResultTest, MakeChallengeResponse) {
TpmChallengeKeyResult result =
TpmChallengeKeyResult::MakeChallengeResponse(GetChallengeResponse());
EXPECT_TRUE(result.IsSuccess());
EXPECT_EQ(result.result_code, TpmChallengeKeyResultCode::kSuccess);
EXPECT_EQ(result.challenge_response, GetChallengeResponse());
EXPECT_EQ(result.public_key, "");
}
TEST(TpmChallengeKeyResultTest, MakePublicKey) {
TpmChallengeKeyResult result =
TpmChallengeKeyResult::MakePublicKey(GetPublicKey());
EXPECT_TRUE(result.IsSuccess());
EXPECT_EQ(result.result_code, TpmChallengeKeyResultCode::kSuccess);
EXPECT_EQ(result.challenge_response, "");
EXPECT_EQ(result.public_key, GetPublicKey());
}
TEST(TpmChallengeKeyResultTest, MakeSuccess) {
TpmChallengeKeyResult result = TpmChallengeKeyResult::MakeSuccess();
EXPECT_TRUE(result.IsSuccess());
EXPECT_EQ(result.result_code, TpmChallengeKeyResultCode::kSuccess);
EXPECT_EQ(result.challenge_response, "");
EXPECT_EQ(result.public_key, "");
}
TEST(TpmChallengeKeyResultTest, MakeError) {
TpmChallengeKeyResult result = TpmChallengeKeyResult::MakeError(
TpmChallengeKeyResultCode::kGetPublicKeyFailedError);
EXPECT_FALSE(result.IsSuccess());
EXPECT_EQ(result.result_code,
TpmChallengeKeyResultCode::kGetPublicKeyFailedError);
EXPECT_EQ(result.challenge_response, "");
EXPECT_EQ(result.public_key, "");
EXPECT_EQ(result.GetErrorMessage(),
TpmChallengeKeyResult::kGetPublicKeyFailedErrorMsg);
}
TEST(TpmChallengeKeyResultTest, OperatorEqual) {
TpmChallengeKeyResult result1 =
TpmChallengeKeyResult::MakeError(TpmChallengeKeyResultCode::kDbusError);
TpmChallengeKeyResult result2 =
TpmChallengeKeyResult::MakePublicKey(GetPublicKey());
TpmChallengeKeyResult result3 =
TpmChallengeKeyResult::MakePublicKey(GetPublicKey2());
EXPECT_TRUE(result1 == result1);
EXPECT_EQ(result1, TpmChallengeKeyResult::MakeError(
TpmChallengeKeyResultCode::kDbusError));
EXPECT_TRUE(result1 != result2);
EXPECT_TRUE(result2 != result3);
}
} // namespace
} // namespace attestation
} // namespace chromeos
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment