| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
use std::{cell::RefCell, rc::Rc}; |
| 8 |
|
| 9 |
use neqo_crypto::{ |
| 10 |
constants::{HandshakeMessage, TLS_HS_CLIENT_HELLO, TLS_HS_ENCRYPTED_EXTENSIONS}, |
| 11 |
ext::{ExtensionHandler, ExtensionHandlerResult, ExtensionWriterResult}, |
| 12 |
Client, Server, |
| 13 |
}; |
| 14 |
use test_fixture::fixture_init; |
| 15 |
|
| 16 |
mod handshake; |
| 17 |
use crate::handshake::connect; |
| 18 |
|
| 19 |
struct NoopExtensionHandler; |
| 20 |
impl ExtensionHandler for NoopExtensionHandler {} |
| 21 |
|
| 22 |
// This test just handshakes. It doesn't really do anything about capturing the |
| 23 |
#[test] |
| 24 |
fn noop_extension_handler() { |
| 25 |
fixture_init(); |
| 26 |
let mut client = Client::new("server.example", true).expect("should create client"); |
| 27 |
let mut server = Server::new(&["key"]).expect("should create server"); |
| 28 |
|
| 29 |
client |
| 30 |
.extension_handler(0xffff, Rc::new(RefCell::new(NoopExtensionHandler))) |
| 31 |
.expect("installed"); |
| 32 |
server |
| 33 |
.extension_handler(0xffff, Rc::new(RefCell::new(NoopExtensionHandler))) |
| 34 |
.expect("installed"); |
| 35 |
|
| 36 |
connect(&mut client, &mut server); |
| 37 |
} |
| 38 |
|
| 39 |
#[derive(Debug, Default)] |
| 40 |
struct SimpleExtensionHandler { |
| 41 |
written: bool, |
| 42 |
handled: bool, |
| 43 |
} |
| 44 |
|
| 45 |
impl SimpleExtensionHandler { |
| 46 |
pub const fn negotiated(&self) -> bool { |
| 47 |
self.written && self.handled |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
impl ExtensionHandler for SimpleExtensionHandler { |
| 52 |
fn write(&mut self, msg: HandshakeMessage, d: &mut [u8]) -> ExtensionWriterResult { |
| 53 |
match msg { |
| 54 |
TLS_HS_CLIENT_HELLO | TLS_HS_ENCRYPTED_EXTENSIONS => { |
| 55 |
self.written = true; |
| 56 |
d[0] = 77; |
| 57 |
ExtensionWriterResult::Write(1) |
| 58 |
} |
| 59 |
_ => ExtensionWriterResult::Skip, |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
fn handle(&mut self, msg: HandshakeMessage, d: &[u8]) -> ExtensionHandlerResult { |
| 64 |
match msg { |
| 65 |
TLS_HS_CLIENT_HELLO | TLS_HS_ENCRYPTED_EXTENSIONS => { |
| 66 |
self.handled = true; |
| 67 |
if d.len() != 1 { |
| 68 |
ExtensionHandlerResult::Alert(50) // decode_error |
| 69 |
} else if d[0] == 77 { |
| 70 |
ExtensionHandlerResult::Ok |
| 71 |
} else { |
| 72 |
ExtensionHandlerResult::Alert(47) // illegal_parameter |
| 73 |
} |
| 74 |
} |
| 75 |
_ => ExtensionHandlerResult::Alert(110), // unsupported_extension |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
#[test] |
| 81 |
fn simple_extension() { |
| 82 |
fixture_init(); |
| 83 |
let mut client = Client::new("server.example", true).expect("should create client"); |
| 84 |
let mut server = Server::new(&["key"]).expect("should create server"); |
| 85 |
|
| 86 |
let client_handler = Rc::new(RefCell::new(SimpleExtensionHandler::default())); |
| 87 |
let ch = Rc::clone(&client_handler); |
| 88 |
client |
| 89 |
.extension_handler(0xffff, ch) |
| 90 |
.expect("client handler installed"); |
| 91 |
let server_handler = Rc::new(RefCell::new(SimpleExtensionHandler::default())); |
| 92 |
let sh = Rc::clone(&server_handler); |
| 93 |
server |
| 94 |
.extension_handler(0xffff, sh) |
| 95 |
.expect("server handler installed"); |
| 96 |
|
| 97 |
connect(&mut client, &mut server); |
| 98 |
|
| 99 |
assert!(client_handler.borrow().negotiated()); |
| 100 |
assert!(server_handler.borrow().negotiated()); |
| 101 |
} |