rust/hedgewars-network-protocol/tests/test.rs
author unc0rr
Wed, 30 Jun 2021 00:18:53 +0200
changeset 15810 ee84e417d8d0
parent 15804 747278149393
child 15811 a855f32ab3ca
permissions -rw-r--r--
Add parser and idempotention tests for server messages
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     1
use proptest::{
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     2
    arbitrary::any,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     3
    proptest,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     4
    strategy::{BoxedStrategy, Just, Strategy},
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     5
};
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     6
15810
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
     7
use hedgewars_network_protocol::messages::{HwProtocolMessage, HwServerMessage};
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
     8
use hedgewars_network_protocol::parser::{message, server_message};
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     9
use hedgewars_network_protocol::types::{GameCfg, ServerVar, TeamInfo, VoteType};
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    10
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    11
use hedgewars_network_protocol::types::testing::*;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    12
use hedgewars_network_protocol::{proto_msg_case, proto_msg_match};
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    13
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    14
pub fn gen_proto_msg() -> BoxedStrategy<HwProtocolMessage> where {
15810
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    15
    use hedgewars_network_protocol::messages::HwProtocolMessage::*;
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    16
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    17
    let res = (0..=55).no_shrink().prop_flat_map(|i| {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    18
        proto_msg_match!(i, def = Ping,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    19
            0 => Ping(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    20
            1 => Pong(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    21
            2 => Quit(Option<Ascii>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    22
            4 => Global(Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    23
            5 => Watch(u32),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    24
            6 => ToggleServerRegisteredOnly(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    25
            7 => SuperPower(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    26
            8 => Info(Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    27
            9 => Nick(Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    28
            10 => Proto(u16),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    29
            11 => Password(Ascii, Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    30
            12 => Checker(u16, Ascii, Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    31
            13 => List(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    32
            14 => Chat(Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    33
            15 => CreateRoom(Ascii, Option<Ascii>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    34
            16 => JoinRoom(Ascii, Option<Ascii>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    35
            17 => Follow(Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    36
            18 => Rnd(Vec<Ascii>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    37
            19 => Kick(Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    38
            20 => Ban(Ascii, Ascii, u32),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    39
            21 => BanIp(Ascii, Ascii, u32),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    40
            22 => BanNick(Ascii, Ascii, u32),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    41
            23 => BanList(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    42
            24 => Unban(Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    43
            25 => SetServerVar(ServerVar),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    44
            26 => GetServerVar(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    45
            27 => RestartServer(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    46
            28 => Stats(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    47
            29 => Part(Option<Ascii>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    48
            30 => Cfg(GameCfg),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    49
            31 => AddTeam(Box<TeamInfo>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    50
            32 => RemoveTeam(Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    51
            33 => SetHedgehogsNumber(Ascii, u8),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    52
            34 => SetTeamColor(Ascii, u8),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    53
            35 => ToggleReady(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    54
            36 => StartGame(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    55
            37 => EngineMessage(Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    56
            38 => RoundFinished(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    57
            39 => ToggleRestrictJoin(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    58
            40 => ToggleRestrictTeams(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    59
            41 => ToggleRegisteredOnly(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    60
            42 => RoomName(Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    61
            43 => Delegate(Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    62
            44 => TeamChat(Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    63
            45 => MaxTeams(u8),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    64
            46 => Fix(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    65
            47 => Unfix(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    66
            48 => Greeting(Option<Ascii>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    67
            49 => CallVote(Option<VoteType>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    68
            50 => Vote(bool),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    69
            51 => ForceVote(bool),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    70
            52 => Save(Ascii, Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    71
            53 => Delete(Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    72
            54 => SaveRoom(Ascii),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    73
            55 => LoadRoom(Ascii)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    74
        )
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    75
    });
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    76
    res.boxed()
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    77
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    78
15810
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    79
pub fn gen_server_msg() -> BoxedStrategy<HwServerMessage> where {
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    80
    use hedgewars_network_protocol::messages::HwServerMessage::*;
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    81
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    82
    let res = (0..=55).no_shrink().prop_flat_map(|i| {
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    83
        proto_msg_match!(i, def = Ping,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    84
                    0 => Connected(Ascii, u32),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    85
                    1 => Redirect(u16),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    86
                    2 => Ping(),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    87
                    3 => Pong(),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    88
                    4 => Bye(Ascii),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    89
                    5 => Nick(Ascii),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    90
                    6 => Proto(u16),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    91
                    7 => AskPassword(Ascii),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    92
                    8 => ServerAuth(Ascii),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    93
                    9 => LogonPassed(),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    94
                    10 => LobbyLeft(Ascii, Ascii),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    95
                    11 => LobbyJoined(Vec<Ascii>),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    96
        //            12 => ChatMsg { Ascii, Ascii },
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    97
                    13 => ClientFlags(Ascii, Vec<Ascii>),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    98
                    14 => Rooms(Vec<Ascii>),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
    99
                    15 => RoomAdd(Vec<Ascii>),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   100
                    16=> RoomJoined(Vec<Ascii>),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   101
                    17 => RoomLeft(Ascii, Ascii),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   102
                    18 => RoomRemove(Ascii),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   103
                    19 => RoomUpdated(Ascii, Vec<Ascii>),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   104
                    20 => Joining(Ascii),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   105
                    21 => TeamAdd(Vec<Ascii>),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   106
                    22 => TeamRemove(Ascii),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   107
                    23 => TeamAccepted(Ascii),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   108
                    24 => TeamColor(Ascii, u8),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   109
                    25 => HedgehogsNumber(Ascii, u8),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   110
                    26 => ConfigEntry(Ascii, Vec<Ascii>),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   111
                    27 => Kicked(),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   112
                    28 => RunGame(),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   113
                    29 => ForwardEngineMessage(Vec<Ascii>),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   114
                    30 => RoundFinished(),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   115
                    31 => ReplayStart(),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   116
                    32 => Info(Vec<Ascii>),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   117
                    33 => ServerMessage(Ascii),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   118
                    34 => ServerVars(Vec<Ascii>),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   119
                    35 => Notice(Ascii),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   120
                    36 => Warning(Ascii),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   121
                    37 => Error(Ascii)
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   122
                )
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   123
    });
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   124
    res.boxed()
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   125
}
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   126
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   127
proptest! {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   128
    #[test]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   129
    fn is_parser_composition_idempotent(ref msg in gen_proto_msg()) {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   130
        println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes());
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   131
        assert_eq!(message(msg.to_raw_protocol().as_bytes()), Ok((&b""[..], msg.clone())))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   132
    }
15810
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   133
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   134
    #[test]
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   135
    fn is_server_message_parser_composition_idempotent(ref msg in gen_server_msg()) {
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   136
        println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes());
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   137
        assert_eq!(server_message(msg.to_raw_protocol().as_bytes()), Ok((&b""[..], msg.clone())))
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   138
    }
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   139
}