From 1b216dbebd28c77614714bc08293fed65cf0fb4f Mon Sep 17 00:00:00 2001
From: Jason Dusek <jason.dusek@gmail.com>
Date: Mon, 13 Feb 2017 19:16:01 -0800
Subject: [PATCH] Handle (a) changes in Screech and (b) borrow issue

---
 contrib/external-tests/rust/Cargo.toml  |  2 +-
 contrib/external-tests/rust/src/main.rs | 66 +++++++++++++++++----------------
 2 files changed, 36 insertions(+), 32 deletions(-)

diff --git a/contrib/external-tests/rust/Cargo.toml b/contrib/external-tests/rust/Cargo.toml
index c064905..32499e5 100644
--- a/contrib/external-tests/rust/Cargo.toml
+++ b/contrib/external-tests/rust/Cargo.toml
@@ -3,7 +3,7 @@ name = "wireguard-keepalive"
 version = "0.1.0"
 authors = ["jason@zx2c4.com"]
 [dependencies]
-screech = { git = "https://github.com/trevp/screech" }
+screech = { git = "https://github.com/solidsnack/screech", branch = "wg" }
 rust-crypto = "*"
 byteorder = "*"
 rustc-serialize = "*"
diff --git a/contrib/external-tests/rust/src/main.rs b/contrib/external-tests/rust/src/main.rs
index 232375c..65baf47 100644
--- a/contrib/external-tests/rust/src/main.rs
+++ b/contrib/external-tests/rust/src/main.rs
@@ -38,39 +38,43 @@ fn main() {
 	owner.set_s(my_keypair);
 	owner.set_rs(&their_public);
 	let mut cipherstate1 : CipherState<CipherChaChaPoly> = Default::default();
-        let mut cipherstate2 : CipherState<CipherChaChaPoly> = Default::default();
-	let mut handshake = HandshakeState::new_from_owner(&mut owner, true, HandshakePattern::IK, "WireGuard v0 zx2c4 Jason@zx2c4.com".as_bytes(), Some(&my_preshared[..]), &mut cipherstate1, &mut cipherstate2);
+	let mut cipherstate2 : CipherState<CipherChaChaPoly> = Default::default();
 
-	let now = time::get_time();
-	let mut tai64n = [0; 12];
-	BigEndian::write_i64(&mut tai64n[0..], 4611686018427387914ULL + now.sec);
-	BigEndian::write_i32(&mut tai64n[8..], now.nsec);
-	let mut initiation_packet = [0; 148];
-	initiation_packet[0] = 1; /* Type: Initiation */
-	initiation_packet[1] = 0; /* Reserved */
-	initiation_packet[2] = 0; /* Reserved */
-	initiation_packet[3] = 0; /* Reserved */
-	LittleEndian::write_u32(&mut initiation_packet[4..], 28); /* Sender index: 28 (arbitrary) */
-	handshake.write_message(&tai64n, &mut initiation_packet[8..]);
-	let mut mac_material = [0; 148];
-	memcpy(&mut mac_material, &their_public);
-	memcpy(&mut mac_material[32..], &initiation_packet[0..116]);
-	let mut mac = [0; 16];
-	Blake2s::blake2s(&mut mac, &mac_material, &my_preshared);
-	memcpy(&mut initiation_packet[116..], &mac);
-	socket.send_to(&initiation_packet, &send_addr).unwrap();
+	let their_index = {
+		let mut handshake = HandshakeState::new_from_owner(&mut owner, true, HandshakePattern::IK, "WireGuard v0 zx2c4 Jason@zx2c4.com".as_bytes(), Some(&my_preshared[..]), &mut cipherstate1, &mut cipherstate2);
 
-	let mut response_packet = [0; 92];
-	socket.recv_from(&mut response_packet).unwrap();
-	assert!(response_packet[0] == 2 /* Type: Response */);
-	assert!(response_packet[1] == 0 /* Reserved */);
-	assert!(response_packet[2] == 0 /* Reserved */);
-	assert!(response_packet[3] == 0 /* Reserved */);
-	let their_index = LittleEndian::read_u32(&response_packet[4..]);
-	let our_index = LittleEndian::read_u32(&response_packet[8..]);
-	assert!(our_index == 28);
-	let (payload_len, last) = handshake.read_message(&response_packet[12..60], &mut empty_payload).unwrap();
-	assert!(payload_len == 0 && last);
+		let now = time::get_time();
+		let mut tai64n = [0; 12];
+		BigEndian::write_i64(&mut tai64n[0..], 4611686018427387914 + now.sec);
+		BigEndian::write_i32(&mut tai64n[8..], now.nsec);
+		let mut initiation_packet = [0; 148];
+		initiation_packet[0] = 1; /* Type: Initiation */
+		initiation_packet[1] = 0; /* Reserved */
+		initiation_packet[2] = 0; /* Reserved */
+		initiation_packet[3] = 0; /* Reserved */
+		LittleEndian::write_u32(&mut initiation_packet[4..], 28); /* Sender index: 28 (arbitrary) */
+		handshake.write_message(&tai64n, &mut initiation_packet[8..]);
+		let mut mac_material = [0; 148];
+		memcpy(&mut mac_material, &their_public);
+		memcpy(&mut mac_material[32..], &initiation_packet[0..116]);
+		let mut mac = [0; 16];
+		Blake2s::blake2s(&mut mac, &mac_material, &my_preshared);
+		memcpy(&mut initiation_packet[116..], &mac);
+		socket.send_to(&initiation_packet, &send_addr).unwrap();
+
+		let mut response_packet = [0; 92];
+		socket.recv_from(&mut response_packet).unwrap();
+		assert!(response_packet[0] == 2 /* Type: Response */);
+		assert!(response_packet[1] == 0 /* Reserved */);
+		assert!(response_packet[2] == 0 /* Reserved */);
+		assert!(response_packet[3] == 0 /* Reserved */);
+		let their_index = LittleEndian::read_u32(&response_packet[4..]);
+		let our_index = LittleEndian::read_u32(&response_packet[8..]);
+		assert!(our_index == 28);
+		let (payload_len, last) = handshake.read_message(&response_packet[12..60], &mut empty_payload).unwrap();
+		assert!(payload_len == 0 && last);
+		their_index
+	};
 
 	let mut keepalive_packet = [0; 32];
 	keepalive_packet[0] = 4; /* Type: Data */
-- 
2.10.0

