Commit df9737a4 authored by nagayama15's avatar nagayama15

Add the watermarker test

parent 5809f290
(module
(export "f1" (func $f1))
(export "f2" (func $f2))
(export "f3" (func $f3))
(import "env" "g1" (func $g1))
(import "env" "g2" (func $g2))
(import "env" "g3" (func $g3))
(func $f1 (result i32) (i32.const 1))
(func $f2 (result i32) (i32.const 2))
(func $f3 (result i32) (i32.const 3))
)
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
#include "../BitStreamReader.hpp"
namespace kyut::watermarker { namespace kyut::watermarker {
namespace { namespace {
/** /**
......
...@@ -3,7 +3,9 @@ ...@@ -3,7 +3,9 @@
#include <wasm.h> #include <wasm.h>
#include "../BitStreamReader.hpp" namespace kyut {
class BitStreamReader;
}
namespace kyut::watermarker { namespace kyut::watermarker {
std::size_t embedFunctionOrdering(wasm::Module &module, BitStreamReader &stream, std::size_t divisions); std::size_t embedFunctionOrdering(wasm::Module &module, BitStreamReader &stream, std::size_t divisions);
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <wasm-io.h> #include <wasm-io.h>
#include "kyut/BitStreamReader.hpp"
#include "kyut/watermarker/FunctionOrderingWatermarker.hpp" #include "kyut/watermarker/FunctionOrderingWatermarker.hpp"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
......
add_executable(test_kyut add_executable(test_kyut
test_kyut.cpp test_kyut.cpp
test_BitStreamReader.cpp kyut/test_BitStreamReader.cpp
kyut/watermarker/test_FunctionOrderingWatermarker.cpp
) )
target_link_libraries(test_kyut target_link_libraries(test_kyut
......
...@@ -2,15 +2,6 @@ ...@@ -2,15 +2,6 @@
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
namespace {
constexpr std::size_t operator""_zu(unsigned long long x) noexcept {
return static_cast<std::size_t>(x);
}
constexpr std::uint64_t operator""_u64(unsigned long long x) noexcept {
return static_cast<std::uint64_t>(x);
}
} // namespace
BOOST_AUTO_TEST_SUITE(kyut) BOOST_AUTO_TEST_SUITE(kyut)
BOOST_AUTO_TEST_SUITE(bit_stream_reader) BOOST_AUTO_TEST_SUITE(bit_stream_reader)
...@@ -20,53 +11,53 @@ BOOST_AUTO_TEST_CASE(read_bit) { ...@@ -20,53 +11,53 @@ BOOST_AUTO_TEST_CASE(read_bit) {
BitStreamReader reader{std::begin(data), std::end(data)}; BitStreamReader reader{std::begin(data), std::end(data)};
// 0x89 == 0b1000'1001 // 0x89 == 0b1000'1001
BOOST_REQUIRE_EQUAL(reader.tell(), 0_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{0});
BOOST_REQUIRE_EQUAL(reader.readBit(), true); BOOST_REQUIRE_EQUAL(reader.readBit(), true);
BOOST_REQUIRE_EQUAL(reader.tell(), 1_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{1});
BOOST_REQUIRE_EQUAL(reader.readBit(), false); BOOST_REQUIRE_EQUAL(reader.readBit(), false);
BOOST_REQUIRE_EQUAL(reader.tell(), 2_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{2});
BOOST_REQUIRE_EQUAL(reader.readBit(), false); BOOST_REQUIRE_EQUAL(reader.readBit(), false);
BOOST_REQUIRE_EQUAL(reader.tell(), 3_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{3});
BOOST_REQUIRE_EQUAL(reader.readBit(), false); BOOST_REQUIRE_EQUAL(reader.readBit(), false);
BOOST_REQUIRE_EQUAL(reader.tell(), 4_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{4});
BOOST_REQUIRE_EQUAL(reader.readBit(), true); BOOST_REQUIRE_EQUAL(reader.readBit(), true);
BOOST_REQUIRE_EQUAL(reader.tell(), 5_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{5});
BOOST_REQUIRE_EQUAL(reader.readBit(), false); BOOST_REQUIRE_EQUAL(reader.readBit(), false);
BOOST_REQUIRE_EQUAL(reader.tell(), 6_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{6});
BOOST_REQUIRE_EQUAL(reader.readBit(), false); BOOST_REQUIRE_EQUAL(reader.readBit(), false);
BOOST_REQUIRE_EQUAL(reader.tell(), 7_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{7});
BOOST_REQUIRE_EQUAL(reader.readBit(), true); BOOST_REQUIRE_EQUAL(reader.readBit(), true);
BOOST_REQUIRE_EQUAL(reader.tell(), 0_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{0});
BOOST_REQUIRE_EQUAL(reader.readBit(), true); BOOST_REQUIRE_EQUAL(reader.readBit(), true);
BOOST_REQUIRE_EQUAL(reader.tell(), 1_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{1});
BOOST_REQUIRE_EQUAL(reader.readBit(), false); BOOST_REQUIRE_EQUAL(reader.readBit(), false);
BOOST_REQUIRE_EQUAL(reader.tell(), 2_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{2});
BOOST_REQUIRE_EQUAL(reader.readBit(), false); BOOST_REQUIRE_EQUAL(reader.readBit(), false);
BOOST_REQUIRE_EQUAL(reader.tell(), 3_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{3});
BOOST_REQUIRE_EQUAL(reader.readBit(), false); BOOST_REQUIRE_EQUAL(reader.readBit(), false);
BOOST_REQUIRE_EQUAL(reader.tell(), 4_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{4});
} }
BOOST_AUTO_TEST_CASE(read) { BOOST_AUTO_TEST_CASE(read) {
constexpr std::uint8_t data[4] = {0x89, 0xab, 0xcd, 0xef}; constexpr std::uint8_t data[4] = {0x89, 0xab, 0xcd, 0xef};
BitStreamReader reader{std::begin(data), std::end(data)}; BitStreamReader reader{std::begin(data), std::end(data)};
BOOST_REQUIRE_EQUAL(reader.tell(), 0_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{0});
BOOST_REQUIRE_EQUAL(reader.read(4), 0x8_u64); BOOST_REQUIRE_EQUAL(reader.read(4), std::uint64_t{0x8});
BOOST_REQUIRE_EQUAL(reader.tell(), 4_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{4});
BOOST_REQUIRE_EQUAL(reader.read(4), 0x9_u64); BOOST_REQUIRE_EQUAL(reader.read(4), std::uint64_t{0x9});
BOOST_REQUIRE_EQUAL(reader.tell(), 8_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{8});
BOOST_REQUIRE_EQUAL(reader.read(8), 0xab_u64); BOOST_REQUIRE_EQUAL(reader.read(8), std::uint64_t{0xab});
BOOST_REQUIRE_EQUAL(reader.tell(), 16_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{16});
BOOST_REQUIRE_EQUAL(reader.read(16), 0xcdef_u64); BOOST_REQUIRE_EQUAL(reader.read(16), std::uint64_t{0xcdef});
BOOST_REQUIRE_EQUAL(reader.tell(), 0_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{0});
BOOST_REQUIRE_EQUAL(reader.read(24), 0x89abcd_u64); BOOST_REQUIRE_EQUAL(reader.read(24), std::uint64_t{0x89abcd});
BOOST_REQUIRE_EQUAL(reader.tell(), 24_zu); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{24});
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
......
#include <kyut/watermarker/FunctionOrderingWatermarker.hpp>
#include <boost/test/unit_test.hpp>
#include <wasm-io.h>
#include <kyut/BitStreamReader.hpp>
BOOST_AUTO_TEST_SUITE(kyut)
BOOST_AUTO_TEST_SUITE(watermarker)
BOOST_AUTO_TEST_CASE(function_ordering_watermarker) {
wasm::Module module;
wasm::ModuleReader{}.read("../example/test1.wast", module);
BOOST_REQUIRE_EQUAL(module.functions.size(), std::size_t{6});
BOOST_REQUIRE_EQUAL(module.functions[0]->name, "g1");
BOOST_REQUIRE_EQUAL(module.functions[1]->name, "g2");
BOOST_REQUIRE_EQUAL(module.functions[2]->name, "g3");
BOOST_REQUIRE_EQUAL(module.functions[3]->name, "f1");
BOOST_REQUIRE_EQUAL(module.functions[4]->name, "f2");
BOOST_REQUIRE_EQUAL(module.functions[5]->name, "f3");
// Embed 0b00
{
BitStreamReader s{{0b0000'0000}};
const auto numBitsEmbedded = embedFunctionOrdering(module, s, 10);
BOOST_REQUIRE_EQUAL(numBitsEmbedded, std::size_t{2});
BOOST_REQUIRE_EQUAL(module.functions.size(), std::size_t{6});
BOOST_REQUIRE_EQUAL(module.functions[0]->name, "g1");
BOOST_REQUIRE_EQUAL(module.functions[1]->name, "g2");
BOOST_REQUIRE_EQUAL(module.functions[2]->name, "g3");
BOOST_REQUIRE_EQUAL(module.functions[3]->name, "f1");
BOOST_REQUIRE_EQUAL(module.functions[4]->name, "f2");
BOOST_REQUIRE_EQUAL(module.functions[5]->name, "f3");
}
// Embed 0b01
{
BitStreamReader s{{0b0100'0000}};
const auto numBitsEmbedded = embedFunctionOrdering(module, s, 10);
BOOST_REQUIRE_EQUAL(numBitsEmbedded, std::size_t{2});
BOOST_REQUIRE_EQUAL(module.functions.size(), std::size_t{6});
BOOST_REQUIRE_EQUAL(module.functions[0]->name, "g1");
BOOST_REQUIRE_EQUAL(module.functions[1]->name, "g2");
BOOST_REQUIRE_EQUAL(module.functions[2]->name, "g3");
BOOST_REQUIRE_EQUAL(module.functions[3]->name, "f2");
BOOST_REQUIRE_EQUAL(module.functions[4]->name, "f1");
BOOST_REQUIRE_EQUAL(module.functions[5]->name, "f3");
}
// Embed 0b10
{
BitStreamReader s{{0b1000'0000}};
const auto numBitsEmbedded = embedFunctionOrdering(module, s, 10);
BOOST_REQUIRE_EQUAL(numBitsEmbedded, std::size_t{2});
BOOST_REQUIRE_EQUAL(module.functions.size(), std::size_t{6});
BOOST_REQUIRE_EQUAL(module.functions[0]->name, "g1");
BOOST_REQUIRE_EQUAL(module.functions[1]->name, "g2");
BOOST_REQUIRE_EQUAL(module.functions[2]->name, "g3");
BOOST_REQUIRE_EQUAL(module.functions[3]->name, "f3");
BOOST_REQUIRE_EQUAL(module.functions[4]->name, "f1");
BOOST_REQUIRE_EQUAL(module.functions[5]->name, "f2");
}
// Embed 0b11
{
BitStreamReader s{{0b1100'0000}};
const auto numBitsEmbedded = embedFunctionOrdering(module, s, 10);
BOOST_REQUIRE_EQUAL(numBitsEmbedded, std::size_t{2});
BOOST_REQUIRE_EQUAL(module.functions.size(), std::size_t{6});
BOOST_REQUIRE_EQUAL(module.functions[0]->name, "g1");
BOOST_REQUIRE_EQUAL(module.functions[1]->name, "g2");
BOOST_REQUIRE_EQUAL(module.functions[2]->name, "g3");
BOOST_REQUIRE_EQUAL(module.functions[3]->name, "f1");
BOOST_REQUIRE_EQUAL(module.functions[4]->name, "f3");
BOOST_REQUIRE_EQUAL(module.functions[5]->name, "f2");
}
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment