Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 17 | use apkverify::{testing::assert_contains, verify}; |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 18 | use std::matches; |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 19 | |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 20 | #[test] |
| 21 | fn test_verify_v3() { |
| 22 | assert!(verify("tests/data/test.apex").is_ok()); |
| 23 | } |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 24 | |
| 25 | #[test] |
Andrew Scull | c208eb4 | 2022-05-22 16:17:52 +0000 | [diff] [blame] | 26 | fn test_verify_v3_ecdsa_sha256_p256() { |
| 27 | assert!(verify("tests/data/v3-only-with-ecdsa-sha256-p256.apk").is_ok()); |
| 28 | } |
| 29 | |
| 30 | #[test] |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 31 | fn test_verify_v3_digest_mismatch() { |
| 32 | let res = verify("tests/data/v3-only-with-rsa-pkcs1-sha512-8192-digest-mismatch.apk"); |
| 33 | assert!(res.is_err()); |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 34 | assert_contains(&res.unwrap_err().to_string(), "Digest mismatch"); |
Jooyung Han | 543e712 | 2021-08-11 01:48:45 +0900 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | #[test] |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 38 | fn test_verify_v3_cert_and_public_key_mismatch() { |
Jooyung Han | 543e712 | 2021-08-11 01:48:45 +0900 | [diff] [blame] | 39 | let res = verify("tests/data/v3-only-cert-and-public-key-mismatch.apk"); |
| 40 | assert!(res.is_err()); |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 41 | assert_contains(&res.unwrap_err().to_string(), "Public key mismatch"); |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 42 | } |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 43 | |
| 44 | #[test] |
| 45 | fn test_verify_truncated_cd() { |
| 46 | use zip::result::ZipError; |
| 47 | let res = verify("tests/data/v2-only-truncated-cd.apk"); |
| 48 | // TODO(jooyung): consider making a helper for err assertion |
| 49 | assert!(matches!( |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 50 | res.unwrap_err().root_cause().downcast_ref::<ZipError>().unwrap(), |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 51 | ZipError::InvalidArchive(_), |
| 52 | )); |
| 53 | } |