1use crate::Uuid;
2
3impl Uuid {
4 pub fn new_v3(namespace: &Uuid, name: &[u8]) -> Uuid {
38 crate::Builder::from_md5_bytes(crate::md5::hash(namespace.as_bytes(), name)).into_uuid()
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45
46 #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
47 use wasm_bindgen_test::*;
48
49 use crate::{std::string::ToString, Variant, Version};
50
51 static FIXTURE: &'static [(&'static Uuid, &'static str, &'static str)] = &[
52 (
53 &Uuid::NAMESPACE_DNS,
54 "example.org",
55 "04738bdf-b25a-3829-a801-b21a1d25095b",
56 ),
57 (
58 &Uuid::NAMESPACE_DNS,
59 "rust-lang.org",
60 "c6db027c-615c-3b4d-959e-1a917747ca5a",
61 ),
62 (
63 &Uuid::NAMESPACE_DNS,
64 "42",
65 "5aab6e0c-b7d3-379c-92e3-2bfbb5572511",
66 ),
67 (
68 &Uuid::NAMESPACE_DNS,
69 "lorem ipsum",
70 "4f8772e9-b59c-3cc9-91a9-5c823df27281",
71 ),
72 (
73 &Uuid::NAMESPACE_URL,
74 "example.org",
75 "39682ca1-9168-3da2-a1bb-f4dbcde99bf9",
76 ),
77 (
78 &Uuid::NAMESPACE_URL,
79 "rust-lang.org",
80 "7ed45aaf-e75b-3130-8e33-ee4d9253b19f",
81 ),
82 (
83 &Uuid::NAMESPACE_URL,
84 "42",
85 "08998a0c-fcf4-34a9-b444-f2bfc15731dc",
86 ),
87 (
88 &Uuid::NAMESPACE_URL,
89 "lorem ipsum",
90 "e55ad2e6-fb89-34e8-b012-c5dde3cd67f0",
91 ),
92 (
93 &Uuid::NAMESPACE_OID,
94 "example.org",
95 "f14eec63-2812-3110-ad06-1625e5a4a5b2",
96 ),
97 (
98 &Uuid::NAMESPACE_OID,
99 "rust-lang.org",
100 "6506a0ec-4d79-3e18-8c2b-f2b6b34f2b6d",
101 ),
102 (
103 &Uuid::NAMESPACE_OID,
104 "42",
105 "ce6925a5-2cd7-327b-ab1c-4b375ac044e4",
106 ),
107 (
108 &Uuid::NAMESPACE_OID,
109 "lorem ipsum",
110 "5dd8654f-76ba-3d47-bc2e-4d6d3a78cb09",
111 ),
112 (
113 &Uuid::NAMESPACE_X500,
114 "example.org",
115 "64606f3f-bd63-363e-b946-fca13611b6f7",
116 ),
117 (
118 &Uuid::NAMESPACE_X500,
119 "rust-lang.org",
120 "bcee7a9c-52f1-30c6-a3cc-8c72ba634990",
121 ),
122 (
123 &Uuid::NAMESPACE_X500,
124 "42",
125 "c1073fa2-d4a6-3104-b21d-7a6bdcf39a23",
126 ),
127 (
128 &Uuid::NAMESPACE_X500,
129 "lorem ipsum",
130 "02f09a3f-1624-3b1d-8409-44eff7708208",
131 ),
132 ];
133
134 #[test]
135 #[cfg_attr(
136 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
137 wasm_bindgen_test
138 )]
139 fn test_new() {
140 for &(ref ns, ref name, _) in FIXTURE {
141 let uuid = Uuid::new_v3(*ns, name.as_bytes());
142 assert_eq!(uuid.get_version(), Some(Version::Md5));
143 assert_eq!(uuid.get_variant(), Variant::RFC4122);
144 }
145 }
146
147 #[test]
148 #[cfg_attr(
149 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
150 wasm_bindgen_test
151 )]
152 fn test_hyphenated_string() {
153 for &(ref ns, ref name, ref expected) in FIXTURE {
154 let uuid = Uuid::new_v3(*ns, name.as_bytes());
155 assert_eq!(uuid.hyphenated().to_string(), *expected);
156 }
157 }
158}