File: metadata.rs

package info (click to toggle)
rust-rust-embed 8.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,184 kB
  • sloc: javascript: 4; makefile: 2
file content (56 lines) | stat: -rw-r--r-- 2,160 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use rust_embed::{Embed, EmbeddedFile};
use sha2::Digest;
use std::{fs, time::SystemTime};

#[derive(Embed)]
#[folder = "examples/public/"]
struct Asset;

#[test]
fn hash_is_accurate() {
  let index_file: EmbeddedFile = Asset::get("index.html").expect("index.html exists");
  let mut hasher = sha2::Sha256::new();
  hasher.update(index_file.data);
  let expected_hash: [u8; 32] = hasher.finalize().into();

  assert_eq!(index_file.metadata.sha256_hash(), expected_hash);
}

#[test]
#[cfg(not(feature = "deterministic-timestamps"))]
fn last_modified_is_accurate() {
  let index_file: EmbeddedFile = Asset::get("index.html").expect("index.html exists");

  let metadata = fs::metadata(format!("{}/examples/public/index.html", env!("CARGO_MANIFEST_DIR"))).unwrap();
  let expected_datetime_utc = metadata.modified().unwrap().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();

  assert_eq!(index_file.metadata.last_modified(), Some(expected_datetime_utc));
}

#[test]
#[cfg(not(feature = "deterministic-timestamps"))]
fn create_is_accurate() {
  let index_file: EmbeddedFile = Asset::get("index.html").expect("index.html exists");

  let metadata = fs::metadata(format!("{}/examples/public/index.html", env!("CARGO_MANIFEST_DIR"))).unwrap();
  let expected_datetime_utc = metadata.created().unwrap().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();

  assert_eq!(index_file.metadata.created(), Some(expected_datetime_utc));
}

#[test]
#[cfg(feature = "deterministic-timestamps")]
fn deterministic_timestamps_are_zero() {
  let index_file: EmbeddedFile = Asset::get("index.html").expect("index.html exists");

  assert_eq!(
    index_file.metadata.last_modified(),
    Some(0),
    "last_modified should be 0 with deterministic-timestamps"
  );
  assert_eq!(index_file.metadata.created(), Some(0), "created should be 0 with deterministic-timestamps");

  let metadata = fs::metadata(format!("{}/examples/public/index.html", env!("CARGO_MANIFEST_DIR"))).unwrap();
  let fs_modified = metadata.modified().unwrap().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
  assert_ne!(fs_modified, 0, "Filesystem modified time should not be 0");
}