Cross Reference: /pkg/src/tests/api/t_action.py
t_action.py revision 3234
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
1516N/A#!/usr/bin/python
305N/A#
305N/A# CDDL HEADER START
305N/A#
305N/A# The contents of this file are subject to the terms of the
305N/A# Common Development and Distribution License (the "License").
305N/A# You may not use this file except in compliance with the License.
305N/A#
305N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
305N/A# or http://www.opensolaris.org/os/licensing.
305N/A# See the License for the specific language governing permissions
305N/A# and limitations under the License.
305N/A#
305N/A# When distributing Covered Code, include this CDDL HEADER in each
305N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
305N/A# If applicable, add the following below this CDDL HEADER, with the
305N/A# fields enclosed by brackets "[]" replaced with your own identifying
305N/A# information: Portions Copyright [yyyy] [name of copyright owner]
305N/A#
305N/A# CDDL HEADER END
305N/A#
305N/A
2026N/A#
3158N/A# Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
2026N/A#
305N/A
1715N/Aimport testutils
1715N/Aif __name__ == "__main__":
1715N/A testutils.setup_environment("../../../proto")
1715N/Aimport pkg5unittest
1715N/A
3234N/Aimport six
526N/Aimport unittest
305N/Aimport pkg.actions as action
2026N/Aimport pkg.actions.generic as generic
2026N/Aimport pkg.actions.signature as signature
2026N/Aimport pkg.client.api_errors as api_errors
305N/A
430N/Aimport os
430N/Aimport sys
430N/A
430N/Aclass TestActions(pkg5unittest.Pkg5TestCase):
305N/A
2026N/A
2026N/A act_strings = [
2026N/A "set name=foo value=foo",
2026N/A "set name=foo value=\"\"",
2026N/A "set name=foo value=f'o'o",
2026N/A "set name=foo value='f\"o \"o'",
2026N/A "set name=foo value='b\"a \"r' value='f\"o \"o'",
2026N/A "set name=foo value=\"f'o 'o\"",
2026N/A "set name=foo value=\"b'a 'r\" value=\"f'o 'o\"",
2026N/A "set name=foo value='f\"o \\' \"o'",
2026N/A "set name=foo value='b\"a \\' \"r' value='f\"o \\' \"o'",
2026N/A "set name=foo value='\"foo\"'",
2026N/A "set name=foo value='\"bar\"'value='\"foo\"'",
2026N/A "set name=foo value=\"'foo'\"",
2026N/A "set name=foo value=\"'bar'\" value=\"'foo'\"",
2026N/A "set name=foo value='\"fo\\\'o\"'",
2026N/A "set name=foo value='\"ba\\\'r\"' value='\"fo\\\'o\"'",
2026N/A "set name=foo value=\"'fo\\\"o'\"",
2026N/A "set name=foo value=\"'ba\\\"r'\" value=\"'fo\\\"o'\"",
2026N/A 'set name=foo value=ab value="" value=c',
2026N/A "file 12345 name=foo path=/tmp/foo",
2026N/A "file 12345 name=foo attr=bar path=/tmp/foo",
2026N/A "file 12345 name=foo attr=bar attr=bar path=/tmp/foo",
2026N/A "file 12345 name=foo attr=bar path=/tmp/foo",
2026N/A "file 12345 name=foo path=/tmp/foo attr=bar ",
2026N/A "file 12345 name=foo path=/tmp/foo attr=bar ",
2026N/A "file 12345 name=\"foo bar\" attr=\"bar baz\" path=/tmp/foo",
2026N/A "file 12345 name=\"foo bar\" attr=\"bar baz\" path=/tmp/foo",
2026N/A "file 12345 name=foo value=barbaz path=/tmp/foo",
2026N/A "file 12345 name=foo value=\"bar baz\" path=/tmp/foo",
2026N/A "file 12345 name=\"foo bar\" value=baz path=/tmp/foo",
2026N/A "file 12345 name=foo value=barbazquux path=/tmp/foo",
2026N/A "file 12345 name=foo value=\"bar baz quux\" path=/tmp/foo",
2026N/A "file 12345 name=\"foo bar baz\" value=quux path=/tmp/foo",
2026N/A "file 12345 name=\"foo\" value=\"bar\" path=/tmp/foo",
2026N/A "file 12345 name=foo value=\"bar\" path=/tmp/foo",
2026N/A "file 12345 name=\"foo\" value=bar path=/tmp/foo",
2026N/A "file 12345 name='foo' value=bar path=/tmp/foo",
2026N/A "file 12345 name='f\"o\"o' value=bar path=/tmp/foo",
2026N/A "file 12345 name='f\\'o\\'o' value=bar path=/tmp/foo",
2026N/A "file 12345 name=foo\tvalue=bar path=/tmp/foo",
2026N/A "driver alias=pci1234,56 alias=pci4567,89 class=scsi name=lsimega",
2026N/A "signature 12345 algorithm=foo",
2026N/A ]
2476N/A
591N/A def assertAttributeValue(self, action, attr, value):
591N/A attrs = action.attrs[attr]
591N/A
591N/A if isinstance(attrs, list):
591N/A attrs.sort()
591N/A if isinstance(value, list):
591N/A value.sort()
591N/A
591N/A if attrs != value:
591N/A self.fail("""\
591N/AIncorrect attribute value.
3158N/A Expected: {0}
3158N/A Actual: {1}""".format(value, attrs))
591N/A
591N/A def assertAttributes(self, action, attrlist):
591N/A if sorted(action.attrs.keys()) != sorted(attrlist):
591N/A self.fail("""\
591N/AIncorrect attribute list.
3158N/A Expected: {0}
3158N/A Actual: {1}""".format(sorted(attrlist), sorted(action.attrs.keys())))
591N/A
315N/A def test_action_parser(self):
873N/A action.fromstr("file 12345 name=foo path=/tmp/foo")
873N/A action.fromstr("file 12345 name=foo attr=bar path=/tmp/foo")
873N/A action.fromstr("file 12345 name=foo attr=bar attr=bar path=/tmp/foo")
305N/A
873N/A action.fromstr("file 12345 name=foo path=/tmp/foo attr=bar")
873N/A action.fromstr("file 12345 name=foo path=/tmp/foo attr=bar ")
873N/A action.fromstr("file 12345 name=foo path=/tmp/foo attr=bar ")
315N/A
873N/A action.fromstr("file 12345 name=\"foo bar\" path=\"/tmp/foo\" attr=\"bar baz\"")
873N/A action.fromstr("file 12345 name=\"foo bar\" path=\"/tmp/foo\" attr=\"bar baz\"")
315N/A
873N/A action.fromstr("file 12345 name=foo value=barbaz path=/tmp/foo")
873N/A action.fromstr("file 12345 name=foo value=\"bar baz\" path=/tmp/foo")
873N/A action.fromstr("file 12345 name=\"foo bar\" value=baz path=/tmp/foo")
381N/A
873N/A action.fromstr("file 12345 name=foo value=barbazquux path=/tmp/foo")
873N/A action.fromstr("file 12345 name=foo value=\"bar baz quux\" path=/tmp/foo")
873N/A action.fromstr("file 12345 name=\"foo bar baz\" value=quux path=/tmp/foo")
381N/A
873N/A action.fromstr("file 12345 name=\"foo\" value=\"bar\" path=/tmp/foo")
873N/A action.fromstr("file 12345 name=foo value=\"bar\" path=/tmp/foo")
873N/A action.fromstr("file 12345 name=\"foo\" value=bar path=/tmp/foo")
381N/A
2026N/A action.fromstr("signature 12345 algorithm=foo")
1423N/A
2639N/A # For convenience, we allow set actions to be expressed as
2639N/A # "<name>=<value>", rather than "name=<name> value=<value>", but
2639N/A # we always convert to the latter. Verify that both forms are
2639N/A # parsed as expected.
2639N/A a = action.fromstr("set pkg.obsolete=true")
2639N/A a2 = action.fromstr("set name=pkg.obsolete value=true")
2639N/A self.assertEqual(str(a), str(a2))
2639N/A self.assertAttributes(a, ["name", "value"])
2639N/A self.assertAttributeValue(a, "name", "pkg.obsolete")
2639N/A self.assertAttributeValue(a, "value", "true")
2639N/A
591N/A # Single quoted value
873N/A a = action.fromstr("file 12345 name='foo' value=bar path=/tmp/foo")
873N/A self.assertAttributes(a, ["name", "value", "path"])
591N/A self.assertAttributeValue(a, "name", "foo")
591N/A self.assertAttributeValue(a, "value", "bar")
941N/A self.assertAttributeValue(a, "path", "tmp/foo")
591N/A
591N/A # Make sure that unescaped double quotes are parsed properly
591N/A # inside a single-quoted value.
873N/A a = action.fromstr("file 12345 name='f\"o\"o' value=bar path=/tmp/foo")
873N/A self.assertAttributes(a, ["name", "path", "value"])
591N/A self.assertAttributeValue(a, "name", "f\"o\"o")
591N/A self.assertAttributeValue(a, "value", "bar")
941N/A self.assertAttributeValue(a, "path", "tmp/foo")
591N/A
591N/A # Make sure that escaped single quotes are parsed properly
591N/A # inside a single-quoted value.
873N/A a = action.fromstr("file 12345 name='f\\'o\\'o' value=bar path=/tmp/foo")
873N/A self.assertAttributes(a, ["name", "path", "value"])
591N/A self.assertAttributeValue(a, "name", "f'o'o")
591N/A self.assertAttributeValue(a, "value", "bar")
941N/A self.assertAttributeValue(a, "path", "tmp/foo")
591N/A
591N/A # You should be able to separate key/value pairs with tabs as
591N/A # well as spaces.
873N/A a = action.fromstr("file 12345 name=foo\tvalue=bar path=/tmp/foo")
873N/A self.assertAttributes(a, ["name", "path", "value"])
591N/A self.assertAttributeValue(a, "name", "foo")
591N/A self.assertAttributeValue(a, "value", "bar")
941N/A self.assertAttributeValue(a, "path", "tmp/foo")
591N/A
591N/A # Unescaped, unpaired quotes are allowed in the middle of values
591N/A # without further quoting
873N/A a = action.fromstr("file 12345 name=foo\"bar path=/tmp/foo")
873N/A self.assertAttributes(a, ["name", "path"])
591N/A self.assertAttributeValue(a, "name", "foo\"bar")
941N/A self.assertAttributeValue(a, "path", "tmp/foo")
591N/A
591N/A # They can even be paired. Note this is not like shell quoting.
873N/A a = action.fromstr("file 12345 name=foo\"bar\"baz path=/tmp/foo")
873N/A self.assertAttributes(a, ["name", "path"])
591N/A self.assertAttributeValue(a, "name", "foo\"bar\"baz")
941N/A self.assertAttributeValue(a, "path", "tmp/foo")
591N/A
591N/A # An unquoted value can end in an escaped backslash
873N/A a = action.fromstr("file 12345 name=foo\\ path=/tmp/foo")
873N/A self.assertAttributes(a, ["name", "path"])
591N/A self.assertAttributeValue(a, "name", "foo\\")
941N/A self.assertAttributeValue(a, "path", "tmp/foo")
591N/A
591N/A # An action with multiple identical attribute names should
591N/A # result in an attribute with a list value.
591N/A a = action.fromstr("driver alias=pci1234,56 alias=pci4567,89 class=scsi name=lsimega")
591N/A self.assertAttributes(a, ["alias", "class", "name"])
591N/A self.assertAttributeValue(a, "alias", ["pci1234,56", "pci4567,89"])
305N/A
1384N/A # An action with an empty value.
1384N/A a = action.fromstr('set name=foo value=""')
1384N/A self.assertAttributes(a, ["name", "value"])
1384N/A self.assertAttributeValue(a, "name", "foo")
1384N/A self.assertAttributeValue(a, "value", "")
1384N/A
1384N/A # An action with an empty value as part of a list.
1384N/A a = action.fromstr('set name=foo value=ab value="" value=c')
1384N/A self.assertAttributes(a, ["name", "value"])
1384N/A self.assertAttributeValue(a, "name", "foo")
1384N/A self.assertAttributeValue(a, "value", ["ab", "c", ""])
1384N/A
1618N/A # An action with its key attribute and extra attributes that
1618N/A # are not used by the package system.
1618N/A a = action.fromstr('license license="Common Development and '
1618N/A 'Distribution License 1.0 (CDDL)" custom="foo" '
1618N/A 'bool_val=true')
1618N/A self.assertAttributes(a, ["license", "custom", "bool_val"])
1618N/A self.assertAttributeValue(a, "license", 'Common Development '
1618N/A 'and Distribution License 1.0 (CDDL)')
1618N/A self.assertAttributeValue(a, "custom", "foo")
1618N/A self.assertAttributeValue(a, "bool_val", "true")
1618N/A
2233N/A # Really long actions with lots of backslash-escaped quotes
2233N/A # should work.
2233N/A a = action.fromstr(r'set name=pkg.description value="Sphinx is a tool that makes it easy to create intelligent \"and beautiful documentation f\"or Python projects (or \"other documents consisting of\" multiple reStructuredText so\"urces), written by Georg Bran\"dl. It was originally created\" to translate the new Python \"documentation, but has now be\"en cleaned up in the hope tha\"t it will be useful to many o\"ther projects. Sphinx uses re\"StructuredText as its markup \"language, and many of its str\"engths come from the power an\"d straightforwardness of reSt\"ructuredText and its parsing \"and translating suite, the Do\"cutils. Although it is still \"under constant development, t\"he following features are alr\"eady present, work fine and c\"an be seen \"in action\" \"in the Python docs: * Output \"formats: HTML (including Wind\"ows HTML Help) and LaTeX, for\" printable PDF versions * Ext\"ensive cross-references: sema\"ntic markup and automatic lin\"ks for functions, classes, gl\"ossary terms and similar piec\"es of information * Hierarchi\"cal structure: easy definitio\"n of a document tree, with au\"tomatic links to siblings, pa\"rents and children * Automati\"c indices: general index as w\"ell as a module index * Code \"handling: automatic highlight\"ing using the Pygments highli\"ghter * Various extensions ar\"e available, e.g. for automat\"ic testing of snippets and in\"clusion of appropriately formatted docstrings."')
2233N/A self.assert_(a.attrs["value"].count('"') == 45)
2233N/A
2457N/A # Make sure that the hash member of the action object properly
2457N/A # contains the value of the "hash" named attribute.
2457N/A a = action.fromstr("file hash=abc123 path=usr/bin/foo mode=0755 owner=root group=bin")
2457N/A self.assert_(a.hash == "abc123")
2457N/A
1618N/A def test_action_license(self):
1618N/A """Test license action attributes."""
1618N/A
1618N/A # Verify license attributes for must-accept / must-display
1618N/A # contain expected values.
1618N/A a = action.fromstr('license license="Common Development and '
1618N/A 'Distribution License 1.0 (CDDL)" custom="foo" '
1618N/A 'bool_val=true')
1618N/A self.assertEqual(a.must_accept, False)
1618N/A self.assertEqual(a.must_display, False)
1618N/A
1618N/A a = action.fromstr('license license="Common Development and '
1618N/A 'Distribution License 1.0 (CDDL)" must-accept=true '
1618N/A 'must-display=False')
1618N/A self.assertEqual(a.must_accept, True)
1618N/A self.assertEqual(a.must_display, False)
1618N/A
1618N/A a = action.fromstr('license license="Common Development and '
1618N/A 'Distribution License 1.0 (CDDL)" must-accept=True '
1618N/A 'must-display=true')
1618N/A self.assertEqual(a.must_accept, True)
1618N/A self.assertEqual(a.must_display, True)
1618N/A
1618N/A a = action.fromstr('license license="Common Development and '
1618N/A 'Distribution License 1.0 (CDDL)" must-accept=True ')
1618N/A self.assertEqual(a.must_accept, True)
1618N/A self.assertEqual(a.must_display, False)
1618N/A
315N/A def test_action_tostr(self):
1938N/A """Test that actions convert to strings properly. This means
1938N/A that we can feed the resulting string back into fromstr() and
1938N/A get an identical action back."""
381N/A
2026N/A for s in self.act_strings:
1938N/A self.debug(str(s))
1938N/A a = action.fromstr(s)
1938N/A s2 = str(a)
1938N/A a2 = action.fromstr(s2)
1938N/A if a.different(a2):
1938N/A self.debug("a1 " + str(a))
1938N/A self.debug("a2 " + str(a2))
1938N/A self.assert_(not a.different(a2))
1384N/A
2457N/A # The one place that invariant doesn't hold is when you specify
2457N/A # the payload hash as the named attribute "hash", in which case
2457N/A # the resulting re-stringification emits the payload hash as a
2457N/A # positional attribute again ...
2457N/A s = "file hash=abc123 path=usr/bin/foo mode=0755 owner=root group=bin"
2457N/A self.debug(s)
2457N/A a = action.fromstr(s)
2457N/A s2 = str(a)
2457N/A self.assert_(s2.startswith("file abc123 "))
2457N/A self.assert_("hash=abc123" not in s2)
2457N/A a2 = action.fromstr(s2)
2457N/A self.assert_(not a.different(a2))
2457N/A
2457N/A # ... unless of course the hash can't be represented that way.
2685N/A d = {
2685N/A "hash=abc=123": "abc=123",
2685N/A "hash=\"one with spaces\"": "one with spaces",
2685N/A "hash='one with \" character'": 'one with " character',
2685N/A "hash=\"'= !@$%^\)(*\"": "'= !@$%^\)(*",
2685N/A """hash="\\"'= \\ " """:""""'= \\ """,
2685N/A '\\' : '\\'
2685N/A }
2685N/A
3158N/A astr = "file {0} path=usr/bin/foo mode=0755 owner=root group=bin"
3234N/A for k, v in six.iteritems(d):
3158N/A a = action.fromstr(astr.format(k))
2685N/A self.assert_(action.fromstr(str(a)) == a)
2685N/A self.assert_(a.hash == v)
2685N/A self.assert_(k in str(a))
2457N/A
2026N/A def test_action_sig_str(self):
2026N/A sig_act = action.fromstr(
2026N/A "signature 54321 algorithm=baz")
2026N/A for s in self.act_strings:
2026N/A # action.sig_str should return an identical string each
2026N/A # time it's called. Also, parsing the result of
2026N/A # sig_str so produce the same action.
2026N/A a = action.fromstr(s)
2026N/A s2 = a.sig_str(sig_act, generic.Action.sig_version)
2026N/A s3 = a.sig_str(sig_act, generic.Action.sig_version)
2026N/A # If s2 is None, then s was a different signature
2026N/A # action, so there is no output to parse.
2026N/A if s2 is None:
2026N/A continue
2026N/A self.assertEqual(s2, s3)
2026N/A a2 = action.fromstr(s2)
2026N/A if a.different(a2):
2026N/A self.debug("a1 " + str(a))
2026N/A self.debug("a2 " + str(a2))
2026N/A self.assert_(not a.different(a2))
2026N/A s4 = a.sig_str(sig_act, generic.Action.sig_version)
2026N/A self.assertEqual(s2, s4)
2026N/A # Test that using an unknown sig_version triggers the
2026N/A # appropriate exception.
2026N/A self.assertRaises(api_errors.UnsupportedSignatureVersion,
2026N/A sig_act.sig_str, sig_act, -1)
2026N/A a = action.fromstr(self.act_strings[0])
2026N/A self.assertRaises(api_errors.UnsupportedSignatureVersion,
2026N/A a.sig_str, sig_act, -1)
2026N/A # Test that the sig_str of a signature action other than the
2026N/A # argument action is None.
2026N/A sig_act2 = action.fromstr(
2026N/A "signature 98765 algorithm=foobar")
2026N/A self.assert_(sig_act.sig_str(sig_act2,
2026N/A generic.Action.sig_version) is None)
2026N/A self.assert_(sig_act2.sig_str(sig_act,
2026N/A generic.Action.sig_version) is None)
2026N/A
591N/A def assertMalformed(self, text):
591N/A malformed = False
591N/A
591N/A try:
591N/A action.fromstr(text)
3171N/A except action.MalformedActionError as e:
1973N/A assert e.actionstr == text
591N/A malformed = True
591N/A
1234N/A # If the action isn't malformed, something is wrong.
591N/A self.assert_(malformed, "Action not malformed: " + text)
591N/A
873N/A def assertInvalid(self, text):
873N/A invalid = False
873N/A
873N/A try:
873N/A action.fromstr(text)
873N/A except action.InvalidActionError:
873N/A invalid = True
873N/A
1234N/A # If the action isn't invalid, something is wrong.
1234N/A self.assert_(invalid, "Action not invalid: " + text)
873N/A
315N/A def test_action_errors(self):
591N/A # Unknown action type
591N/A self.assertRaises(action.UnknownActionError, action.fromstr,
591N/A "moop bar=baz")
2639N/A self.assertRaises(action.UnknownActionError, action.fromstr,
2639N/A "setbar=baz quux=quark")
591N/A
2639N/A # Nothing but the action type or type is malformed.
591N/A self.assertMalformed("moop")
2639N/A self.assertMalformed("setbar=baz")
305N/A
591N/A # Bad quoting: missing close quote
873N/A self.assertMalformed("file 12345 path=/tmp/foo name=\"foo bar")
873N/A self.assertMalformed("file 12345 path=/tmp/foo name=\"foo bar\\")
873N/A
591N/A # Bad quoting: quote in key
873N/A self.assertMalformed("file 12345 path=/tmp/foo \"name=foo bar")
873N/A self.assertMalformed("file 12345 path=/tmp/foo na\"me=foo bar")
873N/A self.assertMalformed("file 1234 path=/tmp/foo \"foo\"=bar")
305N/A
591N/A # Missing key
873N/A self.assertMalformed("file 1234 path=/tmp/foo =\"\"")
873N/A self.assertMalformed("file path=/tmp/foo =")
873N/A self.assertMalformed("file 1234 path=/tmp/foo =")
873N/A self.assertMalformed("file 1234 path=/tmp/foo ==")
873N/A self.assertMalformed("file 1234 path=/tmp/foo ===")
305N/A
591N/A # Missing value
873N/A self.assertMalformed("file 1234 path=/tmp/foo broken=")
873N/A self.assertMalformed("file 1234 path=/tmp/foo broken= ")
873N/A self.assertMalformed("file 1234 path=/tmp/foo broken")
591N/A
591N/A # Whitespace in key
873N/A self.assertMalformed("file 1234 path=/tmp/foo bro ken")
591N/A
1234N/A # Attribute value is invalid.
1234N/A self.assertInvalid("depend type=unknown fmri=foo@1.0")
1234N/A
1234N/A # Missing required attribute 'type'.
1234N/A self.assertInvalid("depend fmri=foo@1.0")
1234N/A
1234N/A # Missing key attribute 'fmri'.
1234N/A self.assertInvalid("depend type=require")
1234N/A
2476N/A # Mutiple fmri values only allowed for require-any deps.
2476N/A self.assertInvalid("depend type=require fmri=foo fmri=bar")
2476N/A
2254N/A # 'path' attribute specified multiple times
2254N/A self.assertInvalid("file 1234 path=foo path=foo mode=777 owner=root group=root")
2254N/A self.assertInvalid("link path=foo path=foo target=link")
2254N/A self.assertInvalid("dir path=foo path=foo mode=777 owner=root group=root")
2254N/A
1659N/A # 'data' used as an attribute key
1659N/A self.assertInvalid("file 1234 path=/tmp/foo data=rubbish")
1659N/A
1234N/A # Missing required attribute 'path'.
873N/A self.assertRaises(action.InvalidActionError, action.fromstr,
873N/A "file 1234 owner=foo")
305N/A
1234N/A # Missing required attribute 'name'.
873N/A self.assertRaises(action.InvalidActionError, action.fromstr,
873N/A "driver alias=pci1234,56 alias=pci4567,89 class=scsi")
305N/A
1973N/A # Verify malformed actions > 255 characters don't cause corrupt
1973N/A # exception action strings.
1973N/A self.assertMalformed("""legacy arch=i386 category=GNOME2,application,JDSosol desc="XScreenSaver is two things: it is both a large collection of screen savers (distributed in the "hacks" packages) and it is also the framework for blanking and locking the screen (this package)." hotline="Please contact your local service provider" name="XScreenSaver is two things: it is both a large collection of screen savers (distributed in the "hacks" packages) and it is also the framework for blanking and locking the screen (this package)." pkg=SUNWxscreensaver vendor="XScreenSaver Community" version=5.11,REV=110.0.4.2010.07.08.22.18""")
1973N/A self.assertMalformed("""legacy arch=i386 category=GNOME2,application,JDSosol desc="XScreenSaver is two things: it is both a large collection of screen savers (distributed in the "hacks" packages) and it is also the framework for blanking and locking the screen (this package)." hotline="Please contact your local service provider" name="XScreenSaver is two things: it is both a large collection of screen savers (distributed in the "hacks" packages) and it is also the framework for blanking and locking the screen (this package)." pkg=SUNWxscreensaver-l10n vendor="XScreenSaver Community" version=5.11,REV=110.0.4.2010.07.08.22.18""")
1973N/A
2026N/A # Missing required attribute 'algorithm'.
2026N/A self.assertRaises(action.InvalidActionError, action.fromstr,
2026N/A "signature 12345 pkg.cert=bar")
2026N/A
2457N/A # The payload hash can't be specified as both a named and a
2457N/A # positional attribute if they're not identical.
2457N/A self.assertInvalid("file xyz789 hash=abc123 path=usr/bin/foo mode=0755 owner=root group=bin")
2457N/A action.fromstr("file abc123 hash=abc123 path=usr/bin/foo mode=0755 owner=root group=bin")
2457N/A
1755N/A def test_validate(self):
1755N/A """Verify that action validate() works as expected; currently
1755N/A only used during publication or action execution failure."""
1755N/A
1755N/A fact = "file 12345 name=foo path=/tmp/foo mode=XXX"
1755N/A dact = "dir path=/tmp mode=XXX"
1755N/A
2453N/A def assert_invalid_attrs(astr):
2453N/A bad_act = action.fromstr(astr)
2453N/A try:
2453N/A bad_act.validate()
3171N/A except Exception as e:
2453N/A self.debug(str(e))
2476N/A else:
3158N/A self.debug("expected failure validating: {0}".format(
3158N/A astr))
2453N/A
2453N/A self.assertRaises(
2453N/A action.InvalidActionAttributesError,
2453N/A bad_act.validate)
2453N/A
2476N/A # Verify predicate and target attributes of FMRIs must be valid.
2476N/A for nact in (
2476N/A # FMRI value is invalid.
2476N/A "depend type=require-any fmri=foo fmri=bar fmri=invalid@abc",
2476N/A # Predicate is missing for conditional dependency.
2476N/A "depend type=conditional fmri=foo",
2476N/A # Predicate value is invalid.
2476N/A "depend type=conditional predicate=-invalid fmri=foo",
2476N/A # Predicate isn't valid for dependency type.
2476N/A "depend type=require predicate=1invalid fmri=foo",
2476N/A # root-image attribute is only valid for origin dependencies.
2476N/A "depend type=require fmri=foo root-image=true",
2476N/A # Multiple values for predicate are not allowed.
3138N/A "depend type=conditional predicate=foo predicate=bar fmri=baz",
3138N/A # Multiple values for ignore-check are not allowed.
3138N/A "depend type=require fmri=foo ignore-check=true ignore-check=false"):
2476N/A assert_invalid_attrs(nact)
2476N/A
2476N/A # Verify multiple values for file attributes are rejected.
2476N/A for attr in ("pkg.size", "pkg.csize", "chash", "preserve",
2476N/A "overlay", "elfhash", "original_name", "facet.doc",
2639N/A "owner", "group"):
2476N/A nact = "file path=/usr/bin/foo owner=root group=root " \
3158N/A "mode=0555 {attr}=1 {attr}=2 {attr}=3".format(
3158N/A attr=attr)
2476N/A assert_invalid_attrs(nact)
2476N/A
2476N/A # Verify invalid values are not allowed for mode attribute on
2685N/A # file and dir actions.
1755N/A for act in (fact, dact):
2685N/A for bad_mode in ("", 'mode=""', "mode=???",
1755N/A "mode=44755", "mode=44", "mode=999", "mode=0898"):
1755N/A nact = act.replace("mode=XXX", bad_mode)
2453N/A assert_invalid_attrs(nact)
2453N/A
2476N/A # Verify multiple values aren't allowed for legacy action
2476N/A # attributes.
2476N/A for attr in ("category", "desc", "hotline", "name", "vendor",
2476N/A "version"):
3158N/A nact = "legacy pkg=SUNWcs {attr}=1 {attr}=2".format(
3158N/A attr=attr)
2476N/A assert_invalid_attrs(nact)
2476N/A
2476N/A # Verify multiple values aren't allowed for gid of group.
2476N/A nact = "group groupname=staff gid=100 gid=101"
2476N/A assert_invalid_attrs(nact)
2476N/A
2476N/A # Verify only numeric value is allowed for gid of group.
2476N/A nact = "group groupname=staff gid=abc"
2476N/A assert_invalid_attrs(nact)
2476N/A
2476N/A # Verify multiple values are not allowed for must-accept and
2476N/A # must-display attributes of license actions.
2476N/A for attr in ("must-accept", "must-display"):
3158N/A nact = "license license=copyright {attr}=true " \
3158N/A "{attr}=false".format(attr=attr)
2476N/A assert_invalid_attrs(nact)
2476N/A
2476N/A # Ensure link and hardlink attributes are validated properly.
2453N/A for aname in ("link", "hardlink"):
2453N/A # Action with mediator without mediator properties is
2453N/A # invalid.
3158N/A nact = "{0} path=usr/bin/vi target=../sunos/bin/edit " \
3158N/A "mediator=vi".format(aname)
2453N/A assert_invalid_attrs(nact)
2453N/A
2453N/A # Action with multiple mediator values is invalid.
3158N/A nact = "{0} path=usr/bin/vi target=../sunos/bin/edit " \
2453N/A "mediator=vi mediator=vim " \
3158N/A "mediator-implementatio=svr4".format(aname)
2453N/A assert_invalid_attrs(nact)
2453N/A
2453N/A # Action with mediator properties without mediator
2453N/A # is invalid.
2453N/A props = {
2453N/A "mediator-version": "1.0",
2453N/A "mediator-implementation": "svr4",
2453N/A "mediator-priority": "site",
2453N/A }
3234N/A for prop, val in six.iteritems(props):
3158N/A nact = "{0} path=usr/bin/vi " \
3158N/A "target=../sunos/bin/edit {1}={2}".format(aname,
2453N/A prop, val)
2453N/A assert_invalid_attrs(nact)
2453N/A
2453N/A # Action with multiple values for any property is
2453N/A # invalid.
3234N/A for prop, val in six.iteritems(props):
3158N/A nact = "{0} path=usr/bin/vi " \
2453N/A "target=../sunos/bin/edit mediator=vi " \
3158N/A "{1}={2} {3}={4} ".format(aname, prop, val, prop,
2453N/A val)
2453N/A if prop == "mediator-priority":
2453N/A # mediator-priority alone isn't
2453N/A # valid, so test multiple value
2453N/A # invalid, add something.
2453N/A nact += " mediator-version=1.0"
2453N/A assert_invalid_attrs(nact)
2453N/A
2453N/A # Verify invalid mediator names are rejected.
2453N/A for value in ("not/valid", "not valid", "not.valid"):
3158N/A nact = "{0} path=usr/bin/vi target=vim " \
3158N/A "mediator=\"{1}\" mediator-implementation=vim" \
3158N/A .format(aname, value)
2453N/A assert_invalid_attrs(nact)
2453N/A
2453N/A # Verify invalid mediator-versions are rejected.
2453N/A for value in ("1.a", "abc", ".1"):
3158N/A nact = "{0} path=usr/bin/vi target=vim " \
3158N/A "mediator=vim mediator-version={1}" \
3158N/A .format(aname, value)
2453N/A assert_invalid_attrs(nact)
2453N/A
2476N/A # Verify invalid mediator-implementations are rejected.
2453N/A for value in ("1.a", "@", "@1", "vim@.1",
2453N/A "vim@abc"):
3158N/A nact = "{0} path=usr/bin/vi target=vim " \
3158N/A "mediator=vim mediator-implementation={1}" \
3158N/A .format(aname, value)
2453N/A assert_invalid_attrs(nact)
1755N/A
2476N/A # Verify multiple targets are not allowed.
3158N/A nact = "{0} path=/usr/bin/foo target=bar target=baz".format(
3158N/A aname)
2476N/A assert_invalid_attrs(nact)
2476N/A
2476N/A # Verify multiple values are not allowed for set actions such as
2476N/A # pkg.description, pkg.obsolete, pkg.renamed, and pkg.summary.
2476N/A for attr in ("pkg.description", "pkg.obsolete", "pkg.renamed",
3197N/A "pkg.summary", "pkg.depend.explicit-install"):
3158N/A nact = "set name={0} value=true value=false".format(attr)
2476N/A assert_invalid_attrs(nact)
2476N/A
2476N/A # Verify signature action attribute 'value' is required during
2476N/A # publication.
2476N/A nact = "signature 12345 algorithm=foo"
2476N/A assert_invalid_attrs(nact)
2476N/A
2476N/A # Verify multiple values aren't allowed for user attributes.
2476N/A for attr in ("password", "group", "gcos-field", "home-dir",
2476N/A "login-shell", "ftpuser"):
3158N/A nact = "user username=user {attr}=ab {attr}=cd ".format(
3158N/A attr=attr)
2476N/A assert_invalid_attrs(nact)
2476N/A
2476N/A for attr in ("uid", "lastchg", "min","max", "warn", "inactive",
2476N/A "expire", "flag"):
3158N/A nact = "user username=user {attr}=1 {attr}=2".format(
3158N/A attr=attr)
2476N/A assert_invalid_attrs(nact)
2476N/A
2476N/A # Verify only numeric values are allowed for user attributes
2476N/A # expecting a number.
2476N/A for attr in ("uid", "lastchg", "min","max", "warn", "inactive",
2476N/A "expire", "flag"):
3158N/A nact = "user username=user {0}=abc".format(attr)
2476N/A assert_invalid_attrs(nact)
2476N/A
3160N/A # Malformed pkg actuators
3160N/A assert_invalid_attrs(
3160N/A "set name=pkg.additional-update-on-uninstall "
3160N/A "value=&@M")
3160N/A assert_invalid_attrs(
3160N/A "set name=pkg.additional-update-on-uninstall "
3160N/A "value=A@1 value=&@M")
3160N/A # Unknown actuator (should pass)
3160N/A act = action.fromstr(
3160N/A "set name=pkg.additional-update-on-update value=A@1")
3160N/A act.validate()
1234N/A
305N/Aif __name__ == "__main__":
305N/A unittest.main()