diff --git a/concore.hpp b/concore.hpp index e3fb5e1..5b8d549 100644 --- a/concore.hpp +++ b/concore.hpp @@ -632,8 +632,14 @@ class Concore{ } } - catch(...){ - cout<<"skipping +"<= SHM_SIZE) { - std::cerr << "ERROR: write_SM payload (" << result.size() - << " bytes) exceeds " << SHM_SIZE - 1 - << "-byte shared memory limit. Data truncated!" << std::endl; + throw std::runtime_error( + "concore SHM write failed: payload (" + + std::to_string(result.size()) + + " bytes) exceeds SHM_SIZE (" + + std::to_string(SHM_SIZE) + + "). Aborting. No data written. Increase SHM_SIZE in concore.hpp." + ); } std::strncpy(sharedData_create, result.c_str(), SHM_SIZE - 1); sharedData_create[SHM_SIZE - 1] = '\0'; @@ -696,8 +709,11 @@ class Concore{ } } - catch(...){ - cout<<"skipping +"<= SHM_SIZE) { + throw std::runtime_error( + "concore SHM write failed: payload (" + + std::to_string(val.size()) + + " bytes) exceeds SHM_SIZE (" + + std::to_string(SHM_SIZE) + + "). Aborting. No data written. Increase SHM_SIZE in concore.hpp." + ); + } try { if(shmId_create != -1){ if (sharedData_create == nullptr) throw 506; - if (val.size() >= SHM_SIZE) { - std::cerr << "ERROR: write_SM payload (" << val.size() - << " bytes) exceeds " << SHM_SIZE - 1 - << "-byte shared memory limit. Data truncated!" << std::endl; - } std::strncpy(sharedData_create, val.c_str(), SHM_SIZE - 1); sharedData_create[SHM_SIZE - 1] = '\0'; } else throw 505; } - catch(...){ - cout<<"skipping +"< + #include + #include + + int main() {{ + try {{ + Concore concore; + std::filesystem::create_directories("out/1"); + concore.delay = 0; + concore.simtime = 0; + std::string payload({payload_size}, 'a'); + concore.write(1, "payload", payload); + if (std::filesystem::exists("out/1/payload")) {{ + std::cerr << "write used the file path instead of shared memory" << std::endl; + return 2; + }} + return 0; + }} catch (const std::exception& error) {{ + std::cerr << error.what() << std::endl; + return 1; + }} + }} + """ + ).lstrip(), + encoding="utf-8", + ) + + compile_result = subprocess.run( + [ + "g++", + "-std=c++17", + "-I", + str(REPO_ROOT), + "-o", + str(binary_file), + str(source_file), + ], + capture_output=True, + text=True, + timeout=60, + cwd=temp_path, + ) + if compile_result.returncode != 0: + pytest.fail(f"g++ compile failed:\n{compile_result.stderr}") + + return subprocess.run( + [str(binary_file)], + capture_output=True, + text=True, + timeout=10, + cwd=temp_path, + ) + + +def test_oversized_payload_throws(): + result = _compile_and_run(5000) + assert result.returncode != 0 + assert "Aborting" in result.stderr + assert "truncated" not in result.stderr.lower() + + +def test_within_limit_succeeds(): + result = _compile_and_run(100) + assert result.returncode == 0 + assert "Aborting" not in result.stderr + assert "write used the file path instead of shared memory" not in result.stderr + + +def test_exactly_at_limit_throws(): + result = _compile_and_run(4096) + assert result.returncode != 0 + assert "Aborting" in result.stderr + + +def test_one_under_limit_succeeds(): + result = _compile_and_run(4095) + assert result.returncode == 0 + assert "Aborting" not in result.stderr + assert "write used the file path instead of shared memory" not in result.stderr