Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Core/MMKV_IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,10 @@ bool MMKV::setDataForKey(MMBuffer &&data, MMKVKey_t key, bool isDataHolder) {
if ((!isDataHolder && data.length() == 0) || isKeyEmpty(key)) {
return false;
}
if (isReadOnly()) {
MMKVWarning("[%s] file readonly", m_mmapID.c_str());
return false;
}
uint32_t ignoredValueLength = 0;
if (!encodedValueLength(data.length(), isDataHolder, ignoredValueLength)) {
MMKVError("[%s] reject value too large to encode: %zu", m_mmapID.c_str(), data.length());
Expand Down
15 changes: 15 additions & 0 deletions POSIX/demo/UnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,20 @@ void testReadOnlyExpiration() {
printf("test read-only expiration: passed\n");
}

void testReadOnlyRejectsSet() {
auto kv = MMKV::mmkvWithID("read_only_set");
kv->clearAll();
assert(kv->set("value1", "key"));
kv->close();

kv = MMKV::mmkvWithID("read_only_set", MMKV_SINGLE_PROCESS | MMKV_READ_ONLY);
assert(!kv->set("value2", "key"));
string value;
assert(kv->getString("key", value) && value == "value1");
kv->close();
printf("test read-only set: passed\n");
}

void testFirstWriteAfterReopenPreservesValue() {
const string id = "first-write-expiration";
auto kv = MMKV::mmkvWithID(id);
Expand Down Expand Up @@ -1351,6 +1365,7 @@ int main(int argc, char *argv[]) {
#endif
testFirstReadAfterReopenRespectsExpiration();
testReadOnlyExpiration();
testReadOnlyRejectsSet();
testFirstWriteAfterReopenPreservesValue();
testFirstWriteAfterReopenOverloads();
}