Enforce .scar file naming restrictions
Enforces .scar file or smart contract naming restrictions, which
means the smart contract must not contain a '_'.
An error is returned if these restrictions are not met.
@@ -74,6 +74,8 @@ impl SmartContractArchive {
/// The scar file to load will be named `<name>_<version>.scar`, where `<name>` is the name
/// specified for this method and `<version>` is a valid semver string.
///
+ /// The `name` argument for this method must not contain any underscores, '_'.
+ ///
/// The `version` argument for this method must be a valid semver requirement string. The
/// latest contract (based on semver requirement rules) that matches the given name and meets
/// the version requirements will be loaded.
@@ -213,6 +215,9 @@ pub struct SmartContractMetadata {
fn find_scar<P: AsRef<Path>>(name: &str, version: &str, paths: &[P]) -> Result<PathBuf, Error> {
let file_name_pattern = format!("{}_*.scar", name);
+
+ validate_contract_name(name)?;
+
let version_req = VersionReq::parse(version)?;
// Start with all scar files that match the name, from all paths
@@ -256,6 +261,16 @@ fn find_scar<P: AsRef<Path>>(name: &str, version: &str, paths: &[P]) -> Result<P
})
}
+fn validate_contract_name(name: &str) -> Result<(), Error> {
+ if name.contains('_') {
+ return Err(Error::new(&format!(
+ "invalid scar file name, must not include '_': {}",
+ name
+ )));
+ }
+ Ok(())
+}
+
fn path_is_manifest(path: &std::path::Path) -> bool {
path.file_name()
.map(|file_name| file_name == "manifest.yaml")
@@ -316,6 +331,24 @@ mod tests {
);
}
+ /// Verify that an error is returned when the contract name contains '_'.
+ #[test]
+ fn find_scar_invalid_name() {
+ let dir = new_temp_dir();
+ write_mock_scar(&dir, "non_existent", "0.1.0");
+
+ assert!(find_scar("non_existent", "0.1.0", &[&dir]).is_err());
+ }
+
+ /// Verify that an error is returned when the contract version is an invalid semver string.
+ #[test]
+ fn find_scar_invalid_version() {
+ let dir = new_temp_dir();
+ write_mock_scar(&dir, "nonexistent", "0.1..0");
+
+ assert!(find_scar("nonexistent", "0.1..0", &[&dir]).is_err());
+ }
+
/// Verify that an error is returned when no contract with the given name exists.
#[test]
fn find_scar_no_matching_name() {
