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

realm / realm-core / 2213

10 Apr 2024 11:21PM UTC coverage: 91.792% (-0.8%) from 92.623%
2213

push

Evergreen

web-flow
Add missing availability checks for SecCopyErrorMessageString (#7577)

This requires iOS 11.3 and we currently target iOS 11.

94842 of 175770 branches covered (53.96%)

7 of 22 new or added lines in 2 files covered. (31.82%)

1861 existing lines in 82 files now uncovered.

242866 of 264583 relevant lines covered (91.79%)

5593111.45 hits per line

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

59.84
/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,613✔
47
    return ex.code() == m_code && ex.what() == m_message;
2,613✔
48
}
2,613✔
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
bool create_dummy_realm(std::string path, std::shared_ptr<Realm>* out)
82
{
12✔
83
    Realm::Config config;
12✔
84
    config.path = path;
12✔
85
    try {
12✔
86
        auto realm = _impl::RealmCoordinator::get_coordinator(path)->get_realm(config, none);
12✔
87
        REQUIRE_REALM_EXISTS(path);
12!
88
        if (out) {
12✔
89
            *out = std::move(realm);
×
90
        }
×
91
        return true;
12✔
UNCOV
92
    }
×
UNCOV
93
    catch (std::exception&) {
×
UNCOV
94
        return false;
×
UNCOV
95
    }
×
96
}
12✔
97

98
std::vector<char> make_test_encryption_key(const char start)
99
{
23✔
100
    std::vector<char> vector;
23✔
101
    vector.reserve(64);
23✔
102
    for (int i = 0; i < 64; i++) {
1,495✔
103
        vector.emplace_back((start + i) % 128);
1,472✔
104
    }
1,472✔
105
    return vector;
23✔
106
}
23✔
107

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

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

55✔
135
    std::string unencoded_prefix = nlohmann::json({"alg", "HS256"}).dump();
110✔
136
    std::string unencoded_body =
110✔
137
        nlohmann::json(
110✔
138
            {{"user_data", {{"token", in}}}, {"exp", *exp}, {"iat", *iat}, {"access", {"download", "upload"}}})
110✔
139
            .dump();
110✔
140

55✔
141
    std::string encoded_prefix, encoded_body;
110✔
142
    encoded_prefix.resize(util::base64_encoded_size(unencoded_prefix.size()));
110✔
143
    encoded_body.resize(util::base64_encoded_size(unencoded_body.size()));
110✔
144
    util::base64_encode(unencoded_prefix, encoded_prefix);
110✔
145
    util::base64_encode(unencoded_body, encoded_body);
110✔
146
    std::string suffix = "Et9HFtf9R3GEMA0IICOfFMVXY7kkTX1wr4qCyhIf58U";
110✔
147
    return encoded_prefix + "." + encoded_body + "." + suffix;
110✔
148
}
110✔
149

150
std::string random_string(std::string::size_type length)
151
{
32,671✔
152
    static auto& chrs = "abcdefghijklmnopqrstuvwxyz"
32,671✔
153
                        "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
32,671✔
154
    thread_local static std::mt19937 rg{std::random_device{}()};
32,671✔
155
    thread_local static std::uniform_int_distribution<std::string::size_type> pick(0, sizeof(chrs) - 2);
32,671✔
156
    std::string s;
32,671✔
157
    s.reserve(length);
32,671✔
158
    while (length--)
257,359,211✔
159
        s += chrs[pick(rg)];
257,326,540✔
160
    return s;
32,671✔
161
}
32,671✔
162

163
int64_t random_int()
164
{
28,872✔
165
    thread_local std::mt19937_64 rng(std::random_device{}());
28,872✔
166
    return rng();
28,872✔
167
}
28,872✔
168

169
static bool file_is_on_exfat(const std::string& path)
UNCOV
170
{
×
171
#if REALM_PLATFORM_APPLE
172
    if (path.empty())
×
173
        return false;
174

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

187
bool chmod_supported(const std::string& path)
UNCOV
188
{
×
UNCOV
189
#ifndef _WIN32
×
UNCOV
190
    if (getuid() == 0) {
×
191
        return false; // running as root
×
192
    }
×
UNCOV
193
    if (file_is_on_exfat(path)) {
×
194
        return false;
×
195
    }
×
UNCOV
196
    return true;
×
197
#else
198
    static_cast<void>(path);
199
    return false;
200
#endif
UNCOV
201
}
×
202

203
int get_permissions(const std::string& path)
UNCOV
204
{
×
UNCOV
205
    int perms = 0;
×
UNCOV
206
#ifndef _WIN32
×
UNCOV
207
    REALM_ASSERT(!path.empty());
×
UNCOV
208
    struct stat statbuf;
×
UNCOV
209
    int ret = ::stat(path.c_str(), &statbuf);
×
UNCOV
210
    REALM_ASSERT_EX(ret == 0, ret, errno);
×
UNCOV
211
    perms = statbuf.st_mode;
×
212
#else
213
    static_cast<void>(path);
214
#endif
UNCOV
215
    return perms;
×
UNCOV
216
}
×
217

218
void chmod(const std::string& path, int permissions)
UNCOV
219
{
×
UNCOV
220
#ifndef _WIN32
×
UNCOV
221
    int ret = ::chmod(path.c_str(), permissions);
×
UNCOV
222
    REALM_ASSERT_EX(ret == 0, ret, errno);
×
223
#else
224
    static_cast<void>(path);
225
    static_cast<void>(permissions);
226
#endif
UNCOV
227
}
×
228

229
} // 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