File: validate_test.go

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (413 lines) | stat: -rw-r--r-- 15,580 bytes parent folder | download | duplicates (2)
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
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2021 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package validate_test

import (
	"bytes"
	"fmt"
	"sort"
	"testing"

	. "gopkg.in/check.v1"

	"github.com/snapcore/snapd/polkit/validate"
)

type validateSuite struct{}

var _ = Suite(&validateSuite{})

func Test(t *testing.T) {
	TestingT(t)
}

func validateString(xml string) ([]string, error) {
	return validate.ValidatePolicy(bytes.NewBufferString(xml))
}

func (s *validateSuite) TestRootElement(c *C) {
	// Extra elements after root
	_, err := validateString("<policyconfig/><policyconfig/>")
	c.Check(err, ErrorMatches, `invalid XML: additional data after root element`)

	// Extra incomplete elements after root element
	_, err = validateString("<policyconfig/><incomplete>")
	c.Check(err, ErrorMatches, `invalid XML: additional data after root element`)

	// Wrong root element
	_, err = validateString("<xyz/>")
	c.Check(err, ErrorMatches, `expected element type <policyconfig> but have <xyz>`)

	// Wrong namespace for root element
	_, err = validateString(`<policyconfig xmlns="http://example.org/ns"/>`)
	c.Check(err, ErrorMatches, `root element must be <policyconfig>`)

	// Invalid XML
	_, err = validateString("<policyconfig>incomplete")
	c.Check(err, ErrorMatches, `XML syntax error on line .*`)
}

func (s *validateSuite) TestPolicyConfigElement(c *C) {
	_, err := validateString("<policyconfig/>")
	c.Check(err, IsNil)

	// Extra attributes are not allowed
	_, err = validateString(`<policyconfig foo="bar"/>`)
	c.Check(err, ErrorMatches, `<policyconfig> element contains unexpected attributes`)

	// Unexpected child elements
	_, err = validateString("<policyconfig><xyz/></policyconfig>")
	c.Check(err, ErrorMatches, `<policyconfig> element contains unexpected children`)

	// Unexpected character data
	_, err = validateString("<policyconfig>xyz</policyconfig>")
	c.Check(err, ErrorMatches, `<policyconfig> element contains unexpected character data`)

	// Supports <vendor>, <vendor_url>, and <icon_name> parameters
	_, err = validateString(`<policyconfig>
  <vendor>vendor</vendor>
  <vendor_url>url</vendor_url>
  <icon_name>icon</icon_name>
</policyconfig>`)
	c.Check(err, IsNil)

	// Duplicates of those elements are not allowed
	_, err = validateString(`<policyconfig>
  <vendor>vendor</vendor>
  <vendor>vendor</vendor>
</policyconfig>`)
	c.Check(err, ErrorMatches, `multiple <vendor> elements found under <policyconfig>`)

	_, err = validateString(`<policyconfig>
  <vendor_url>url</vendor_url>
  <vendor_url>url</vendor_url>
</policyconfig>`)
	c.Check(err, ErrorMatches, `multiple <vendor_url> elements found under <policyconfig>`)

	_, err = validateString(`<policyconfig>
  <icon_name>icon</icon_name>
  <icon_name>icon</icon_name>
</policyconfig>`)
	c.Check(err, ErrorMatches, `multiple <icon_name> elements found under <policyconfig>`)
}

func validateAction(xml string) ([]string, error) {
	return validateString("<policyconfig>" + xml + "</policyconfig>")
}

func (s *validateSuite) TestActionElement(c *C) {
	// The ID of an action is extracted on successful validation
	actionIDs, err := validateAction(`<action id="foo">
  <description>desc</description>
  <message>msg</message>
</action>`)
	c.Check(err, IsNil)
	c.Check(actionIDs, DeepEquals, []string{"foo"})

	// Actions must have an ID
	_, err = validateAction("<action/>")
	c.Check(err, ErrorMatches, `<action> elements must have an ID`)

	// Other attributes are not allowed
	_, err = validateAction(`<action bar="foo"/>`)
	c.Check(err, ErrorMatches, `<action> element contains unexpected attributes`)

	// Unexpected child elements are not allowed
	_, err = validateAction(`<action id="foo"><xyz/></action>`)
	c.Check(err, ErrorMatches, `<action> element contains unexpected children`)

	// Character data not allowed inside element
	_, err = validateAction(`<action id="foo">xyz</action>`)
	c.Check(err, ErrorMatches, `<action> element contains unexpected character data`)

	// Action elements can also contain <vendor>, <vendor_url>,
	// and <icon_name> elements.
	_, err = validateAction(`<action id="foo">
  <description>desc</description><message>msg</message>
  <vendor>vendor</vendor>
  <vendor_url>url</vendor_url>
  <icon_name>icon</icon_name>
</action>`)
	c.Check(err, IsNil)

	// Empty versions of those elements are not allowed
	_, err = validateAction(`<action id="foo">
  <description>desc</description><message>msg</message>
  <vendor/>
</action>`)
	c.Check(err, ErrorMatches, `<vendor> element has no character data`)

	// Duplicates of those elements are not allowed
	_, err = validateAction(`<action id="foo">
  <description>desc</description><message>msg</message>
  <vendor>vendor</vendor>
  <vendor>vendor</vendor>
</action>`)
	c.Check(err, ErrorMatches, `multiple <vendor> elements found under <action>`)

	_, err = validateAction(`<action id="foo">
  <description>desc</description><message>msg</message>
  <vendor_url>url</vendor_url>
  <vendor_url>url</vendor_url>
</action>`)
	c.Check(err, ErrorMatches, `multiple <vendor_url> elements found under <action>`)

	_, err = validateAction(`<action id="foo">
  <description>desc</description><message>msg</message>
  <icon_name>icon</icon_name>
  <icon_name>icon</icon_name>
</action>`)
	c.Check(err, ErrorMatches, `multiple <icon_name> elements found under <action>`)

	// The <description> and <message> elements accept
	// gettext-domain and xml:lang attributes
	_, err = validateAction(`<action id="foo">
  <description gettext-domain="bar" xml:lang="en-GB">desc</description>
  <message gettext-domain="bar" xml:lang="en-GB">desc</message>
</action>`)
	c.Check(err, IsNil)

	// Other attributes or child elements on <description> and
	// <message> are forbidden
	_, err = validateAction(`<action id="foo">
  <description bar="foo">desc</description>
  <message>msg</message>
</action>`)
	c.Check(err, ErrorMatches, `<description> element contains unexpected attributes`)

	_, err = validateAction(`<action id="foo">
  <description>desc<xyz/></description>
  <message>msg</message>
</action>`)
	c.Check(err, ErrorMatches, `<description> element contains unexpected children`)

	_, err = validateAction(`<action id="foo">
  <description>desc</description>
  <message bar="foo">msg</message>
</action>`)
	c.Check(err, ErrorMatches, `<message> element contains unexpected attributes`)

	_, err = validateAction(`<action id="foo">
  <description>desc</description>
  <message>msg<xyz/></message>
</action>`)
	c.Check(err, ErrorMatches, `<message> element contains unexpected children`)

	// Multiple <description> and <message> children are allowed
	// children
	_, err = validateAction(`<action id="foo">
  <description>desc</description>
  <description>desc</description>
  <description>desc</description>
  <message>msg</message>
</action>`)
	c.Check(err, IsNil)

	_, err = validateAction(`<action id="foo">
  <description>desc</description>
  <message>msg</message>
  <message>msg</message>
  <message>msg</message>
</action>`)
	c.Check(err, IsNil)

	// But at least one is required
	_, err = validateAction(`<action id="foo">
  <message>msg</message>
</action>`)
	c.Check(err, ErrorMatches, `<action> element missing <description> child`)

	_, err = validateAction(`<action id="foo">
  <description>desc</description>
</action>`)
	c.Check(err, ErrorMatches, `<action> element missing <message> child`)
}

func validateActionDefaults(xml string) error {
	_, err := validateAction(fmt.Sprintf(`<action id="foo">
  <description>desc</description><message>msg</message>
  %s
</action>`, xml))
	return err
}

func (s *validateSuite) TestDefaultsElement(c *C) {
	// Actions can have a single <defaults> element
	err := validateActionDefaults(`<defaults/>`)
	c.Check(err, IsNil)

	err = validateActionDefaults(`<defaults/><defaults/>`)
	c.Check(err, ErrorMatches, `<action> element has multiple <defaults> children`)

	// The <defaults> element does not accept attributes, unknown children or character data
	err = validateActionDefaults(`<defaults foo="bar"/>`)
	c.Check(err, ErrorMatches, `<defaults> element contains unexpected attributes`)

	err = validateActionDefaults(`<defaults>xyz</defaults>`)
	c.Check(err, ErrorMatches, `<defaults> element contains unexpected character data`)

	err = validateActionDefaults(`<defaults><xyz/></defaults>`)
	c.Check(err, ErrorMatches, `<defaults> element contains unexpected children`)

	// The defaults section contains default access rules for the action
	err = validateActionDefaults(`<defaults>
  <allow_any>yes</allow_any>
  <allow_inactive>yes</allow_inactive>
  <allow_active>yes</allow_active>
</defaults>`)
	c.Check(err, IsNil)

	for _, mode := range []string{"allow_any", "allow_inactive", "allow_active"} {
		// Only one instance of the element is allowed
		err = validateActionDefaults(fmt.Sprintf(`<defaults>
  <%s>yes</%s>
  <%s>yes</%s>
</defaults>`, mode, mode, mode, mode))
		c.Check(err, ErrorMatches, fmt.Sprintf("multiple <%s> elements found under <defaults>", mode))

		// No attributes or child elements allowed
		err = validateActionDefaults(fmt.Sprintf(`<defaults>
  <%s foo="bar">yes</%s>
</defaults>`, mode, mode))
		c.Check(err, ErrorMatches, fmt.Sprintf("<%s> element contains unexpected attributes", mode))

		err = validateActionDefaults(fmt.Sprintf(`<defaults>
  <%s>yes<xyz/></%s>
</defaults>`, mode, mode))
		c.Check(err, ErrorMatches, fmt.Sprintf("<%s> element contains unexpected children", mode))

		// Unknown or missing values are rejected
		err = validateActionDefaults(fmt.Sprintf(`<defaults>
  <%s>unknown</%s>
</defaults>`, mode, mode))
		c.Check(err, ErrorMatches, fmt.Sprintf(`invalid value for <%s>: "unknown"`, mode))

		err = validateActionDefaults(fmt.Sprintf(`<defaults>
  <%s/>
</defaults>`, mode))
		c.Check(err, ErrorMatches, fmt.Sprintf(`invalid value for <%s>: ""`, mode))

		// Known values are accepted:
		for _, value := range []string{"no", "yes", "auth_self", "auth_admin", "auth_self_keep", "auth_admin_keep"} {
			err = validateActionDefaults(fmt.Sprintf(`<defaults>
  <%s>%s</%s>
</defaults>`, mode, value, mode))
			c.Check(err, IsNil)
		}
	}
}

func validateAnnotation(xml string) ([]string, error) {
	return validateAction(fmt.Sprintf(`<action id="action_id">
  <description>desc</description><message>msg</message>
  %s
</action>`, xml))
}

func (s *validateSuite) TestAnnotateElement(c *C) {
	actionIDs, err := validateAnnotation(`<annotate key="org.freedesktop.policykit.imply">implied_id</annotate>`)
	c.Check(err, IsNil)
	sort.Strings(actionIDs)
	c.Check(actionIDs, DeepEquals, []string{"action_id", "implied_id"})

	// <annotate> elements do not accept unknown attributes or
	// child elements
	_, err = validateAnnotation(`<annotate foo="bar"/>`)
	c.Check(err, ErrorMatches, `<annotate> element contains unexpected attributes`)
	_, err = validateAnnotation(`<annotate><xyz/></annotate>`)
	c.Check(err, ErrorMatches, `<annotate> element contains unexpected children`)

	// The key parameter is required
	_, err = validateAnnotation(`<annotate/>`)
	c.Check(err, ErrorMatches, `<annotate> elements must have a key attribute`)

	// At present, only "imply" annotations are accepted
	_, err = validateAnnotation(`<annotate key="xyz">foo</annotate>`)
	c.Check(err, ErrorMatches, `unsupported annotation "xyz"`)

	// "imply" annotations take a whitespace separated list of
	// action IDs that are returned by the validation function
	actionIDs, err = validateAnnotation(`<annotate key="org.freedesktop.policykit.imply">id1 id2 id3 id3</annotate>`)
	c.Check(err, IsNil)
	sort.Strings(actionIDs)
	c.Check(actionIDs, DeepEquals, []string{"action_id", "id1", "id2", "id3"})

	// Annotation elements must not be empty
	_, err = validateAnnotation(`<annotate key="org.freedesktop.policykit.imply"/>`)
	c.Check(err, ErrorMatches, `<annotate> elements must contain character data`)

	// Multiple <annotate> elements are accepted
	actionIDs, err = validateAnnotation(`
<annotate key="org.freedesktop.policykit.imply">id1</annotate>
<annotate key="org.freedesktop.policykit.imply">id2</annotate>`)
	c.Check(err, IsNil)
	sort.Strings(actionIDs)
	c.Check(actionIDs, DeepEquals, []string{"action_id", "id1", "id2"})
}

func (s *validateSuite) TestActionIDExtraction(c *C) {
	actionIDs, err := validateString(`<policyconfig>
  <!-- a comment -->
  <action id="action1">
    <description>desc1</description>
    <message>msg1</message>
  </action>
  <action id="action2">
    <description>desc1</description>
    <message>msg1</message>
    <annotate key="org.freedesktop.policykit.imply">action3</annotate>
  </action>
  <action id="action3">
    <description>desc1</description>
    <message>msg1</message>
    <annotate key="org.freedesktop.policykit.imply">action2 action4</annotate>
  </action>
</policyconfig>`)
	c.Check(err, IsNil)
	sort.Strings(actionIDs)
	c.Check(actionIDs, DeepEquals, []string{"action1", "action2", "action3", "action4"})
}

func (s *validateSuite) TestValidateRuleFileName(c *C) {
	// Happy case
	c.Check(validate.ValidateRuleFileName("/path/to/rule/foo.bar.rules"), IsNil)
	// Errors
	c.Check(validate.ValidateRuleFileName(".rules"), ErrorMatches, `invalid polkit rule file name: rule file name cannot be empty`)
	c.Check(validate.ValidateRuleFileName("foo"), ErrorMatches, `invalid polkit rule file name: "foo" must end with "\.rules"`)
	c.Check(validate.ValidateRuleFileName(".foo.rules"), ErrorMatches, `invalid polkit rule file name: ".*" does not match .*`)
	c.Check(validate.ValidateRuleFileName("---.rules"), ErrorMatches, `invalid polkit rule file name: ".*" does not match .*`)
	c.Check(validate.ValidateRuleFileName("--.--.rules"), ErrorMatches, `invalid polkit rule file name: ".*" does not match .*`)
	c.Check(validate.ValidateRuleFileName("foo.---.rules"), ErrorMatches, `invalid polkit rule file name: ".*" does not match .*`)
	c.Check(validate.ValidateRuleFileName("foo..rules"), ErrorMatches, `invalid polkit rule file name: ".*" does not match .*`)
	c.Check(validate.ValidateRuleFileName("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.rules"), ErrorMatches, `invalid polkit rule file name: ".*" is longer than 64 characters`)
}

func (s *validateSuite) TestValidateRuleNameSuffix(c *C) {
	// Happy case
	c.Check(validate.ValidateRuleNameSuffix("foo.bar"), IsNil)
	// Errors
	c.Check(validate.ValidateRuleNameSuffix(""), ErrorMatches, `rule file name cannot be empty`)
	c.Check(validate.ValidateRuleNameSuffix("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), ErrorMatches, `".*" is longer than 64 characters`)
	c.Check(validate.ValidateRuleNameSuffix("?"), ErrorMatches, `"\?" does not match .*`)
	c.Check(validate.ValidateRuleNameSuffix("---"), ErrorMatches, `".*" does not match .*`)
	c.Check(validate.ValidateRuleNameSuffix("--.--"), ErrorMatches, `".*" does not match .*`)
	c.Check(validate.ValidateRuleNameSuffix("foo.---"), ErrorMatches, `".*" does not match .*`)
	c.Check(validate.ValidateRuleNameSuffix("foo."), ErrorMatches, `".*" does not match .*`)
}