aboutsummaryrefslogtreecommitdiff
path: root/tests/test_parse_simple_string.rs
blob: d2e30ab72380b558d2cf4b021ebad765adc60bdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use codecrafters_redis::resp_parser::*;

#[test]
fn test_valid_simple_strings() {
    // Basic valid cases
    assert_eq!(parse_simple_strings(b"+OK\r\n").unwrap().0, "OK");
    assert_eq!(parse_simple_strings(b"+PONG\r\n").unwrap().0, "PONG");
    assert_eq!(
        parse_simple_strings(b"+Hello World\r\n").unwrap().0,
        "Hello World"
    );

    // Empty string
    assert_eq!(parse_simple_strings(b"+\r\n").unwrap().0, "");

    // String with spaces and special characters (but no \r or \n)
    assert_eq!(
        parse_simple_strings(b"+Hello, World! 123\r\n").unwrap().0,
        "Hello, World! 123"
    );

    // String with various ASCII characters
    assert_eq!(
        parse_simple_strings(b"+!@#$%^&*()_+-={}[]|\\:;\"'<>?,./ \r\n")
            .unwrap()
            .0,
        "!@#$%^&*()_+-={}[]|\\:;\"'<>?,./ "
    );

    // Unicode characters (should work with UTF-8)
    assert_eq!(
        parse_simple_strings(b"+\xc3\xa9\xc3\xa1\xc3\xb1\r\n")
            .unwrap()
            .0,
        "éáñ"
    );
}

#[test]
fn test_full_resync_string() {
    let input = b"+FULLRESYNC 75cd7bc10c49047e0d163660f3b90625b1af31dc 0\r\n".to_vec();
    assert_eq!(
        parse(&input).unwrap().0,
        "FULLRESYNC 75cd7bc10c49047e0d163660f3b90625b1af31dc 0"
    )
}

#[test]
fn test_invalid_prefix() {
    // Missing '+' prefix
    assert_eq!(
        parse_simple_strings(b"OK\r\n").err().unwrap().message(),
        "WRONGTYPE Operation against a key holding the wrong kind of value"
    );

    // Wrong prefix
    assert_eq!(
        parse_simple_strings(b"-Error\r\n").err().unwrap().message(),
        "WRONGTYPE Operation against a key holding the wrong kind of value"
    );
    assert_eq!(
        parse_simple_strings(b":123\r\n").err().unwrap().message(),
        "WRONGTYPE Operation against a key holding the wrong kind of value"
    );
    assert_eq!(
        parse_simple_strings(b"$5\r\nhello\r\n")
            .err()
            .unwrap()
            .message(),
        "WRONGTYPE Operation against a key holding the wrong kind of value"
    );
}

#[test]
fn test_missing_crlf_terminator() {
    // No CRLF at all
    assert_eq!(
        parse_simple_strings(b"+OK").err().unwrap().message(),
        "ERR Unexpected end of input"
    );

    // Only \r
    assert_eq!(
        parse_simple_strings(b"+OK\r").err().unwrap().message(),
        "ERR Unexpected end of input"
    );

    // Only \n
    assert_eq!(
        parse_simple_strings(b"+OK\n").err().unwrap().message(),
        "ERR Unexpected end of input"
    );

    // Wrong order (\n\r instead of \r\n)
    assert_eq!(
        parse_simple_strings(b"+OK\n\r").err().unwrap().message(),
        "ERR Unexpected end of input"
    );
}

#[test]
fn test_invalid_characters_in_content() {
    // Contains \r in content
    assert_eq!(
        parse_simple_strings(b"+Hello\rWorld\r\n")
            .err()
            .unwrap()
            .message(),
        "ERR invalid value"
    );

    // Contains \n in content
    assert_eq!(
        parse_simple_strings(b"+Hello\nWorld\r\n")
            .err()
            .unwrap()
            .message(),
        "ERR invalid value"
    );
}

#[test]
fn test_empty_input() {
    assert_eq!(
        parse_simple_strings(b"").err().unwrap().message(),
        "ERR Empty data"
    );
}

#[test]
fn test_with_trailing_data() {
    // RESP simple string with extra data after CRLF (should be ignored)
    assert_eq!(parse_simple_strings(b"+OK\r\nextra_data").unwrap().0, "OK");
    assert_eq!(
        parse_simple_strings(b"+PONG\r\n+another_string\r\n")
            .unwrap()
            .0,
        "PONG"
    );
}

#[test]
fn test_real_world_redis_responses() {
    // Common Redis simple string responses
    assert_eq!(parse_simple_strings(b"+OK\r\n").unwrap().0, "OK");
    assert_eq!(parse_simple_strings(b"+PONG\r\n").unwrap().0, "PONG");
    assert_eq!(parse_simple_strings(b"+QUEUED\r\n").unwrap().0, "QUEUED");

    // Redis status responses
    assert_eq!(
        parse_simple_strings(b"+Background saving started\r\n")
            .unwrap()
            .0,
        "Background saving started"
    );
    assert_eq!(
        parse_simple_strings(b"+Background saving successfully finished\r\n")
            .unwrap()
            .0,
        "Background saving successfully finished"
    );
}

#[test]
fn test_edge_cases() {
    // Just the prefix and CRLF
    assert_eq!(parse_simple_strings(b"+\r\n").unwrap().0, "");

    // Long string
    let long_string = "a".repeat(1000);
    let mut input = b"+".to_vec();
    input.extend_from_slice(long_string.as_bytes());
    input.extend_from_slice(b"\r\n");
    assert_eq!(parse_simple_strings(&input).unwrap().0, long_string);

    // String with only spaces
    assert_eq!(parse_simple_strings(b"+   \r\n").unwrap().0, "   ");

    // String with tabs and other whitespace
    assert_eq!(parse_simple_strings(b"+\t  \t\r\n").unwrap().0, "\t  \t");
}

#[test]
fn test_binary_safety_within_limits() {
    // Non-UTF8 bytes (but no \r or \n)
    let mut input = b"+".to_vec();
    input.extend_from_slice(&[0xFF, 0xFE, 0xFD]); // Invalid UTF-8
    input.extend_from_slice(b"\r\n");

    // Should handle invalid UTF-8 gracefully with replacement characters
    if let RespType::SimpleString(data) = parse_simple_strings(&input).unwrap().0 {
        assert!(!data.is_empty()); // Should contain replacement characters
    }
}