// AkisCozucu: bayt-bayt besleme, tek pakette çoklu çerçeve, kırpık bekleme, // yanlış sihir/tavan → bozuk akış (bağlantı kes sinyali), sifirla kurtarışı. #include "fiko/orkestra/akis.hpp" #include "fiko_test.hpp" using namespace fiko; using namespace fiko::orkestra; namespace { std::string ornekWire(SurecMesajTip tip, const std::string& govde) { SurecCerceve c; c.tip = tip; c.kaynakPid = 5; c.korelasyon = 99; c.govde = govde; return surecproto::cerceveSerialize(c); } } // namespace FIKO_TEST(akis_bayt_bayt_besleme) { AkisCozucu cozucu; std::vector out; const std::string w = ornekWire(SurecMesajTip::TaskAta, "is-tarifi"); for (char b : w) FIKO_KONTROL(cozucu.besle(&b, 1, out)); // hiçbir ara adım bozuk değil FIKO_KONTROL(out.size() == 1); FIKO_KONTROL(out[0].tip == SurecMesajTip::TaskAta); FIKO_KONTROL(out[0].govde == "is-tarifi"); FIKO_KONTROL(out[0].korelasyon == 99); FIKO_KONTROL(cozucu.bekleyenBayt() == 0); } FIKO_TEST(akis_tek_pakette_coklu_cerceve) { AkisCozucu cozucu; std::vector out; const std::string w = ornekWire(SurecMesajTip::Komut, "a") + ornekWire(SurecMesajTip::Onay, "b") + ornekWire(SurecMesajTip::NodeIlan, ""); // boş gövde de geçerli FIKO_KONTROL(cozucu.besle(w.data(), w.size(), out)); FIKO_KONTROL(out.size() == 3); FIKO_KONTROL(out[1].tip == SurecMesajTip::Onay && out[1].govde == "b"); FIKO_KONTROL(out[2].govde.empty()); } FIKO_TEST(akis_kirpik_govde_bekler) { AkisCozucu cozucu; std::vector out; const std::string w = ornekWire(SurecMesajTip::DosyaParca, std::string(1000, 'x')); FIKO_KONTROL(cozucu.besle(w.data(), w.size() - 400, out)); FIKO_KONTROL(out.empty()); // gövde tamam değil — bekliyor FIKO_KONTROL(cozucu.bekleyenBayt() > 0); FIKO_KONTROL(cozucu.besle(w.data() + w.size() - 400, 400, out)); FIKO_KONTROL(out.size() == 1 && out[0].govde.size() == 1000); } FIKO_TEST(akis_coklu_cerceve_arti_kismi_kalan) { // Ofset+tek-erase yolu: 3 tam çerçeve + 4.'ün yarısı → 3 çıkar, kalan bekler, // ikinci beslemede 4. tamamlanır (tüketim ofseti erase ile doğru hizalanmalı). AkisCozucu cozucu; std::vector out; const std::string w1 = ornekWire(SurecMesajTip::Komut, "bir"); const std::string w2 = ornekWire(SurecMesajTip::Onay, "iki"); const std::string w3 = ornekWire(SurecMesajTip::NodeIlan, "uc"); const std::string w4 = ornekWire(SurecMesajTip::DosyaParca, "dort"); const std::string paket = w1 + w2 + w3 + w4; const size_t kes = paket.size() - 3; // w4'ün son 3 baytı eksik FIKO_KONTROL(cozucu.besle(paket.data(), kes, out)); FIKO_KONTROL(out.size() == 3); FIKO_KONTROL(out[2].govde == "uc"); FIKO_KONTROL(cozucu.bekleyenBayt() == w4.size() - 3); // yalnız kısmi w4 kaldı FIKO_KONTROL(cozucu.besle(paket.data() + kes, 3, out)); FIKO_KONTROL(out.size() == 4 && out[3].govde == "dort"); FIKO_KONTROL(cozucu.bekleyenBayt() == 0); } FIKO_TEST(akis_yanlis_sihir_bozuk) { AkisCozucu cozucu; std::vector out; const std::string cop = "GET / HTTP/1.1\r\nHost: saldiri\r\n\r\n"; // yabancı protokol FIKO_KONTROL(!cozucu.besle(cop.data(), cop.size(), out)); FIKO_KONTROL(out.empty()); // Bozuk akışa devam beslemesi de false (çağıran bağlantıyı kesmeli). const std::string w = ornekWire(SurecMesajTip::Komut, "a"); FIKO_KONTROL(!cozucu.besle(w.data(), w.size(), out)); // sifirla → yeni bağlantı için tekrar kullanılabilir. cozucu.sifirla(); FIKO_KONTROL(cozucu.besle(w.data(), w.size(), out)); FIKO_KONTROL(out.size() == 1); } FIKO_TEST(akis_tavan_tam_sinir) { // govdeUzunluk == maksGovde SINIRDA kabul (> maks red). Sınır davranışı kilitli. AkisCozucu tam(100); std::vector out; const std::string w = ornekWire(SurecMesajTip::DosyaParca, std::string(100, 'x')); FIKO_KONTROL(tam.besle(w.data(), w.size(), out)); // tam sınır → geçer FIKO_KONTROL(out.size() == 1 && out[0].govde.size() == 100); AkisCozucu asan(99); std::vector out2; FIKO_KONTROL(!asan.besle(w.data(), w.size(), out2)); // 1 fazla → red } FIKO_TEST(akis_sifir_govde_ve_baslik_sinir) { // Boş gövdeli çerçeve geçerli; başlık tam-1 bayt eksikse bekler (bozuk değil). AkisCozucu c; std::vector out; const std::string w = ornekWire(SurecMesajTip::Heartbeat, ""); FIKO_KONTROL(w.size() == AkisCozucu::BASLIK); // 28B başlık + 0 gövde FIKO_KONTROL(c.besle(w.data(), w.size() - 1, out)); // başlık eksik → bekle (bozuk DEĞİL) FIKO_KONTROL(out.empty() && c.bekleyenBayt() == w.size() - 1); FIKO_KONTROL(c.besle(w.data() + w.size() - 1, 1, out)); FIKO_KONTROL(out.size() == 1 && out[0].govde.empty()); } FIKO_TEST(akis_govde_tavani) { AkisCozucu cozucu(1024); // 1 KB tavan std::vector out; const std::string w = ornekWire(SurecMesajTip::DosyaParca, std::string(2048, 'x')); FIKO_KONTROL(!cozucu.besle(w.data(), w.size(), out)); // bellek şişirme reddi FIKO_KONTROL(out.empty()); AkisCozucu genis(4096); FIKO_KONTROL(genis.besle(w.data(), w.size(), out)); // tavan altında sorunsuz FIKO_KONTROL(out.size() == 1); }