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

Chore(protocol): Fix MySQL OK and EOF protocol length condition. #160

Merged
merged 3 commits into from
Jul 4, 2022
Merged
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
129 changes: 127 additions & 2 deletions pisa-proxy/protocol/mysql/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,16 @@ pub fn length_encoded_string(data: &mut BytesMut) -> (Vec<u8>, bool) {
(data.split_to(num as usize).to_vec(), false)
}

// https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_basic_eof_packet.html
#[inline]
pub fn is_eof(data: &[u8]) -> bool {
data.len() < 9 + 4 && *unsafe { data.get_unchecked(4) } == EOF_HEADER
data.len() < 9 && *unsafe { data.get_unchecked(4) } == EOF_HEADER
}

// https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_basic_ok_packet.html
#[inline]
pub fn is_ok(data: &[u8]) -> bool {
data.len() > 7 + 4 && *unsafe { data.get_unchecked(4) } == OK_HEADER
data.len() > 7 && *unsafe { data.get_unchecked(4) } == OK_HEADER
}

#[inline]
Expand All @@ -208,9 +210,83 @@ pub fn get_length(buf: &[u8]) -> usize {
#[cfg(test)]
mod test {
use bytes::BytesMut;
use crate::util::{calc_caching_sha2password, calc_password, compare, get_length, is_eof, is_ok, length_encode_int, random_buf};

use super::{length_encoded_string, BufExt};

#[test]
fn test_random_buf() {
let result = random_buf(6);
assert_eq!(result.len(), 6);
}

#[test]
fn test_calc_password() {
let scramble = [0x70, 0x69, 0x73, 0x61, 0x6e, 0x69, 0x78];
let password = [0x31, 0x32, 0x33, 0x34, 0x35, 0x36];
let calc_password = calc_password(&scramble[..], &password[..]);
let result = [139, 87, 122, 170, 122, 110, 60, 78, 2, 63, 208, 152, 19, 86, 207, 190, 178, 51, 61, 127];
assert_eq!(calc_password, &result[..])
}

#[test]
fn test_calc_caching_sha2password() {
let scramble = [0x70, 0x69, 0x73, 0x61, 0x6e, 0x69, 0x78];
let password = [0x31, 0x32, 0x33, 0x34, 0x35, 0x36];
let calc_password = calc_caching_sha2password(&scramble[..], &password[..]);
let result = [97, 231, 153, 111, 85, 161, 188, 166, 190, 240, 239, 147, 138, 193, 141, 190, 194, 120, 170, 210, 235, 241, 79, 175, 198, 189, 36, 193, 105, 166, 179, 173];
assert_eq!(calc_password, &result[..])
}

#[test]
fn test_compare_success() {
let scramble = [0x31, 0x32, 0x33, 0x34, 0x35, 0x36];
let password = [0x31, 0x32, 0x33, 0x34, 0x35, 0x36];
let result = compare(&scramble[..], &password[..]);
assert_eq!(result, true)
}

#[test]
fn test_compare_fail() {
let scramble = [0x30, 0x32, 0x33, 0x34, 0x35, 0x36];
let password = [0x31, 0x32, 0x33, 0x34, 0x35, 0x36];
let result = compare(&scramble[..], &password[..]);
assert_eq!(result, false)
}

#[test]
fn test_length_encode_int() {
let data = [0xfb, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37];
let (a, b, c) = length_encode_int(&data[..]);
assert_eq!(a, 0);
assert_eq!(b, true);
assert_eq!(c, 1);

let data = [0xfc, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37];
let (a, b, c) = length_encode_int(&data[..]);
assert_eq!(a, 12796);
assert_eq!(b, false);
assert_eq!(c, 3);

let data = [0xfd, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37];
let (a, b, c) = length_encode_int(&data[..]);
assert_eq!(a, 3289597);
assert_eq!(b, false);
assert_eq!(c, 4);

let data = [0xfe, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37];
let (a, b, c) = length_encode_int(&data[..]);
assert_eq!(a, 3978425819141911038);
assert_eq!(b, false);
assert_eq!(c, 9);

let data = [0x00, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37];
let (a, b, c) = length_encode_int(&data[..]);
assert_eq!(a, 0);
assert_eq!(b, false);
assert_eq!(c, 1);
}

#[test]
fn test_length_enc_string() {
let data = [0x04, 0x55, 0x73, 0x65, 0x72];
Expand All @@ -230,4 +306,53 @@ mod test {
let name = std::str::from_utf8(&info).unwrap();
assert_eq!(name, "User");
}

#[test]
fn test_is_eof_success() {
let data = [0x05, 0x00, 0x00, 0x05, 0xfe, 0x00, 0x00];
let result = is_eof(&data[..]);
assert_eq!(result, true);
}

#[test]
fn test_is_eof_data_error() {
let data = [0x05, 0x00, 0x00, 0x05, 0xff, 0x00, 0x00, 0x02, 0x00];
let result = is_eof(&data[..]);
assert_eq!(result, false);
}

#[test]
fn test_is_eof_length_error() {
let data = [0x05, 0x00, 0x00, 0x05, 0xfe, 0x00, 0x00, 0x02, 0x00];
let result = is_eof(&data[..]);
assert_eq!(result, false);
}

#[test]
fn test_is_ok_success() {
let data = [0x07, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00];
let result = is_ok(&data[..]);
assert_eq!(result, true);
}

#[test]
fn test_is_ok_data_error() {
let data = [0x05, 0x00, 0x00, 0x05, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00];
let result = is_ok(&data[..]);
assert_eq!(result, false);
}

#[test]
fn test_is_ok_length_error() {
let data = [0x05, 0x00, 0x00, 0x05, 0x00, 0x00];
let result = is_eof(&data[..]);
assert_eq!(result, false);
}

#[test]
fn test_get_length() {
let data = [0x05, 0x00, 0x00, 0x05, 0x00, 0x00];
let result = get_length(&data[..]);
assert_eq!(result, 5);
}
}