Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration test example #121

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ rpath = true
lto = true

[profile.release.package."*"]
opt-level = 3
opt-level = 3
1 change: 1 addition & 0 deletions tests/integration-data/tf2-success.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"query":{"address":"193.221.192.41","port":null,"game":"tf2"},"server":{"tcp_port":null,"udp_port":27015,"packet_size":350},"packets":[{"direction":"ToServer","protocol":"Udp","src_port":49599,"dst_port":27015,"data":[255,255,255,255,84,83,111,117,114,99,101,32,69,110,103,105,110,101,32,81,117,101,114,121,0]},{"direction":"FromServer","protocol":"Udp","src_port":27015,"dst_port":49599,"data":[255,255,255,255,73,17,1,1,1,1,1,1,1,1,1,85,71,67,46,84,70,32,124,32,84,114,97,100,101,32,77,105,110,101,99,114,97,102,116,32,35,54,32,124,32,70,82,69,69,32,73,84,69,77,83,33,32,226,150,136,226,150,136,226,150,136,226,150,136,0,116,114,97,100,101,95,109,105,110,101,99,114,97,102,116,95,114,101,97,108,109,115,95,118,50,0,116,102,0,1,1,226,150,136,226,150,136,226,150,136,32,70,82,69,69,32,73,84,69,77,83,32,226,150,136,226,150,136,226,150,136,46,0,184,1,18,32,0,100,108,0,1,56,51,48,56,54,56,52,0,177,135,105,34,30,29,0,0,0,48,1,1,49,44,97,102,107,44,97,108,108,116,97,108,107,44,98,97,99,107,112,97,99,107,46,116,102,44,103,114,97,118,105,116,121,44,105,100,108,101,44,105,110,99,114,101,97,115,101,100,95,109,97,120,112,108,97,121,101,114,115,44,105,116,101,109,115,44,109,99,44,109,105,110,101,99,114,97,102,116,44,116,114,97,100,101,44,116,114,97,100,105,110,103,44,0,184,1,0,0,0,0,0,0]},{"direction":"ToServer","protocol":"Udp","src_port":49599,"dst_port":27015,"data":[255,255,255,255,85,255,255,255,255]},{"direction":"FromServer","protocol":"Udp","src_port":27015,"dst_port":49599,"data":[255,255,255,255,65,208,64,212,206]},{"direction":"ToServer","protocol":"Udp","src_port":49599,"dst_port":27015,"data":[255,255,255,255,85,208,64,212,206]},{"direction":"FromServer","protocol":"Udp","src_port":27015,"dst_port":49599,"data":[255,255,255,255,68,18,0,108,109,110,111,112,113,0,0,0,0,0,219,255,210,70,0,105,97,106,97,107,97,108,0,0,0,0,0,219,226,133,70,0,71,72,73,74,75,76,77,78,79,80,0,51,0,0,0,76,151,144,69,0,97,121,97,122,97,65,97,66,97,67,97,68,0,9,0,0,0,181,185,82,69,0,97,98,99,100,101,102,103,104,105,106,107,0,10,0,0,0,34,120,24,69,0,97,109,97,110,0,38,0,0,0,129,98,242,68,0,117,97,118,97,119,97,120,0,3,0,0,0,57,229,223,68,0,97,102,97,103,97,104,97,0,4,0,0,0,242,171,209,68,0,97,89,97,90,98,97,98,98,98,99,98,100,0,9,0,0,0,149,156,185,68,0,97,83,97,84,97,85,97,86,97,87,97,88,0,8,0,0,0,67,172,165,68,0,114,115,116,117,118,119,120,121,122,65,66,67,0,31,0,0,0,77,119,142,68,0,97,111,97,112,97,0,31,0,0,0,205,33,122,68,0,97,82,0,35,0,0,0,22,211,76,68,0,97,69,97,70,97,71,97,72,97,73,97,74,0,7,0,0,0,124,241,48,68,0,68,69,70,0,20,0,0,0,197,130,14,68,0,81,82,83,84,85,86,87,88,89,90,97,97,97,98,97,99,97,100,97,101,0,8,0,0,0,150,105,207,67,0,113,97,114,97,115,97,116,97,0,0,0,0,0,232,241,135,67,0,97,75,97,76,97,77,97,78,97,79,97,80,97,81,0,0,0,0,0,199,35,202,66]}],"value":{"name":"\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001UGC.TF | Trade Minecraft #6 | FREE ITEMS! ████","map":"trade_minecraft_realms_v2","has_password":false,"players_online":18,"players_maximum":32,"player_names":["abcdefghijk","aSaTaUaVaWaX","uavawax","QRSTUVWXYZaaabacadae","GHIJKLMNOP","aoapa","DEF","ayazaAaBaCaD","aEaFaGaHaIaJ","afagaha","aKaLaMaNaOaPaQ","aR","lmnopq","qarasata","iajakal","aYaZbabbbcbd","rstuvwxyzABC","aman"]},"replay_version":1}
78 changes: 78 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#![cfg(test)]
#![cfg(feature = "integration_tests")]

use std::collections::HashSet;
use std::fs::OpenOptions;
use std::net::ToSocketAddrs;
use std::path::Path;

use gamedig::protocols::types::CommonResponseJson;
use gamedig::protocols::ExtraRequestSettings;
use gamedig::query_with_timeout_and_extra_settings;
use gamedig::GAMES;

use net_replay_test;

pub struct SelfImpl;
impl net_replay_test::implementations::QueryImplementation for SelfImpl {
fn query_server(
&self,
options: &net_replay_test::QueryOptions,
) -> Result<net_replay_test::value::CommonValue, Box<dyn std::error::Error + 'static>> {
let game = GAMES.get(&options.game).ok_or("Unknown game")?;

let ip = format!("{}:0", options.address)
.to_socket_addrs()?
.next()
.ok_or("Given hostname did not resolve to an IP")?
.ip();

let extra_settings = ExtraRequestSettings::default()
.set_gather_rules(false)
.set_gather_players(true)
.set_hostname(options.address.clone());

let output = query_with_timeout_and_extra_settings(game, &ip, options.port, None, Some(extra_settings))?;

Ok(from_gamedig_to_net_replay(output.as_json()))
}
}

fn from_gamedig_to_net_replay(value: CommonResponseJson<'_>) -> net_replay_test::value::CommonValue {
net_replay_test::value::CommonValue {
name: value.name.map(|v| v.to_string()),
map: value.map.map(|v| v.to_string()),
has_password: value.has_password,
players_online: Some(value.players_online.into()),
players_maximum: Some(value.players_maximum.into()),
player_names: value
.players
.map(|players| {
players
.into_iter()
.map(|player| player.name.to_string())
.collect()
})
.unwrap_or(HashSet::new()),
}
}

fn get_impl() -> Box<dyn net_replay_test::implementations::QueryImplementation> { Box::new(SelfImpl) }

pub fn do_replay_test<P: AsRef<Path>>(replay_path: P) {
let file = OpenOptions::new()
.read(true)
.open(replay_path)
.expect("Replay file should exist");

let query_replay = serde_json::from_reader(file).expect("Replay should be valid format");

let result = net_replay_test::replay(get_impl(), query_replay).expect("Replay should succeed");

assert!(result);

println!("{:?}", result);
}

#[test]
fn integration_test_tf2() { do_replay_test("./tests/integration-data/tf2-success.json"); }
Loading