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#
3296N/A# Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
2026N/A#
305N/A
3339N/Afrom . import testutils
1715N/Aif __name__ == "__main__":
1715N/A testutils.setup_environment("../../../proto")
1715N/Aimport pkg5unittest
1715N/A
3234N/Aimport six
3402N/Aimport sys
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
3402N/Aimport pkg.digest
3402N/Afrom pkg.client.debugvalues import DebugValues
3402N/Aif sys.version_info[:2] >= (3, 4):
3402N/A from importlib import reload
3402N/Aelse:
3402N/A from imp import reload
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."')
3339N/A self.assertTrue(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")
3339N/A self.assertTrue(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
3312N/A def __assert_action_str(self, astr, expected, expattrs):
3312N/A """Private helper function for action stringification
3312N/A testing."""
3312N/A act = action.fromstr(astr)
3312N/A self.assertEqualDiff(expected, str(act))
3312N/A self.assertEqualDiff(expattrs, act.attrs)
3312N/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
3312N/A get an identical action back (excluding a few cases detailed in
3312N/A the test)."""
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))
3339N/A self.assertTrue(not a.different(a2))
1384N/A
3312N/A # The first case 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)
3339N/A self.assertTrue(s2.startswith("file abc123 "))
3339N/A self.assertTrue("hash=abc123" not in s2)
2457N/A a2 = action.fromstr(s2)
3339N/A self.assertTrue(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))
3339N/A self.assertTrue(action.fromstr(str(a)) == a)
3339N/A self.assertTrue(a.hash == v)
3339N/A self.assertTrue(k in str(a))
2457N/A
3312N/A # The attributes are verified separately from the stringified
3312N/A # action in the tests below to ensure that the attributes were
3312N/A # parsed independently and not as a single value (e.g.
3312N/A # 'file path=etc/foo\nfacet.debug=true' is parsed as having a
3312N/A # path attribute and a facet.debug attribute).
3312N/A
3312N/A # The next case that invariant doesn't hold is when you have
3312N/A # multiple, quoted values for a single attribute (this case
3312N/A # primarily exists for use with line-continuation support
3312N/A # offered by the Manifest class).
3312N/A expected = 'set name=pkg.description value="foo bar baz"'
3312N/A expattrs = { 'name': 'pkg.description', 'value': 'foo bar baz' }
3312N/A for astr in (
3312N/A "set name=pkg.description value='foo ''bar ''baz'",
3312N/A "set name=pkg.description value='foo ' 'bar ' 'baz'",
3312N/A 'set name=pkg.description value="foo " "bar " "baz"'):
3312N/A self.__assert_action_str(astr, expected, expattrs)
3312N/A
3312N/A expected = "set name=pkg.description value='foo \"bar\" baz'"
3312N/A expattrs = { 'name': 'pkg.description',
3312N/A 'value': 'foo "bar" baz' }
3312N/A for astr in (
3312N/A "set name=pkg.description value='foo \"bar\" ''baz'",
3312N/A "set name=pkg.description value='foo \"bar\" '\"baz\""):
3312N/A self.__assert_action_str(astr, expected, expattrs)
3312N/A
3312N/A # The next case that invariant doesn't hold is when there are
3312N/A # multiple whitespace characters between attributes or after the
3312N/A # action type.
3312N/A expected = 'set name=pkg.description value=foo'
3312N/A expattrs = { 'name': 'pkg.description', 'value': 'foo' }
3312N/A for astr in (
3312N/A "set name=pkg.description value=foo",
3312N/A "set name=pkg.description value=foo",
3312N/A "set name=pkg.description value=foo",
3312N/A "set\n name=pkg.description \nvalue=foo",
3312N/A "set\t\nname=pkg.description\t\nvalue=foo"):
3312N/A # To force stressing the parsing logic a bit more, we
3312N/A # parse an action with a multi-value attribute that
3312N/A # needs concatention each time before we parse a
3312N/A # single-value attribute that needs concatenation.
3312N/A #
3312N/A # This simulates a refcount bug that was found during
3312N/A # development and serves as an extra stress-test.
3312N/A self.__assert_action_str(
3312N/A 'set name=multi-value value=bar value="foo ""baz"',
3312N/A 'set name=multi-value value=bar value="foo baz"',
3312N/A { 'name': 'multi-value',
3312N/A 'value': ['bar', 'foo baz'] })
3312N/A
3312N/A self.__assert_action_str(astr, expected, expattrs)
3312N/A
3312N/A astr = 'file path=etc/foo\nfacet.debug=true'
3312N/A expected = 'file NOHASH facet.debug=true path=etc/foo'
3312N/A expattrs = { 'path': 'etc/foo', 'facet.debug': 'true' }
3312N/A self.__assert_action_str(astr, expected, expattrs)
3312N/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))
3339N/A self.assertTrue(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")
3339N/A self.assertTrue(sig_act.sig_str(sig_act2,
2026N/A generic.Action.sig_version) is None)
3339N/A self.assertTrue(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
3312N/A self.debug(text)
3312N/A self.debug(str(e))
591N/A malformed = True
591N/A
1234N/A # If the action isn't malformed, something is wrong.
3339N/A self.assertTrue(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.
3339N/A self.assertTrue(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= ")
3312N/A self.assertMalformed("file 1234 path=/tmp/foo broken=\t")
3312N/A self.assertMalformed("file 1234 path=/tmp/foo broken=\n")
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")
3312N/A self.assertMalformed("file 1234 path=/tmp/foo\tbro\tken")
3312N/A self.assertMalformed("file 1234 path=/tmp/foo\nbro\nken")
3312N/A self.assertMalformed("file 1234 path ='/tmp/foo")
3312N/A self.assertMalformed("file 1234 path\t=/tmp/foo")
3312N/A self.assertMalformed("file 1234 path\n=/tmp/foo")
591N/A
1234N/A # Missing key attribute 'fmri'.
1234N/A self.assertInvalid("depend type=require")
1234N/A
3366N/A # XXX Fails in Python 3.4 due to module import issue; see
3366N/A # set_invalid_action_error in actions/_common.c.
3339N/A if six.PY2:
3366N/A # Multiple values not allowed for 'fmri' if 'type' is
3366N/A # multi-valued.
3366N/A self.assertInvalid("depend type=require "
3366N/A "type=require-any fmri=foo fmri=bar")
3296N/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
3366N/A def assertActionError(astr,
3366N/A error=action.InvalidActionAttributesError):
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
3366N/A self.assertRaises(error, bad_act.validate)
2453N/A
3417N/A # Verify mode attributes for file specified more than once are
3417N/A # rejected.
3417N/A nact = "file path=/usr/bin/foo owner=root group=root " \
3417N/A "mode=0555 mode=0555"
3417N/A assertActionError(nact)
3417N/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",
3366N/A # Missing required attribute 'type'.
3366N/A "depend fmri=foo@1.0"
3366N/A # type is invalid.
3366N/A "depend type=unknown fmri=foo@1.0",
3366N/A # Multiple values never allowed for depend action 'type' attribute.
3366N/A "depend type=require type=require-any fmri=foo",
3366N/A # Mutiple fmri values only allowed for require-any deps.
3366N/A "depend type=require fmri=foo fmri=bar",
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"):
3366N/A assertActionError(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)
3366N/A assertActionError(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)
3366N/A assertActionError(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)
3366N/A assertActionError(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"
3366N/A assertActionError(nact)
2476N/A
2476N/A # Verify only numeric value is allowed for gid of group.
2476N/A nact = "group groupname=staff gid=abc"
3366N/A assertActionError(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)
3366N/A assertActionError(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)
3366N/A assertActionError(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)
3366N/A assertActionError(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)
3366N/A assertActionError(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"
3366N/A assertActionError(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)
3366N/A assertActionError(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)
3366N/A assertActionError(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)
3366N/A assertActionError(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)
3366N/A assertActionError(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)
3366N/A assertActionError(nact)
2476N/A
2476N/A # Verify signature action attribute 'value' is required during
2476N/A # publication.
2476N/A nact = "signature 12345 algorithm=foo"
3366N/A assertActionError(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)
3366N/A assertActionError(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)
3366N/A assertActionError(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)
3366N/A assertActionError(nact)
2476N/A
3160N/A # Malformed pkg actuators
3366N/A assertActionError(
3160N/A "set name=pkg.additional-update-on-uninstall "
3160N/A "value=&@M")
3366N/A assertActionError(
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()