From 38b011273bb92b83c61053ae2fcd80aa9320315c Mon Sep 17 00:00:00 2001 From: Sayan Nandan Date: Mon, 15 Feb 2021 10:25:53 +0530 Subject: [PATCH] Add fix and tests for VE/S/00001 This commit now checks if the second value passed to MKSNAP points to any parent/root directory before performing any action. Signed-off-by: Sayan Nandan --- .gitignore | 3 +- .idea/workspace.xml | 75 +++++++++++++++++++++++ Cargo.lock | 115 +++++++++++++++-------------------- cli/Cargo.toml | 6 +- libtdb/Cargo.toml | 4 +- server/Cargo.toml | 18 +++--- server/src/admin/mksnap.rs | 23 ++++++- server/src/cli.yml | 4 +- server/src/dbnet.rs | 2 +- server/src/tests/kvengine.rs | 21 ++++++- tdb-bench/Cargo.toml | 8 +-- tdb-bench/src/cli.yml | 8 +-- tdb-bench/src/main.rs | 41 ++++++------- tdb-macros/Cargo.toml | 6 +- tdb-macros/src/lib.rs | 8 +-- 15 files changed, 216 insertions(+), 126 deletions(-) create mode 100644 .idea/workspace.xml diff --git a/.gitignore b/.gitignore index 81f77565..3fc07f35 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ data.bin /server/snapshots snapstore.bin -snapstore.partmap \ No newline at end of file +snapstore.partmap +/snapshots \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 00000000..1ead40c1 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1610003825159 + + + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 576d4841..c5117730 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,9 +65,9 @@ checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" [[package]] name = "bytes" -version = "0.6.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0dcbc35f504eb6fc275a6d20e4ebcda18cf50d40ba6fabff8c711fa16cb3b16" +checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" [[package]] name = "cc" @@ -81,6 +81,12 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "chrono" version = "0.4.19" @@ -121,15 +127,15 @@ dependencies = [ [[package]] name = "devtimer" -version = "4.0.0" +version = "4.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6035b7b9244bf9637cd7ef80b5e1c54404bef92cccd34738c85c45f04ae8b244" +checksum = "907339959a92f6b98846570500c0a567c9aecbb3871cef00561eb5d20d47b7c1" [[package]] name = "env_logger" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26ecb66b4bdca6c1409b40fb255eefc2bd4f6d135dab3c3124f80ffa2a9661e" +checksum = "17392a012ea30ef05a610aa97dfb49496e71c9f676b27879922ea5bdf60d9d3f" dependencies = [ "atty", "humantime", @@ -144,21 +150,15 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394" -[[package]] -name = "futures-core" -version = "0.3.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59f5fff90fd5d971f936ad674802482ba441b6f09ba5e15fd8b39145582ca399" - [[package]] name = "getrandom" -version = "0.1.15" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", - "wasi 0.9.0+wasi-snapshot-preview1", + "wasi", ] [[package]] @@ -241,11 +241,11 @@ dependencies = [ [[package]] name = "log" -version = "0.4.11" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -338,7 +338,7 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c361aa727dd08437f2f1447be8b59a33b0edd15e0fcee698f935613d9efbca9b" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "cloudabi", "instant", "libc", @@ -370,20 +370,19 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.7" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" dependencies = [ "proc-macro2", ] [[package]] name = "rand" -version = "0.7.3" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" dependencies = [ - "getrandom", "libc", "rand_chacha", "rand_core", @@ -392,9 +391,9 @@ dependencies = [ [[package]] name = "rand_chacha" -version = "0.2.2" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" dependencies = [ "ppv-lite86", "rand_core", @@ -402,18 +401,18 @@ dependencies = [ [[package]] name = "rand_core" -version = "0.5.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" dependencies = [ "getrandom", ] [[package]] name = "rand_hc" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" dependencies = [ "rand_core", ] @@ -426,9 +425,9 @@ checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" [[package]] name = "regex" -version = "1.4.2" +version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c" +checksum = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a" dependencies = [ "aho-corasick", "memchr", @@ -438,9 +437,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.6.21" +version = "0.6.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189" +checksum = "b5eb417147ba9860a96cfe72a0b93bf88fee1744b5636ec99ab20c1aa9376581" [[package]] name = "ryu" @@ -456,18 +455,18 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.118" +version = "1.0.123" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800" +checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.118" +version = "1.0.123" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c84d3526699cd55261af4b941e4e725444df67aa4f9e6a3564f18030d12672df" +checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31" dependencies = [ "proc-macro2", "quote", @@ -476,9 +475,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.60" +version = "1.0.62" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1500e84d27fe482ed1dc791a56eddc2f230046a040fa908c08bda1d9fb615779" +checksum = "ea1c6153794552ea7cf7cf63b1231a25de00ec90db326ba6264440fa08e31486" dependencies = [ "itoa", "ryu", @@ -495,12 +494,6 @@ dependencies = [ "libc", ] -[[package]] -name = "slab" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" - [[package]] name = "smallvec" version = "1.4.1" @@ -513,7 +506,7 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fd8b795c389288baa5f355489c65e71fd48a02104600d15c4cfbc561e9e429d" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", "redox_syscall", "winapi", @@ -527,9 +520,9 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" [[package]] name = "syn" -version = "1.0.54" +version = "1.0.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a2af957a63d6bd42255c359c93d9bfdb97076bd3b820897ce55ffbfbf107f44" +checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" dependencies = [ "proc-macro2", "quote", @@ -614,19 +607,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" dependencies = [ "libc", - "wasi 0.10.0+wasi-snapshot-preview1", + "wasi", "winapi", ] [[package]] name = "tokio" -version = "0.3.6" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "720ba21c25078711bf456d607987d95bce90f7c3bea5abe1db587862e7a1e87c" +checksum = "e8190d04c665ea9e6b6a0dc45523ade572c088d2e6566244c1122671dbf4ae3a" dependencies = [ "autocfg", "bytes", - "futures-core", "libc", "memchr", "mio", @@ -635,16 +627,15 @@ dependencies = [ "parking_lot", "pin-project-lite", "signal-hook-registry", - "slab", "tokio-macros", "winapi", ] [[package]] name = "tokio-macros" -version = "0.3.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21d30fdbb5dc2d8f91049691aa1a9d4d4ae422a21c334ce8936e5886d30c5c45" +checksum = "caf7b11a536f46a809a8a9f0bb4237020f70ecbf115b842360afb127ea2fda57" dependencies = [ "proc-macro2", "quote", @@ -653,9 +644,9 @@ dependencies = [ [[package]] name = "toml" -version = "0.5.7" +version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75cf45bb0bef80604d001caaec0d09da99611b3c0fd39d3080468875cdb65645" +checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" dependencies = [ "serde", ] @@ -690,12 +681,6 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" -[[package]] -name = "wasi" -version = "0.9.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" - [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 2c436129..b1953e92 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -8,8 +8,8 @@ edition = "2018" [dependencies] libtdb = {path = "../libtdb"} -tokio = {version = "0.3.6", features = ["full"]} -bytes = "0.6.0" -regex = "1.4.2" +tokio = {version = "1.2.0", features = ["full"]} +bytes = "1.0.1" +regex = "1.4.3" lazy_static = "1.4.0" clap = {version = "2.33.3", features=["yaml"]} \ No newline at end of file diff --git a/libtdb/Cargo.toml b/libtdb/Cargo.toml index 35a8cfd4..7a85dfcd 100644 --- a/libtdb/Cargo.toml +++ b/libtdb/Cargo.toml @@ -8,5 +8,5 @@ edition = "2018" [dependencies] lazy_static = "1.4.0" -bytes = "0.6.0" -termcolor = "1.1.1" \ No newline at end of file +bytes = "1.0.1" +termcolor = "1.1.2" \ No newline at end of file diff --git a/server/Cargo.toml b/server/Cargo.toml index 778a7f68..585612d5 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -7,24 +7,24 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -tokio = { version = "0.3.6", features = ["full"] } -bytes = "0.6.0" +tokio = { version = "1.2.0", features = ["full"] } +bytes = "1.0.1" libtdb = {path ="../libtdb"} bincode = "1.3.1" parking_lot = "0.11.1" lazy_static = "1.4.0" -serde_derive = "1.0.117" -serde = {version = "1.0.118", features= ["derive"]} -toml = "0.5.7" +serde_derive = "1.0.123" +serde = {version = "1.0.123", features= ["derive"]} +toml = "0.5.8" clap = {version = "2.33.3", features=["yaml"]} -env_logger = "0.8.2" -log = "0.4.11" +env_logger = "0.8.3" +log = "0.4.14" chrono = "0.4.19" -regex = "1.4.2" +regex = "1.4.3" tdb_macros = {path="../tdb-macros"} [target.'cfg(not(target_env = "msvc"))'.dependencies] jemallocator = "0.3.2" [dev-dependencies] -tokio = { version = "0.3.6", features = ["test-util"] } +tokio = { version = "1.2.0", features = ["test-util"] } diff --git a/server/src/admin/mksnap.rs b/server/src/admin/mksnap.rs index 07a8b55d..8bc49356 100644 --- a/server/src/admin/mksnap.rs +++ b/server/src/admin/mksnap.rs @@ -28,7 +28,7 @@ use crate::resp::GroupBegin; use libtdb::terrapipe::RespCodes; use libtdb::TResult; use std::hint::unreachable_unchecked; -use std::path::PathBuf; +use std::path::{Component, PathBuf}; /// Create a snapshot /// @@ -106,6 +106,25 @@ pub async fn mksnap(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> let mut path = PathBuf::from(DIR_SNAPSHOT); path.push("remote"); path.push(snapname.to_owned() + ".snapshot"); + let illegal_snapshot = path + .components() + .filter(|dir| { + // Sanitize snapshot name, to avoid directory traversal attacks + // If the snapshot name has any root directory or parent directory, then + // we'll allow it to pass through this adaptor. + // As a result, this iterator will give us a count of the 'bad' components + dir == &Component::RootDir || dir == &Component::ParentDir + }) + .count() + != 0; + if illegal_snapshot { + con.write_response(GroupBegin(1)).await?; + return con + .write_response(RespCodes::OtherError(Some( + "err-invalid-snapshot-name".to_owned(), + ))) + .await; + } let failed; { match diskstore::flush_data(&path, &handle.acquire_read().get_ref()) { @@ -131,4 +150,4 @@ pub async fn mksnap(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> .await; } } -} +} \ No newline at end of file diff --git a/server/src/cli.yml b/server/src/cli.yml index 04ba4cdf..c936391f 100644 --- a/server/src/cli.yml +++ b/server/src/cli.yml @@ -19,10 +19,10 @@ # # -name: TerrabaseDB Server +name: Skybase Server version: 0.5.0 author: Sayan N. -about: The TerrabaseDB Database server +about: The Skybase Database server args: - config: short: c diff --git a/server/src/dbnet.rs b/server/src/dbnet.rs index 85a9598b..507a808f 100644 --- a/server/src/dbnet.rs +++ b/server/src/dbnet.rs @@ -139,7 +139,7 @@ impl Listener { loop { // Take the permit first, but we won't use it right now // that's why we will forget it - self.climit.acquire().await.forget(); + self.climit.acquire().await.unwrap().forget(); let stream = self.accept().await?; let mut chandle = CHandler { db: self.db.clone(), diff --git a/server/src/tests/kvengine.rs b/server/src/tests/kvengine.rs index 037bcb6c..3dfed413 100644 --- a/server/src/tests/kvengine.rs +++ b/server/src/tests/kvengine.rs @@ -23,8 +23,7 @@ mod __private { use crate::protocol::responses::fresp; use libtdb::terrapipe; - use tokio::io::AsyncReadExt; - use tokio::prelude::*; + use tokio::io::{AsyncReadExt, AsyncWriteExt}; /// Test a HEYA query: The server should return HEY! async fn test_heya() { let heya = terrapipe::proc_query("HEYA"); @@ -82,7 +81,6 @@ mod __private { ) where T: AsRef, { - use tokio::prelude::*; let mut query = String::from("MSET "); query.push_str(values_split_with_whitespace.as_ref()); let count_bytes_len = homwany.to_string().as_bytes().len(); @@ -663,4 +661,21 @@ mod __private { stream.read_exact(&mut response).await.unwrap(); assert_eq!(res_should_be, response); } + async fn test_mksnap_sanitization() { + let res_should_be = "#2\n*1\n#2\n&1\n!25\nerr-invalid-snapshot-name\n" + .to_owned() + .into_bytes(); + // First check parent directory syntax + let query = terrapipe::proc_query("MKSNAP ../../badsnappy"); + stream.write_all(&query).await.unwrap(); + let mut response = vec![0; res_should_be.len()]; + stream.read_exact(&mut response).await.unwrap(); + assert_eq!(res_should_be, response); + // Now check root directory syntax + let query = terrapipe::proc_query("MKSNAP /var/omgcrazysnappy"); + stream.write_all(&query).await.unwrap(); + let mut response = vec![0; res_should_be.len()]; + stream.read_exact(&mut response).await.unwrap(); + assert_eq!(res_should_be, response); + } } diff --git a/tdb-bench/Cargo.toml b/tdb-bench/Cargo.toml index 92bd6305..7c03be2b 100644 --- a/tdb-bench/Cargo.toml +++ b/tdb-bench/Cargo.toml @@ -7,9 +7,9 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -rand = "0.7.3" -devtimer = "4.0.0" +rand = "0.8.3" +devtimer = "4.0.1" libtdb = {path="../libtdb"} clap = {version = "2.33.3", features=["yaml"]} -serde = {version="1.0.118", features=["derive"]} -serde_json = "1.0.60" +serde = {version="1.0.123", features=["derive"]} +serde_json = "1.0.62" diff --git a/tdb-bench/src/cli.yml b/tdb-bench/src/cli.yml index ae09dc82..23fe4fb4 100644 --- a/tdb-bench/src/cli.yml +++ b/tdb-bench/src/cli.yml @@ -19,12 +19,12 @@ # # -name: TerrabaseDB Benchmark Tool -version: 0.5.0 +name: Skybase Benchmark Tool +version: 0.5-hotfix.1 author: Sayan N. about: | - The TerrabaseDB benchmark tool can be used to benchmark TerrabaseDB installations. - If you find any issues, then report one here: https://github.com/terrabasedb/terrabasedb + The Skybase benchmark tool can be used to benchmark Skybase installations. + If you find any issues, then report one here: https://github.com/skybasedb/skybase args: - connections: short: c diff --git a/tdb-bench/src/main.rs b/tdb-bench/src/main.rs index cc196dca..1369e0bc 100644 --- a/tdb-bench/src/main.rs +++ b/tdb-bench/src/main.rs @@ -1,7 +1,7 @@ /* * Created on Sun Sep 13 2020 * - * This file is a part of TerrabaseDB + * This file is a part of Skybase * Copyright (c) 2020, Sayan Nandan * * This program is free software: you can redistribute it and/or modify @@ -28,7 +28,7 @@ mod benchtool { use devtimer::DevTime; use libtdb::terrapipe; use rand::distributions::Alphanumeric; - use rand::{thread_rng, Rng}; + use rand::thread_rng; use serde::Serialize; use std::io::prelude::*; use std::net::{self, TcpStream}; @@ -167,7 +167,7 @@ mod benchtool { }, None => host.push_str("2003"), } - let rand = thread_rng(); + let mut rand = thread_rng(); if let Some(matches) = matches.subcommand_matches("testkey") { let numkeys = matches.value_of("count").unwrap(); if let Ok(num) = numkeys.parse::() { @@ -175,17 +175,11 @@ mod benchtool { println!("Generating keys ..."); let keys: Vec = (0..num) .into_iter() - .map(|_| { - let rand_string: String = rand.sample_iter(&Alphanumeric).take(8).collect(); - rand_string - }) + .map(|_| ran_string(8, &mut rand)) .collect(); let values: Vec = (0..num) .into_iter() - .map(|_| { - let rand_string: String = rand.sample_iter(&Alphanumeric).take(8).collect(); - rand_string - }) + .map(|_| ran_string(8, &mut rand)) .collect(); let set_packs: Vec> = (0..num) .map(|idx| terrapipe::proc_query(format!("SET {} {}", keys[idx], values[idx]))) @@ -225,26 +219,18 @@ mod benchtool { max_queries, (packet_size * 2), // key size + value size ); - let rand = thread_rng(); + let mut rand = thread_rng(); let mut dt = DevTime::new_complex(); // Create separate connection pools for get and set operations let mut setpool = Netpool::new(max_connections, &host); let mut getpool = Netpool::new(max_connections, &host); let keys: Vec = (0..max_queries) .into_iter() - .map(|_| { - let rand_string: String = - rand.sample_iter(&Alphanumeric).take(packet_size).collect(); - rand_string - }) + .map(|_| ran_string(packet_size, &mut rand)) .collect(); let values: Vec = (0..max_queries) .into_iter() - .map(|_| { - let rand_string: String = - rand.sample_iter(&Alphanumeric).take(packet_size).collect(); - rand_string - }) + .map(|_| ran_string(packet_size, &mut rand)) .collect(); /* We create three vectors of vectors: `set_packs`, `get_packs` and `del_packs` @@ -309,8 +295,17 @@ mod benchtool { fn calc(reqs: usize, time: u128) -> f64 { reqs as f64 / (time as f64 / 1_000_000_000 as f64) } + + fn ran_string(len: usize, rand: impl rand::Rng) -> String { + let rand_string: String = rand + .sample_iter(&Alphanumeric) + .take(len) + .map(char::from) + .collect(); + rand_string + } } fn main() { benchtool::runner(); -} +} \ No newline at end of file diff --git a/tdb-macros/Cargo.toml b/tdb-macros/Cargo.toml index a44f4864..6a27c632 100644 --- a/tdb-macros/Cargo.toml +++ b/tdb-macros/Cargo.toml @@ -10,7 +10,7 @@ edition = "2018" proc-macro = true [dependencies] -syn = {version = "1.0.54", features = ["full"]} -quote = "1.0.7" -rand = "0.7.3" +syn = {version = "1.0.60", features = ["full"]} +quote = "1.0.9" +rand = "0.8.3" proc-macro2 = "1.0.24" diff --git a/tdb-macros/src/lib.rs b/tdb-macros/src/lib.rs index 14eb3728..58e48e05 100644 --- a/tdb-macros/src/lib.rs +++ b/tdb-macros/src/lib.rs @@ -67,7 +67,7 @@ fn parse_dbtest(mut input: syn::ItemFn, rand: u16) -> Result TokenStream { over by Hyper-V, which is why we'll prevent attempts to bind to them, if the OS is Windows. */ - let mut rand: u16 = rng.gen_range(1025, 65535); + let mut rand: u16 = rng.gen_range(1025..=65535); #[cfg(not(target_os = "windows"))] { while in_set.contains(&rand) { - rand = rng.gen_range(1025, 65535); + rand = rng.gen_range(1025..=65535); } } #[cfg(target_os = "windows")] @@ -195,7 +195,7 @@ fn parse_test_module(args: TokenStream, item: TokenStream) -> TokenStream { in_set.insert(5357); in_set.insert(7680); while in_set.contains(&rand) || (rand >= 49670 && rand <= 50293) { - rand = rng.gen_range(1025, 65535); + rand = rng.gen_range(1025..=65535); } } in_set.insert(rand);