|
| 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::{endpoint_from, 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 | +} |
| 99 | + |
| 100 | +#[nativelink_test] |
| 101 | +async fn test_endpoint_from_http() -> Result<(), Error> { |
| 102 | + let endpoint = endpoint_from("http://localhost:50051", None)?; |
| 103 | + assert_eq!(endpoint.uri().scheme_str(), Some("http")); |
| 104 | + assert_eq!(endpoint.uri().host(), Some("localhost")); |
| 105 | + assert_eq!(endpoint.uri().port_u16(), Some(50051)); |
| 106 | + Ok(()) |
| 107 | +} |
| 108 | + |
| 109 | +#[nativelink_test] |
| 110 | +async fn test_endpoint_from_https_with_tls() -> Result<(), Error> { |
| 111 | + let tls_config = load_client_config(&Some(ClientTlsConfig { |
| 112 | + use_native_roots: Some(true), |
| 113 | + ca_file: None, |
| 114 | + cert_file: None, |
| 115 | + key_file: None, |
| 116 | + }))?; |
| 117 | + let endpoint = endpoint_from("https://example.com", tls_config)?; |
| 118 | + assert_eq!(endpoint.uri().scheme_str(), Some("https")); |
| 119 | + assert_eq!(endpoint.uri().host(), Some("example.com")); |
| 120 | + Ok(()) |
| 121 | +} |
| 122 | + |
| 123 | +#[nativelink_test] |
| 124 | +async fn test_endpoint_from_grpcs_with_tls() -> Result<(), Error> { |
| 125 | + let tls_config = load_client_config(&Some(ClientTlsConfig { |
| 126 | + use_native_roots: Some(true), |
| 127 | + ca_file: None, |
| 128 | + cert_file: None, |
| 129 | + key_file: None, |
| 130 | + }))?; |
| 131 | + let endpoint = endpoint_from("grpcs://example.com", tls_config)?; |
| 132 | + assert_eq!(endpoint.uri().scheme_str(), Some("https")); |
| 133 | + assert_eq!(endpoint.uri().host(), Some("example.com")); |
| 134 | + Ok(()) |
| 135 | +} |
| 136 | + |
| 137 | +#[nativelink_test] |
| 138 | +async fn test_endpoint_from_https_without_tls() -> Result<(), Error> { |
| 139 | + let result = endpoint_from("https://example.com", None); |
| 140 | + assert!(matches!( |
| 141 | + result, |
| 142 | + Err(e) if e.to_string().contains("is https or grpcs, but no TLS configuration was provided") |
| 143 | + )); |
| 144 | + Ok(()) |
| 145 | +} |
| 146 | + |
| 147 | +#[nativelink_test] |
| 148 | +async fn test_endpoint_from_http_with_tls() -> Result<(), Error> { |
| 149 | + let tls_config = load_client_config(&Some(ClientTlsConfig { |
| 150 | + use_native_roots: Some(true), |
| 151 | + ca_file: None, |
| 152 | + cert_file: None, |
| 153 | + key_file: None, |
| 154 | + }))?; |
| 155 | + let result = endpoint_from("http://example.com:8080", tls_config); |
| 156 | + assert!(matches!( |
| 157 | + result, |
| 158 | + Err(e) if e.to_string().contains("but the scheme is not https or grpcs") |
| 159 | + )); |
| 160 | + Ok(()) |
| 161 | +} |
| 162 | + |
| 163 | +#[nativelink_test] |
| 164 | +async fn test_endpoint_from_invalid_uri() -> Result<(), Error> { |
| 165 | + let result = endpoint_from("not a valid uri", None); |
| 166 | + assert!(matches!( |
| 167 | + result, |
| 168 | + Err(e) if e.to_string().contains("Unable to parse endpoint") |
| 169 | + )); |
| 170 | + Ok(()) |
| 171 | +} |
| 172 | + |
| 173 | +#[nativelink_test] |
| 174 | +async fn test_endpoint_from_missing_authority() -> Result<(), Error> { |
| 175 | + let tls_config = load_client_config(&Some(ClientTlsConfig { |
| 176 | + use_native_roots: Some(true), |
| 177 | + ca_file: None, |
| 178 | + cert_file: None, |
| 179 | + key_file: None, |
| 180 | + }))?; |
| 181 | + let result = endpoint_from("/path/no/authority", tls_config); |
| 182 | + assert!(matches!( |
| 183 | + result, |
| 184 | + Err(e) if e.to_string().contains("Unable to determine authority of endpoint") |
| 185 | + )); |
| 186 | + Ok(()) |
| 187 | +} |
0 commit comments