eroman | 751d179 | 2014-08-26 06:57:20 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 5 | #include "components/webcrypto/status.h" |
eroman | 751d179 | 2014-08-26 06:57:20 | [diff] [blame] | 6 | #include "base/stl_util.h" |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 7 | #include "components/webcrypto/algorithm_dispatch.h" |
eroman | 7bcd6a7 | 2015-09-16 02:41:21 | [diff] [blame] | 8 | #include "components/webcrypto/algorithms/test_helpers.h" |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 9 | #include "components/webcrypto/crypto_data.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 10 | #include "third_party/blink/public/platform/web_crypto_algorithm.h" |
| 11 | #include "third_party/blink/public/platform/web_crypto_algorithm_params.h" |
| 12 | #include "third_party/blink/public/platform/web_crypto_key_algorithm.h" |
eroman | 751d179 | 2014-08-26 06:57:20 | [diff] [blame] | 13 | |
eroman | 751d179 | 2014-08-26 06:57:20 | [diff] [blame] | 14 | namespace webcrypto { |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | // Tests several Status objects against their expected hard coded values, as |
| 19 | // well as ensuring that comparison of Status objects works. |
| 20 | // Comparison should take into account both the error details, as well as the |
| 21 | // error type. |
| 22 | TEST(WebCryptoStatusTest, Basic) { |
| 23 | // Even though the error message is the same, these should not be considered |
| 24 | // the same by the tests because the error type is different. |
| 25 | EXPECT_NE(Status::DataError(), Status::OperationError()); |
| 26 | EXPECT_NE(Status::Success(), Status::OperationError()); |
| 27 | |
| 28 | EXPECT_EQ(Status::Success(), Status::Success()); |
eroman | 398e7e1 | 2014-11-08 03:05:10 | [diff] [blame] | 29 | EXPECT_EQ(Status::ErrorJwkMemberWrongType("kty", "string"), |
| 30 | Status::ErrorJwkMemberWrongType("kty", "string")); |
eroman | 751d179 | 2014-08-26 06:57:20 | [diff] [blame] | 31 | |
| 32 | Status status = Status::Success(); |
| 33 | |
| 34 | EXPECT_FALSE(status.IsError()); |
| 35 | EXPECT_EQ("", status.error_details()); |
| 36 | |
| 37 | status = Status::OperationError(); |
| 38 | EXPECT_TRUE(status.IsError()); |
| 39 | EXPECT_EQ("", status.error_details()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 40 | EXPECT_EQ(blink::kWebCryptoErrorTypeOperation, status.error_type()); |
eroman | 751d179 | 2014-08-26 06:57:20 | [diff] [blame] | 41 | |
| 42 | status = Status::DataError(); |
| 43 | EXPECT_TRUE(status.IsError()); |
| 44 | EXPECT_EQ("", status.error_details()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 45 | EXPECT_EQ(blink::kWebCryptoErrorTypeData, status.error_type()); |
eroman | 751d179 | 2014-08-26 06:57:20 | [diff] [blame] | 46 | |
| 47 | status = Status::ErrorUnsupported(); |
| 48 | EXPECT_TRUE(status.IsError()); |
| 49 | EXPECT_EQ("The requested operation is unsupported", status.error_details()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 50 | EXPECT_EQ(blink::kWebCryptoErrorTypeNotSupported, status.error_type()); |
eroman | 751d179 | 2014-08-26 06:57:20 | [diff] [blame] | 51 | |
eroman | 398e7e1 | 2014-11-08 03:05:10 | [diff] [blame] | 52 | status = Status::ErrorJwkMemberMissing("kty"); |
eroman | 751d179 | 2014-08-26 06:57:20 | [diff] [blame] | 53 | EXPECT_TRUE(status.IsError()); |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 54 | EXPECT_EQ("The required JWK member \"kty\" was missing", |
eroman | 751d179 | 2014-08-26 06:57:20 | [diff] [blame] | 55 | status.error_details()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 56 | EXPECT_EQ(blink::kWebCryptoErrorTypeData, status.error_type()); |
eroman | 751d179 | 2014-08-26 06:57:20 | [diff] [blame] | 57 | |
eroman | 398e7e1 | 2014-11-08 03:05:10 | [diff] [blame] | 58 | status = Status::ErrorJwkMemberWrongType("kty", "string"); |
eroman | 751d179 | 2014-08-26 06:57:20 | [diff] [blame] | 59 | EXPECT_TRUE(status.IsError()); |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 60 | EXPECT_EQ("The JWK member \"kty\" must be a string", status.error_details()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 61 | EXPECT_EQ(blink::kWebCryptoErrorTypeData, status.error_type()); |
eroman | 751d179 | 2014-08-26 06:57:20 | [diff] [blame] | 62 | |
| 63 | status = Status::ErrorJwkBase64Decode("n"); |
| 64 | EXPECT_TRUE(status.IsError()); |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 65 | EXPECT_EQ( |
| 66 | "The JWK member \"n\" could not be base64url decoded or contained " |
| 67 | "padding", |
| 68 | status.error_details()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 69 | EXPECT_EQ(blink::kWebCryptoErrorTypeData, status.error_type()); |
eroman | 751d179 | 2014-08-26 06:57:20 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | } // namespace |
| 73 | |
| 74 | } // namespace webcrypto |