diff mbox series

[10/13] tests: Add QDict clone-flatten test

Message ID 20180509165530.29561-11-mreitz@redhat.com
State New
Headers show
Series block: Try to create well typed json:{} filenames | expand

Commit Message

Max Reitz May 9, 2018, 4:55 p.m. UTC
This new test verifies that qdict_flatten() does not modify a shallow
clone of the given QDict.

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 tests/check-qdict.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

Comments

Eric Blake May 11, 2018, 6:46 p.m. UTC | #1
On 05/09/2018 11:55 AM, Max Reitz wrote:
> This new test verifies that qdict_flatten() does not modify a shallow
> clone of the given QDict.
> 
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
>   tests/check-qdict.c | 33 +++++++++++++++++++++++++++++++++
>   1 file changed, 33 insertions(+)
> 

I'm not sure I even want to know how long it took you to debug the crash 
that you obviously hit before adding the fix in 9/13 plus this test ;)

Reviewed-by: Eric Blake <eblake@redhat.com>
Max Reitz May 11, 2018, 9:41 p.m. UTC | #2
On 2018-05-11 20:46, Eric Blake wrote:
> On 05/09/2018 11:55 AM, Max Reitz wrote:
>> This new test verifies that qdict_flatten() does not modify a shallow
>> clone of the given QDict.
>>
>> Signed-off-by: Max Reitz <mreitz@redhat.com>
>> ---
>>   tests/check-qdict.c | 33 +++++++++++++++++++++++++++++++++
>>   1 file changed, 33 insertions(+)
>>
> 
> I'm not sure I even want to know how long it took you to debug the crash
> that you obviously hit before adding the fix in 9/13 plus this test ;)

Thank you very much, I made myself forget about that trauma already.

In short, I wondered why the whole thing worked for null-co directly:

driver=null-co,size=512
=> {"driver": "null-co", "size": 512}

But not for null-co through raw:

driver=raw,file.driver=null-co,file.size=512
=> {"file": {}}

(Or something like that, I don't remember exactly.)

With some debugging sprinkled into block.c, I could see that the correct
options were there on the null-co level...  But for some reason they
disappeared one level above.

Then I recalled that dict cloning is just a shallow cloning and looked
for the culprit...

Of course, in reality, much more cursing was involved.

Max
diff mbox series

Patch

diff --git a/tests/check-qdict.c b/tests/check-qdict.c
index ef5d17f815..7960b4a385 100644
--- a/tests/check-qdict.c
+++ b/tests/check-qdict.c
@@ -329,6 +329,38 @@  static void qdict_flatten_test(void)
     qobject_unref(dict3);
 }
 
+static void qdict_clone_flatten_test(void)
+{
+    QDict *dict1 = qdict_new();
+    QDict *dict2 = qdict_new();
+    QDict *cloned_dict1;
+
+    /*
+     * Test that we can clone and flatten
+     *    { "a": { "b": 42 } }
+     * without modifying the clone.
+     */
+
+    qdict_put_int(dict2, "b", 42);
+    qdict_put(dict1, "a", dict2);
+
+    cloned_dict1 = qdict_clone_shallow(dict1);
+
+    qdict_flatten(dict1);
+
+    g_assert(qdict_size(dict1) == 1);
+    g_assert(qdict_get_int(dict1, "a.b") == 42);
+
+    g_assert(qdict_size(cloned_dict1) == 1);
+    g_assert(qdict_get_qdict(cloned_dict1, "a") == dict2);
+
+    g_assert(qdict_size(dict2) == 1);
+    g_assert(qdict_get_int(dict2, "b") == 42);
+
+    qobject_unref(dict1);
+    qobject_unref(cloned_dict1);
+}
+
 static void qdict_array_split_test(void)
 {
     QDict *test_dict = qdict_new();
@@ -1045,6 +1077,7 @@  int main(int argc, char **argv)
     g_test_add_func("/public/to_qdict", qobject_to_qdict_test);
     g_test_add_func("/public/iterapi", qdict_iterapi_test);
     g_test_add_func("/public/flatten", qdict_flatten_test);
+    g_test_add_func("/public/clone-flatten", qdict_clone_flatten_test);
     g_test_add_func("/public/array_split", qdict_array_split_test);
     g_test_add_func("/public/array_entries", qdict_array_entries_test);
     g_test_add_func("/public/join", qdict_join_test);