Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85803784
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
36 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..81370d4
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,82 @@
+workflow:
+ rules:
+ - if: '$CI_PIPELINE_SOURCE == "trigger"'
+ - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
+ when: never
+ - if: $CI_COMMIT_BRANCH
+ - if: '$CI_COMMIT_TAG !~ /^phabricator\//'
+
+stages:
+ - prepare
+ - build
+ - report
+
+.report:
+ image:
+ name: 'reg.lily.kazv.moe/infra/phorge-ci-tools:servant'
+ rules:
+ - if: $TARGET_PHID
+ when: always
+ - when: never
+ before_script:
+ - pipelineUrl="$CI_PROJECT_URL"/-/pipelines/"$CI_PIPELINE_ID"
+
+report-start:
+ extends: .report
+ stage: prepare
+ script:
+ - 'echo "{\"receiver\": \"$TARGET_PHID\", \"type\": \"work\", \"unit\": [{\"name\": \"GitLab CI (information only)\", \"result\": \"skip\", \"details\": \"$pipelineUrl\", \"format\": \"remarkup\"}]}" | /tools/arcanist/bin/arc call-conduit --conduit-uri https://iron.lily-is.land/ --conduit-token "$CONDUIT_TOKEN" -- harbormaster.sendmessage'
+
+.build:
+ stage: build
+ image:
+ name: ubuntu:24.04
+ script:
+ - apt-get update
+ - DEBIAN_FRONTEND=noninteractive apt-get -y upgrade
+ - DEBIAN_FRONTEND=noninteractive apt-get -y install cargo build-essential g++ libolm-dev libboost-json-dev wget cmake
+ # catch2 is broken in ubuntu, see https://bugs.launchpad.net/ubuntu/+source/catch2/+bug/2081647
+ - wget https://github.com/catchorg/Catch2/archive/v3.3.2.tar.gz -O catch2.tar.gz
+ - tar -xf catch2.tar.gz
+ - cd Catch2-*
+ - mkdir build && cd build && \
+ - cmake .. -DCATCH_ENABLE_WERROR=OFF -DBUILD_TESTING=OFF
+ - make -j$(($(nproc) / 2)) install
+ - cd ../..
+ - rm -rf Catch2-* catch2.tar.gz
+ - cd cpp
+ - mv -v build.rs build.rs.orig
+ - grep -v 'cargo::rerun-if-changed' build.rs.orig > build.rs
+ - make BUILD_TYPE="$BUILD_TYPE" test
+
+build:release:
+ extends:
+ - .build
+ before_script:
+ - BUILD_TYPE=release
+
+build:debug:
+ extends:
+ - .build
+ before_script:
+ - BUILD_TYPE=debug
+
+report-success:
+ extends: .report
+ rules:
+ - if: $TARGET_PHID
+ when: on_success
+ - when: never
+ stage: report
+ script:
+ - 'echo "{\"receiver\": \"$TARGET_PHID\", \"type\": \"pass\"}" | /tools/arcanist/bin/arc call-conduit --conduit-uri https://iron.lily-is.land/ --conduit-token "$CONDUIT_TOKEN" -- harbormaster.sendmessage'
+
+report-failure:
+ extends: .report
+ rules:
+ - if: $TARGET_PHID
+ when: on_failure
+ - when: never
+ stage: report
+ script:
+ - 'echo "{\"receiver\": \"$TARGET_PHID\", \"type\": \"fail\"}" | /tools/arcanist/bin/arc call-conduit --conduit-uri https://iron.lily-is.land/ --conduit-token "$CONDUIT_TOKEN" -- harbormaster.sendmessage'
diff --git a/cpp/Makefile b/cpp/Makefile
index dfd8096..62a93f7 100644
--- a/cpp/Makefile
+++ b/cpp/Makefile
@@ -1,90 +1,75 @@
all: dynamic-lib
.PHONY: all dynamic-lib test install
BUILD_TYPE := debug
ifeq ($(BUILD_TYPE), release)
CARGO_ARGS = --release
else
CARGO_ARGS =
endif
VERSION = 0.1.0
SOVERSION = 1
TARGET_DIR = ../target/$(BUILD_TYPE)
CXXBRIDGE_DIR = $(TARGET_DIR)/../cxxbridge
STATIC_LIB = $(TARGET_DIR)/libvodozemac.a
SHARED_LIB_NAME = libvodozemac.so
SHARED_LIB_PREFIX = $(TARGET_DIR)/$(SHARED_LIB_NAME)
SHARED_LIBS = $(SHARED_LIB_PREFIX) $(SHARED_LIB_PREFIX).$(VERSION) $(SHARED_LIB_PREFIX).$(SOVERSION)
HEADER = $(CXXBRIDGE_DIR)/vodozemac/src/lib.rs.h
SOURCES = src/account.rs src/group_sessions.rs src/lib.rs src/sas.rs src/session.rs src/types.rs build.rs
AR ?= ar
CXX ?= c++
LN ?= ln
INSTALL ?= install
CP ?= cp
# Packagers may need to override them all.
PREFIX = /usr/local
DESTDIR =
LIBDIR = $(PREFIX)/lib
INCLUDEDIR = $(PREFIX)/include
$(STATIC_LIB): $(SOURCES)
cargo build $(CARGO_ARGS)
dynamic-lib: $(SHARED_LIB_PREFIX)
$(SHARED_LIB_PREFIX).$(SOVERSION): $(SHARED_LIB_PREFIX).$(VERSION)
$(LN) -sfv libvodozemac.so.$(VERSION) $(SHARED_LIB_PREFIX).$(SOVERSION)
$(SHARED_LIB_PREFIX): $(SHARED_LIB_PREFIX).$(SOVERSION)
$(LN) -sfv libvodozemac.so.$(SOVERSION) $(SHARED_LIB_PREFIX)
$(SHARED_LIB_PREFIX).$(VERSION): $(STATIC_LIB)
rm -rf objects && \
mkdir objects && \
cd objects && \
$(AR) x ../$(STATIC_LIB) && \
cd .. && \
$(CXX) objects/*.o -shared -fPIC -Wl,-soname=$(SHARED_LIB_NAME) -o $(SHARED_LIB_PREFIX).$(VERSION)
-TEST_CXXFLAGS = -Wall -g -pthread -std=c++17 -lgtest_main \
- -lgtest -lolm -lboost_json -lpthread -I $(CXXBRIDGE_DIR) \
- -L $(TARGET_DIR) -l vodozemac -ldl -Wl,-rpath,$(TARGET_DIR)
+TEST_CXXFLAGS = -Wall -g -std=c++17 -I $(CXXBRIDGE_DIR)
+TEST_LDFLAGS = -lCatch2Main -lCatch2 -lolm -lboost_json \
+ -L $(TARGET_DIR) -l vodozemac -ldl -Wl,-rpath,$(TARGET_DIR) \
+ -L /usr/local/lib -Wl,-rpath,/usr/local/lib
TEST_DEPS = $(SHARED_LIB_PREFIX).$(VERSION) tests/util.hpp
+TESTS = account-test session-test sas-test group-session-test
-account-test: $(TEST_DEPS) tests/account.cpp
- $(CXX) $(TEST_CXXFLAGS) \
- tests/account.cpp -o account-test
+%-test: tests/%.cpp $(TEST_DEPS)
+ $(CXX) $(TEST_CXXFLAGS) $< $(TEST_LDFLAGS) -o $@
-
-session-test: $(TEST_DEPS) tests/session.cpp
- $(CXX) $(TEST_CXXFLAGS) \
- tests/session.cpp -o session-test
-
-sas-test: $(TEST_DEPS) tests/sas.cpp
- $(CXX) $(TEST_CXXFLAGS) \
- tests/sas.cpp -o sas-test
-
-group-session-test: $(TEST_DEPS) tests/group_session.cpp
- $(CXX) $(TEST_CXXFLAGS) \
- tests/group_session.cpp -o group-session-test
-
-test: account-test session-test sas-test group-session-test
- ./account-test --gtest_color=yes
- ./session-test --gtest_color=yes
- ./sas-test --gtest_color=yes
- ./group-session-test --gtest_color=yes
+test: $(TESTS)
+ for i in $^; do ./$$i || exit 1; done
install:
$(INSTALL) -d $(DESTDIR)$(INCLUDEDIR) $(DESTDIR)$(LIBDIR)
$(INSTALL) -m644 $(STATIC_LIB) $(DESTDIR)$(LIBDIR)
$(INSTALL) -m755 $(SHARED_LIB_PREFIX).$(VERSION) $(DESTDIR)$(LIBDIR)
$(LN) -sfv $(SHARED_LIB_NAME).$(VERSION) $(DESTDIR)$(LIBDIR)/$(SHARED_LIB_NAME).$(SOVERSION)
$(LN) -sfv $(SHARED_LIB_NAME).$(SOVERSION) $(DESTDIR)$(LIBDIR)/$(SHARED_LIB_NAME)
$(CP) $(HEADER) $(DESTDIR)$(INCLUDEDIR)/vodozemac.h
diff --git a/cpp/tests/account.cpp b/cpp/tests/account.cpp
index 08fff12..74fdfed 100644
--- a/cpp/tests/account.cpp
+++ b/cpp/tests/account.cpp
@@ -1,174 +1,179 @@
#include "../../target/cxxbridge/vodozemac/src/lib.rs.h"
#include <string>
#include <unordered_set>
#include <olm/olm.h>
#include <boost/json.hpp>
-#include "gtest/gtest.h"
+#include <catch2/catch_test_macros.hpp>
#include "util.hpp"
using namespace rust;
using namespace vodozemac;
-TEST(AccountTest, AccountCreation) {
+TEST_CASE("AccountCreation", "[AccountTest]") {
auto alice = olm::new_account();
auto key = alice->ed25519_key();
auto encoded_key = key->to_base64();
- EXPECT_NE(encoded_key.length(), 0);
+ REQUIRE(encoded_key.length() != 0);
}
-TEST(AccountTest, OneTimeKeyGeneration) {
+TEST_CASE("OneTimeKeyGeneration", "[AccountTest]") {
auto alice = olm::new_account();
- EXPECT_EQ(alice->one_time_keys().size(), 0);
+ REQUIRE(alice->one_time_keys().size() == 0);
auto result = alice->generate_one_time_keys(10);
- EXPECT_EQ(result->created().size(), 10);
- EXPECT_EQ(alice->one_time_keys().size(), 10);
+ REQUIRE(result->created().size() == 10);
+ REQUIRE(alice->one_time_keys().size() == 10);
alice->mark_keys_as_published();
- EXPECT_EQ(alice->one_time_keys().size(), 0);
+ REQUIRE(alice->one_time_keys().size() == 0);
}
-TEST(AccountTest, OneTimeKeysRemovedWhenTooMany) {
+TEST_CASE("OneTimeKeysRemovedWhenTooMany", "[AccountTest]") {
auto alice = olm::new_account();
// Vodozemac will remove old one-time keys when the total number of generated one-time keys exceeds an internal constant.
// That internal constant is not a part of the public API, that's why we keep generating new keys until the first removal.
static constexpr std::size_t upper_bound = 1000000;
// The long variable names are needed for better error messages in case the test fails.
std::size_t total_number_of_generated_one_time_keys = 0;
std::size_t total_number_of_one_time_keys_removed = 0;
while (total_number_of_one_time_keys_removed == 0 && total_number_of_generated_one_time_keys < upper_bound) {
auto result = alice->generate_one_time_keys(alice->max_number_of_one_time_keys());
total_number_of_generated_one_time_keys += result->created().size();
total_number_of_one_time_keys_removed += result->removed().size();
}
- EXPECT_GT(total_number_of_one_time_keys_removed, 0) << " Generated " << upper_bound << " one-time keys, but no keys were removed.";
- EXPECT_EQ(alice->one_time_keys().size() + total_number_of_one_time_keys_removed, total_number_of_generated_one_time_keys);
+ SECTION("After generating really many one-time keys the oldest ones should be removed") {
+ REQUIRE(total_number_of_one_time_keys_removed > 0);
+ REQUIRE(alice->one_time_keys().size() + total_number_of_one_time_keys_removed == total_number_of_generated_one_time_keys);
+ }
}
-TEST(AccountTest, FallbackKeyGeneration) {
+TEST_CASE("FallbackKeyGeneration", "[AccountTest]") {
auto alice = olm::new_account();
- EXPECT_EQ(alice->fallback_key().size(), 0);
+ REQUIRE(alice->fallback_key().size() == 0);
auto previous_fallback_key = alice->generate_fallback_key();
- EXPECT_EQ(alice->fallback_key().size(), 1);
- EXPECT_EQ(previous_fallback_key.size(), 0);
+ REQUIRE(alice->fallback_key().size() == 1);
+ REQUIRE(previous_fallback_key.size() == 0);
alice->mark_keys_as_published();
- EXPECT_EQ(alice->fallback_key().size(), 0);
+ REQUIRE(alice->fallback_key().size() == 0);
}
-TEST(AccountTest, FallbackKeyDeprecation) {
+TEST_CASE("FallbackKeyDeprecation", "[AccountTest]") {
auto alice = olm::new_account();
- EXPECT_EQ(alice->fallback_key().size(), 0);
+ REQUIRE(alice->fallback_key().size() == 0);
auto previous_fallback_key = alice->generate_fallback_key();
- EXPECT_EQ(alice->fallback_key().size(), 1);
- EXPECT_EQ(previous_fallback_key.size(), 0) <<
- "When a fallback key is generated for the first time, there should not be any old fallback keys to remove.";
+ REQUIRE(alice->fallback_key().size() == 1);
+ SECTION("When a fallback key is generated for the first time, there should not be any old fallback keys to remove.") {
+ REQUIRE(previous_fallback_key.size() == 0);
+ }
alice->mark_keys_as_published();
- EXPECT_EQ(alice->fallback_key().size(), 0);
+ REQUIRE(alice->fallback_key().size() == 0);
// olm::Account stores up to two fallback keys.
// The old fallback key should not be removed, yet.
previous_fallback_key = alice->generate_fallback_key();
- EXPECT_EQ(alice->fallback_key().size(), 1);
- EXPECT_EQ(previous_fallback_key.size(), 0) <<
- "After a fallback key has been generated twice, the old fallback key should not be removed yet.";
+ REQUIRE(alice->fallback_key().size() == 1);
+ SECTION("After a fallback key has been generated twice, the old fallback key should not be removed yet.") {
+ REQUIRE(previous_fallback_key.size() == 0);
+ }
alice->mark_keys_as_published();
- EXPECT_EQ(alice->fallback_key().size(), 0);
+ REQUIRE(alice->fallback_key().size() == 0);
// Now we expect the old fallback key to be removed.
previous_fallback_key = alice->generate_fallback_key();
- EXPECT_EQ(alice->fallback_key().size(), 1);
- EXPECT_EQ(previous_fallback_key.size(), 1) <<
- "After a fallback key has been generated three times, the old fallback key should be removed.";
+ REQUIRE(alice->fallback_key().size() == 1);
+ SECTION("After a fallback key has been generated three times, the old fallback key should be removed.") {
+ REQUIRE(previous_fallback_key.size() == 1);
+ }
}
-TEST(AccountTest, MaxKeysTest) {
+TEST_CASE("MaxKeysTest", "[AccountTest]") {
auto alice = olm::new_account();
auto max_keys = alice->max_number_of_one_time_keys();
- EXPECT_GT(max_keys, 0);
- EXPECT_LT(max_keys, 1000);
+ REQUIRE(max_keys > 0);
+ REQUIRE(max_keys < 1000);
}
-TEST(AccountTest, PickleTest) {
+TEST_CASE("PickleTest", "[AccountTest]") {
auto alice = olm::new_account();
auto pickle = alice->pickle(PICKLE_KEY);
auto unpickled = olm::account_from_pickle(pickle, PICKLE_KEY);
- EXPECT_EQ(alice->curve25519_key()->to_base64(),
- unpickled->curve25519_key()->to_base64());
+ REQUIRE(alice->curve25519_key()->to_base64() ==
+ unpickled->curve25519_key()->to_base64());
}
-TEST(AccountTest, PickleFromLibolmTest) {
+TEST_CASE("PickleFromLibolmTest", "[AccountTest]") {
auto [data, alice] = new_olm_account();
auto random = gen_random(olm_account_generate_one_time_keys_random_length(alice, 10));
check_olm_error(olm_account_generate_one_time_keys(alice, 10, random.data(), random.size()));
auto pickle = std::string(olm_pickle_account_length(alice), '\0');
check_olm_error(olm_pickle_account(alice, OLM_PICKLE_KEY.data(), OLM_PICKLE_KEY.size(), pickle.data(), pickle.size()));
auto unpickled = olm::account_from_libolm_pickle(pickle, Slice<const unsigned char>(OLM_PICKLE_KEY.data(), OLM_PICKLE_KEY.size()));
{
auto identity_keys = std::string(olm_account_identity_keys_length(alice), '\0');
check_olm_error(olm_account_identity_keys(alice, identity_keys.data(), identity_keys.size()));
auto parsed_keys = boost::json::parse(identity_keys).as_object();
auto curve25519_key = String(as_std_string(parsed_keys.at("curve25519").as_string()));
auto ed25519_key = String(as_std_string(parsed_keys.at("ed25519").as_string()));
- EXPECT_EQ(curve25519_key,
+ REQUIRE(curve25519_key ==
unpickled->curve25519_key()->to_base64());
- EXPECT_EQ(ed25519_key,
+ REQUIRE(ed25519_key ==
unpickled->ed25519_key()->to_base64());
}
{
auto one_time_keys = std::string(olm_account_one_time_keys_length(alice), '\0');
check_olm_error(olm_account_one_time_keys(alice, one_time_keys.data(), one_time_keys.size()));
auto parsed_keys = boost::json::parse(one_time_keys).as_object().at("curve25519").as_object();
// The key id will change from libolm to vodozemac, but the key themselves won't.
auto one_time_keys_set = std::unordered_set<std::string>();
for (const auto &pair : parsed_keys) {
one_time_keys_set.insert(as_std_string(pair.value().as_string()));
}
auto unpickled_one_time_keys = unpickled->one_time_keys();
auto unpickled_one_time_keys_set = std::unordered_set<std::string>();
for (const auto &k : unpickled_one_time_keys) {
unpickled_one_time_keys_set.insert(static_cast<std::string>(k.key->to_base64()));
}
- EXPECT_EQ(one_time_keys_set, unpickled_one_time_keys_set);
+ REQUIRE(one_time_keys_set == unpickled_one_time_keys_set);
}
}
-TEST(AccountTest, SignAndVerify) {
+TEST_CASE("SignAndVerify", "[AccountTest]") {
auto alice = olm::new_account();
auto key = alice->ed25519_key();
std::string str = "{}";
auto signature = alice->sign(Str(str));
- EXPECT_NO_THROW(key->verify(Str(str), *signature));
+ REQUIRE_NOTHROW(key->verify(Str(str), *signature));
{
auto cloned_signature = types::ed25519_signature_from_base64(signature->to_base64());
- EXPECT_NO_THROW(key->verify(Str(str), *cloned_signature));
+ REQUIRE_NOTHROW(key->verify(Str(str), *cloned_signature));
}
{
auto cloned_key = types::ed25519_key_from_base64(key->to_base64());
- EXPECT_NO_THROW(cloned_key->verify(Str(str), *signature));
+ REQUIRE_NOTHROW(cloned_key->verify(Str(str), *signature));
}
std::string str2 = "{\"foo\": \"bar\"}";
auto signature2 = alice->sign(Str(str2));
- EXPECT_ANY_THROW(key->verify(Str(str2), *signature));
- EXPECT_ANY_THROW(key->verify(Str(str), *signature2));
+ REQUIRE_THROWS(key->verify(Str(str2), *signature));
+ REQUIRE_THROWS(key->verify(Str(str), *signature2));
}
diff --git a/cpp/tests/group_session.cpp b/cpp/tests/group-session.cpp
similarity index 70%
rename from cpp/tests/group_session.cpp
rename to cpp/tests/group-session.cpp
index 8acb49d..5b46e95 100644
--- a/cpp/tests/group_session.cpp
+++ b/cpp/tests/group-session.cpp
@@ -1,185 +1,185 @@
#include "../../target/cxxbridge/vodozemac/src/lib.rs.h"
#include <olm/outbound_group_session.h>
#include <olm/inbound_group_session.h>
-#include "gtest/gtest.h"
+#include <catch2/catch_test_macros.hpp>
#include "util.hpp"
using namespace rust;
using namespace vodozemac;
struct SessionCreationResult {
Box<megolm::GroupSession> outbound;
Box<megolm::InboundGroupSession> inbound;
};
SessionCreationResult create_session() {
auto outbound = megolm::new_group_session();
auto session_key = outbound->session_key();
auto inbound = megolm::new_inbound_group_session(*session_key);
auto ret = SessionCreationResult{
std::move(outbound),
std::move(inbound),
};
return ret;
}
struct LibolmSessionCreationResult {
std::vector<uint8_t> outbound_data;
OlmOutboundGroupSession *outbound;
std::vector<uint8_t> inbound_data;
OlmInboundGroupSession *inbound;
};
LibolmSessionCreationResult create_libolm_session() {
LibolmSessionCreationResult res{
std::vector<uint8_t>(olm_outbound_group_session_size()),
0,
std::vector<uint8_t>(olm_inbound_group_session_size()),
0,
};
res.outbound = olm_outbound_group_session(res.outbound_data.data());
auto random = gen_random(olm_init_outbound_group_session_random_length(res.outbound));
check_olm_error(olm_init_outbound_group_session(res.outbound, random.data(), random.size()));
auto session_key = std::vector<uint8_t>(olm_outbound_group_session_key_length(res.outbound));
check_olm_error(olm_outbound_group_session_key(res.outbound, session_key.data(), session_key.size()));
res.inbound = olm_inbound_group_session(res.inbound_data.data());
olm_init_inbound_group_session(res.inbound, session_key.data(), session_key.size());
return res;
}
-TEST(GroupSessionTest, Creation) {
+TEST_CASE("Creation", "[GroupSessionTest]") {
auto [outbound, inbound] = create_session();
auto outbound_id = outbound->session_id();
auto inbound_id = inbound->session_id();
- EXPECT_NE(outbound_id.length(), 0);
- EXPECT_STREQ(outbound_id.c_str(), inbound_id.c_str());
+ REQUIRE(outbound_id.length() != 0);
+ REQUIRE(outbound_id == inbound_id);
}
-TEST(GroupSessionTest, MessageIndex) {
+TEST_CASE("MessageIndex", "[GroupSessionTest]") {
auto [outbound, inbound] = create_session();
- EXPECT_EQ(outbound->message_index(), 0);
- EXPECT_EQ(inbound->first_known_index(), 0);
+ REQUIRE(outbound->message_index() == 0);
+ REQUIRE(inbound->first_known_index() == 0);
outbound->encrypt("Hello");
auto inbound2 = megolm::new_inbound_group_session(*outbound->session_key());
- EXPECT_EQ(outbound->message_index(), 1);
- EXPECT_EQ(inbound2->first_known_index(), 1);
+ REQUIRE(outbound->message_index() == 1);
+ REQUIRE(inbound2->first_known_index() == 1);
}
-TEST(GroupSessionTest, Pickle) {
+TEST_CASE("Pickle", "[GroupSessionTest]") {
auto session = megolm::new_group_session();
auto pickle = session->pickle(PICKLE_KEY);
auto unpickled = megolm::group_session_from_pickle(pickle, PICKLE_KEY);
- ASSERT_STREQ(session->session_id().c_str(), unpickled->session_id().c_str());
- EXPECT_EQ(session->message_index(), unpickled->message_index());
+ REQUIRE(session->session_id() == unpickled->session_id());
+ REQUIRE(session->message_index() == unpickled->message_index());
}
-TEST(GroupSessionTest, PickleLibolm) {
+TEST_CASE("PickleLibolm", "[GroupSessionTest]") {
auto [_1, outbound, _2, inbound] = create_libolm_session();
auto pickle = std::string(olm_pickle_outbound_group_session_length(outbound), '\0');
check_olm_error(olm_pickle_outbound_group_session(outbound, OLM_PICKLE_KEY.data(), OLM_PICKLE_KEY.size(), pickle.data(), pickle.size()));
auto unpickled = megolm::group_session_from_libolm_pickle(String(pickle), Slice<const unsigned char>(OLM_PICKLE_KEY.data(), OLM_PICKLE_KEY.size()));
auto session_id = std::vector<uint8_t>(olm_outbound_group_session_id_length(outbound));
check_olm_error(olm_outbound_group_session_id(outbound, session_id.data(), session_id.size()));
- ASSERT_EQ(as_std_string(session_id), static_cast<std::string>(unpickled->session_id()));
+ REQUIRE(as_std_string(session_id) == static_cast<std::string>(unpickled->session_id()));
auto message_index = olm_outbound_group_session_message_index(outbound);
- EXPECT_EQ(message_index, unpickled->message_index());
+ REQUIRE(message_index == unpickled->message_index());
}
-TEST(GroupSessionTest, PickleInbound) {
+TEST_CASE("PickleInbound", "[GroupSessionTest]") {
auto [outbound, inbound] = create_session();
auto pickle = inbound->pickle(PICKLE_KEY);
auto unpickled =
megolm::inbound_group_session_from_pickle(pickle, PICKLE_KEY);
- ASSERT_STREQ(inbound->session_id().c_str(), unpickled->session_id().c_str());
- EXPECT_EQ(inbound->first_known_index(), unpickled->first_known_index());
+ REQUIRE(inbound->session_id() == unpickled->session_id());
+ REQUIRE(inbound->first_known_index() == unpickled->first_known_index());
}
-TEST(GroupSessionTest, PickleInboundLibolm) {
+TEST_CASE("PickleInboundLibolm", "[GroupSessionTest]") {
auto [_1, outbound, _2, inbound] = create_libolm_session();
auto pickle = std::string(olm_pickle_inbound_group_session_length(inbound), '\0');
check_olm_error(olm_pickle_inbound_group_session(inbound, OLM_PICKLE_KEY.data(), OLM_PICKLE_KEY.size(), pickle.data(), pickle.size()));
auto unpickled =
megolm::inbound_group_session_from_libolm_pickle(pickle, Slice<const unsigned char>(OLM_PICKLE_KEY.data(), OLM_PICKLE_KEY.size()));
auto session_id = std::vector<uint8_t>(olm_inbound_group_session_id_length(inbound));
check_olm_error(olm_inbound_group_session_id(inbound, session_id.data(), session_id.size()));
- ASSERT_EQ(as_std_string(session_id), static_cast<std::string>(unpickled->session_id()));
+ REQUIRE(as_std_string(session_id) == static_cast<std::string>(unpickled->session_id()));
auto first_known_index = olm_inbound_group_session_first_known_index(inbound);
- EXPECT_EQ(first_known_index, unpickled->first_known_index());
+ REQUIRE(first_known_index == unpickled->first_known_index());
}
-TEST(GroupSessionTest, UnpicklingFail) {
- EXPECT_ANY_THROW(megolm::group_session_from_pickle("", PICKLE_KEY));
- EXPECT_ANY_THROW(megolm::inbound_group_session_from_pickle("", PICKLE_KEY));
+TEST_CASE("UnpicklingFail", "[GroupSessionTest]") {
+ REQUIRE_THROWS(megolm::group_session_from_pickle("", PICKLE_KEY));
+ REQUIRE_THROWS(megolm::inbound_group_session_from_pickle("", PICKLE_KEY));
}
-TEST(GroupSessionTest, DecryptionFail) {
+TEST_CASE("DecryptionFail", "[GroupSessionTest]") {
auto [outbound, inbound] = create_session();
auto outbound2 = megolm::new_group_session();
auto message = outbound2->encrypt("Hello");
- EXPECT_ANY_THROW(inbound->decrypt(*message));
+ REQUIRE_THROWS(inbound->decrypt(*message));
}
-TEST(GroupSessionTest, Encryption) {
+TEST_CASE("Encryption", "[GroupSessionTest]") {
auto [outbound, inbound] = create_session();
auto plaintext = "It's a secret to everybody";
auto message = outbound->encrypt(plaintext);
auto decrypted = inbound->decrypt(*message);
- EXPECT_EQ(as_std_string(decrypted.plaintext), std::string(plaintext));
- EXPECT_EQ(decrypted.message_index, 0);
+ REQUIRE(as_std_string(decrypted.plaintext) == std::string(plaintext));
+ REQUIRE(decrypted.message_index == 0);
plaintext = "Another secret";
message = outbound->encrypt(plaintext);
decrypted = inbound->decrypt(*message);
- EXPECT_EQ(as_std_string(decrypted.plaintext), std::string(plaintext));
- EXPECT_EQ(decrypted.message_index, 1);
+ REQUIRE(as_std_string(decrypted.plaintext) == std::string(plaintext));
+ REQUIRE(decrypted.message_index == 1);
}
-TEST(GroupSessionTest, SessionExport) {
+TEST_CASE("SessionExport", "[GroupSessionTest]") {
auto [outbound, inbound] = create_session();
auto imported = megolm::import_inbound_group_session(*inbound->export_at(0));
- EXPECT_STREQ(outbound->session_id().c_str(), imported->session_id().c_str());
+ REQUIRE(outbound->session_id() == imported->session_id());
auto plaintext = "It's a secret to everybody";
auto message = outbound->encrypt(plaintext);
auto decrypted = imported->decrypt(*message);
- EXPECT_EQ(as_std_string(decrypted.plaintext), std::string(plaintext));
- EXPECT_EQ(decrypted.message_index, 0);
+ REQUIRE(as_std_string(decrypted.plaintext) == std::string(plaintext));
+ REQUIRE(decrypted.message_index == 0);
plaintext = "Another secret";
message = outbound->encrypt(plaintext);
decrypted = imported->decrypt(*message);
- EXPECT_EQ(as_std_string(decrypted.plaintext), std::string(plaintext));
- EXPECT_EQ(decrypted.message_index, 1);
+ REQUIRE(as_std_string(decrypted.plaintext) == std::string(plaintext));
+ REQUIRE(decrypted.message_index == 1);
}
diff --git a/cpp/tests/sas.cpp b/cpp/tests/sas.cpp
index f5364ec..b64a7a3 100644
--- a/cpp/tests/sas.cpp
+++ b/cpp/tests/sas.cpp
@@ -1,63 +1,63 @@
#include "../../target/cxxbridge/vodozemac/src/lib.rs.h"
-#include "gtest/gtest.h"
+#include <catch2/catch_test_macros.hpp>
using namespace rust;
using namespace vodozemac;
-TEST(SasTest, Creation) {
+TEST_CASE("Creation", "[SasTest]") {
auto alice = sas::new_sas();
auto bob = sas::new_sas();
auto alice_key = alice->public_key()->to_base64();
auto bob_key = bob->public_key()->to_base64();
- EXPECT_STRNE(alice_key.c_str(), bob_key.c_str());
+ REQUIRE(alice_key != bob_key);
}
-TEST(SasTest, SharedSecret) {
+TEST_CASE("SharedSecret", "[SasTest]") {
auto alice = sas::new_sas();
auto bob = sas::new_sas();
auto alice_key = alice->public_key();
auto bob_key = bob->public_key();
auto alice_established = alice->diffie_hellman(*bob_key);
auto bob_established = bob->diffie_hellman(*alice_key);
}
-TEST(SasTest, ShortAuthString) {
+TEST_CASE("ShortAuthString", "[SasTest]") {
auto alice = sas::new_sas();
auto bob = sas::new_sas();
auto alice_key = alice->public_key();
auto bob_key = bob->public_key();
auto alice_established = alice->diffie_hellman(*bob_key);
auto bob_established = bob->diffie_hellman(*alice_key);
auto alice_bytes = alice_established->bytes("");
auto bob_bytes = bob_established->bytes("");
- ASSERT_EQ(alice_bytes->emoji_indices(), bob_bytes->emoji_indices());
- ASSERT_EQ(alice_bytes->decimals(), bob_bytes->decimals());
+ REQUIRE(alice_bytes->emoji_indices() == bob_bytes->emoji_indices());
+ REQUIRE(alice_bytes->decimals() == bob_bytes->decimals());
}
-TEST(SasTest, CalculateMac) {
+TEST_CASE("CalculateMac", "[SasTest]") {
auto alice = sas::new_sas();
auto bob = sas::new_sas();
auto alice_key = alice->public_key();
auto bob_key = bob->public_key();
auto alice_established = alice->diffie_hellman(*bob_key);
auto bob_established = bob->diffie_hellman(*alice_key);
auto alice_mac = alice_established->calculate_mac("Hello", "world");
auto bob_mac = bob_established->calculate_mac("Hello", "world");
- ASSERT_STREQ(alice_mac->to_base64().c_str(), bob_mac->to_base64().c_str());
+ REQUIRE(alice_mac->to_base64() == bob_mac->to_base64());
- EXPECT_NO_THROW(alice_established->verify_mac("Hello", "world", *bob_mac));
- EXPECT_NO_THROW(bob_established->verify_mac("Hello", "world", *alice_mac));
- EXPECT_ANY_THROW(bob_established->verify_mac("", "", *alice_mac));
+ REQUIRE_NOTHROW(alice_established->verify_mac("Hello", "world", *bob_mac));
+ REQUIRE_NOTHROW(bob_established->verify_mac("Hello", "world", *alice_mac));
+ REQUIRE_THROWS(bob_established->verify_mac("", "", *alice_mac));
}
diff --git a/cpp/tests/session.cpp b/cpp/tests/session.cpp
index 04745ba..7204c5a 100644
--- a/cpp/tests/session.cpp
+++ b/cpp/tests/session.cpp
@@ -1,193 +1,193 @@
#include "../../target/cxxbridge/vodozemac/src/lib.rs.h"
-#include "gtest/gtest.h"
+#include <catch2/catch_test_macros.hpp>
#include <boost/json.hpp>
#include "util.hpp"
using namespace rust;
using namespace vodozemac;
struct SessionCreationResult {
Box<olm::Account> alice;
Box<olm::Account> bob;
Box<olm::Session> session;
};
SessionCreationResult create_session() {
Box<olm::Account> alice = olm::new_account();
auto bob = olm::new_account();
bob->generate_one_time_keys(1);
auto one_time_keys = bob->one_time_keys();
auto [key_id, one_time_key] = std::move(one_time_keys.front());
auto identity_key = bob->curve25519_key();
auto session = alice->create_outbound_session(*identity_key, *one_time_key);
auto ret = SessionCreationResult{
std::move(alice),
std::move(bob),
std::move(session),
};
return ret;
}
-TEST(SessionTest, Creation) {
+TEST_CASE("Creation", "[SessionTest]") {
auto [alice, bob, session] = create_session();
auto session_id = session->session_id();
- EXPECT_NE(session_id.length(), 0);
+ REQUIRE(session_id.length() != 0);
}
-TEST(SessionTest, IdUniqueness) {
+TEST_CASE("IdUniqueness", "[SessionTest]") {
auto [alice1, bob1, session] = create_session();
auto [alice2, bob2, session2] = create_session();
auto session_id = session->session_id();
auto session2_id = session2->session_id();
- EXPECT_STRNE(session_id.c_str(), session2_id.c_str());
+ REQUIRE(session_id != session2_id);
}
-TEST(SessionTest, Pickle) {
+TEST_CASE("Pickle", "[SessionTest]") {
auto [alice, bob, session] = create_session();
auto pickle = session->pickle(PICKLE_KEY);
auto unpickled = olm::session_from_pickle(pickle, PICKLE_KEY);
auto session_id = session->session_id();
auto session2_id = unpickled->session_id();
- EXPECT_STREQ(session_id.c_str(), session2_id.c_str());
+ REQUIRE(session_id == session2_id);
}
-TEST(SessionTest, PickleFromLibolm) {
+TEST_CASE("PickleFromLibolm", "[SessionTest]") {
auto [_1, alice] = new_olm_account();
auto [_2, bob] = new_olm_account();
auto random = gen_random(olm_account_generate_one_time_keys_random_length(bob, 1));
check_olm_error(olm_account_generate_one_time_keys(bob, 1, random.data(), random.size()));
auto one_time_keys = std::string(olm_account_one_time_keys_length(bob), '\0');
check_olm_error(olm_account_one_time_keys(bob, one_time_keys.data(), one_time_keys.size()));
auto parsed_keys = boost::json::parse(one_time_keys).as_object().at("curve25519").as_object();
auto key_id = as_std_string(parsed_keys.begin()->key());
auto one_time_key = as_std_string(parsed_keys.begin()->value().as_string());
auto identity_keys = std::string(olm_account_identity_keys_length(alice), '\0');
check_olm_error(olm_account_identity_keys(alice, identity_keys.data(), identity_keys.size()));
parsed_keys = boost::json::parse(identity_keys).as_object();
auto identity_key = String(as_std_string(parsed_keys.at("curve25519").as_string()));
auto data = std::vector<uint8_t>(olm_session_size());
auto session = olm_session(data.data());
random = gen_random(olm_create_outbound_session_random_length(session));
check_olm_error(olm_create_outbound_session(session, alice, identity_key.data(), identity_key.size(), one_time_key.data(), one_time_key.size(), random.data(), random.size()));
auto pickle = std::string(olm_pickle_session_length(session), '\0');
check_olm_error(olm_pickle_session(session, OLM_PICKLE_KEY.data(), OLM_PICKLE_KEY.size(), pickle.data(), pickle.size()));
auto unpickled = olm::session_from_libolm_pickle(pickle, Slice<const unsigned char>(OLM_PICKLE_KEY.data(), OLM_PICKLE_KEY.size()));
auto session_id = std::string(olm_session_id_length(session), '\0');
check_olm_error(olm_session_id(session, session_id.data(), session_id.size()));
auto session2_id = unpickled->session_id();
- EXPECT_STREQ(session_id.c_str(), session2_id.c_str());
+ REQUIRE(session_id == as_std_string(session2_id));
}
-TEST(SessionTest, InvalidPickle) {
- EXPECT_ANY_THROW(olm::session_from_pickle("", PICKLE_KEY));
+TEST_CASE("InvalidPickle", "[SessionTest]") {
+ REQUIRE_THROWS(olm::session_from_pickle("", PICKLE_KEY));
}
-TEST(SessionTest, Encryption) {
+TEST_CASE("Encryption", "[SessionTest]") {
auto [alice, bob, session] = create_session();
auto alice_key = alice->curve25519_key();
auto plaintext = "It's a secret to everybody";
auto message = session->encrypt(plaintext);
auto [bob_session, decrypted] =
bob->create_inbound_session(*alice_key, *message);
- EXPECT_STREQ(session->session_id().c_str(),
- bob_session->session_id().c_str());
+ REQUIRE(session->session_id() ==
+ bob_session->session_id());
- EXPECT_EQ(std::string(plaintext), as_std_string(decrypted));
+ REQUIRE(std::string(plaintext) == as_std_string(decrypted));
}
-TEST(SessionTest, InvalidDecryption) {
+TEST_CASE("InvalidDecryption", "[SessionTest]") {
auto parts = olm::OlmMessageParts{
0,
"",
};
- EXPECT_ANY_THROW(olm::olm_message_from_parts(parts));
+ REQUIRE_THROWS(olm::olm_message_from_parts(parts));
}
-TEST(SessionTest, MultipleMessageDecryption) {
+TEST_CASE("MultipleMessageDecryption", "[SessionTest]") {
auto [alice, bob, session] = create_session();
auto alice_key = alice->curve25519_key();
auto plaintext = "It's a secret to everybody";
auto message = session->encrypt(plaintext);
auto [bob_session, decrypted] =
bob->create_inbound_session(*alice_key, *message);
- EXPECT_STREQ(session->session_id().c_str(),
- bob_session->session_id().c_str());
+ REQUIRE(session->session_id() ==
+ bob_session->session_id());
- EXPECT_EQ(std::string(plaintext), as_std_string(decrypted));
+ REQUIRE(std::string(plaintext) == as_std_string(decrypted));
plaintext = "Grumble grumble";
message = bob_session->encrypt(plaintext);
decrypted = session->decrypt(*message);
- EXPECT_EQ(std::string(plaintext), as_std_string(decrypted));
+ REQUIRE(std::string(plaintext) == as_std_string(decrypted));
}
-TEST(SessionTest, PreKeyMatches) {
+TEST_CASE("PreKeyMatches", "[SessionTest]") {
auto [alice, bob, session] = create_session();
auto alice_key = alice->curve25519_key();
auto plaintext = "It's a secret to everybody";
auto message = session->encrypt(plaintext);
auto [bob_session, decrypted] =
bob->create_inbound_session(*alice_key, *message);
plaintext = "Grumble grumble";
message = session->encrypt(plaintext);
- EXPECT_TRUE(bob_session->session_matches(*message));
+ REQUIRE(bob_session->session_matches(*message));
}
-TEST(SessionTest, PreKeyDoesNotMatch) {
+TEST_CASE("PreKeyDoesNotMatch", "[SessionTest]") {
auto [alice, bob, session] = create_session();
auto [alice2, bob2, session2] = create_session();
auto alice_key = alice->curve25519_key();
auto plaintext = "It's a secret to everybody";
auto message = session->encrypt(plaintext);
auto [bob_session, decrypted] =
bob->create_inbound_session(*alice_key, *message);
plaintext = "Grumble grumble";
message = session2->encrypt(plaintext);
- EXPECT_FALSE(bob_session->session_matches(*message));
+ REQUIRE_FALSE(bob_session->session_matches(*message));
}
-TEST(SessionTest, InvalidOneTimeKey) {
- EXPECT_ANY_THROW(types::curve_key_from_base64(""));
+TEST_CASE("InvalidOneTimeKey", "[SessionTest]") {
+ REQUIRE_THROWS(types::curve_key_from_base64(""));
}
diff --git a/cpp/tests/util.hpp b/cpp/tests/util.hpp
index 407434c..3966866 100644
--- a/cpp/tests/util.hpp
+++ b/cpp/tests/util.hpp
@@ -1,40 +1,40 @@
#pragma once
#include <random>
#include <algorithm>
#include <array>
#include <vector>
-#include <gtest/gtest.h>
+#include <catch2/catch_test_macros.hpp>
#include <olm/olm.h>
inline std::array<uint8_t, 32> PICKLE_KEY = {};
// olm pickles are of variable length and are then hashed to obtain the actual key
inline std::vector<uint8_t> OLM_PICKLE_KEY = {0, 7, 2, 1};
[[nodiscard]] inline std::vector<uint8_t> gen_random(int len)
{
auto rd = std::random_device{};
auto ret = std::vector<uint8_t>(len, '\0');
std::generate(ret.begin(), ret.end(), [&] { return rd(); });
return ret;
}
inline void check_olm_error(std::size_t code)
{
- EXPECT_NE(code, olm_error());
+ REQUIRE(code != olm_error());
}
template<class T>
[[nodiscard]] inline std::string as_std_string(const T &range)
{
return std::string(range.begin(), range.end());
}
std::pair<std::vector<uint8_t>, OlmAccount *> new_olm_account()
{
auto data = std::vector<uint8_t>(olm_account_size());
auto account = olm_account(data.data());
auto random = gen_random(olm_create_account_random_length(account));
check_olm_error(olm_create_account(account, random.data(), random.size()));
return {std::move(data), account};
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, Oct 9, 2:04 PM (1 d, 15 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1784754
Default Alt Text
(36 KB)
Attached To
Mode
rVB vodozemac-bindings
Attached
Detach File
Event Timeline
Log In to Comment