File: expand_exec_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 (168 lines) | stat: -rw-r--r-- 4,793 bytes parent folder | download | duplicates (3)
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
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2023 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 desktopentry_test

import (
	. "gopkg.in/check.v1"

	"github.com/snapcore/snapd/desktop/desktopentry"
)

func (s *desktopentrySuite) TestExpandExecHelper(c *C) {
	de := &desktopentry.DesktopEntry{
		Filename: "/path/file.desktop",
		Name:     "App Name",
		Icon:     "/path/icon.png",
	}
	for i, tc := range []struct {
		in   string
		uris []string
		out  []string
		err  string
	}{{
		in:  "foo --bar",
		out: []string{"foo", "--bar"},
	}, {
		in:   "foo --bar",
		uris: []string{"file:///test"},
		out:  []string{"foo", "--bar", "/test"},
	}, {
		in:   "foo --bar",
		uris: []string{"file:///Bob%27s%20files/x"},
		out:  []string{"foo", "--bar", "/Bob's files/x"},
	}, {
		in:   "foo --bar",
		uris: []string{"http://example.org"},
		err:  `"http://example.org" is not a file URI`,
	}, {
		in:  "foo %f",
		out: []string{"foo"},
	}, {
		in:  "foo %F",
		out: []string{"foo"},
	}, {
		in:  "foo %u",
		out: []string{"foo"},
	}, {
		in:  "foo %U",
		out: []string{"foo"},
	}, {
		in:   "foo %f bar",
		uris: []string{"file:///test1", "file:///test2"},
		out:  []string{"foo", "/test1", "bar"},
	}, {
		in:   "foo %F bar",
		uris: []string{"file:///test1", "file:///test2", "file:///test3"},
		out:  []string{"foo", "/test1", "/test2", "/test3", "bar"},
	}, {
		in:   "foo %u bar",
		uris: []string{"file:///test1", "file:///test2"},
		out:  []string{"foo", "/test1", "bar"},
	}, {
		in:   "foo %U bar",
		uris: []string{"file:///test1", "file:///test2", "file:///test3"},
		out:  []string{"foo", "/test1", "/test2", "/test3", "bar"},
	}, {
		in:   "foo %f",
		uris: []string{"http://example.org"},
		err:  `"http://example.org" is not a file URI`,
	}, {
		in:   "foo %F",
		uris: []string{"http://example.org"},
		err:  `"http://example.org" is not a file URI`,
	}, {
		in:   "foo %u",
		uris: []string{"http://example.org"},
		out:  []string{"foo", "http://example.org"},
	}, {
		in:   "foo %U",
		uris: []string{"http://example.org", "http://example.com"},
		out:  []string{"foo", "http://example.org", "http://example.com"},
	}, {
		in:  "foo %i bar",
		out: []string{"foo", "--icon", "/path/icon.png", "bar"},
	}, {
		in:  "foo %c bar",
		out: []string{"foo", "App Name", "bar"},
	}, {
		in:  "foo %k bar",
		out: []string{"foo", "/path/file.desktop", "bar"},
	}, {
		in:  `foo --bar "%%p" %U %D +%s %%`,
		out: []string{"foo", "--bar", "%p", "+", "%"},
	}, {
		in:   "skype --share-file=%f",
		uris: []string{"file:///test"},
		out:  []string{"skype", "--share-file=/test"},
	}, {
		in:  `foo "double quotes" 'single quotes'`,
		out: []string{"foo", "double quotes", "single quotes"},
	}, {
		in:  `foo "unclosed double quote`,
		err: "EOF found when expecting closing quote",
	}, {
		in:   `foo %f`,
		uris: []string{"/foo"},
		err:  `"/foo" is not an absolute URI`,
	}, {
		in:   `foo %f`,
		uris: []string{"foo/bar"},
		err:  `"foo/bar" is not an absolute URI`,
	}, {
		in:   `foo %f`,
		uris: []string{"file:foo/bar"},
		err:  `"file:foo/bar" does not have an absolute file path`,
	}, {
		// Environment variables are not expanded.
		in:  `foo $PATH`,
		out: []string{"foo", "$PATH"},
	}, {
		// Special characters within URIs are preserved in the
		// final command line.
		in:   `foo %f`,
		uris: []string{"file:///special%20chars%5c%27%22%25%20%24foo"},
		out:  []string{"foo", `/special chars\'"% $foo`},
	}, {
		// Undefined behaviour: macro within a single quoted string
		in:   `foo '-f %U %%bar'`,
		uris: []string{"http://example.org"},
		out:  []string{"foo", "-f http://example.org %bar"},
	}, {
		// Undefined behaviour: macro within a double quoted string
		in:   `foo "-f %f %%bar"`,
		uris: []string{"file:///test%27.txt"},
		out:  []string{"foo", "-f '/test'''.txt' %bar"},
	}, {
		// Undefined behaviour: macro within a double quoted string
		in:   `foo "-f %f %%bar"`,
		uris: []string{"file:///test%22.txt"},
		err:  `EOF found when expecting closing quote`,
	}} {
		c.Logf("tc %d", i)

		args, err := desktopentry.ExpandExec(de, tc.in, tc.uris)
		if tc.err != "" {
			c.Assert(err, ErrorMatches, tc.err)
			continue
		}
		c.Check(err, IsNil)
		c.Check(args, DeepEquals, tc.out)
	}
}