diff --git a/.github/workflows/clippy.yml b/.github/workflows/clippy.yml index 619164cb87..9dfb03d57f 100644 --- a/.github/workflows/clippy.yml +++ b/.github/workflows/clippy.yml @@ -42,6 +42,8 @@ jobs: run: cargo clippy -p sample_component_hello_world - name: Clippy sample_component_json_validator run: cargo clippy -p sample_component_json_validator + - name: Clippy sample_component_json_validator_client + run: cargo clippy -p sample_component_json_validator_client - name: Clippy sample_component_json_validator_winrt run: cargo clippy -p sample_component_json_validator_winrt - name: Clippy sample_component_json_validator_winrt_client diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6b1d7685a7..f165fa093e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -66,6 +66,8 @@ jobs: run: cargo test -p sample_component_hello_world --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test sample_component_json_validator run: cargo test -p sample_component_json_validator --target ${{ matrix.target }} ${{ matrix.etc }} + - name: Test sample_component_json_validator_client + run: cargo test -p sample_component_json_validator_client --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test sample_component_json_validator_winrt run: cargo test -p sample_component_json_validator_winrt --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test sample_component_json_validator_winrt_client @@ -154,10 +156,10 @@ jobs: run: cargo test -p test_array --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_bcrypt run: cargo test -p test_bcrypt --target ${{ matrix.target }} ${{ matrix.etc }} - - name: Test test_bstr - run: cargo test -p test_bstr --target ${{ matrix.target }} ${{ matrix.etc }} - name: Clean run: cargo clean + - name: Test test_bstr + run: cargo test -p test_bstr --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_calling_convention run: cargo test -p test_calling_convention --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_cfg_generic @@ -256,10 +258,10 @@ jobs: run: cargo test -p test_targets --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_unions run: cargo test -p test_unions --target ${{ matrix.target }} ${{ matrix.etc }} - - name: Test test_variant - run: cargo test -p test_variant --target ${{ matrix.target }} ${{ matrix.etc }} - name: Clean run: cargo clean + - name: Test test_variant + run: cargo test -p test_variant --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_wdk run: cargo test -p test_wdk --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_weak diff --git a/crates/samples/components/json_validator_client/Cargo.toml b/crates/samples/components/json_validator_client/Cargo.toml new file mode 100644 index 0000000000..cf321c95da --- /dev/null +++ b/crates/samples/components/json_validator_client/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "sample_component_json_validator_client" +version = "0.0.0" +edition = "2021" +publish = false + +[build-dependencies] +cc = "1.0" + +# TODO: this causes a warning about lack of linkage target. The point is to ensure that this binary dependency is built first but +# Cargo doesn't respect cdylib targets. https://github.com/rust-lang/cargo/issues/7825 +[dependencies.sample_component_json_validator] +path = "../json_validator" diff --git a/crates/samples/components/json_validator_client/build.rs b/crates/samples/components/json_validator_client/build.rs new file mode 100644 index 0000000000..a326957e0d --- /dev/null +++ b/crates/samples/components/json_validator_client/build.rs @@ -0,0 +1,9 @@ +fn main() { + println!("cargo:rerun-if-changed=src/client.cpp"); + + cc::Build::new() + .cpp(true) + .std("c++20") + .file("src/client.cpp") + .compile("client"); +} diff --git a/crates/samples/components/json_validator_client/src/client.cpp b/crates/samples/components/json_validator_client/src/client.cpp new file mode 100644 index 0000000000..5cbb455848 --- /dev/null +++ b/crates/samples/components/json_validator_client/src/client.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#pragma comment(lib, "ole32") + +typedef HRESULT (__stdcall *CreateJsonValidator)(char const* schema, size_t schema_len, uintptr_t* handle); + +typedef HRESULT (__stdcall *ValidateJson)(uintptr_t handle, char const* value, size_t value_len, char** sanitized_value, size_t* sanitized_value_len); + +typedef void (__stdcall *CloseJsonValidator)(uintptr_t handle); + +extern "C" { + void __stdcall client() { + auto library = LoadLibraryExW(L"sample_component_json_validator.dll", 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); + assert(library != 0); + + auto create = reinterpret_cast(GetProcAddress(library, "CreateJsonValidator")); + assert(create); + + auto validate = reinterpret_cast(GetProcAddress(library, "ValidateJson")); + assert(validate); + + auto close = reinterpret_cast(GetProcAddress(library, "CloseJsonValidator")); + assert(close); + + std::string_view schema = "{\"maxLength\": 5}"; + std::string_view json = "\"Hello\" "; // trailing space will be removed from sanitized result + std::string_view json_invalid = "\"Hello world\""; // this json is too long + + uintptr_t validator = 0; + assert(S_OK == create(schema.data(), schema.size(), &validator)); + + char* sanitized_value = nullptr; + size_t sanitized_value_len = 0; + assert(S_OK == validate(validator, json.data(), json.size(), &sanitized_value, &sanitized_value_len)); + std::string_view sanitized(sanitized_value, sanitized_value_len); + assert(sanitized == "\"Hello\""); + CoTaskMemFree(sanitized_value); + + assert(E_INVALIDARG == validate(validator, json_invalid.data(), json_invalid.size(), &sanitized_value, &sanitized_value_len)); + + close(validator); + } +} diff --git a/crates/samples/components/json_validator_client/src/lib.rs b/crates/samples/components/json_validator_client/src/lib.rs new file mode 100644 index 0000000000..c6f84ba608 --- /dev/null +++ b/crates/samples/components/json_validator_client/src/lib.rs @@ -0,0 +1,9 @@ +#[test] +fn test() { + extern "system" { + fn client(); + } + unsafe { + client(); + } +}