From 295280577394079de67bc2fcb95d9dff5a4fbfaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konrad=20Go=C5=82awski?= Date: Thu, 21 Dec 2023 16:11:36 +0100 Subject: [PATCH] Code refactor --- .cargo/README.md | 67 ++-- .github/workflows/rust.yml | 86 +---- CHANGELOG.md | 13 +- Cargo.toml | 47 ++- LICENSE | 2 +- README.md | 66 ++-- deny.toml | 108 ------ examples/md5.rs | 5 +- examples/sha1.rs | 5 +- examples/sha2_224.rs | 5 +- examples/sha2_256.rs | 5 +- examples/sha2_384.rs | 5 +- examples/sha2_512.rs | 5 +- src/directory.rs | 62 ---- src/lib.rs | 730 +++++++++++++++---------------------- src/path.rs | 49 --- src/read.rs | 41 --- tests/args.rs | 38 -- tests/common.rs | 17 - tests/md5.rs | 581 ----------------------------- tests/sha1.rs | 581 ----------------------------- tests/sha2_224.rs | 581 ----------------------------- tests/sha2_256.rs | 713 ------------------------------------ tests/sha2_384.rs | 713 ------------------------------------ tests/sha2_512.rs | 581 ----------------------------- 25 files changed, 384 insertions(+), 4722 deletions(-) delete mode 100644 deny.toml delete mode 100644 src/directory.rs delete mode 100644 src/path.rs delete mode 100644 src/read.rs delete mode 100644 tests/args.rs delete mode 100644 tests/common.rs delete mode 100644 tests/md5.rs delete mode 100644 tests/sha1.rs delete mode 100644 tests/sha2_224.rs delete mode 100644 tests/sha2_256.rs delete mode 100644 tests/sha2_384.rs delete mode 100644 tests/sha2_512.rs diff --git a/.cargo/README.md b/.cargo/README.md index d48e8e3..7d4cbc6 100644 --- a/.cargo/README.md +++ b/.cargo/README.md @@ -1,30 +1,22 @@ # chksum -[![GitHub](https://img.shields.io/badge/github-ferric--bytes%2Fchksum-24292e?style=flat-square&logo=github "GitHub")](https://github.com/ferric-bytes/chksum) -[![docs.rs](https://img.shields.io/docsrs/chksum?style=flat-square&logo=docsdotrs "docs.rs")](https://docs.rs/chksum) -[![Coverage](https://img.shields.io/codecov/c/gh/ferric-bytes/chksum?style=flat-square&logo=codecov "Coverage")](https://app.codecov.io/gh/ferric-bytes/chksum) -[![MSRV](https://img.shields.io/badge/MSRV-1.65.0-informational?style=flat-square "MSRV")](https://github.com/ferric-bytes/chksum/blob/master/Cargo.toml) -[![deps.rs](https://deps.rs/crate/chksum/0.2.2/status.svg?style=flat-square "deps.rs")](https://deps.rs/crate/chksum/0.2.2) +[![GitHub](https://img.shields.io/badge/github-chksum--rs%2Flib-24292e?style=flat-square&logo=github "GitHub")](https://github.com/chksum-rs/lib) +[![Build](https://img.shields.io/github/actions/workflow/status/chksum-rs/lib/rust.yml?branch=master&style=flat-square&logo=github "Build")](https://github.com/chksum-rs/lib/actions/workflows/rust.yml) +[![docs.rs](https://img.shields.io/docsrs/chksum?style=flat-square&logo=docsdotrs "docs.rs")](https://docs.rs/chksum/) +[![MSRV](https://img.shields.io/badge/MSRV-1.70.0-informational?style=flat-square "MSRV")](https://github.com/chksum-rs/lib/blob/master/Cargo.toml) +[![deps.rs](https://deps.rs/crate/chksum/0.3.0/status.svg?style=flat-square "deps.rs")](https://deps.rs/crate/chksum/0.3.0) [![unsafe forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg?style=flat-square "unsafe forbidden")](https://github.com/rust-secure-code/safety-dance) -[![LICENSE](https://img.shields.io/github/license/ferric-bytes/chksum?style=flat-square "LICENSE")](https://github.com/ferric-bytes/chksum/blob/master/LICENSE) +[![LICENSE](https://img.shields.io/github/license/chksum-rs/lib?style=flat-square "LICENSE")](https://github.com/chksum-rs/lib/blob/master/LICENSE) -High-level interface for easy calculation of checksum digest for files, directories, stdin and more. - -## Features - -- Written in pure Rust -- Easy to use interface -- No unsafe code -- Configurable via Cargo features +An implementation of various hash functions with a straightforward interface for computing digests of bytes, files, directories, and more. ## Setup -Add the following entry to the `dependencies` section of your `Cargo.toml` file: +To use this crate, add the following entry to your `Cargo.toml` file in the `dependencies` section: ```toml [dependencies] -# ... -chksum = "0.2.2" +chksum = "0.3.0" ``` Alternatively, you can use the [`cargo add`](https://doc.rust-lang.org/cargo/commands/cargo-add.html) subcommand: @@ -35,44 +27,33 @@ cargo add chksum ## Usage -Use `chksum` function and `File` as an input. +Use the `chksum` function to calcualate digest of file, directory and so on. ```rust -use std::fs::File; - -use chksum::chksum; -use chksum::hash::SHA2_224; +use chksum::sha2_256; let file = File::open(path)?; -let digest = chksum::(file)?; +let digest = sha2_256::chksum(file)?; assert_eq!( digest.to_hex_lowercase(), - "a39b86d838273f5ff4879c26f85e3cb333bb44d73b24f275bad1a6c6" + "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061" ); ``` -Alternatively use `ReadDir` as an input. - -```rust -use std::fs::read_dir; - -use chksum::chksum; -use chksum::hash::SHA2_256; - -let dir = read_dir(path)?; -let digest = chksum::(dir)?; -assert_eq!( - digest.to_hex_lowercase(), - "5c3bfbc8614adc72d3ec0e9b15a1fd1c55cee63e34af5a4ff058eb2eef7d8482" -); -``` +For more usage examples, refer to the documentation available at [docs.rs](https://docs.rs/chksum/). -For more usage examples, refer to the documentation available at [docs.rs](https://docs.rs/chksum). +## Hash Algorithms -## Low-level interface +This crate provides implementations for the following hash algorithms: -Check [`chksum-hash`](https://crates.io/crates/chksum-hash) for low-level interface. +* MD5 +* SHA-1 +* SHA-2 + * SHA-2 224 + * SHA-2 256 + * SHA-2 384 + * SHA-2 512 ## License -MIT +This crate is licensed under the MIT License. diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 6e94b80..d217de2 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -10,21 +10,19 @@ on: push: branches: - master - - dev paths: - ".github/workflows/*.yml" - "Cargo.toml" + - "examples/**.rs" - "src/**.rs" - - "tests/**.rs" pull_request: branches: - master - - dev paths: - ".github/workflows/*.yml" - "Cargo.toml" + - "examples/**.rs" - "src/**.rs" - - "tests/**.rs" jobs: lint: @@ -55,55 +53,14 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} args: --all-features -- --deny clippy::cargo - deny: - runs-on: ubuntu-latest - name: Deny - steps: - - name: Repository checkout - uses: actions/checkout@v3 - - name: Setup Rust - uses: actions-rs/toolchain@v1 - with: - toolchain: nightly - default: true - profile: minimal - - name: Install cargo deny - uses: actions-rs/install@v0.1 - with: - crate: cargo-deny - - name: Run cargo deny - uses: actions-rs/cargo@v1 - with: - command: deny - args: --all-features check - - security-audit: - name: Security Audit - runs-on: ubuntu-latest - steps: - - name: Repository checkout - uses: actions/checkout@v3 - - name: Setup Rust - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - default: true - profile: minimal - - name: Run audit check - uses: actions-rs/audit-check@v1 - with: - token: ${{ secrets.GITHUB_TOKEN }} - build-and-test-linux: needs: - - deny - lint - - security-audit runs-on: ubuntu-latest strategy: fail-fast: false matrix: - toolchain: [1.65.0, stable, nightly] + toolchain: [1.70.0, stable, nightly] name: "Build and test (OS: Linux, Toolchain: ${{ matrix.toolchain }})" steps: - name: Repository checkout @@ -127,14 +84,12 @@ jobs: build-and-test-macos: needs: - - deny - lint - - security-audit runs-on: macos-latest strategy: fail-fast: false matrix: - toolchain: [1.65.0, stable, nightly] + toolchain: [1.70.0, stable, nightly] name: "Build and test (OS: MacOS, Toolchain: ${{ matrix.toolchain }})" steps: - name: Repository checkout @@ -158,14 +113,12 @@ jobs: build-and-test-windows: needs: - - deny - lint - - security-audit runs-on: windows-latest strategy: fail-fast: false matrix: - toolchain: [1.65.0, stable, nightly] + toolchain: [1.70.0, stable, nightly] name: "Build and test (OS: Windows, Toolchain: ${{ matrix.toolchain }})" steps: - name: Repository checkout @@ -186,32 +139,3 @@ jobs: with: command: test args: --all-features --verbose - - coverage: - needs: - - deny - - lint - - security-audit - runs-on: ubuntu-latest - name: Coverage - steps: - - name: Repository checkout - uses: actions/checkout@v3 - - name: Setup Rust - uses: actions-rs/toolchain@v1 - with: - toolchain: nightly - default: true - profile: minimal - - name: Install cargo tarpaulin - uses: actions-rs/install@v0.1 - with: - crate: cargo-tarpaulin - - name: Run cargo tarpaulin - run: cargo tarpaulin --all-features --engine llvm --fail-under 50 --ignore-tests --out xml --timeout 120 - - name: Upload to codecov.io - if: ${{ always() }} - uses: codecov/codecov-action@v3 - with: - token: ${{ secrets.CODECOV_TOKEN }} - fail_ci_if_error: true diff --git a/CHANGELOG.md b/CHANGELOG.md index ff12c4c..681f271 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.3.0] - 2023-12-21 + +### Changed + +- Code refactor. + ## [0.2.2] - 2023-08-21 ### Added @@ -40,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Initial release. -[0.2.2]: https://github.com/ferric-bytes/chksum/compare/v0.2.1...v0.2.2 -[0.2.1]: https://github.com/ferric-bytes/chksum/compare/v0.2.0...v0.2.1 -[0.2.0]: https://github.com/ferric-bytes/chksum/releases/tag/v0.2.0 +[0.3.0]: https://github.com/chksum-rs/lib/releases/tag/v0.3.0 +[0.2.2]: https://github.com/chksum-rs/lib/compare/v0.2.1...v0.2.2 +[0.2.1]: https://github.com/chksum-rs/lib/compare/v0.2.0...v0.2.1 +[0.2.0]: https://github.com/chksum-rs/lib/releases/tag/v0.2.0 diff --git a/Cargo.toml b/Cargo.toml index ae76708..a8d316a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,14 +1,14 @@ [package] name = "chksum" -version = "0.2.2" +version = "0.3.0" authors = ["Konrad Goławski "] edition = "2021" -rust-version = "1.65.0" -description = "High-level interface for easy calculation of checksum digest for files, directories, stdin and more." +rust-version = "1.70.0" +description = "An implementation of hash functions with a straightforward interface for computing digests of bytes, files, directories, and more." readme = ".cargo/README.md" -repository = "https://github.com/ferric-bytes/chksum" +repository = "https://github.com/chksum-rs/lib" license = "MIT" -keywords = ["checksum", "directory-checksum", "file-checksum", "hash"] +keywords = ["checksum", "hash", "directory-checksum", "file-checksum", "stdin-checksum"] categories = ["algorithms", "cryptography", "filesystem"] [package.metadata.docs.rs] @@ -16,30 +16,29 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [dependencies] -chksum-hash = { version = "0.4.3", default-features = false, features = ["error"] } -is-terminal = "0.4.9" -thiserror = "1.0.44" - -[dev-dependencies] -assert_fs = "1.0.13" -thiserror = "1.0.44" +chksum-core = "0.0.0" +chksum-hash = { version = "0.5.0", default-features = false } +chksum-md5 = { version = "0.0.0", optional = true } +chksum-sha1 = { version = "0.0.0", optional = true } +chksum-sha2 = { version = "0.0.0", default-features = false, optional = true } +chksum-reader = { version = "0.0.0", optional = true } +chksum-writer = { version = "0.0.0", optional = true } [features] default = [ # algorithms - "md5", - "sha1", - "sha2", + "md5", "sha1", "sha2", ] -# compilation -unstable = ["chksum-hash/unstable"] - # algorithms -md5 = ["chksum-hash/md5"] -sha1 = ["chksum-hash/sha1"] +md5 = ["chksum-md5", "chksum-hash/md5"] +sha1 = ["chksum-sha1", "chksum-hash/sha1"] sha2 = ["sha2-224", "sha2-256", "sha2-384", "sha2-512", "chksum-hash/sha2"] -sha2-224 = ["chksum-hash/sha2-224"] -sha2-256 = ["chksum-hash/sha2-256"] -sha2-384 = ["chksum-hash/sha2-384"] -sha2-512 = ["chksum-hash/sha2-512"] +sha2-224 = ["chksum-sha2/224", "chksum-hash/sha2-224"] +sha2-256 = ["chksum-sha2/256", "chksum-hash/sha2-256"] +sha2-384 = ["chksum-sha2/384", "chksum-hash/sha2-384"] +sha2-512 = ["chksum-sha2/512", "chksum-hash/sha2-512"] + +# extra options +reader = ["chksum-reader", "chksum-md5?/reader", "chksum-sha1?/reader", "chksum-sha2?/reader"] +writer = ["chksum-writer", "chksum-md5?/writer", "chksum-sha1?/writer", "chksum-sha2?/writer"] diff --git a/LICENSE b/LICENSE index 10c2349..a3790ad 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Konrad Goławski +Copyright (c) 2020-2023 Konrad Goławski Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 6ab817d..04f1bde 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,22 @@ # chksum -[![Build](https://img.shields.io/github/actions/workflow/status/ferric-bytes/chksum/rust.yml?branch=master&style=flat-square&logo=github "Build")](https://github.com/ferric-bytes/chksum/actions/workflows/rust.yml) [![crates.io](https://img.shields.io/crates/v/chksum?style=flat-square&logo=rust "crates.io")](https://crates.io/crates/chksum) -[![docs.rs](https://img.shields.io/docsrs/chksum?style=flat-square&logo=docsdotrs "docs.rs")](https://docs.rs/chksum) -[![Coverage](https://img.shields.io/codecov/c/gh/ferric-bytes/chksum?style=flat-square&logo=codecov "Coverage")](https://app.codecov.io/gh/ferric-bytes/chksum) -[![MSRV](https://img.shields.io/badge/MSRV-1.65.0-informational?style=flat-square "MSRV")](https://github.com/ferric-bytes/chksum/blob/master/Cargo.toml) -[![deps.rs](https://deps.rs/crate/chksum/0.2.2/status.svg?style=flat-square "deps.rs")](https://deps.rs/crate/chksum/0.2.2) +[![Build](https://img.shields.io/github/actions/workflow/status/chksum-rs/lib/rust.yml?branch=master&style=flat-square&logo=github "Build")](https://github.com/chksum-rs/lib/actions/workflows/rust.yml) +[![docs.rs](https://img.shields.io/docsrs/chksum?style=flat-square&logo=docsdotrs "docs.rs")](https://docs.rs/chksum/) +[![MSRV](https://img.shields.io/badge/MSRV-1.70.0-informational?style=flat-square "MSRV")](https://github.com/chksum-rs/lib/blob/master/Cargo.toml) +[![deps.rs](https://deps.rs/crate/chksum/0.3.0/status.svg?style=flat-square "deps.rs")](https://deps.rs/crate/chksum/0.3.0) [![unsafe forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg?style=flat-square "unsafe forbidden")](https://github.com/rust-secure-code/safety-dance) -[![LICENSE](https://img.shields.io/github/license/ferric-bytes/chksum?style=flat-square "LICENSE")](https://github.com/ferric-bytes/chksum/blob/master/LICENSE) +[![LICENSE](https://img.shields.io/github/license/chksum-rs/lib?style=flat-square "LICENSE")](https://github.com/chksum-rs/lib/blob/master/LICENSE) -High-level interface for easy calculation of checksum digest for files, directories, stdin and more. - -## Features - -- Written in pure Rust -- Easy to use interface -- No unsafe code -- Configurable via Cargo features +An implementation of various hash functions with a straightforward interface for computing digests of bytes, files, directories, and more. ## Setup -Add the following entry to the `dependencies` section of your `Cargo.toml` file: +To use this crate, add the following entry to your `Cargo.toml` file in the `dependencies` section: ```toml [dependencies] -# ... -chksum = "0.2.2" +chksum = "0.3.0" ``` Alternatively, you can use the [`cargo add`](https://doc.rust-lang.org/cargo/commands/cargo-add.html) subcommand: @@ -36,44 +27,33 @@ cargo add chksum ## Usage -Use `chksum` function and `File` as an input. +Use the `chksum` function to calcualate digest of file, directory and so on. ```rust -use std::fs::File; - -use chksum::chksum; -use chksum::hash::SHA2_224; +use chksum::sha2_256; let file = File::open(path)?; -let digest = chksum::(file)?; +let digest = sha2_256::chksum(file)?; assert_eq!( digest.to_hex_lowercase(), - "a39b86d838273f5ff4879c26f85e3cb333bb44d73b24f275bad1a6c6" + "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061" ); ``` -Alternatively use `ReadDir` as an input. - -```rust -use std::fs::read_dir; - -use chksum::chksum; -use chksum::hash::SHA2_256; - -let dir = read_dir(path)?; -let digest = chksum::(dir)?; -assert_eq!( - digest.to_hex_lowercase(), - "5c3bfbc8614adc72d3ec0e9b15a1fd1c55cee63e34af5a4ff058eb2eef7d8482" -); -``` +For more usage examples, refer to the documentation available at [docs.rs](https://docs.rs/chksum/). -For more usage examples, refer to the documentation available at [docs.rs](https://docs.rs/chksum). +## Hash Algorithms -## Low-level interface +This crate provides implementations for the following hash algorithms: -Check [`chksum-hash`](https://github.com/ferric-bytes/chksum-hash) for low-level interface. +* MD5 +* SHA-1 +* SHA-2 + * SHA-2 224 + * SHA-2 256 + * SHA-2 384 + * SHA-2 512 ## License -MIT +This crate is licensed under the MIT License. diff --git a/deny.toml b/deny.toml deleted file mode 100644 index e2bad86..0000000 --- a/deny.toml +++ /dev/null @@ -1,108 +0,0 @@ -# If 1 or more target triples (and optionally, target_features) are specified, -# only the specified targets will be checked when running `cargo deny check`. -# This means, if a particular package is only ever used as a target specific -# dependency, such as, for example, the `nix` crate only being used via the -# `target_family = "unix"` configuration, that only having windows targets in -# this list would mean the nix crate, as well as any of its exclusive -# dependencies not shared by any other crates, would be ignored, as the target -# list here is effectively saying which targets you are building for. -targets = [ - # The triple can be any string, but only the target triples built in to - # rustc (as of 1.40) can be checked against actual config expressions - #{ triple = "x86_64-unknown-linux-musl" }, - # You can also specify which target_features you promise are enabled for a - # particular target. target_features are currently not validated against - # the actual valid features supported by the target architecture. - #{ triple = "wasm32-unknown-unknown", features = ["atomics"] }, -] - -# This section is considered when running `cargo deny check advisories` -# More documentation for the advisories section can be found here: -# https://embarkstudios.github.io/cargo-deny/checks/advisories/cfg.html -[advisories] -# The path where the advisory database is cloned/fetched into -db-path = "~/.cargo/advisory-db" -# The url(s) of the advisory databases to use -db-urls = ["https://github.com/rustsec/advisory-db"] -# The lint level for security vulnerabilities -vulnerability = "deny" -# The lint level for unmaintained crates -unmaintained = "warn" -# The lint level for crates that have been yanked from their source registry -yanked = "warn" -# The lint level for crates with security notices. Note that as of -# 2019-12-17 there are no security notice advisories in -# https://github.com/rustsec/advisory-db -notice = "warn" - -# If this is true, then cargo deny will use the git executable to fetch advisory database. -# If this is false, then it uses a built-in git library. -# Setting this to true can be helpful if you have special authentication requirements that cargo-deny does not support. -# See Git Authentication for more information about setting up git authentication. -git-fetch-with-cli = true - -# This section is considered when running `cargo deny check licenses` -# More documentation for the licenses section can be found here: -# https://embarkstudios.github.io/cargo-deny/checks/licenses/cfg.html -[licenses] -# The lint level for crates which do not have a detectable license -unlicensed = "deny" -# List of explicitly allowed licenses -# See https://spdx.org/licenses/ for list of possible licenses -# [possible values: any SPDX 3.11 short identifier (+ optional exception)]. -allow = [ - "Apache-2.0", - "MIT", -] -# Lint level for licenses considered copyleft -copyleft = "warn" -# Blanket approval or denial for OSI-approved or FSF Free/Libre licenses -# * both - The license will be approved if it is both OSI-approved *AND* FSF -# * either - The license will be approved if it is either OSI-approved *OR* FSF -# * osi-only - The license will be approved if is OSI-approved *AND NOT* FSF -# * fsf-only - The license will be approved if is FSF *AND NOT* OSI-approved -# * neither - This predicate is ignored and the default lint level is used -allow-osi-fsf-free = "either" -# Lint level used when no other predicates are matched -# 1. License isn't in the allow or deny lists -# 2. License isn't copyleft -# 3. License isn't OSI/FSF, or allow-osi-fsf-free = "neither" -default = "deny" - -[licenses.private] -# If true, ignores workspace crates that aren't published, or are only -# published to private registries. -# To see how to mark a crate as unpublished (to the official registry), -# visit https://doc.rust-lang.org/cargo/reference/manifest.html#the-publish-field. -ignore = false - -# This section is considered when running `cargo deny check bans`. -# More documentation about the 'bans' section can be found here: -# https://embarkstudios.github.io/cargo-deny/checks/bans/cfg.html -[bans] -# Lint level for when multiple versions of the same crate are detected -multiple-versions = "warn" -# Lint level for when a crate version requirement is `*` -wildcards = "allow" -# The graph highlighting used when creating dotgraphs for crates -# with multiple versions -# * lowest-version - The path to the lowest versioned duplicate is highlighted -# * simplest-path - The path to the version with the fewest edges is highlighted -# * all - Both lowest-version and simplest-path are used -highlight = "all" - -# This section is considered when running `cargo deny check sources`. -# More documentation about the 'sources' section can be found here: -# https://embarkstudios.github.io/cargo-deny/checks/sources/cfg.html -[sources] -# Lint level for what to happen when a crate from a crate registry that is not -# in the allow list is encountered -unknown-registry = "deny" -# Lint level for what to happen when a crate from a git repository that is not -# in the allow list is encountered -unknown-git = "deny" -# List of URLs for allowed crate registries. Defaults to the crates.io index -# if not specified. If it is specified but empty, no registries are allowed. -allow-registry = ["https://github.com/rust-lang/crates.io-index"] -# List of URLs for allowed Git repositories -allow-git = [] diff --git a/examples/md5.rs b/examples/md5.rs index c2dfda7..22ec160 100644 --- a/examples/md5.rs +++ b/examples/md5.rs @@ -1,8 +1,7 @@ use std::env; use std::path::PathBuf; -use chksum::hash::MD5; -use chksum::{chksum, Result}; +use chksum::{chksum, Result, MD5}; fn main() -> Result<()> { // Skip the first argument because it is not necessary to calculate digest of the binary itself @@ -11,7 +10,7 @@ fn main() -> Result<()> { let path = PathBuf::from(arg); // Calculate digest - let digest = chksum::(&path)?; + let digest = chksum::(&path)?; // Print digest let path = path.display(); diff --git a/examples/sha1.rs b/examples/sha1.rs index e0aa973..98f733c 100644 --- a/examples/sha1.rs +++ b/examples/sha1.rs @@ -1,8 +1,7 @@ use std::env; use std::path::PathBuf; -use chksum::hash::SHA1; -use chksum::{chksum, Result}; +use chksum::{chksum, Result, SHA1}; fn main() -> Result<()> { // Skip the first argument because it is not necessary to calculate digest of the binary itself @@ -11,7 +10,7 @@ fn main() -> Result<()> { let path = PathBuf::from(arg); // Calculate digest - let digest = chksum::(&path)?; + let digest = chksum::(&path)?; // Print digest let path = path.display(); diff --git a/examples/sha2_224.rs b/examples/sha2_224.rs index 9fb15d2..7b77fce 100644 --- a/examples/sha2_224.rs +++ b/examples/sha2_224.rs @@ -1,8 +1,7 @@ use std::env; use std::path::PathBuf; -use chksum::hash::SHA2_224; -use chksum::{chksum, Result}; +use chksum::{chksum, Result, SHA2_224}; fn main() -> Result<()> { // Skip the first argument because it is not necessary to calculate digest of the binary itself @@ -11,7 +10,7 @@ fn main() -> Result<()> { let path = PathBuf::from(arg); // Calculate digest - let digest = chksum::(&path)?; + let digest = chksum::(&path)?; // Print digest let path = path.display(); diff --git a/examples/sha2_256.rs b/examples/sha2_256.rs index cb38c46..887388b 100644 --- a/examples/sha2_256.rs +++ b/examples/sha2_256.rs @@ -1,8 +1,7 @@ use std::env; use std::path::PathBuf; -use chksum::hash::SHA2_256; -use chksum::{chksum, Result}; +use chksum::{chksum, Result, SHA2_256}; fn main() -> Result<()> { // Skip the first argument because it is not necessary to calculate digest of the binary itself @@ -11,7 +10,7 @@ fn main() -> Result<()> { let path = PathBuf::from(arg); // Calculate digest - let digest = chksum::(&path)?; + let digest = chksum::(&path)?; // Print digest let path = path.display(); diff --git a/examples/sha2_384.rs b/examples/sha2_384.rs index 9ad243e..5757d09 100644 --- a/examples/sha2_384.rs +++ b/examples/sha2_384.rs @@ -1,8 +1,7 @@ use std::env; use std::path::PathBuf; -use chksum::hash::SHA2_384; -use chksum::{chksum, Result}; +use chksum::{chksum, Result, SHA2_384}; fn main() -> Result<()> { // Skip the first argument because it is not necessary to calculate digest of the binary itself @@ -11,7 +10,7 @@ fn main() -> Result<()> { let path = PathBuf::from(arg); // Calculate digest - let digest = chksum::(&path)?; + let digest = chksum::(&path)?; // Print digest let path = path.display(); diff --git a/examples/sha2_512.rs b/examples/sha2_512.rs index adf1f41..1699903 100644 --- a/examples/sha2_512.rs +++ b/examples/sha2_512.rs @@ -1,8 +1,7 @@ use std::env; use std::path::PathBuf; -use chksum::hash::SHA2_512; -use chksum::{chksum, Result}; +use chksum::{chksum, Result, SHA2_512}; fn main() -> Result<()> { // Skip the first argument because it is not necessary to calculate digest of the binary itself @@ -11,7 +10,7 @@ fn main() -> Result<()> { let path = PathBuf::from(arg); // Calculate digest - let digest = chksum::(&path)?; + let digest = chksum::(&path)?; // Print digest let path = path.display(); diff --git a/src/directory.rs b/src/directory.rs deleted file mode 100644 index 521314e..0000000 --- a/src/directory.rs +++ /dev/null @@ -1,62 +0,0 @@ -use std::fs::{DirEntry, ReadDir}; -use std::io; - -use crate::error::Error; -use crate::hash::Update; -use crate::path::PathChksumer; -use crate::{Args, Chksumer}; - -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub(crate) struct DirEntryChksumer<'a, T> -where - T: Update, -{ - pub(crate) hash: T, - pub(crate) args: &'a Args, -} - -impl<'a, T> Chksumer for DirEntryChksumer<'a, T> -where - T: Update, -{ - type Error = Error; - - #[inline] - fn update(mut self, data: DirEntry) -> Result { - let path = data.path(); - let Self { hash, args } = self; - let PathChksumer { hash, args } = PathChksumer { hash, args }.update(path)?; - self = Self { hash, args }; - Ok(self) - } -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub(crate) struct ReadDirChksumer<'a, T> -where - T: Update, -{ - pub(crate) hash: T, - pub(crate) args: &'a Args, -} - -impl<'a, T> Chksumer for ReadDirChksumer<'a, T> -where - T: Update, -{ - type Error = Error; - - #[inline] - fn update(mut self, data: ReadDir) -> Result { - let Self { hash, args } = self; - let DirEntryChksumer { hash, args } = { - let mut entries = data.collect::>>()?; - entries.sort_by_key(DirEntry::path); - entries - .into_iter() - .try_fold(DirEntryChksumer { hash, args }, Chksumer::update)? - }; - self = Self { hash, args }; - Ok(self) - } -} diff --git a/src/lib.rs b/src/lib.rs index 56eef6d..8d2b391 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,608 +1,450 @@ -//! High-level interface for easy calculation of checksum digest for files, directories, stdin and more. +//! This crate provides an implementation of various hash functions with a straightforward interface for computing digests of bytes, files, directories, and more. +//! +//! For a low-level interface, you can explore the [`chksum_hash`] crate. //! //! # Setup //! -//! Update your `Cargo.toml` by adding entry to `dependencies` section. +//! To use this crate, add the following entry to your `Cargo.toml` file in the `dependencies` section: //! //! ```toml //! [dependencies] -//! # ... -//! chksum = "0.2.2" +//! chksum = "0.3.0" //! ``` //! -//! Alternatively use [`cargo add`](https://doc.rust-lang.org/cargo/commands/cargo-add.html) subcommand. +//! Alternatively, you can use the [`cargo add`](https://doc.rust-lang.org/cargo/commands/cargo-add.html) subcommand: //! //! ```sh //! cargo add chksum -//! ``` +//! ``` //! //! # Usage //! -//! ## File -//! -//! Use [`File`](std::fs::File) or [`&File`](std::fs::File) as an input. +//! Use the [`chksum`] function to calcualate digest of file, directory and so on. //! //! ```rust -//! # use std::fs::File; //! # use std::path::Path; +//! use std::fs::File; +//! //! # use chksum::Result; -//! use chksum::chksum; -//! use chksum::hash::MD5; +//! use chksum::sha2_256; //! //! # fn wrapper(path: &Path) -> Result<()> { //! let file = File::open(path)?; -//! let digest = chksum::(file)?; +//! let digest = sha2_256::chksum(file)?; //! assert_eq!( //! digest.to_hex_lowercase(), -//! "b35e02f32d924c3da7ca8613ea91deb0" +//! "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061" //! ); //! # Ok(()) //! # } //! ``` //! -//! ## Directory +//! # Input Types +//! +//! ## Bytes //! -//! Use [`ReadDir`](std::fs::ReadDir) as an input. +//! ### Array //! //! ```rust -//! # use std::fs::read_dir; -//! # use std::path::Path; //! # use chksum::Result; -//! use chksum::chksum; -//! use chksum::hash::MD5; +//! use chksum::sha2_256; //! -//! # fn wrapper(path: &Path) -> Result<()> { -//! let dir = read_dir(path)?; -//! let digest = chksum::(dir)?; +//! # fn wrapper() -> Result<()> { +//! let data = [0, 1, 2, 3]; +//! let digest = sha2_256::chksum(data)?; //! assert_eq!( //! digest.to_hex_lowercase(), -//! "30672e8a0cb95ef3b0a29601f2874ba5" +//! "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061" //! ); //! # Ok(()) //! # } //! ``` //! -//! ## Paths +//! ### Vec +//! +//! ```rust +//! # use chksum::Result; +//! use chksum::sha2_256; +//! +//! # fn wrapper() -> Result<()> { +//! let data = vec![0, 1, 2, 3]; +//! let digest = sha2_256::chksum(data)?; +//! assert_eq!( +//! digest.to_hex_lowercase(), +//! "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061" +//! ); +//! # Ok(()) +//! # } +//! ``` //! -//! Use [`&Path`](std::path::Path), [`PathBuf`](std::path::PathBuf) or [`&PathBuf`](std::path::PathBuf) as an input. +//! ### Slice //! //! ```rust -//! # use std::path::{Path, PathBuf}; //! # use chksum::Result; -//! use chksum::chksum; -//! use chksum::hash::MD5; +//! use chksum::sha2_256; //! -//! # fn wrapper(path: &Path) -> Result<()> { -//! let digest = chksum::(path)?; +//! # fn wrapper() -> Result<()> { +//! let data = &[0, 1, 2, 3]; +//! let digest = sha2_256::chksum(data)?; //! assert_eq!( //! digest.to_hex_lowercase(), -//! "0bccc59d6997a74e1813d058aa1ad80d" +//! "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061" //! ); //! # Ok(()) //! # } //! ``` //! -//! ## Stdin +//! ## Strings //! -//! Use [`StdinLock`](std::io::StdinLock) as an input. +//! ### str //! //! ```rust -//! # use std::io::stdin; //! # use chksum::Result; -//! use chksum::chksum; -//! use chksum::hash::MD5; +//! use chksum::sha2_256; //! //! # fn wrapper() -> Result<()> { -//! let handle = stdin().lock(); -//! let digest = chksum::(handle)?; +//! let data = "&str"; +//! let digest = sha2_256::chksum(data)?; //! assert_eq!( //! digest.to_hex_lowercase(), -//! "e91509789cba933399182e6a8864bdd8" +//! "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061" //! ); //! # Ok(()) //! # } //! ``` //! -//! # Algorithms +//! ### String //! -//! ## MD5 +//! ```rust +//! # use chksum::Result; +//! use chksum::sha2_256; +//! +//! # fn wrapper() -> Result<()> { +//! let data = String::from("String"); +//! let digest = sha2_256::chksum(data)?; +//! assert_eq!( +//! digest.to_hex_lowercase(), +//! "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061" +//! ); +//! # Ok(()) +//! # } +//! ``` //! -//! Use [`MD5`](hash::MD5) struct to calculate MD5 digest. +//! ## File //! //! ```rust -//! # use std::fs::File; //! # use std::path::Path; +//! use std::fs::File; +//! //! # use chksum::Result; -//! use chksum::chksum; -//! use chksum::hash::MD5; +//! use chksum::sha2_256; //! //! # fn wrapper(path: &Path) -> Result<()> { -//! let digest = chksum::(path)?; +//! let file = File::open(path)?; +//! let digest = sha2_256::chksum(file)?; //! assert_eq!( //! digest.to_hex_lowercase(), -//! "3081d73d94e101bfa7bf39a3ef7351e9" +//! "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061" //! ); //! # Ok(()) //! # } //! ``` //! -//! ## SHA-1 +//! ## Directory //! -//! Use [`SHA1`](hash::SHA1) struct to calculate SHA-1 digest. +//! ```rust +//! # use std::path::Path; +//! use std::fs::read_dir; +//! +//! # use chksum::Result; +//! use chksum::sha2_256; +//! +//! # fn wrapper(path: &Path) -> Result<()> { +//! let readdir = read_dir(path)?; +//! let digest = sha2_256::chksum(readdir)?; +//! assert_eq!( +//! digest.to_hex_lowercase(), +//! "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061" +//! ); +//! # Ok(()) +//! # } +//! ``` +//! +//! ## Path //! //! ```rust -//! # use std::fs::File; //! # use std::path::Path; +//! use std::path::PathBuf; +//! //! # use chksum::Result; -//! use chksum::chksum; -//! use chksum::hash::SHA1; +//! use chksum::sha2_256; //! //! # fn wrapper(path: &Path) -> Result<()> { -//! let digest = chksum::(path)?; +//! let path = PathBuf::from(path); +//! let digest = sha2_256::chksum(path)?; //! assert_eq!( //! digest.to_hex_lowercase(), -//! "d2b8d92228efb73147151566f059d1cace37046e" +//! "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061" //! ); //! # Ok(()) //! # } //! ``` //! -//! ## SHA-2 +//! ## Standard Input +//! +//! ```rust +//! use std::io::stdin; +//! +//! # use chksum::Result; +//! use chksum::sha2_256; +//! +//! # fn wrapper() -> Result<()> { +//! let stdin = stdin(); +//! let digest = sha2_256::chksum(stdin)?; +//! assert_eq!( +//! digest.to_hex_lowercase(), +//! "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061" +//! ); +//! # Ok(()) +//! # } +//! ``` //! -//! ### SHA-2 224 +//! # Algorithms //! -//! Use [`SHA2_224`](hash::SHA2_224) struct to calculate SHA-2 224 digest. +//! ## MD5 //! //! ```rust -//! # use std::fs::File; //! # use std::path::Path; +//! use std::fs::File; +//! //! # use chksum::Result; -//! use chksum::chksum; -//! use chksum::hash::SHA2_224; +//! use chksum::md5; //! //! # fn wrapper(path: &Path) -> Result<()> { -//! let digest = chksum::(path)?; +//! let file = File::open(path)?; +//! let digest = md5::chksum(file)?; //! assert_eq!( //! digest.to_hex_lowercase(), -//! "cfa726b42c9ef788e99eb81e4dce181763feac230485dde49dba717a" +//! "5c71dbb287630d65ca93764c34d9aa0d" //! ); //! # Ok(()) //! # } //! ``` //! -//! ### SHA-2 256 -//! -//! Use [`SHA2_256`](hash::SHA2_256) struct to calculate SHA-2 256 digest. +//! ## SHA-1 //! //! ```rust -//! # use std::fs::File; //! # use std::path::Path; +//! use std::fs::File; +//! //! # use chksum::Result; -//! use chksum::chksum; -//! use chksum::hash::SHA2_256; +//! use chksum::sha1; //! //! # fn wrapper(path: &Path) -> Result<()> { -//! let digest = chksum::(path)?; +//! let file = File::open(path)?; +//! let digest = sha1::chksum(file)?; //! assert_eq!( //! digest.to_hex_lowercase(), -//! "10a0933e11c86746636370a913ba9be34bc2ac2871585cb0e573c354e7116772" +//! "9fc42adac31303d68b444e6129f13f6093a0e045" //! ); //! # Ok(()) //! # } //! ``` //! -//! ### SHA-2 384 +//! ## SHA-2 224 +//! +//! ```rust +//! # use std::path::Path; +//! use std::fs::File; +//! +//! # use chksum::Result; +//! use chksum::sha2_224; +//! +//! # fn wrapper(path: &Path) -> Result<()> { +//! let file = File::open(path)?; +//! let digest = sha2_224::chksum(file)?; +//! assert_eq!( +//! digest.to_hex_lowercase(), +//! "90382cbfda2656313ad61fd74b32ddfa4bcc118f660bd4fba9228ced" +//! ); +//! # Ok(()) +//! # } +//! ``` //! -//! Use [`SHA2_384`](hash::SHA2_384) struct to calculate SHA-2 384 digest. +//! ## SHA-2 256 //! //! ```rust -//! # use std::fs::File; //! # use std::path::Path; +//! use std::fs::File; +//! //! # use chksum::Result; -//! use chksum::chksum; -//! use chksum::hash::SHA2_384; +//! use chksum::sha2_256; //! //! # fn wrapper(path: &Path) -> Result<()> { -//! let digest = chksum::(path)?; +//! let file = File::open(path)?; +//! let digest = sha2_256::chksum(file)?; //! assert_eq!( //! digest.to_hex_lowercase(), -//! "7d44140596271726d0e57b3f97615d615d771d48b9b62a0b9da053e68e11992fa9166795671999d036a1e2c17a60414e" +//! "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061" //! ); //! # Ok(()) //! # } //! ``` //! -//! ### SHA-2 512 +//! ## SHA-2 384 +//! +//! ```rust +//! # use std::path::Path; +//! use std::fs::File; +//! +//! # use chksum::Result; +//! use chksum::sha2_384; +//! +//! # fn wrapper(path: &Path) -> Result<()> { +//! let file = File::open(path)?; +//! let digest = sha2_384::chksum(file)?; +//! assert_eq!( +//! digest.to_hex_lowercase(), +//! "12ecdfd463a85a301b7c29a43bf4b19cdfc6e5e86a5f40396aa6ae3368a7e5b0ed31f3bef2eb3071577ba610b4ed1cb8" +//! ); +//! # Ok(()) +//! # } +//! ``` //! -//! Use [`SHA2_512`](hash::SHA2_512) struct to calculate SHA-2 512 digest. +//! ## SHA-2 512 //! //! ```rust -//! # use std::fs::File; //! # use std::path::Path; +//! use std::fs::File; +//! //! # use chksum::Result; -//! use chksum::chksum; -//! use chksum::hash::SHA2_512; +//! use chksum::sha2_512; //! //! # fn wrapper(path: &Path) -> Result<()> { -//! let digest = chksum::(path)?; +//! let file = File::open(path)?; +//! let digest = sha2_512::chksum(file)?; //! assert_eq!( //! digest.to_hex_lowercase(), -//! "6bf5a2ab9e922a5723a937acd2b8afa685d20c1f9f16d435fce8c7fd41c3425bbed990bb0a16124f2b62147cd4342496769b789e7186a0e2d85dafc1e5dc626b" +//! "ed59c5759a9ece516cec0c0623142d0e9fe70a27d750eee7fd38f4550d50addd873d0fa1a51fc823c1e3d5cada203f4a05d8325caacb7d3e0727a701f3f07e5f" //! ); //! # Ok(()) //! # } //! ``` //! -//! # Feature flags +//! # Features //! //! ## Algorithms //! -//! * `md5`: Enables MD5 hash algorithm. -//! * `sha1`: Enables SHA-1 hash algorithm. -//! * `sha2`: Enables SHA-2 hash family algorithms. -//! * `sha2-224`: Enables only SHA-2 224 hash algorithm. -//! * `sha2-256`: Enables only SHA-2 256 hash algorithm. -//! * `sha2-384`: Enables only SHA-2 384 hash algorithm. -//! * `sha2-512`: Enables only SHA-2 512 hash algorithm. +//! Cargo features are utilized to enable or disable specific hash algorithms. +//! +//! * `md5` enables MD5, accessible via the [`md5`] module. +//! * `sha1` enables SHA-1, accessible via the [`sha1`] module. +//! * `sha2-224` enables SHA-2 224, accessible via the [`sha2_224`] module. +//! * `sha2-256` enables SHA-2 256, accessible via the [`sha2_256`] module. +//! * `sha2-384` enables SHA-2 384, accessible via the [`sha2_384`] module. +//! * `sha2-512` enables SHA-2 512, accessible via the [`sha2_512`] module. +//! +//! By default, all of these features are enabled. +//! +//! To customize your setup, disable the default features and enable only those that you need in your `Cargo.toml` file: +//! +//! ```toml +//! [dependencies] +//! chksum = { version = "0.3.0", default-features = false, features = ["sha1", "sha2-256", "sha2-512"] } +//! ``` +//! +//! Alternatively, you can use the [`cargo add`](https://doc.rust-lang.org/cargo/commands/cargo-add.html) subcommand: +//! +//! ```shell +//! cargo add chksum --no-default-features --features sha1,sha2-256,sha2-512 +//! ``` +//! +//! ## Extra Options +//! +//! Cargo features are also utilized to enable extra options. +//! +//! * `reader` enables the `reader` module with the `Reader` struct within each variant module. +//! * `writer` enables the `writer` module with the `Writer` struct within each variant module. +//! +//! By default, neither of these features is enabled. //! -//! By default all of them are enabled. +//! To customize your setup, disable the default features and enable only those that you need in your `Cargo.toml` file: //! -//! ## Compilation +//! ```toml +//! [dependencies] +//! chksum = { version = "0.3.0", features = ["reader", "writer"] } +//! ``` //! -//! * `unstable`: Enables unstable options (like build script). +//! Alternatively, you can use the [`cargo add`](https://doc.rust-lang.org/cargo/commands/cargo-add.html) subcommand: //! -//! By default none of them are enabled. +//! ```shell +//! cargo add chksum --features reader,writer +//! ``` //! //! # License //! -//! MIT +//! This crate is licensed under the MIT License. #![cfg_attr(docsrs, feature(doc_auto_cfg))] #![forbid(unsafe_code)] -pub(crate) mod directory; -pub(crate) mod error; -pub(crate) mod path; -pub(crate) mod read; - -use std::fs::{DirEntry, File, ReadDir}; -use std::io::{Stdin, StdinLock}; -use std::path::{Path, PathBuf}; -use std::result; - -pub use chksum_hash as hash; -use is_terminal::IsTerminal; - -use crate::directory::{DirEntryChksumer, ReadDirChksumer}; -pub use crate::error::{Error, Result}; -use crate::hash::Update; -use crate::path::PathChksumer; -use crate::read::ReadChksumer; - -/// A trait for objects which are able to calculate checksum of given input. -pub trait Chksum: Update { - /// The type of the returned error. - type Error; - - /// Calculates checksum of given input. - /// - /// Check [`chksum`] function for more details. - #[inline] - fn chksum(data: T) -> result::Result { - let args = Args::default(); - Self::chksum_with(data, &args) - } - - #[doc(hidden)] // TODO: create documentation - fn chksum_with(data: T, args: &Args) -> result::Result; -} - -impl Chksum for T -where - T: Default + Update, -{ - type Error = Error; - - #[inline] - fn chksum_with(data: DirEntry, args: &Args) -> result::Result { - let hash = Self::default(); - let DirEntryChksumer { hash, .. } = DirEntryChksumer { hash, args }.update(data)?; - let digest = hash.digest(); - Ok(digest) - } -} - -impl Chksum for T -where - T: Default + Update, -{ - type Error = Error; - - #[inline] - fn chksum_with(data: File, args: &Args) -> result::Result { - Self::chksum_with(&data, args) - } -} - -impl Chksum<&File> for T -where - T: Default + Update, -{ - type Error = Error; - - #[inline] - fn chksum_with(data: &File, args: &Args) -> result::Result { - if data.is_terminal() { - return Err(Error::IsTerminal); - } - let hash = Self::default(); - let ReadChksumer { hash, .. } = ReadChksumer { hash, args }.update(data)?; - let digest = hash.digest(); - Ok(digest) - } -} - -impl Chksum<&Path> for T -where - T: Default + Update, -{ - type Error = Error; - - #[inline] - fn chksum_with(data: &Path, args: &Args) -> result::Result { - let hash = Self::default(); - let PathChksumer { hash, .. } = PathChksumer { hash, args }.update(data)?; - let digest = hash.digest(); - Ok(digest) - } -} - -impl Chksum for T -where - T: Default + Update, -{ - type Error = Error; - - #[inline] - fn chksum_with(data: PathBuf, args: &Args) -> result::Result { - Self::chksum_with(&data, args) - } -} - -impl Chksum<&PathBuf> for T -where - T: Default + Update, -{ - type Error = Error; - - #[inline] - fn chksum_with(data: &PathBuf, args: &Args) -> result::Result { - Self::chksum_with(data.as_path(), args) - } -} - -impl Chksum for T -where - T: Default + Update, -{ - type Error = Error; - - #[inline] - fn chksum_with(data: ReadDir, args: &Args) -> result::Result { - let hash = Self::default(); - let ReadDirChksumer { hash, .. } = ReadDirChksumer { hash, args }.update(data)?; - let digest = hash.digest(); - Ok(digest) - } -} - -impl Chksum for T -where - T: Default + Update, -{ - type Error = Error; - - #[inline] - fn chksum_with(data: Stdin, args: &Args) -> result::Result { - Self::chksum_with(data.lock(), args) - } -} - -impl<'a, T> Chksum> for T -where - T: Default + Update, -{ - type Error = Error; - - #[inline] - fn chksum_with(data: StdinLock<'a>, args: &Args) -> result::Result { - if data.is_terminal() { - let error = Error::IsTerminal; - return Err(error); - } - let hash = Self::default(); - let ReadChksumer { hash, .. } = ReadChksumer { hash, args }.update(data)?; - let digest = hash.digest(); - Ok(digest) - } -} - -#[doc(hidden)] // TODO: create documentation -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub struct ArgsBuilder { - pub chunk_size: Option, -} - -impl ArgsBuilder { - #[inline] - #[must_use] - pub const fn new() -> Self { - let chunk_size = None; - Self { chunk_size } - } - - #[inline] - #[must_use] - pub const fn chunk_size(mut self, chunk_size: usize) -> Self { - self.chunk_size = Some(chunk_size); - self - } - - #[inline] - #[must_use] - pub const fn build(self) -> Args { - let Self { chunk_size } = self; - Args { chunk_size } - } -} - -impl Default for ArgsBuilder { - #[inline] - fn default() -> Self { - Self::new() - } -} - -#[doc(hidden)] // TODO: create documentation -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub struct Args { - pub chunk_size: Option, -} - -impl Args { - #[must_use] - pub const fn new() -> Self { - let chunk_size = None; - Self { chunk_size } - } -} - -impl Default for Args { - #[inline] - fn default() -> Self { - Self::new() - } -} - -/// Internal trait to implement different checksumers. -pub(crate) trait Chksumer -where - Self: Sized, -{ - /// Error type returned when something wrong happened. - type Error; - - /// Update checksumer with incoming data. - fn update(self, data: T) -> result::Result; -} - -/// Calculates checksum of given input. -/// -/// # Examples -/// -/// Choose preferred hash algorithm. -/// -/// ```rust -/// # use std::fs::File; -/// # use std::path::Path; -/// # use chksum::Result; -/// use chksum::chksum; -/// use chksum::hash::{MD5, SHA1, SHA2_224}; -/// -/// # fn wrapper_md5(path: &Path) -> Result<()> { -/// let file = File::open(path)?; -/// let digest = chksum::(file)?; -/// assert_eq!( -/// digest.to_hex_lowercase(), -/// "91de52cc35ec212a8f406ce89ca28a95" -/// ); -/// # Ok(()) -/// # } -/// -/// // or -/// -/// # fn wrapper_sha1(path: &Path) -> Result<()> { -/// let file = File::open(path)?; -/// let digest = chksum::(file)?; -/// assert_eq!( -/// digest.to_hex_lowercase(), -/// "399445db3210104118c24061d73900e3d6f3af53" -/// ); -/// # Ok(()) -/// # } -/// -/// // or -/// -/// # fn wrapper_sha2_224(path: &Path) -> Result<()> { -/// let file = File::open(path)?; -/// let digest = chksum::(file)?; -/// assert_eq!( -/// digest.to_hex_lowercase(), -/// "15b4f8536af87424871a8e89d1193a3bd759b2306ea0bc5007f2105b" -/// ); -/// # Ok(()) -/// # } -/// ``` -/// -/// You can use different types as an input. -/// -/// ```rust -/// # use std::fs::{read_dir, File}; -/// # use std::path::{Path, PathBuf}; -/// # use chksum::Result; -/// use chksum::chksum; -/// use chksum::hash::{SHA2_224, SHA2_256, SHA2_384}; -/// -/// # fn wrapper_file(path: &Path) -> Result<()> { -/// // use File as an input -/// let file = File::open(path)?; -/// let digest = chksum::(file)?; -/// assert_eq!( -/// digest.to_hex_lowercase(), -/// "a657747d15c24a9998186a5798284f1ee2621c4591f766709b4e616d" -/// ); -/// # Ok(()) -/// # } -/// -/// // or -/// -/// # fn wrapper_read_dir(path: &Path) -> Result<()> { -/// // use ReadDir as an input -/// let dir = read_dir(path)?; -/// let digest = chksum::(dir)?; -/// assert_eq!( -/// digest.to_hex_lowercase(), -/// "6c671fec2a733e1275c8e8cb4eb025b13d433cde3d31f6d61e7a216a6d853cb0" -/// ); -/// # Ok(()) -/// # } -/// -/// // or -/// -/// # fn wrapper_path(path: &Path) -> Result<()> { -/// // use PathBuf as an input -/// let path = PathBuf::from(path); -/// let digest = chksum::(path)?; -/// assert_eq!( -/// digest.to_hex_lowercase(), -/// "701e7356774cb7d22d89a0843250c555bf5318f2546f2b64e6a62d22b2ce9a8acda618109eb95683605b97c37fc9ae0e" -/// ); -/// # Ok(()) -/// # } -/// ``` -#[inline] -pub fn chksum(data: U) -> result::Result -where - T: Chksum, -{ - T::chksum(data) -} - -#[doc(hidden)] // TODO: create documentation -#[inline] -pub fn chksum_with(data: U, args: &Args) -> result::Result -where - T: Chksum, -{ - T::chksum_with(data, args) -} +#[doc(no_inline)] +pub use chksum_core::{chksum, hash, Chksumable, Digest, Error, Hash, Hashable, Result}; +#[cfg(docsrs)] +use chksum_hash; +#[cfg(feature = "md5")] +#[doc(no_inline)] +pub use chksum_md5 as md5; +#[cfg(feature = "md5")] +#[doc(no_inline)] +pub use chksum_md5::MD5; +#[cfg(feature = "reader")] +#[doc(no_inline)] +pub use chksum_reader as reader; +#[cfg(feature = "reader")] +#[doc(no_inline)] +pub use chksum_reader::Reader; +#[cfg(feature = "sha1")] +#[doc(no_inline)] +pub use chksum_sha1 as sha1; +#[cfg(feature = "sha1")] +#[doc(no_inline)] +pub use chksum_sha1::SHA1; +#[cfg(any( + feature = "sha2-224", + feature = "sha2-256", + feature = "sha2-384", + feature = "sha2-512", +))] +#[doc(no_inline)] +pub use chksum_sha2 as sha2; +#[cfg(feature = "sha2-224")] +#[doc(no_inline)] +pub use chksum_sha2::sha2_224; +#[cfg(feature = "sha2-256")] +#[doc(no_inline)] +pub use chksum_sha2::sha2_256; +#[cfg(feature = "sha2-384")] +#[doc(no_inline)] +pub use chksum_sha2::sha2_384; +#[cfg(feature = "sha2-512")] +#[doc(no_inline)] +pub use chksum_sha2::sha2_512; +#[cfg(feature = "sha2-224")] +#[doc(no_inline)] +pub use chksum_sha2::SHA2_224; +#[cfg(feature = "sha2-256")] +#[doc(no_inline)] +pub use chksum_sha2::SHA2_256; +#[cfg(feature = "sha2-384")] +#[doc(no_inline)] +pub use chksum_sha2::SHA2_384; +#[cfg(feature = "sha2-512")] +#[doc(no_inline)] +pub use chksum_sha2::SHA2_512; +#[cfg(feature = "writer")] +#[doc(no_inline)] +pub use chksum_writer as writer; +#[cfg(feature = "writer")] +#[doc(no_inline)] +pub use chksum_writer::Writer; diff --git a/src/path.rs b/src/path.rs deleted file mode 100644 index 5013efe..0000000 --- a/src/path.rs +++ /dev/null @@ -1,49 +0,0 @@ -use std::fs::{read_dir, File}; -use std::path::Path; - -use is_terminal::IsTerminal; - -use crate::directory::ReadDirChksumer; -use crate::error::Error; -use crate::hash::Update; -use crate::read::ReadChksumer; -use crate::{Args, Chksumer}; - -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub(crate) struct PathChksumer<'a, T> -where - T: Update, -{ - pub(crate) hash: T, - pub(crate) args: &'a Args, -} - -impl<'a, T, U> Chksumer for PathChksumer<'a, T> -where - T: Update, - U: AsRef, -{ - type Error = Error; - - #[inline] - fn update(mut self, data: U) -> Result { - let data = data.as_ref(); - let metadata = data.metadata()?; - if metadata.is_dir() { - let directory = read_dir(data)?; - let Self { hash, args } = self; - let ReadDirChksumer { hash, args } = ReadDirChksumer { hash, args }.update(directory)?; - self = Self { hash, args }; - } else { - // if not a directory then treat as a file - let file = File::open(data)?; - if file.is_terminal() { - return Err(Error::IsTerminal); - } - let Self { hash, args } = self; - let ReadChksumer { hash, args } = ReadChksumer { hash, args }.update(file)?; - self = Self { hash, args }; - } - Ok(self) - } -} diff --git a/src/read.rs b/src/read.rs deleted file mode 100644 index 3946d3e..0000000 --- a/src/read.rs +++ /dev/null @@ -1,41 +0,0 @@ -use std::io::{BufRead, BufReader, Read}; - -use crate::error::Error; -use crate::hash::Update; -use crate::{Args, Chksumer}; - -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub(crate) struct ReadChksumer<'a, T> -where - T: Update, -{ - pub(crate) hash: T, - pub(crate) args: &'a Args, -} - -impl<'a, T, U> Chksumer for ReadChksumer<'a, T> -where - T: Update, - U: Read, -{ - type Error = Error; - - fn update(mut self, data: U) -> Result { - let Self { mut hash, args } = self; - let mut data = match args.chunk_size { - Some(chunk_size) => BufReader::with_capacity(chunk_size, data), - None => BufReader::new(data), - }; - loop { - let buffer = data.fill_buf()?; - let length = buffer.len(); - if length == 0 { - break; - } - hash = hash.update(buffer); - data.consume(length); - } - self = Self { hash, args }; - Ok(self) - } -} diff --git a/tests/args.rs b/tests/args.rs deleted file mode 100644 index 5f7b595..0000000 --- a/tests/args.rs +++ /dev/null @@ -1,38 +0,0 @@ -use chksum::{Args, ArgsBuilder}; - -#[test] -fn args_new() { - let args = Args::new(); - assert_eq!(args, Args { chunk_size: None }); -} - -#[test] -fn args_default() { - let args = Args::default(); - assert_eq!(args, Args { chunk_size: None }); -} - -#[test] -fn args_builder_new() { - let args = ArgsBuilder::new().build(); - assert_eq!(args, Args { chunk_size: None }); -} - -#[test] -fn args_builder_default() { - let args = ArgsBuilder::default().build(); - assert_eq!(args, Args { chunk_size: None }); -} - -#[test] -fn args_builder_chunk_size() { - for chunk_size in [1, 2, 1024, 9999] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - assert_eq!( - args, - Args { - chunk_size: Some(chunk_size) - } - ) - } -} diff --git a/tests/common.rs b/tests/common.rs deleted file mode 100644 index f4689f3..0000000 --- a/tests/common.rs +++ /dev/null @@ -1,17 +0,0 @@ -use std::io::Error as IoError; -use std::result; - -use assert_fs::fixture::FixtureError; -use chksum::Error as ChksumError; - -#[derive(Debug, thiserror::Error)] -pub enum Error { - #[error(transparent)] - ChksumError(#[from] ChksumError), - #[error(transparent)] - FixtureError(#[from] FixtureError), - #[error(transparent)] - IoError(#[from] IoError), -} - -pub type Result = result::Result<(), Error>; diff --git a/tests/md5.rs b/tests/md5.rs deleted file mode 100644 index 978b180..0000000 --- a/tests/md5.rs +++ /dev/null @@ -1,581 +0,0 @@ -use std::fs::{read_dir, File}; - -use assert_fs::prelude::{FileTouch, FileWriteBin, PathChild}; -use assert_fs::TempDir; -use chksum::hash::MD5; -use chksum::{chksum, chksum_with, ArgsBuilder}; - -mod common; -use common::Result; - -#[test] -fn empty_directory_as_path() -> Result { - let temp_dir = TempDir::new()?; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - Ok(()) -} - -#[test] -fn empty_directory_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - } - - Ok(()) -} - -#[test] -fn empty_directory_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - Ok(()) -} - -#[test] -fn empty_directory_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - } - - Ok(()) -} - -#[test] -fn empty_directory_as_readdir() -> Result { - let temp_dir = TempDir::new()?; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - Ok(()) -} - -#[test] -fn empty_directory_as_readdir_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_path() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_path_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_pathbuf() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_readdir() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_readdir_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_path() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_path_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_pathbuf() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_readdir() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_readdir_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - } - - Ok(()) -} - -#[test] -fn empty_file_as_path() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = child.path(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - Ok(()) -} - -#[test] -fn empty_file_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.path(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - } - - Ok(()) -} - -#[test] -fn empty_file_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - let file = &child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - Ok(()) -} - -#[test] -fn empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - let file = &child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - } - - Ok(()) -} - -#[test] -fn empty_file_as_file() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - let file = &File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - Ok(()) -} - -#[test] -fn empty_file_as_file_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - - let file = &File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d41d8cd98f00b204e9800998ecf8427e"); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_path() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = child.path(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - - Ok(()) -} - -#[test] -fn non_empty_file_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.path(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - - let file = &child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - - Ok(()) -} - -#[test] -fn non_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - - let file = &child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_file() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - - let file = &File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - - Ok(()) -} - -#[test] -fn non_empty_file_as_file_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - - let file = &File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "8d777f385d3dfec8815d20f7496026dc"); - } - - Ok(()) -} diff --git a/tests/sha1.rs b/tests/sha1.rs deleted file mode 100644 index 3097e46..0000000 --- a/tests/sha1.rs +++ /dev/null @@ -1,581 +0,0 @@ -use std::fs::{read_dir, File}; - -use assert_fs::prelude::{FileTouch, FileWriteBin, PathChild}; -use assert_fs::TempDir; -use chksum::hash::SHA1; -use chksum::{chksum, chksum_with, ArgsBuilder}; - -mod common; -use common::Result; - -#[test] -fn empty_directory_as_path() -> Result { - let temp_dir = TempDir::new()?; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - Ok(()) -} - -#[test] -fn empty_directory_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - } - - Ok(()) -} - -#[test] -fn empty_directory_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - Ok(()) -} - -#[test] -fn empty_directory_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - } - - Ok(()) -} - -#[test] -fn empty_directory_as_readdir() -> Result { - let temp_dir = TempDir::new()?; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - Ok(()) -} - -#[test] -fn empty_directory_as_readdir_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_path() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_path_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_pathbuf() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_readdir() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_readdir_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_path() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_path_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_pathbuf() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_readdir() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_readdir_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - } - - Ok(()) -} - -#[test] -fn empty_file_as_path() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = child.path(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - Ok(()) -} - -#[test] -fn empty_file_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.path(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - } - - Ok(()) -} - -#[test] -fn empty_file_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - let file = &child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - Ok(()) -} - -#[test] -fn empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - let file = &child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - } - - Ok(()) -} - -#[test] -fn empty_file_as_file() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - let file = &File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - Ok(()) -} - -#[test] -fn empty_file_as_file_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - let file = &File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_path() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = child.path(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - - Ok(()) -} - -#[test] -fn non_empty_file_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.path(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - - let file = &child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - - Ok(()) -} - -#[test] -fn non_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - - let file = &child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_file() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - - let file = &File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - - Ok(()) -} - -#[test] -fn non_empty_file_as_file_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - - let file = &File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"); - } - - Ok(()) -} diff --git a/tests/sha2_224.rs b/tests/sha2_224.rs deleted file mode 100644 index 0aa5f59..0000000 --- a/tests/sha2_224.rs +++ /dev/null @@ -1,581 +0,0 @@ -use std::fs::{read_dir, File}; - -use assert_fs::prelude::{FileTouch, FileWriteBin, PathChild}; -use assert_fs::TempDir; -use chksum::hash::SHA2_224; -use chksum::{chksum, chksum_with, ArgsBuilder}; - -mod common; -use common::Result; - -#[test] -fn empty_directory_as_path() -> Result { - let temp_dir = TempDir::new()?; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - Ok(()) -} - -#[test] -fn empty_directory_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - } - - Ok(()) -} - -#[test] -fn empty_directory_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - Ok(()) -} - -#[test] -fn empty_directory_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - } - - Ok(()) -} - -#[test] -fn empty_directory_as_readdir() -> Result { - let temp_dir = TempDir::new()?; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - Ok(()) -} - -#[test] -fn empty_directory_as_readdir_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_path() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_path_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_pathbuf() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_readdir() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_readdir_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_path() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_path_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_pathbuf() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_readdir() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_readdir_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - } - - Ok(()) -} - -#[test] -fn empty_file_as_path() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = child.path(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - Ok(()) -} - -#[test] -fn empty_file_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.path(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - } - - Ok(()) -} - -#[test] -fn empty_file_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - let file = &child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - Ok(()) -} - -#[test] -fn empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - let file = &child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - } - - Ok(()) -} - -#[test] -fn empty_file_as_file() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - let file = &File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - Ok(()) -} - -#[test] -fn empty_file_as_file_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - - let file = &File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_path() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = child.path(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - - Ok(()) -} - -#[test] -fn non_empty_file_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.path(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - - let file = &child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - - Ok(()) -} - -#[test] -fn non_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - - let file = &child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_file() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - - let file = &File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - - Ok(()) -} - -#[test] -fn non_empty_file_as_file_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - - let file = &File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "f4739673acc03c424343b452787ee23dd62999a8a9f14f4250995769"); - } - - Ok(()) -} diff --git a/tests/sha2_256.rs b/tests/sha2_256.rs deleted file mode 100644 index ca67b47..0000000 --- a/tests/sha2_256.rs +++ /dev/null @@ -1,713 +0,0 @@ -use std::fs::{read_dir, File}; - -use assert_fs::prelude::{FileTouch, FileWriteBin, PathChild}; -use assert_fs::TempDir; -use chksum::hash::SHA2_256; -use chksum::{chksum, chksum_with, ArgsBuilder}; - -mod common; -use common::Result; - -#[test] -fn empty_directory_as_path() -> Result { - let temp_dir = TempDir::new()?; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - Ok(()) -} - -#[test] -fn empty_directory_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - } - - Ok(()) -} - -#[test] -fn empty_directory_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - Ok(()) -} - -#[test] -fn empty_directory_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - } - - Ok(()) -} - -#[test] -fn empty_directory_as_readdir() -> Result { - let temp_dir = TempDir::new()?; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - Ok(()) -} - -#[test] -fn empty_directory_as_readdir_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_path() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_path_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_pathbuf() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_readdir() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_readdir_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_path() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_path_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_pathbuf() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_readdir() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_readdir_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - } - - Ok(()) -} - -#[test] -fn empty_file_as_path() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = child.path(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - Ok(()) -} - -#[test] -fn empty_file_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.path(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - } - - Ok(()) -} - -#[test] -fn empty_file_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - let file = &child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - Ok(()) -} - -#[test] -fn empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - let file = &child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - } - - Ok(()) -} - -#[test] -fn empty_file_as_file() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - let file = &File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - Ok(()) -} - -#[test] -fn empty_file_as_file_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - let file = &File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_path() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = child.path(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - - Ok(()) -} - -#[test] -fn non_empty_file_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.path(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - - let file = &child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - - Ok(()) -} - -#[test] -fn non_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - - let file = &child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_file() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - - let file = &File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - - Ok(()) -} - -#[test] -fn non_empty_file_as_file_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - - let file = &File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7" - ); - } - - Ok(()) -} diff --git a/tests/sha2_384.rs b/tests/sha2_384.rs deleted file mode 100644 index 0de9541..0000000 --- a/tests/sha2_384.rs +++ /dev/null @@ -1,713 +0,0 @@ -use std::fs::{read_dir, File}; - -use assert_fs::prelude::{FileTouch, FileWriteBin, PathChild}; -use assert_fs::TempDir; -use chksum::hash::SHA2_384; -use chksum::{chksum, chksum_with, ArgsBuilder}; - -mod common; -use common::Result; - -#[test] -fn empty_directory_as_path() -> Result { - let temp_dir = TempDir::new()?; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - Ok(()) -} - -#[test] -fn empty_directory_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - } - - Ok(()) -} - -#[test] -fn empty_directory_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - Ok(()) -} - -#[test] -fn empty_directory_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - } - - Ok(()) -} - -#[test] -fn empty_directory_as_readdir() -> Result { - let temp_dir = TempDir::new()?; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - Ok(()) -} - -#[test] -fn empty_directory_as_readdir_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_path() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_path_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_pathbuf() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_readdir() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_readdir_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_path() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_path_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_pathbuf() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_readdir() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_readdir_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - } - - Ok(()) -} - -#[test] -fn empty_file_as_path() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = child.path(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - Ok(()) -} - -#[test] -fn empty_file_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.path(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - } - - Ok(()) -} - -#[test] -fn empty_file_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - let file = &child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - Ok(()) -} - -#[test] -fn empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - let file = &child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - } - - Ok(()) -} - -#[test] -fn empty_file_as_file() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - let file = &File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - Ok(()) -} - -#[test] -fn empty_file_as_file_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - - let file = &File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_path() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = child.path(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - - Ok(()) -} - -#[test] -fn non_empty_file_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.path(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - - let file = &child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - - Ok(()) -} - -#[test] -fn non_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - - let file = &child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_file() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - - let file = &File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - - Ok(()) -} - -#[test] -fn non_empty_file_as_file_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - - let file = &File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!( - digest, - "2039e0f0b92728499fb88e23ebc3cfd0554b28400b0ed7b753055c88b5865c3c2aa72c6a1a9ae0a755d87900a4a6ff41" - ); - } - - Ok(()) -} diff --git a/tests/sha2_512.rs b/tests/sha2_512.rs deleted file mode 100644 index c37a461..0000000 --- a/tests/sha2_512.rs +++ /dev/null @@ -1,581 +0,0 @@ -use std::fs::{read_dir, File}; - -use assert_fs::prelude::{FileTouch, FileWriteBin, PathChild}; -use assert_fs::TempDir; -use chksum::hash::SHA2_512; -use chksum::{chksum, chksum_with, ArgsBuilder}; - -mod common; -use common::Result; - -#[test] -fn empty_directory_as_path() -> Result { - let temp_dir = TempDir::new()?; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - Ok(()) -} - -#[test] -fn empty_directory_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - } - - Ok(()) -} - -#[test] -fn empty_directory_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - Ok(()) -} - -#[test] -fn empty_directory_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - } - - Ok(()) -} - -#[test] -fn empty_directory_as_readdir() -> Result { - let temp_dir = TempDir::new()?; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - Ok(()) -} - -#[test] -fn empty_directory_as_readdir_with_args() -> Result { - let temp_dir = TempDir::new()?; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_path() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_path_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_pathbuf() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_readdir() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_empty_file_as_readdir_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - temp_dir.child("file.txt").touch()?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_path() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = temp_dir.path(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_path_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.path(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_pathbuf() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - - let dir = &temp_dir.to_path_buf(); - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - } - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_readdir() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - let dir = read_dir(temp_dir.path())?; - let digest = chksum::(dir)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - - Ok(()) -} - -#[test] -fn non_empty_directory_with_non_empty_file_as_readdir_with_args() -> Result { - let temp_dir = { - let temp_dir = TempDir::new()?; - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - temp_dir - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let dir = read_dir(temp_dir.path())?; - let digest = chksum_with::(dir, &args)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - } - - Ok(()) -} - -#[test] -fn empty_file_as_path() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = child.path(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - Ok(()) -} - -#[test] -fn empty_file_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.path(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - } - - Ok(()) -} - -#[test] -fn empty_file_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - let file = &child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - Ok(()) -} - -#[test] -fn empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - let file = &child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - } - - Ok(()) -} - -#[test] -fn empty_file_as_file() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - let file = File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - let file = &File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - Ok(()) -} - -#[test] -fn empty_file_as_file_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - - let file = &File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_path() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = child.path(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - - Ok(()) -} - -#[test] -fn non_empty_file_as_path_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.path(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_pathbuf() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - - let file = &child.to_path_buf(); - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - - Ok(()) -} - -#[test] -fn non_empty_file_as_pathbuf_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - - let file = &child.to_path_buf(); - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - } - - Ok(()) -} - -#[test] -fn non_empty_file_as_file() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - let file = File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - - let file = &File::open(child.path())?; - let digest = chksum::(file)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - - Ok(()) -} - -#[test] -fn non_empty_file_as_file_with_args() -> Result { - let temp_dir = TempDir::new()?; - let child = { - let file = temp_dir.child("file.txt"); - file.touch()?; - file.write_binary(b"data")?; - file - }; - - for chunk_size in [1, 2, 3, 127] { - let args = ArgsBuilder::new().chunk_size(chunk_size).build(); - - let file = File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - - let file = &File::open(child.path())?; - let digest = chksum_with::(file, &args)?.to_hex_lowercase(); - assert_eq!(digest, "77c7ce9a5d86bb386d443bb96390faa120633158699c8844c30b13ab0bf92760b7e4416aea397db91b4ac0e5dd56b8ef7e4b066162ab1fdc088319ce6defc876"); - } - - Ok(()) -}