aboutsummaryrefslogtreecommitdiff
path: root/pw_transfer/integration_test/legacy_binaries_test.py
blob: 671260911a1c21adb3cadd25b701cf9a6522369b (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# Copyright 2022 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""A variety of transfer tests that validate backwards compatibility.

Usage:

   bazel run pw_transfer/integration_test:legacy_binaries_test

Command-line arguments must be provided after a double-dash:

   bazel run pw_transfer/integration_test:legacy_binaries_test -- \
       --server-port 3304

Which tests to run can be specified as command-line arguments:

  bazel run pw_transfer/integration_test:legacy_binaries_test -- \
      LegacyClientTransferIntegrationTests.test_small_client_write_0_cpp

"""

import itertools
from parameterized import parameterized
import random

from pigweed.pw_transfer.integration_test import config_pb2
from pigweed.pw_transfer.integration_test import test_fixture
from test_fixture import TransferIntegrationTestHarness
from rules_python.python.runfiles import runfiles

# Each set of transfer tests uses a different client/server port pair to
# allow tests to be run in parallel.
_SERVER_PORT = 3314
_CLIENT_PORT = 3315


# NOTE: These backwards compatibility tests DO NOT include tests that verify
# expected error cases (e.g. timeouts, unknown resource ID) because legacy
# integration test clients did not support the ability to check those.
# Additionally, there are deliberately NOT back-to-back read/write transfers
# because of known issues with transfer cleanup in the legacy transfer protocol.
class LegacyTransferIntegrationTest(test_fixture.TransferIntegrationTest):
    """This base class defines the tests to run, but isn't run directly."""

    # Explicitly use UNKNOWN_VERSION (the default value of
    # TransferAction.protocol_version), as that will cause protocol_version to
    # be omitted from the generated text proto, which is critical for the legacy
    # client that doesn't support transfer version specifications (and will
    # cause a proto parse error).
    PROTOCOL_VERSION = config_pb2.TransferAction.ProtocolVersion.UNKNOWN_VERSION
    LEGACY_SERVER = False
    LEGACY_CLIENT = False

    def default_config(self) -> test_fixture.TransferConfig:
        # The legacy binaries aren't aware of the max_lifetime_retries field,
        # which was added more recently. Clear it so it isn't encoded into the
        # serialized message.
        config = super().default_config()
        config.client.max_lifetime_retries = 0
        return config

    @parameterized.expand(
        [
            ("cpp"),
            ("java"),
            ("python"),
        ]
    )
    def test_single_byte_client_write(self, client_type):
        if not (self.LEGACY_SERVER or self.LEGACY_CLIENT):
            self.skipTest("No legacy binary in use, skipping")

        if not self.LEGACY_SERVER and (
            client_type == "java" or client_type == "python"
        ):
            self.skipTest("Java and Python legacy clients not yet set up")

        payload = b"?"
        config = self.default_config()
        resource_id = 5
        self.do_single_write(
            "cpp", config, resource_id, payload, self.PROTOCOL_VERSION
        )

    @parameterized.expand(
        [
            ("cpp"),
            ("java"),
            ("python"),
        ]
    )
    def test_small_client_write(self, client_type):
        if not (self.LEGACY_SERVER or self.LEGACY_CLIENT):
            self.skipTest("No legacy binary in use, skipping")

        if not self.LEGACY_SERVER and (
            client_type == "java" or client_type == "python"
        ):
            self.skipTest("Java and Python legacy clients not yet set up")

        payload = b"some data"
        config = self.default_config()
        resource_id = 5
        self.do_single_write(
            "cpp", config, resource_id, payload, self.PROTOCOL_VERSION
        )

    @parameterized.expand(
        [
            ("cpp"),
            ("java"),
            ("python"),
        ]
    )
    def test_medium_hdlc_escape_client_write(self, client_type):
        if not (self.LEGACY_SERVER or self.LEGACY_CLIENT):
            self.skipTest("No legacy binary in use, skipping")

        if not self.LEGACY_SERVER and (
            client_type == "java" or client_type == "python"
        ):
            self.skipTest("Java and Python legacy clients not yet set up")

        payload = b"~" * 8731
        config = self.default_config()
        resource_id = 12345678
        self.do_single_write(
            "cpp", config, resource_id, payload, self.PROTOCOL_VERSION
        )

    @parameterized.expand(
        [
            ("cpp"),
            ("java"),
            ("python"),
        ]
    )
    def test_medium_random_data_client_write(self, client_type):
        if not (self.LEGACY_SERVER or self.LEGACY_CLIENT):
            self.skipTest("No legacy binary in use, skipping")

        if not self.LEGACY_SERVER and (
            client_type == "java" or client_type == "python"
        ):
            self.skipTest("Java and Python legacy clients not yet set up")

        rng = random.Random(1533659510898)
        payload = rng.randbytes(13713)
        config = self.default_config()
        resource_id = 12345678
        self.do_single_write(
            "cpp", config, resource_id, payload, self.PROTOCOL_VERSION
        )

    @parameterized.expand(
        [
            ("cpp"),
            ("java"),
            ("python"),
        ]
    )
    def test_single_byte_client_read(self, client_type):
        if not (self.LEGACY_SERVER or self.LEGACY_CLIENT):
            self.skipTest("No legacy binary in use, skipping")

        if not self.LEGACY_SERVER and (
            client_type == "java" or client_type == "python"
        ):
            self.skipTest("Java and Python legacy clients not yet set up")

        payload = b"?"
        config = self.default_config()
        resource_id = 5
        self.do_single_read(
            "cpp", config, resource_id, payload, self.PROTOCOL_VERSION
        )

    @parameterized.expand(
        [
            ("cpp"),
            ("java"),
            ("python"),
        ]
    )
    def test_small_client_read(self, client_type):
        if not (self.LEGACY_SERVER or self.LEGACY_CLIENT):
            self.skipTest("No legacy binary in use, skipping")

        if not self.LEGACY_SERVER and (
            client_type == "java" or client_type == "python"
        ):
            self.skipTest("Java and Python legacy clients not yet set up")

        payload = b"some data"
        config = self.default_config()
        resource_id = 5
        self.do_single_read(
            "cpp", config, resource_id, payload, self.PROTOCOL_VERSION
        )

    @parameterized.expand(
        [
            ("cpp"),
            ("java"),
            ("python"),
        ]
    )
    def test_medium_hdlc_escape_client_read(self, client_type):
        if not (self.LEGACY_SERVER or self.LEGACY_CLIENT):
            self.skipTest("No legacy binary in use, skipping")

        if self.LEGACY_SERVER:
            self.skipTest("Legacy server has HDLC buffer sizing issues")

        payload = b"~" * 8731
        config = self.default_config()
        resource_id = 5
        self.do_single_read(
            "cpp", config, resource_id, payload, self.PROTOCOL_VERSION
        )

    @parameterized.expand(
        [
            ("cpp"),
            ("java"),
            ("python"),
        ]
    )
    def test_medium_random_data_client_read(self, client_type):
        if not (self.LEGACY_SERVER or self.LEGACY_CLIENT):
            self.skipTest("No legacy binary in use, skipping")

        if self.LEGACY_SERVER:
            self.skipTest("Legacy server has HDLC buffer sizing issues")

        rng = random.Random(1533659510898)
        payload = rng.randbytes(13713)
        config = self.default_config()
        resource_id = 5
        self.do_single_read(
            "cpp", config, resource_id, payload, self.PROTOCOL_VERSION
        )


class LegacyClientTransferIntegrationTests(LegacyTransferIntegrationTest):
    r = runfiles.Create()
    client_binary = r.Rlocation(
        "pw_transfer_test_binaries/cpp_client_528098d5",
        "pw_transfer_test_binaries",
    )
    HARNESS_CONFIG = TransferIntegrationTestHarness.Config(
        cpp_client_binary=client_binary,
        server_port=_SERVER_PORT,
        client_port=_CLIENT_PORT,
    )
    LEGACY_CLIENT = True


class LegacyServerTransferIntegrationTests(LegacyTransferIntegrationTest):
    r = runfiles.Create()
    server_binary = r.Rlocation(
        "pw_transfer_test_binaries/server_528098d5", "pw_transfer_test_binaries"
    )
    HARNESS_CONFIG = TransferIntegrationTestHarness.Config(
        server_binary=server_binary,
        server_port=_SERVER_PORT,
        client_port=_CLIENT_PORT,
    )
    LEGACY_SERVER = True


if __name__ == '__main__':
    test_fixture.run_tests_for(LegacyClientTransferIntegrationTests)
    test_fixture.run_tests_for(LegacyServerTransferIntegrationTests)