File: test_http.py

package info (click to toggle)
rauc 1.15-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,336 kB
  • sloc: ansic: 36,989; python: 3,354; sh: 1,391; xml: 53; makefile: 41
file content (136 lines) | stat: -rw-r--r-- 3,981 bytes parent folder | download
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
import json
import uuid
from pathlib import Path

import pytest
from dasbus.typing import get_native, get_variant

from conftest import have_json, have_service
from helper import run


def test_backend_range(http_server):
    """Test if the backend returns the range parameters correctly."""
    http_server.setup(
        file_path="good-verity-bundle.raucb",
    )

    resp = http_server.get(headers={"Range": "bytes=0-3"})
    resp.raise_for_status()
    assert resp.status_code == 206
    assert resp.content == b"hsqs"

    summary = http_server.get_summary()
    assert summary["requests"] == 1
    assert summary["range_requests"] == ["0:4"]


def test_backend_headers(http_server):
    """Test if the backend returns the request headers correctly."""
    http_server.setup(
        file_path="good-verity-bundle.raucb",
    )

    resp = http_server.head(headers={"RAUC-Test": "value"})
    resp.raise_for_status()
    assert resp.status_code == 200

    summary = http_server.get_summary()
    assert summary["requests"] == 1
    assert summary["first_request_headers"].get("RAUC-Test") == "value"


def prune_standard_headers(headers):
    for k in ["Host", "X-Forwarded-For", "Connection", "Accept", "User-Agent"]:
        try:
            del headers[k]
        except KeyError:
            pass


def is_uuid(value):
    try:
        uuid.UUID(value)
    except ValueError:
        return False
    return True


def is_uptime(value):
    try:
        float(value)
    except ValueError:
        return False
    return True


@pytest.mark.parametrize("api", ["cli", "dbus"])
@have_json
def test_info_headers(create_system_files, system, http_server, api):
    """Test if the info command sends custom headers correctly."""
    if api == "dbus" and not have_service():
        pytest.skip("Missing service")

    system.prepare_minimal_config()
    system.config["handlers"] = {
        "system-info": "bin/systeminfo.sh",
    }
    system.config["streaming"] = {
        "send-headers": "boot-id;machine-id;system-version;serial;variant;transaction-id;uptime",
    }
    system.write_config()
    http_server.setup(
        file_path="good-verity-bundle.raucb",
    )

    if api == "cli":
        out, err, exitcode = run(
            f"{system.prefix} info {http_server.url} --output-format=json -H 'Test-Header: Test-Value'"
        )
        assert exitcode == 0
        info = json.loads(out)
        assert info["compatible"] == "Test Config"
    elif api == "dbus":
        with system.running_service("A"):
            info = system.proxy.InspectBundle(
                http_server.url,
                {
                    "http-headers": get_variant("as", ["Test-Header: Test-Value"]),
                },
            )
        info = get_native(info)
        assert info["update"]["compatible"] == "Test Config"

    summary = http_server.get_summary()
    assert summary["requests"] == 3

    first_headers = summary["first_request_headers"]
    assert first_headers.pop("User-Agent").startswith("rauc/")
    assert is_uuid(first_headers.pop("RAUC-Boot-ID"))
    machine_id = Path("/etc/machine-id")
    if machine_id.exists() and machine_id.stat().st_size > 0:
        assert is_uuid(first_headers.pop("RAUC-Machine-ID"))
    else:
        assert "RAUC-Machine-ID" not in first_headers
    assert is_uptime(first_headers.pop("RAUC-Uptime"))
    prune_standard_headers(first_headers)
    assert first_headers == {
        "Range": "bytes=0-3",
        "Test-Header": "Test-Value",
        "RAUC-Serial": "1234",
        "RAUC-System-Version": "1.0.0",
        "RAUC-Variant": "test-variant-x",
    }

    second_headers = summary["second_request_headers"]
    prune_standard_headers(second_headers)
    assert second_headers == {
        "Range": "bytes=26498-26505",
        "Test-Header": "Test-Value",
    }

    assert summary["range_requests"] == [
        "0:4",  # magic
        "26498:26506",  # CMS size
        "24576:26498",  # CMS data
    ]