• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / 2589

29 Aug 2024 08:02AM UTC coverage: 91.117% (-0.03%) from 91.143%
2589

push

Evergreen

web-flow
RCORE-2242: Include pathname in exception thrown in File::rw_lock (#8000)

* Include pathname in exception thrown in File::rw_lock
* Strengthen check on synced file name size

102810 of 181608 branches covered (56.61%)

14 of 15 new or added lines in 2 files covered. (93.33%)

111 existing lines in 10 files now uncovered.

217373 of 238565 relevant lines covered (91.12%)

5688720.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.82
/test/object-store/util/test_utils.cpp
1
////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2016 Realm Inc.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
//
17
////////////////////////////////////////////////////////////////////////////
18

19
#include <util/test_utils.hpp>
20

21
#include <realm/string_data.hpp>
22
#include <realm/object-store/impl/realm_coordinator.hpp>
23
#include <realm/util/base64.hpp>
24
#include <realm/util/demangle.hpp>
25
#include <realm/util/file.hpp>
26

27
#include <external/json/json.hpp>
28

29
#include <iostream>
30
#include <random>
31
#include <sys/stat.h>
32

33
#ifndef _WIN32
34
#include <unistd.h>
35
#include <sys/types.h>
36
#endif
37

38
#if REALM_PLATFORM_APPLE
39
#include <sys/mount.h>
40
#include <sys/param.h>
41
#endif
42

43
namespace realm {
44

45
bool ExceptionMatcher<void>::match(Exception const& ex) const
46
{
2,615✔
47
    return ex.code() == m_code && ex.what() == m_message;
2,615✔
48
}
2,615✔
49

50
std::string ExceptionMatcher<void>::describe() const
51
{
×
52
    return util::format("Exception(%1, \"%2\")", ErrorCodes::error_string(m_code), m_message);
×
53
}
×
54

55
bool OutOfBoundsMatcher::match(OutOfBounds const& ex) const
56
{
210✔
57
    return ex.code() == ErrorCodes::OutOfBounds && ex.index == m_index && ex.size == m_size && ex.what() == m_message;
210✔
58
}
210✔
59

60
std::string OutOfBoundsMatcher::describe() const
61
{
×
62
    return util::format("OutOfBounds(index=%1, size=%2, \"%3\")", m_index, m_size, m_message);
×
63
}
×
64

65
bool LogicErrorMatcher::match(LogicError const& ex) const
66
{
14✔
67
    return ex.code() == m_code;
14✔
68
}
14✔
69

70
std::string LogicErrorMatcher::describe() const
71
{
×
72
    return util::format("LogicError(%1)", ErrorCodes::error_string(m_code));
×
73
}
×
74

75
std::ostream& operator<<(std::ostream& os, const Exception& e)
76
{
×
77
    os << util::get_type_name(e) << "(" << e.code_string() << ", \"" << e.what() << "\")";
×
78
    return os;
×
79
}
×
80

81
void create_dummy_realm(std::string path, std::shared_ptr<Realm>* out)
82
{
14✔
83
    Realm::Config config;
14✔
84
    config.path = path;
14✔
85
    auto realm = _impl::RealmCoordinator::get_coordinator(path)->get_realm(config, none);
14✔
86
    REQUIRE_REALM_EXISTS(path);
14!
87
    if (out) {
14✔
NEW
88
        *out = std::move(realm);
×
UNCOV
89
    }
×
90
}
14✔
91

92
std::vector<char> make_test_encryption_key(const char start)
93
{
23✔
94
    std::vector<char> vector;
23✔
95
    vector.reserve(64);
23✔
96
    for (int i = 0; i < 64; i++) {
1,495✔
97
        vector.emplace_back((start + i) % 128);
1,472✔
98
    }
1,472✔
99
    return vector;
23✔
100
}
23✔
101

102
// FIXME: Catch2 limitation on old compilers (currently our android CI)
103
// https://github.com/catchorg/Catch2/blob/master/docs/limitations.md#clangg----skipping-leaf-sections-after-an-exception
104
void catch2_ensure_section_run_workaround(bool did_run_a_section, std::string section_name,
105
                                          util::FunctionRef<void()> func)
106
{
28✔
107
    if (did_run_a_section) {
28✔
108
        func();
28✔
109
    }
28✔
110
    else {
×
111
        std::cout << "Skipping test section '" << section_name << "' on this run." << std::endl;
×
112
    }
×
113
}
28✔
114

115
std::string encode_fake_jwt(const std::string& in, util::Optional<int64_t> exp, util::Optional<int64_t> iat)
116
{
128✔
117
    // by default make a valid expiry time so that the sync session pre check
118
    // doesn't trigger a token refresh on first open
119
    using namespace std::chrono_literals;
128✔
120
    if (!exp) {
128✔
121
        std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
20✔
122
        exp = std::chrono::system_clock::to_time_t(now + 60min);
20✔
123
    }
20✔
124
    if (!iat) {
128✔
125
        std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
22✔
126
        iat = std::chrono::system_clock::to_time_t(now - 1s);
22✔
127
    }
22✔
128

129
    std::string unencoded_prefix = nlohmann::json({"alg", "HS256"}).dump();
128✔
130
    std::string unencoded_body =
128✔
131
        nlohmann::json(
128✔
132
            {{"user_data", {{"token", in}}}, {"exp", *exp}, {"iat", *iat}, {"access", {"download", "upload"}}})
128✔
133
            .dump();
128✔
134

135
    std::string encoded_prefix, encoded_body;
128✔
136
    encoded_prefix.resize(util::base64_encoded_size(unencoded_prefix.size()));
128✔
137
    encoded_body.resize(util::base64_encoded_size(unencoded_body.size()));
128✔
138
    util::base64_encode(unencoded_prefix, encoded_prefix);
128✔
139
    util::base64_encode(unencoded_body, encoded_body);
128✔
140
    std::string suffix = "Et9HFtf9R3GEMA0IICOfFMVXY7kkTX1wr4qCyhIf58U";
128✔
141
    return encoded_prefix + "." + encoded_body + "." + suffix;
128✔
142
}
128✔
143

144
std::string random_string(std::string::size_type length)
145
{
33,195✔
146
    static auto& chrs = "abcdefghijklmnopqrstuvwxyz"
33,195✔
147
                        "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
33,195✔
148
    thread_local static std::mt19937 rg{std::random_device{}()};
33,195✔
149
    thread_local static std::uniform_int_distribution<std::string::size_type> pick(0, sizeof(chrs) - 2);
33,195✔
150
    std::string s;
33,195✔
151
    s.reserve(length);
33,195✔
152
    while (length--)
236,394,415✔
153
        s += chrs[pick(rg)];
236,361,220✔
154
    return s;
33,195✔
155
}
33,195✔
156

157
int64_t random_int()
158
{
28,872✔
159
    thread_local std::mt19937_64 rng(std::random_device{}());
28,872✔
160
    return rng();
28,872✔
161
}
28,872✔
162

163
static bool file_is_on_exfat(const std::string& path)
164
{
×
165
#if REALM_PLATFORM_APPLE
166
    if (path.empty())
×
167
        return false;
168

169
    struct statfs fsbuf;
170
    int ret = statfs(path.c_str(), &fsbuf);
171
    REALM_ASSERT_RELEASE(ret == 0);
×
172
    // The documentation and headers helpfully don't list any of the values of
173
    // f_type or provide constants for them
174
    return fsbuf.f_type == 28 /* exFAT */;
175
#else
176
    static_cast<void>(path);
177
    return false;
178
#endif
179
}
×
180

181
bool chmod_supported(const std::string& path)
182
{
×
183
#ifndef _WIN32
×
184
    if (getuid() == 0) {
×
185
        return false; // running as root
×
186
    }
×
187
    if (file_is_on_exfat(path)) {
×
188
        return false;
×
189
    }
×
190
    return true;
×
191
#else
192
    static_cast<void>(path);
193
    return false;
194
#endif
195
}
×
196

197
int get_permissions(const std::string& path)
198
{
×
199
    int perms = 0;
×
200
#ifndef _WIN32
×
201
    REALM_ASSERT(!path.empty());
×
202
    struct stat statbuf;
×
203
    int ret = ::stat(path.c_str(), &statbuf);
×
204
    REALM_ASSERT_EX(ret == 0, ret, errno);
×
205
    perms = statbuf.st_mode;
×
206
#else
207
    static_cast<void>(path);
208
#endif
209
    return perms;
×
210
}
×
211

212
void chmod(const std::string& path, int permissions)
213
{
×
214
#ifndef _WIN32
×
215
    int ret = ::chmod(path.c_str(), permissions);
×
216
    REALM_ASSERT_EX(ret == 0, ret, errno);
×
217
#else
218
    static_cast<void>(path);
219
    static_cast<void>(permissions);
220
#endif
221
}
×
222

223
} // namespace realm
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc