File: test_integration.py

package info (click to toggle)
swugenerator 0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 308 kB
  • sloc: python: 1,429; sh: 105; makefile: 14
file content (279 lines) | stat: -rw-r--r-- 7,845 bytes parent folder | download | duplicates (2)
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
# pylint: disable=C0114,C0116,W0621
"""This file hosts integration tests to ensure tool creates valid SWUs"""
from pathlib import Path
import pytest
import shutil

import libarchive

from swugenerator import main

VALID_KEY = "390ad54490a4a5f53722291023c19e08ffb5c4677a59e958c96ffa6e641df040"
VALID_IV = "d5d601bacfe13100b149177318ebc7a4"
UPDATE_FILES = [
    "rootfs.ubifs",
    "swupdate.ext3.gz.u-boot",
    "sdcard.ext3.gz",
    "bootlogo.bmp",
    "uImage.bin",
    "fpga.txt",
    "bootloader-env",
    "README",
    "erase_at_end",
    "display_info",
]


@pytest.fixture()
def input_directory(tmp_path_factory):
    """Creates a directory to test"""
    return tmp_path_factory.mktemp("input_files")


@pytest.fixture()
def output_directory(tmp_path_factory):
    """Creates a directory to test"""
    return tmp_path_factory.mktemp("output_files")


@pytest.fixture()
def artifactory(input_directory):
    """Creates test files that match the filenames in the sw-description"""
    for filename in UPDATE_FILES:
        file = input_directory / filename
        with file.open("w") as file_fd:
            file_fd.write("THIS IS A TEST UPDATE FILE\n")
    return input_directory.resolve()


def copy_to_test_dir(filename, test_dir) -> Path:
    """Helper function to copy integration test inputs to test directory"""
    source_path = Path(__file__).parent / "test_data/integration_input" / filename
    dest_path = test_dir / filename
    shutil.copy(source_path, dest_path)
    return dest_path.resolve()


@pytest.fixture()
def sw_description_template(input_directory):
    """Copies sw-description template to test directory"""
    filename = "sw-description.in"
    return copy_to_test_dir(filename, input_directory)


@pytest.fixture()
def encrypted_sw_description_template(input_directory):
    """Copies sw-description template to test directory"""
    filename = "enc-sw-description.in"
    return copy_to_test_dir(filename, input_directory)


@pytest.fixture()
def config_file(input_directory):
    """Copies config file to test directory"""
    filename = "config"
    return copy_to_test_dir(filename, input_directory)


@pytest.fixture()
def signing_key(input_directory):
    """Creates signing key"""
    filename = "private.pem"
    return copy_to_test_dir(filename, input_directory)


@pytest.fixture()
def encryption_key(input_directory):
    """Creates encryption key"""
    encryption_key_filename = "valid_key.txt"
    encryption_key_path = input_directory / encryption_key_filename
    with encryption_key_path.open("w") as key_key_fd:
        key_key_fd.write(f"key={VALID_KEY}\niv={VALID_IV}")
    return encryption_key_path


def validate_swu(output_swu, encrypted=False):
    """Helper function to validate the output file against a pre-generated output file"""
    reference_swu = (
        Path(__file__).parent / "test_data/integration_output" / output_swu.name
    )
    with libarchive.Archive(output_swu.open()) as output, libarchive.Archive(
        reference_swu.open()
    ) as reference:
        for output_file, reference_file in zip(output, reference):
            if output_file.pathname != reference_file.pathname:
                print("Names do not match")
                return False
            if output_file.size != reference_file.size:
                print("Sizes do not match")
                return False
            if not encrypted and output.read(output_file.size) != reference.read(
                output_file.size
            ):
                print("File contents are not equal")
                return False
    return True


def test_basic_command_creates_valid_swu(
    artifactory, sw_description_template, config_file, output_directory
):
    output_file = "output.swu"
    command_args = [
        "-s",
        str(sw_description_template),
        "-a",
        str(artifactory),
        "-c",
        str(config_file),
        "-o",
        str((output_directory / output_file).resolve()),
        "create",
    ]
    main.parse_args(command_args)
    assert validate_swu(output_directory / output_file)


def test_command_with_sign_flag_creates_valid_signed_swu(
    artifactory, sw_description_template, config_file, signing_key, output_directory
):
    output_file = "signed_output.swu"
    command_args = [
        "-s",
        str(sw_description_template),
        "-a",
        str(artifactory),
        "-c",
        str(config_file),
        "-k",
        f"RSA,{signing_key}",
        "-o",
        str((output_directory / output_file).resolve()),
        "create",
    ]
    main.parse_args(command_args)
    assert validate_swu(output_directory / output_file)


def test_command_with_encryption_flag_creates_encrypted_swu(
    artifactory,
    encrypted_sw_description_template,
    config_file,
    encryption_key,
    output_directory,
):
    output_file = "output.swu.enc"
    command_args = [
        "-s",
        str(encrypted_sw_description_template),
        "-a",
        str(artifactory),
        "-c",
        str(config_file),
        "-K",
        str(encryption_key),
        "-o",
        str((output_directory / output_file).resolve()),
        "create",
    ]
    main.parse_args(command_args)
    assert validate_swu(output_directory / output_file, encrypted=True)


def test_command_with_sign_flag_and_encrypt_flag_creates_signed_encrypted_swu(
    artifactory,
    encrypted_sw_description_template,
    config_file,
    signing_key,
    encryption_key,
    output_directory,
):
    output_file = "signed_output.swu.enc"
    command_args = [
        "-s",
        str(encrypted_sw_description_template),
        "-a",
        str(artifactory),
        "-c",
        str(config_file),
        "-k",
        f"RSA,{signing_key}",
        "-K",
        str(encryption_key),
        "-o",
        str((output_directory / output_file).resolve()),
        "create",
    ]
    main.parse_args(command_args)
    assert validate_swu(output_directory / output_file, encrypted=True)

def test_creates_and_signs_valid_swu(
    artifactory, sw_description_template, config_file,
    signing_key, output_directory
):
    output_file = "output.swu"
    command_args = [
        "-s",
        str(sw_description_template),
        "-a",
        str(artifactory),
        "-c",
        str(config_file),
        "-o",
        str((output_directory / output_file).resolve()),
        "create",
    ]
    main.parse_args(command_args)
    signed_output_file = "signed_output.swu"
    command_args = [
        "-o",
        str((output_directory / signed_output_file).resolve()),
        "-k",
        f"RSA,{signing_key}",
        "sign",
        "-i",
        str((output_directory / output_file).resolve()),
    ]
    main.parse_args(command_args)

    assert validate_swu(output_directory / signed_output_file)


def test_with_encryption_flag_creates_encrypted_and_sign_swu(
    artifactory,
    encrypted_sw_description_template,
    config_file,
    encryption_key,
    signing_key,
    output_directory,
):
    output_file = "output.swu.enc"
    command_args = [
        "-s",
        str(encrypted_sw_description_template),
        "-a",
        str(artifactory),
        "-c",
        str(config_file),
        "-K",
        str(encryption_key),
        "-o",
        str((output_directory / output_file).resolve()),
        "create",
    ]
    main.parse_args(command_args)
    assert validate_swu(output_directory / output_file, encrypted=True)

    signed_output_file = "signed_output.swu.enc"
    command_args = [
        "-o",
        str((output_directory / signed_output_file).resolve()),
        "-k",
        f"RSA,{signing_key}",
        "sign",
        "-i",
        str((output_directory / output_file).resolve()),
    ]
    main.parse_args(command_args)

    assert validate_swu(output_directory / signed_output_file, encrypted=True)