|
| 1 | +// Copyright 2025 The NativeLink Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use nativelink_config::stores::ClientTlsConfig; |
| 16 | +use nativelink_error::Error; |
| 17 | +use nativelink_macro::nativelink_test; |
| 18 | +use nativelink_util::tls_utils::load_client_config; |
| 19 | +use tempfile::NamedTempFile; |
| 20 | + |
| 21 | +#[nativelink_test] |
| 22 | +async fn test_load_client_config_none() -> Result<(), Error> { |
| 23 | + let config = load_client_config(&None)?; |
| 24 | + assert!(config.is_none()); |
| 25 | + Ok(()) |
| 26 | +} |
| 27 | + |
| 28 | +#[nativelink_test] |
| 29 | +async fn test_load_client_config_native_roots() -> Result<(), Error> { |
| 30 | + let config = load_client_config(&Some(ClientTlsConfig { |
| 31 | + use_native_roots: Some(true), |
| 32 | + ca_file: None, |
| 33 | + cert_file: None, |
| 34 | + key_file: None, |
| 35 | + }))?; |
| 36 | + assert!(config.is_some()); |
| 37 | + Ok(()) |
| 38 | +} |
| 39 | + |
| 40 | +#[nativelink_test] |
| 41 | +async fn test_load_client_config_missing_ca() -> Result<(), Error> { |
| 42 | + let result = load_client_config(&Some(ClientTlsConfig { |
| 43 | + use_native_roots: None, |
| 44 | + ca_file: None, |
| 45 | + cert_file: None, |
| 46 | + key_file: None, |
| 47 | + })); |
| 48 | + assert!(matches!( |
| 49 | + result, |
| 50 | + Err(e) if e.to_string().contains("CA certificate must be provided") |
| 51 | + )); |
| 52 | + Ok(()) |
| 53 | +} |
| 54 | + |
| 55 | +#[nativelink_test] |
| 56 | +async fn test_load_client_config_cert_without_key() -> Result<(), Error> { |
| 57 | + let temp_file = NamedTempFile::new()?; |
| 58 | + let result = load_client_config(&Some(ClientTlsConfig { |
| 59 | + use_native_roots: None, |
| 60 | + ca_file: Some(temp_file.path().to_str().unwrap().to_string()), |
| 61 | + cert_file: Some("tls.crt".to_string()), |
| 62 | + key_file: None, |
| 63 | + })); |
| 64 | + assert!(matches!( |
| 65 | + result, |
| 66 | + Err(e) if e.to_string().contains("Client certificate specified, but no key") |
| 67 | + )); |
| 68 | + Ok(()) |
| 69 | +} |
| 70 | + |
| 71 | +#[nativelink_test] |
| 72 | +async fn test_load_client_config_key_without_cert() -> Result<(), Error> { |
| 73 | + let temp_file = NamedTempFile::new()?; |
| 74 | + let result = load_client_config(&Some(ClientTlsConfig { |
| 75 | + use_native_roots: None, |
| 76 | + ca_file: Some(temp_file.path().to_str().unwrap().to_string()), |
| 77 | + cert_file: None, |
| 78 | + key_file: Some("tls.key".to_string()), |
| 79 | + })); |
| 80 | + assert!(matches!( |
| 81 | + result, |
| 82 | + Err(e) if e.to_string().contains("Client key specified, but no certificate") |
| 83 | + )); |
| 84 | + Ok(()) |
| 85 | +} |
| 86 | + |
| 87 | +#[nativelink_test] |
| 88 | +async fn test_load_client_config_with_cert_files() -> Result<(), Error> { |
| 89 | + let temp_file = NamedTempFile::new()?; |
| 90 | + let config = load_client_config(&Some(ClientTlsConfig { |
| 91 | + use_native_roots: None, |
| 92 | + ca_file: Some(temp_file.path().to_str().unwrap().to_string()), |
| 93 | + cert_file: Some(temp_file.path().to_str().unwrap().to_string()), |
| 94 | + key_file: Some(temp_file.path().to_str().unwrap().to_string()), |
| 95 | + }))?; |
| 96 | + assert!(config.is_some()); |
| 97 | + Ok(()) |
| 98 | +} |
0 commit comments