// Native tests of the AppleGVA preference edit (src/rgfx_gva_pref.h): an // absent file is made, the key is set in an XML plist's top-level dict with // every other byte kept, and binary or unreadable files are left. Build and // run with `make test`. #include "rgfx_gva_pref.h" #include #include #include // The edit of `in` (nullptr: absent), and the bytes it writes. static RgfxGvaPref edit(const char *in, std::string *written, size_t cap = 4096) { std::string out(cap, '\0'); size_t len = 0; RgfxGvaPref verdict = rgfx_gva_pref_edit(in, in ? strlen(in) : 0, &out[0], cap, &len); if (verdict == kRgfxGvaPrefWrite) *written = out.substr(0, len); return verdict; } static const char kHead[] = "\n" "\n" "\n"; int main() { std::string w; // An absent file: a fresh plist of the key alone, which an edit leaves. assert(edit(nullptr, &w) == kRgfxGvaPrefWrite); assert(w == kRgfxGvaFresh); assert(edit(w.c_str(), &w) == kRgfxGvaPrefSet); // No room for it. assert(edit(nullptr, &w, 16) == kRgfxGvaPrefUnreadable); // Another key kept, the key put first in the dict. std::string other = std::string(kHead) + "\n\tgvaDebug\n\t3\n\n\n"; assert(edit(other.c_str(), &w) == kRgfxGvaPrefWrite); assert(w == std::string(kHead) + "\n\tgvaForceAMDKE\n\t\n\tgvaDebug\n" "\t3\n\n\n"); assert(edit(w.c_str(), &w) == kRgfxGvaPrefSet); // An empty dict. std::string empty = std::string(kHead) + "\n\n"; assert(edit(empty.c_str(), &w) == kRgfxGvaPrefWrite); assert(w == std::string(kHead) + "\n\tgvaForceAMDKE\n\t\n\n\n"); // The key false, or of another type, becomes true in place. std::string off = std::string(kHead) + "\n\ta\n\tx\n" "\tgvaForceAMDKE\n\t\n\n\n"; assert(edit(off.c_str(), &w) == kRgfxGvaPrefWrite); assert(w == std::string(kHead) + "\n\ta\n\tx\n" "\tgvaForceAMDKE\n\t\n\n\n"); std::string number = std::string(kHead) + "gvaForceAMDKE0"; assert(edit(number.c_str(), &w) == kRgfxGvaPrefWrite); assert(w == std::string(kHead) + "gvaForceAMDKE"); std::string text = std::string(kHead) + "gvaForceAMDKENO"; assert(edit(text.c_str(), &w) == kRgfxGvaPrefWrite); assert(w == std::string(kHead) + "gvaForceAMDKE"); // The key in a nested dict is another key: the top-level one is added. std::string nested = std::string(kHead) + "ngvaForceAMDKE" ""; assert(edit(nested.c_str(), &w) == kRgfxGvaPrefWrite); assert(w == std::string(kHead) + "\n\tgvaForceAMDKE\n\tn" "gvaForceAMDKE"); // A value of a kind it does not edit, a binary plist, no plist, a // top-level array, a cut-off file: left as they are. std::string data = std::string(kHead) + "gvaForceAMDKEAA=="; assert(edit(data.c_str(), &w) == kRgfxGvaPrefUnreadable); assert(edit("bplist00\xd1\x01\x02", &w) == kRgfxGvaPrefBinary); assert(edit("{ gvaForceAMDKE = 1; }", &w) == kRgfxGvaPrefUnreadable); std::string array = std::string(kHead) + ""; assert(edit(array.c_str(), &w) == kRgfxGvaPrefUnreadable); assert(edit("gvaForceAMDKE1"; assert(edit(cut.c_str(), &w) == kRgfxGvaPrefUnreadable); // No room for the edit. assert(edit(other.c_str(), &w, other.size()) == kRgfxGvaPrefUnreadable); printf("gva_pref_test: all checks passed\n"); return 0; }