forked from libvirt/libvirt
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtestutilsqemuschema.c
692 lines (565 loc) · 21.1 KB
/
testutilsqemuschema.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*
* testutilsqemuschema.c: helper functions for QEMU QAPI schema testing
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include "testutils.h"
#include "testutilsqemu.h"
#include "testutilsqemuschema.h"
#include "qemu/qemu_qapi.h"
struct testQEMUSchemaValidateCtxt {
virHashTablePtr schema;
virBufferPtr debug;
bool allowDeprecated;
};
static int
testQEMUSchemaValidateRecurse(virJSONValuePtr obj,
virJSONValuePtr root,
struct testQEMUSchemaValidateCtxt *ctxt);
static int
testQEMUSchemaValidateBuiltin(virJSONValuePtr obj,
virJSONValuePtr root,
struct testQEMUSchemaValidateCtxt *ctxt)
{
const char *t = virJSONValueObjectGetString(root, "json-type");
const char *s = NULL;
bool b = false;
int ret = -1;
if (STREQ_NULLABLE(t, "value")) {
s = "{any}";
ret = 0;
goto cleanup;
}
switch (virJSONValueGetType(obj)) {
case VIR_JSON_TYPE_STRING:
if (STRNEQ_NULLABLE(t, "string"))
goto cleanup;
s = virJSONValueGetString(obj);
break;
case VIR_JSON_TYPE_NUMBER:
if (STRNEQ_NULLABLE(t, "int") &&
STRNEQ_NULLABLE(t, "number"))
goto cleanup;
s = "{number}";
break;
case VIR_JSON_TYPE_BOOLEAN:
if (STRNEQ_NULLABLE(t, "boolean"))
goto cleanup;
virJSONValueGetBoolean(obj, &b);
if (b)
s = "true";
else
s = "false";
break;
case VIR_JSON_TYPE_NULL:
if (STRNEQ_NULLABLE(t, "null"))
goto cleanup;
break;
case VIR_JSON_TYPE_OBJECT:
case VIR_JSON_TYPE_ARRAY:
goto cleanup;
}
ret = 0;
cleanup:
if (ret == 0)
virBufferAsprintf(ctxt->debug, "'%s': OK", s);
else
virBufferAsprintf(ctxt->debug, "ERROR: expected type '%s', actual type %d",
t, virJSONValueGetType(obj));
return ret;
}
struct testQEMUSchemaValidateObjectMemberData {
virJSONValuePtr rootmembers;
struct testQEMUSchemaValidateCtxt *ctxt;
bool missingMandatory;
};
static virJSONValuePtr
testQEMUSchemaStealObjectMemberByName(const char *name,
virJSONValuePtr members)
{
virJSONValuePtr member;
virJSONValuePtr ret = NULL;
size_t i;
for (i = 0; i < virJSONValueArraySize(members); i++) {
member = virJSONValueArrayGet(members, i);
if (STREQ_NULLABLE(name, virJSONValueObjectGetString(member, "name"))) {
ret = virJSONValueArraySteal(members, i);
break;
}
}
return ret;
}
static int
testQEMUSchemaValidateObjectMember(const char *key,
virJSONValuePtr value,
void *opaque)
{
struct testQEMUSchemaValidateObjectMemberData *data = opaque;
g_autoptr(virJSONValue) keymember = NULL;
const char *keytype;
virJSONValuePtr keyschema = NULL;
int rc;
virBufferStrcat(data->ctxt->debug, key, ": ", NULL);
/* lookup 'member' entry for key */
if (!(keymember = testQEMUSchemaStealObjectMemberByName(key, data->rootmembers))) {
virBufferAddLit(data->ctxt->debug, "ERROR: attribute not in schema\n");
return -1;
}
/* lookup schema entry for keytype */
if (!(keytype = virJSONValueObjectGetString(keymember, "type")) ||
!(keyschema = virHashLookup(data->ctxt->schema, keytype))) {
virBufferAsprintf(data->ctxt->debug, "ERROR: can't find schema for type '%s'\n",
NULLSTR(keytype));
return -2;
}
/* recurse */
rc = testQEMUSchemaValidateRecurse(value, keyschema, data->ctxt);
virBufferAddLit(data->ctxt->debug, "\n");
return rc;
}
static int
testQEMUSchemaValidateObjectMergeVariantMember(size_t pos G_GNUC_UNUSED,
virJSONValuePtr item,
void *opaque)
{
virJSONValuePtr array = opaque;
virJSONValuePtr copy;
if (!(copy = virJSONValueCopy(item)))
return -1;
if (virJSONValueArrayAppend(array, copy) < 0)
return -1;
return 1;
}
/**
* testQEMUSchemaValidateObjectMergeVariant:
*
* Merges schema of variant @variantname in @root into @root and removes the
* 'variants' array from @root.
*/
static int
testQEMUSchemaValidateObjectMergeVariant(virJSONValuePtr root,
const char *variantfield,
const char *variantname,
struct testQEMUSchemaValidateCtxt *ctxt)
{
size_t i;
g_autoptr(virJSONValue) variants = NULL;
virJSONValuePtr variant;
virJSONValuePtr variantschema;
virJSONValuePtr variantschemamembers;
virJSONValuePtr rootmembers;
const char *varianttype = NULL;
if (!(variants = virJSONValueObjectStealArray(root, "variants"))) {
virBufferAddLit(ctxt->debug, "ERROR: missing 'variants' in schema\n");
return -2;
}
for (i = 0; i < virJSONValueArraySize(variants); i++) {
variant = virJSONValueArrayGet(variants, i);
if (STREQ_NULLABLE(variantname,
virJSONValueObjectGetString(variant, "case"))) {
varianttype = virJSONValueObjectGetString(variant, "type");
break;
}
}
if (!varianttype) {
virBufferAsprintf(ctxt->debug, "ERROR: variant '%s' for discriminator '%s' not found\n",
variantname, variantfield);
return -1;
}
if (!(variantschema = virHashLookup(ctxt->schema, varianttype)) ||
!(variantschemamembers = virJSONValueObjectGetArray(variantschema, "members"))) {
virBufferAsprintf(ctxt->debug,
"ERROR: missing schema or schema members for variant '%s'(%s)\n",
variantname, varianttype);
return -2;
}
rootmembers = virJSONValueObjectGetArray(root, "members");
if (virJSONValueArrayForeachSteal(variantschemamembers,
testQEMUSchemaValidateObjectMergeVariantMember,
rootmembers) < 0) {
return -2;
}
return 0;
}
static int
testQEMUSchemaValidateObjectMandatoryMember(size_t pos G_GNUC_UNUSED,
virJSONValuePtr item,
void *opaque G_GNUC_UNUSED)
{
struct testQEMUSchemaValidateObjectMemberData *data = opaque;
if (virJSONValueObjectHasKey(item, "default") != 1) {
virBufferAsprintf(data->ctxt->debug, "ERROR: missing mandatory attribute '%s'\n",
NULLSTR(virJSONValueObjectGetString(item, "name")));
data->missingMandatory = true;
}
return 1;
}
static int
testQEMUSchemaValidateObject(virJSONValuePtr obj,
virJSONValuePtr root,
struct testQEMUSchemaValidateCtxt *ctxt)
{
struct testQEMUSchemaValidateObjectMemberData data = { NULL, ctxt, false };
g_autoptr(virJSONValue) localroot = NULL;
const char *variantfield;
const char *variantname;
if (virJSONValueGetType(obj) != VIR_JSON_TYPE_OBJECT) {
virBufferAddLit(ctxt->debug, "ERROR: not an object");
return -1;
}
virBufferAddLit(ctxt->debug, "{\n");
virBufferAdjustIndent(ctxt->debug, 3);
/* copy schema */
if (!(localroot = virJSONValueCopy(root)))
return -2;
/* remove variant */
if ((variantfield = virJSONValueObjectGetString(localroot, "tag"))) {
if (!(variantname = virJSONValueObjectGetString(obj, variantfield))) {
virBufferAsprintf(ctxt->debug, "ERROR: missing variant discriminator attribute '%s'\n",
variantfield);
return -1;
}
if (testQEMUSchemaValidateObjectMergeVariant(localroot, variantfield,
variantname, ctxt) < 0)
return -1;
}
/* validate members */
data.rootmembers = virJSONValueObjectGetArray(localroot, "members");
if (virJSONValueObjectForeachKeyValue(obj,
testQEMUSchemaValidateObjectMember,
&data) < 0)
return -1;
/* check missing mandatory values */
if (virJSONValueArrayForeachSteal(data.rootmembers,
testQEMUSchemaValidateObjectMandatoryMember,
&data) < 0) {
return -2;
}
if (data.missingMandatory)
return -1;
virBufferAdjustIndent(ctxt->debug, -3);
virBufferAddLit(ctxt->debug, "} OK");
return 0;
}
static int
testQEMUSchemaValidateEnum(virJSONValuePtr obj,
virJSONValuePtr root,
struct testQEMUSchemaValidateCtxt *ctxt)
{
const char *objstr;
virJSONValuePtr values = NULL;
virJSONValuePtr value;
size_t i;
if (virJSONValueGetType(obj) != VIR_JSON_TYPE_STRING) {
virBufferAddLit(ctxt->debug, "ERROR: not a string");
return -1;
}
objstr = virJSONValueGetString(obj);
if (!(values = virJSONValueObjectGetArray(root, "values"))) {
virBufferAsprintf(ctxt->debug, "ERROR: missing enum values in schema '%s'",
NULLSTR(virJSONValueObjectGetString(root, "name")));
return -2;
}
for (i = 0; i < virJSONValueArraySize(values); i++) {
value = virJSONValueArrayGet(values, i);
if (STREQ_NULLABLE(objstr, virJSONValueGetString(value))) {
virBufferAsprintf(ctxt->debug, "'%s' OK", NULLSTR(objstr));
return 0;
}
}
virBufferAsprintf(ctxt->debug, "ERROR: enum value '%s' is not in schema",
NULLSTR(objstr));
return -1;
}
static int
testQEMUSchemaValidateArray(virJSONValuePtr objs,
virJSONValuePtr root,
struct testQEMUSchemaValidateCtxt *ctxt)
{
const char *elemtypename = virJSONValueObjectGetString(root, "element-type");
virJSONValuePtr elementschema;
virJSONValuePtr obj;
size_t i;
if (virJSONValueGetType(objs) != VIR_JSON_TYPE_ARRAY) {
virBufferAddLit(ctxt->debug, "ERROR: not an array\n");
return -1;
}
if (!elemtypename ||
!(elementschema = virHashLookup(ctxt->schema, elemtypename))) {
virBufferAsprintf(ctxt->debug, "ERROR: missing schema for array element type '%s'",
NULLSTR(elemtypename));
return -2;
}
virBufferAddLit(ctxt->debug, "[\n");
virBufferAdjustIndent(ctxt->debug, 3);
for (i = 0; i < virJSONValueArraySize(objs); i++) {
obj = virJSONValueArrayGet(objs, i);
if (testQEMUSchemaValidateRecurse(obj, elementschema, ctxt) < 0)
return -1;
virBufferAddLit(ctxt->debug, ",\n");
}
virBufferAddLit(ctxt->debug, "] OK");
virBufferAdjustIndent(ctxt->debug, -3);
return 0;
}
static int
testQEMUSchemaValidateAlternate(virJSONValuePtr obj,
virJSONValuePtr root,
struct testQEMUSchemaValidateCtxt *ctxt)
{
virJSONValuePtr members;
virJSONValuePtr member;
size_t i;
size_t n;
const char *membertype;
virJSONValuePtr memberschema;
int indent;
int rc;
if (!(members = virJSONValueObjectGetArray(root, "members"))) {
virBufferAddLit(ctxt->debug, "ERROR: missing 'members' for alternate schema");
return -2;
}
virBufferAddLit(ctxt->debug, "(\n");
virBufferAdjustIndent(ctxt->debug, 3);
indent = virBufferGetIndent(ctxt->debug);
n = virJSONValueArraySize(members);
for (i = 0; i < n; i++) {
membertype = NULL;
/* P != NP */
virBufferAsprintf(ctxt->debug, "(alternate %zu/%zu)\n", i + 1, n);
virBufferAdjustIndent(ctxt->debug, 3);
if (!(member = virJSONValueArrayGet(members, i)) ||
!(membertype = virJSONValueObjectGetString(member, "type")) ||
!(memberschema = virHashLookup(ctxt->schema, membertype))) {
virBufferAsprintf(ctxt->debug, "ERROR: missing schema for alternate type '%s'",
NULLSTR(membertype));
return -2;
}
rc = testQEMUSchemaValidateRecurse(obj, memberschema, ctxt);
virBufferAddLit(ctxt->debug, "\n");
virBufferSetIndent(ctxt->debug, indent);
virBufferAsprintf(ctxt->debug, "(/alternate %zu/%zu)\n", i + 1, n);
if (rc == 0) {
virBufferAdjustIndent(ctxt->debug, -3);
virBufferAddLit(ctxt->debug, ") OK");
return 0;
}
}
virBufferAddLit(ctxt->debug, "ERROR: no alternate type was matched");
return -1;
}
static int
testQEMUSchemaValidateDeprecated(virJSONValuePtr root,
const char *name,
struct testQEMUSchemaValidateCtxt *ctxt)
{
virJSONValuePtr features = virJSONValueObjectGetArray(root, "features");
size_t nfeatures;
size_t i;
if (!features)
return 0;
nfeatures = virJSONValueArraySize(features);
for (i = 0; i < nfeatures; i++) {
virJSONValuePtr cur = virJSONValueArrayGet(features, i);
const char *curstr;
if (!cur ||
!(curstr = virJSONValueGetString(cur))) {
virBufferAsprintf(ctxt->debug, "ERROR: features of '%s' are malformed", name);
return -2;
}
if (STREQ(curstr, "deprecated")) {
if (ctxt->allowDeprecated) {
virBufferAsprintf(ctxt->debug, "WARNING: '%s' is deprecated", name);
if (virTestGetVerbose())
g_fprintf(stderr, "\nWARNING: '%s' is deprecated\n", name);
return 0;
} else {
virBufferAsprintf(ctxt->debug, "ERROR: '%s' is deprecated", name);
return -1;
}
}
}
return 0;
}
static int
testQEMUSchemaValidateRecurse(virJSONValuePtr obj,
virJSONValuePtr root,
struct testQEMUSchemaValidateCtxt *ctxt)
{
const char *n = virJSONValueObjectGetString(root, "name");
const char *t = virJSONValueObjectGetString(root, "meta-type");
int rc;
if ((rc = testQEMUSchemaValidateDeprecated(root, n, ctxt)) < 0)
return rc;
if (STREQ_NULLABLE(t, "builtin")) {
return testQEMUSchemaValidateBuiltin(obj, root, ctxt);
} else if (STREQ_NULLABLE(t, "object")) {
return testQEMUSchemaValidateObject(obj, root, ctxt);
} else if (STREQ_NULLABLE(t, "enum")) {
return testQEMUSchemaValidateEnum(obj, root, ctxt);
} else if (STREQ_NULLABLE(t, "array")) {
return testQEMUSchemaValidateArray(obj, root, ctxt);
} else if (STREQ_NULLABLE(t, "alternate")) {
return testQEMUSchemaValidateAlternate(obj, root, ctxt);
}
virBufferAsprintf(ctxt->debug,
"qapi schema meta-type '%s' of type '%s' not handled\n",
NULLSTR(t), NULLSTR(n));
return -2;
}
/**
* testQEMUSchemaValidate:
* @obj: object to validate
* @root: schema entry to start from
* @schema: hash table containing schema entries
* @debug: a virBuffer which will be filled with debug information if provided
*
* Validates whether @obj conforms to the QAPI schema passed in via @schema,
* starting from the node @root. Returns 0, if @obj matches @schema, -1 if it
* does not and -2 if there is a problem with the schema or with internals.
*
* @debug is filled with information regarding the validation process
*/
int
testQEMUSchemaValidate(virJSONValuePtr obj,
virJSONValuePtr root,
virHashTablePtr schema,
bool allowDeprecated,
virBufferPtr debug)
{
struct testQEMUSchemaValidateCtxt ctxt = { .schema = schema,
.debug = debug,
.allowDeprecated = allowDeprecated };
return testQEMUSchemaValidateRecurse(obj, root, &ctxt);
}
/**
* testQEMUSchemaValidateCommand:
* @command: command to validate
* @arguments: arguments of @command to validate
* @schema: hash table containing schema entries
* @allowDeprecated: don't fails schema validation if @command or one of @arguments
* is deprecated
* @allowRemoved: skip validation fully if @command was not found
* @debug: a virBuffer which will be filled with debug information if provided
*
* Validates whether @command and its @arguments conform to the QAPI schema
* passed in via @schema. Returns 0, if the command and args match @schema,
* -1 if it does not and -2 if there is a problem with the schema or with
* internals.
*
* @allowRemoved should generally be used only if it's certain that there's a
* replacement of @command in place.
*
* @debug is filled with information regarding the validation process
*/
int
testQEMUSchemaValidateCommand(const char *command,
virJSONValuePtr arguments,
virHashTablePtr schema,
bool allowDeprecated,
bool allowRemoved,
virBufferPtr debug)
{
struct testQEMUSchemaValidateCtxt ctxt = { .schema = schema,
.debug = debug,
.allowDeprecated = allowDeprecated };
g_autofree char *schemapatharguments = g_strdup_printf("%s/arg-type", command);
g_autoptr(virJSONValue) emptyargs = NULL;
virJSONValuePtr schemarootcommand;
virJSONValuePtr schemarootarguments;
int rc;
if (virQEMUQAPISchemaPathGet(command, schema, &schemarootcommand) < 0 ||
!schemarootcommand) {
if (allowRemoved)
return 0;
virBufferAsprintf(debug, "ERROR: command '%s' not found in the schema", command);
return -1;
}
if ((rc = testQEMUSchemaValidateDeprecated(schemarootcommand, command, &ctxt)) < 0)
return rc;
if (!arguments)
arguments = emptyargs = virJSONValueNewObject();
if (virQEMUQAPISchemaPathGet(schemapatharguments, schema, &schemarootarguments) < 0 ||
!schemarootarguments) {
virBufferAsprintf(debug, "ERROR: failed to look up 'arg-type' of '%s'", command);
return -1;
}
return testQEMUSchemaValidateRecurse(arguments, schemarootarguments, &ctxt);
}
static virJSONValuePtr
testQEMUSchemaLoadReplies(const char *filename)
{
g_autofree char *caps = NULL;
char *schemaReply;
char *end;
g_autoptr(virJSONValue) reply = NULL;
virJSONValuePtr schema = NULL;
if (virTestLoadFile(filename, &caps) < 0)
return NULL;
if (!(schemaReply = strstr(caps, "\"execute\": \"query-qmp-schema\"")) ||
!(schemaReply = strstr(schemaReply, "\n\n")) ||
!(end = strstr(schemaReply + 2, "\n\n"))) {
VIR_TEST_VERBOSE("failed to find reply to 'query-qmp-schema' in '%s'",
filename);
return NULL;
}
schemaReply += 2;
*end = '\0';
if (!(reply = virJSONValueFromString(schemaReply))) {
VIR_TEST_VERBOSE("failed to parse 'query-qmp-schema' reply from '%s'",
filename);
return NULL;
}
if (!(schema = virJSONValueObjectStealArray(reply, "return"))) {
VIR_TEST_VERBOSE("missing qapi schema data in reply in '%s'",
filename);
return NULL;
}
return schema;
}
/**
* testQEMUSchemaGetLatest:
*
* Returns the schema data as the qemu monitor would reply from the latest
* replies file used for qemucapabilitiestest for the x86_64 architecture.
*/
virJSONValuePtr
testQEMUSchemaGetLatest(const char *arch)
{
g_autofree char *capsLatestFile = NULL;
if (!(capsLatestFile = testQemuGetLatestCapsForArch(arch, "replies"))) {
VIR_TEST_VERBOSE("failed to find latest caps replies");
return NULL;
}
VIR_TEST_DEBUG("replies file: '%s'", capsLatestFile);
return testQEMUSchemaLoadReplies(capsLatestFile);
}
virHashTablePtr
testQEMUSchemaLoadLatest(const char *arch)
{
virJSONValuePtr schema;
if (!(schema = testQEMUSchemaGetLatest(arch)))
return NULL;
return virQEMUQAPISchemaConvert(schema);
}
virHashTablePtr
testQEMUSchemaLoad(const char *filename)
{
virJSONValuePtr schema;
if (!(schema = testQEMUSchemaLoadReplies(filename)))
return NULL;
return virQEMUQAPISchemaConvert(schema);
}