Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/cpp test #15

Open
wants to merge 5 commits into
base: feat/memo_ui
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ jobs:
- name: run rust tests
run: make rust_test

cpp_tests:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: true
- name: Install deps
run: |
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
make deps
- run: make cpp_test

clippy:
runs-on: ubuntu-latest
container:
Expand Down
35 changes: 22 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#* limitations under the License.
#********************************************************************************
cmake_minimum_required(VERSION 3.28)
set(CMAKE_VERBOSE_MAKEFILE ON)
include("cmake/HunterGate.cmake")
HunterGate(
URL "https://github.com/cpp-pm/hunter/archive/v0.25.5.tar.gz"
Expand All @@ -22,7 +23,7 @@ HunterGate(
)

if(CMAKE_GENERATOR MATCHES "Ninja")
message(FATAL_ERROR "This project does not support the Ninja generator. "
message(FATAL_ERROR "This project does not support the Ninja generator. "
"Please use Unix Makefiles or another supported generator. "
"This error is typical in CLion. In this case, switch to generator Unix Makefiles.")
endif()
Expand Down Expand Up @@ -177,6 +178,8 @@ target_include_directories(app_lib PUBLIC
##############################################################
## Rust library for CPP tests
set(RUST_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/app/rust")
message(STATUS "Rust library path: ${RUST_LIB}")
message(STATUS "Rust library path2: ${RUST_LIB_DIR}")

# Determine the Rust target triple based on the host system
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
Expand All @@ -200,15 +203,15 @@ else()
endif()

# Use debug mode for debugging tests
set(RUST_TARGET_DIR "${RUST_LIB_DIR}/target/${RUST_TARGET_TRIPLE}/debug")
set(RUST_TARGET_DIR "${RUST_LIB_DIR}/target/${RUST_TARGET_TRIPLE}/release")

# Custom target for the Rust library
add_custom_target(RustLibClean
COMMAND cargo clean
WORKING_DIRECTORY ${RUST_LIB_DIR}
)
add_custom_target(RustLibBuild
COMMAND cargo build --target ${RUST_TARGET_TRIPLE}
COMMAND cargo build --release --target ${RUST_TARGET_TRIPLE} --features cpp_tests
WORKING_DIRECTORY ${RUST_LIB_DIR}
DEPENDS RustLibClean
)
Expand Down Expand Up @@ -239,13 +242,13 @@ if(ENABLE_FUZZING)
target_link_options(fuzz-${target} PRIVATE "-fsanitize=fuzzer")
endforeach()
else()
##############################################################
# Tests
file(GLOB_RECURSE TESTS_SRC
##############################################################
# Tests
file(GLOB_RECURSE TESTS_SRC
${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp)

add_executable(unittests ${TESTS_SRC})
target_include_directories(unittests PRIVATE
add_executable(unittests ${TESTS_SRC})
target_include_directories(unittests PRIVATE
${gtest_SOURCE_DIR}/include
${gmock_SOURCE_DIR}/include
${CONAN_INCLUDE_DIRS_FMT}
Expand All @@ -256,11 +259,17 @@ target_include_directories(unittests PRIVATE
)

target_link_libraries(unittests PRIVATE
rslib
app_lib
GTest::gtest_main
fmt::fmt
JsonCpp::JsonCpp)
app_lib
rslib
GTest::gtest_main
fmt::fmt
JsonCpp::JsonCpp)
# target_link_libraries(unittests PRIVATE
# rslib
# app_lib
# GTest::gtest_main
# fmt::fmt
# JsonCpp::JsonCpp)

add_compile_definitions(TESTVECTORS_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/")
add_test(NAME unittests COMMAND unittests)
Expand Down
4 changes: 4 additions & 0 deletions app/rust/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ rustflags = [
"link-arg=-Tlink.ld",
"-C",
"inline-threshold=0",
"-C",
"panic=abort", # Add this line to disable unwinding
]


Expand All @@ -28,4 +30,6 @@ rustflags = [
"link-arg=-Wl,--gc-sections",
"-C",
"link-arg=-Wl,--as-needed",
"-C",
"panic=abort", # Add this line to disable unwinding
]
7 changes: 0 additions & 7 deletions app/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion app/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ bech32 = { version = "0.11.0", default-features = false }
chacha20poly1305 = { version = "0.10.1", default-features = false }

[dev-dependencies]
hex-literal = "0.4.1"
hex = "0.4.2"
no-std-compat = { version = "0.4.1", features = ["std"] }
blake2b_simd = { version = "1.0.2", default-features = false }
Expand All @@ -57,8 +56,11 @@ overflow-checks = false
strip = true

[profile.dev]
lto = false
panic = "abort"

[features]
clippy = []
derive-debug = []
# use when compiling this crate as a lib for the cpp_tests suite
cpp_tests = []
54 changes: 45 additions & 9 deletions app/rust/src/ffi/c_api.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,62 @@
use crate::constants::FVK_LEN;
use crate::keys::{fvk::FullViewingKey, nk::NullifierKey};
use crate::keys::fvk::FullViewingKey;
use crate::zxerror::ZxErr;
use decaf377::Fq;
use decaf377_rdsa::{SpendAuth, VerificationKey};

#[cfg(all(
not(test),
not(feature = "clippy"),
not(feature = "fuzzing"),
not(feature = "cpp_tests")
))]
extern "C" {
pub fn crypto_getFvkBytes(fvk: *mut u8, len: u16) -> ZxErr;
}

#[cfg(all(
not(test),
not(feature = "clippy"),
not(feature = "fuzzing"),
not(feature = "cpp_tests")
))]
pub fn c_fvk_bytes() -> Result<FullViewingKey, ZxErr> {
use crate::constants::FVK_LEN;
use crate::keys::nk::NullifierKey;
use decaf377::Fq;
use decaf377_rdsa::{SpendAuth, VerificationKey};

unsafe {
let mut fvk_bytes = [0u8; FVK_LEN];
let err = crypto_getFvkBytes(fvk_bytes.as_mut_ptr(), fvk_bytes.len() as u16);

let ak_bytes: [u8; 32] = fvk_bytes[0..32].try_into().unwrap();
let nk_bytes: [u8; 32] = fvk_bytes[32..64].try_into().unwrap();
let ak = VerificationKey::<SpendAuth>::try_from(ak_bytes.as_ref()).unwrap();
let ak_bytes: [u8; 32] = fvk_bytes[0..32]
.try_into()
.map_err(|_| ZxErr::InvalidCryptoSettings)?;
let nk_bytes: [u8; 32] = fvk_bytes[32..64]
.try_into()
.map_err(|_| ZxErr::InvalidCryptoSettings)?;
let ak = VerificationKey::<SpendAuth>::try_from(ak_bytes.as_ref())
.map_err(|_| ZxErr::InvalidCryptoSettings)?;
let nk = NullifierKey(Fq::from_le_bytes_mod_order(nk_bytes.as_ref()));
let fvk = FullViewingKey::from_components(ak, nk).unwrap();
let fvk =
FullViewingKey::from_components(ak, nk).map_err(|_| ZxErr::InvalidCryptoSettings)?;

match err {
ZxErr::Ok => Ok(FullViewingKey::from(fvk)),
ZxErr::Ok => Ok(fvk),
_ => Err(err),
}
}
}

#[cfg(any(test, feature = "clippy", feature = "fuzzing", feature = "cpp_tests"))]
pub fn c_fvk_bytes() -> Result<FullViewingKey, ZxErr> {
use crate::keys::spend_key::SpendKeyBytes;

const SK_BYTES_RAW: [u8; 32] = [
0xa1, 0xff, 0xba, 0x0c, 0x37, 0x93, 0x1f, 0x0a, 0x62, 0x61, 0x37, 0x52, 0x0d, 0xa6, 0x50,
0x63, 0x2d, 0x35, 0x85, 0x3b, 0xf5, 0x91, 0xb3, 0x6b, 0xb4, 0x28, 0x63, 0x0a, 0x4d, 0x87,
0xc4, 0xdc,
];

let sk = SpendKeyBytes::from(SK_BYTES_RAW);

FullViewingKey::derive_from(&sk).map_err(|_| ZxErr::InvalidCryptoSettings)
}
2 changes: 1 addition & 1 deletion app/rust/src/keys/fvk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct FullViewingKey {
impl FullViewingKey {
pub const ACCOUNT_ID_DOMAIN_SEP: &'static [u8] = b"Penumbra_HashFVK";

fn derive_from(spk: &SpendKeyBytes) -> Result<Self, ParserError> {
pub(crate) fn derive_from(spk: &SpendKeyBytes) -> Result<Self, ParserError> {
crate::zlog("FullViewingKey::derive_from\x00");
let ak = spk.verification_key()?;
let nk = spk.nullifier_key()?;
Expand Down
39 changes: 30 additions & 9 deletions app/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@

extern crate no_std_compat as std;

#[cfg(test)]
extern crate hex_literal;

use arrayref as _;
use educe as _;
use poseidon377 as _;
Expand All @@ -43,15 +40,24 @@ pub(crate) use utils::prf::{expand_fq, expand_fr};

fn debug(_msg: &str) {}

#[cfg(all(not(test), not(feature = "clippy"), not(feature = "fuzzing")))]
// for cpp_tests we need to define the panic handler
// the remaining features does not need as dev-dependencies
// are used and their include a handler from std
#[cfg(all(not(test), not(feature = "clippy"), not(feature = "fuzzing"),))]
use core::panic::PanicInfo;

#[cfg(all(not(test), not(feature = "clippy"), not(feature = "fuzzing")))]
#[cfg(all(not(test), not(feature = "clippy"), not(feature = "fuzzing"),))]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}

#[cfg(all(
not(test),
not(feature = "clippy"),
not(feature = "fuzzing"),
not(feature = "cpp_tests")
))]
extern "C" {
fn check_app_canary();
fn pic(link_address: u32) -> u32;
Expand All @@ -60,24 +66,39 @@ extern "C" {
}

pub(crate) fn canary() {
#[cfg(not(any(test, fuzzing)))]
#[cfg(all(
not(test),
not(feature = "clippy"),
not(feature = "fuzzing"),
not(feature = "cpp_tests")
))]
unsafe {
check_app_canary();
}
}

#[cfg(not(any(test, fuzzing)))]
#[cfg(all(
not(test),
not(feature = "clippy"),
not(feature = "fuzzing"),
not(feature = "cpp_tests")
))]
pub fn is_expert_mode() -> bool {
unsafe { app_mode_expert() > 0 }
}

#[cfg(any(test, feature = "fuzzing"))]
#[cfg(any(test, feature = "clippy", feature = "fuzzing", feature = "cpp_tests"))]
pub fn is_expert_mode() -> bool {
true
}

pub fn zlog(_msg: &str) {
#[cfg(not(any(test, fuzzing)))]
#[cfg(all(
not(test),
not(feature = "clippy"),
not(feature = "fuzzing"),
not(feature = "cpp_tests")
))]
unsafe {
zemu_log_stack(_msg.as_bytes().as_ptr());
}
Expand Down
Loading