aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/tests/messages/message_fixture.cpp
blob: e134f62cfbab59be081dce0a73fd029b6e378223 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "message_fixture.h"
#include <vespa/document/repo/documenttyperepo.h>
#include <vespa/document/base/testdocrepo.h>
#include <vespa/vespalib/util/exception.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/testkit/test_path.h>
#include <fcntl.h>
#include <unistd.h>
#include <cassert>
#include <filesystem>

#include <vespa/log/log.h>
LOG_SETUP(".message_fixture");

namespace documentapi {

using document::DocumentTypeRepo;
using document::readDocumenttypesConfig;
using namespace vespalib::make_string_short;

MessageFixture::MessageFixture()
    : _repo(std::make_shared<DocumentTypeRepo>(readDocumenttypesConfig(TEST_PATH("../../../test/cfg/testdoctypes.cfg")))),
      _data_path(TEST_PATH("../../../test/crosslanguagefiles")),
      _protocol(_repo)
{
}

MessageFixture::~MessageFixture() = default;

mbus::Blob
MessageFixture::truncate(mbus::Blob data, size_t bytes)
{
    assert(data.size() > bytes);
    mbus::Blob res(data.size() - bytes);
    memcpy(res.data(), data.data(), res.size());
    return res;
}

mbus::Blob
MessageFixture::pad(mbus::Blob data, size_t bytes)
{
    mbus::Blob res(data.size() + bytes);
    memset(res.data(), 0, res.size());
    memcpy(res.data(), data.data(), data.size());
    return res;
}

bool MessageFixture::file_content_is_unchanged(const std::filesystem::path& filename, const mbus::Blob& data_to_write) {
    if (!std::filesystem::exists(filename)) {
        return false;
    }
    mbus::Blob existing = read_file(filename);
    return ((existing.size() == data_to_write.size())
            && (memcmp(existing.data(), data_to_write.data(), data_to_write.size()) == 0));
}

uint32_t
MessageFixture::serialize(const std::string& filename, const mbus::Routable& routable, Tamper tamper)
{
    const vespalib::Version version = tested_protocol_version();
    const auto path = path_to_file(version.toString() + "-cpp-" + filename + ".dat");
    LOG(info, "Serializing to '%s'...", path.c_str());

    mbus::Blob blob = tamper(_protocol.encode(version, routable));
    if (file_content_is_unchanged(path, blob)) {
        LOG(info, "Serialization for '%s' is unchanged; not overwriting it", path.c_str());
    } else if (!write_file(path, blob)) {
        LOG(error, "Could not open file '%s' for writing.", path.c_str());
        throw vespalib::Exception(fmt("Could not open file '%s' for writing.", path.c_str()), VESPA_STRLOC);
    }
    mbus::Routable::UP obj = _protocol.decode(version, blob);
    if (!obj) {
        LOG(error, "Protocol failed to decode serialized data");
        throw vespalib::Exception("Protocol failed to decode serialized data", VESPA_STRLOC);
    }
    if (routable.getType() != obj->getType()) {
        LOG(error, "Expected class %d, got %d", routable.getType(), obj->getType());
        throw vespalib::Exception(fmt("Expected class %d, got %d", routable.getType(), obj->getType()), VESPA_STRLOC);
    }
    return blob.size();
}

mbus::Routable::UP
MessageFixture::deserialize(const std::string& filename, uint32_t classId, uint32_t lang)
{
    const vespalib::Version version = tested_protocol_version();
    const auto path = path_to_file(version.toString() + (lang == LANG_JAVA ? "-java" : "-cpp") + "-" + filename + ".dat");
    LOG(info, "Deserializing from '%s'...", path.c_str());

    mbus::Blob blob = read_file(path);
    if (blob.size() == 0) {
        LOG(error, "Could not open file '%s' for reading", path.c_str());
        throw vespalib::Exception(fmt("Could not open file '%s' for reading", path.c_str()), VESPA_STRLOC);
    }
    mbus::Routable::UP ret = _protocol.decode(version, blob);

    if (!ret) {
        LOG(error, "Unable to decode class %d", classId);
        throw vespalib::Exception(fmt("Unable to decode class %d", classId), VESPA_STRLOC);
    } else if (classId != ret->getType()) {
        LOG(error, "Expected class %d, got %d", classId, ret->getType());
        throw vespalib::Exception(fmt("Expected class %d, got %d", classId, ret->getType()), VESPA_STRLOC);
    }
    return ret;
}

void
MessageFixture::dump(const mbus::Blob& blob)
{
    fprintf(stderr, "[%ld]: ", blob.size());
    for(size_t i = 0; i < blob.size(); i++) {
        if (blob.data()[i] > 32 && blob.data()[i] < 126) {
            fprintf(stderr, "%c ", blob.data()[i]);
        }
        else {
            fprintf(stderr, "%d ", blob.data()[i]);
        }
    }
    fprintf(stderr, "\n");
}


bool
MessageFixture::write_file(const std::filesystem::path& filename, const mbus::Blob& blob)
{
    int file = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (file == -1) {
        return false;
    }
    if (write(file, blob.data(), blob.size()) != (ssize_t)blob.size()) {
        throw vespalib::Exception("write failed");
    }
    close(file);
    return true;
}

mbus::Blob
MessageFixture::read_file(const std::filesystem::path& filename)
{
    int file = open(filename.c_str(), O_RDONLY);
    int len = (file == -1) ? 0 : lseek(file, 0, SEEK_END);
    mbus::Blob blob(len);
    if (file != -1) {
        lseek(file, 0, SEEK_SET);
        if (read(file, blob.data(), len) != len) {
            throw vespalib::Exception("read failed");
        }
        close(file);
    }

    return blob;
}

}