Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c2d8de221d84595b9fa3e37941238eead22c88e9 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2012-2013 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat - initial API and implementation
 *******************************************************************************/

package org.eclipse.linuxtools.systemtap.ui.ide.test.editors.stp;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;

import java.io.IOException;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPCompletionProcessor;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPDocumentProvider;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPEditor;
import org.eclipse.linuxtools.tools.launch.core.factory.RuntimeProcessFactory;
import org.junit.Test;

public class STPCompletionProcessorTest {

	private static String TEST_STP_SCRIPT = ""+
			"\n"+
			"\n//marker1"+
			"probe syscall.write{\n"+
			"  // Some comment inside a probe\n"+
			"   printf(\"%s fd %d\taddr%d\tcount%dargstr%s\n\", name, fd, buf_uaddr, count, argstr)\n"+
			"}\n"+
			"\n";

	private static class MockSTPDocumentProvider extends STPDocumentProvider{
		private IDocument document;

		MockSTPDocumentProvider(IDocument document){
			this.document = document;
			this.setupDocument(document);
		}

		@Override
		protected IDocument createDocument(Object element) {
			return document;
		}
	}

	private static class MockSTPEditor extends STPEditor{
		public MockSTPEditor(IDocument document) {
			super();
			setDocumentProvider(new MockSTPDocumentProvider(document));
		}
	}

	@Test
	public void testCompletionRequest() {
		Document testDocument = new Document("");
		STPCompletionProcessor completionProcessor = new STPCompletionProcessor();
		ICompletionProposal[] proposals = completionProcessor
				.computeCompletionProposals(testDocument, 0);
		assertNotNull(proposals);
	}

	@Test
	public void testCompletionRequestAtEOF() {
		Document testDocument = new Document(TEST_STP_SCRIPT);
		STPCompletionProcessor completionProcessor = new STPCompletionProcessor();
		ICompletionProposal[] proposals = completionProcessor
				.computeCompletionProposals(testDocument,
						TEST_STP_SCRIPT.length());
		assertNotNull(proposals);
	}

	@Test
	public void testGlobalCompletion() {
		Document testDocument = new Document(TEST_STP_SCRIPT);
		int offset = TEST_STP_SCRIPT.indexOf("//marker1");

		STPCompletionProcessor completionProcessor = new STPCompletionProcessor();
		ICompletionProposal[] proposals = completionProcessor
				.computeCompletionProposals(testDocument,
						offset);

		assertTrue(proposalsContain(proposals, "probe "));
		assertTrue(proposalsContain(proposals, "global "));
		assertTrue(proposalsContain(proposals, "function "));
	}

	@Test
	public void testGlobalPartialCompletion() throws BadLocationException {
		String prefix = "prob";
		ICompletionProposal[] proposals = getCompletionsForPrefix(prefix);
		assertTrue(proposalsContain(proposals, "probe "));
		assertTrue(!proposalsContain(proposals, "global "));
		assertTrue(!proposalsContain(proposals, "function "));
	}

	@Test
	public void testProbeCompletion() throws BadLocationException {
		assumeTrue(stapInstalled());
		String prefix = "probe ";
		ICompletionProposal[] proposals = getCompletionsForPrefix(prefix);
		assertTrue(proposalsContain(proposals, "syscall"));
		assertTrue(!proposalsContain(proposals, "syscall.write"));
	}

	@Test
	public void testMultiProbeCompletion() throws BadLocationException {
		assumeTrue(stapInstalled());
		String prefix = "probe begin,e";
		ICompletionProposal[] proposals = getCompletionsForPrefix(prefix);
		assertTrue(proposalsContain(proposals, "end"));
		assertTrue(proposalsContain(proposals, "error"));

		prefix = "probe myBegin = b";
		proposals = getCompletionsForPrefix(prefix);
		assertTrue(proposalsContain(proposals, "begin"));
	}

	@Test
	public void testGlobalInvalidCompletion() throws BadLocationException {
		ICompletionProposal[] proposals = getCompletionsForPrefix("probe fake.fake");
		assertTrue(proposals.length == 0);
	}

	@Test
	public void testStaticProbeCompletion() throws BadLocationException{
		ICompletionProposal[] proposals = getCompletionsForPrefix("probe kernel.");
		assertTrue(proposalsContain(proposals, "kernel.function(\"PATTERN\")"));
		assertTrue(proposalsContain(proposals, "kernel.mark(\"MARK\")"));
	}

	@Test
	public void testEndProbeCompletion() throws BadLocationException {
		assumeTrue(stapInstalled());

		Document testDocument = new Document(TEST_STP_SCRIPT);
		@SuppressWarnings("unused")
		MockSTPEditor editor = new MockSTPEditor(testDocument);

		int offset = TEST_STP_SCRIPT.indexOf("//marker1");
		String prefix = "probe end{}";
		testDocument.replace(offset, 0, prefix);
		offset += prefix.length() - 1;

		STPCompletionProcessor completionProcessor = new STPCompletionProcessor();
		completionProcessor.waitForInitialization();
		ICompletionProposal[] proposals = completionProcessor
				.computeCompletionProposals(testDocument,
						offset);

		assertTrue(proposalsContain(proposals, "user_int16"));
		assertTrue(proposalsContain(proposals, "user_int32"));
		assertTrue(proposalsContain(proposals, "user_int64"));
	}

	@Test
	public void testProbeVariableCompletion() throws BadLocationException {
		assumeTrue(stapInstalled());

		Document testDocument = new Document(TEST_STP_SCRIPT);
		@SuppressWarnings("unused")
		MockSTPEditor editor = new MockSTPEditor(testDocument);

		int offset = TEST_STP_SCRIPT.indexOf("//marker1");
		String prefix = "probe syscall.write{}";
		testDocument.replace(offset, 0, prefix);
		offset += prefix.length() - 1;

		STPCompletionProcessor completionProcessor = new STPCompletionProcessor();
		ICompletionProposal[] proposals = completionProcessor
				.computeCompletionProposals(testDocument,
						offset);

		assertTrue(proposalsContain(proposals, "fd:long"));
		assertTrue(proposalsContain(proposals, "name:string"));
		assertTrue(proposalsContain(proposals, "buf_uaddr:long"));
	}

	@Test
	public void testStaticProbeNormalizationCompletion() throws BadLocationException{
		ICompletionProposal[] proposals = getCompletionsForPrefix("probe kernel.function(\"PATTERNASDF\").");
		assertTrue(proposalsContain(proposals, "kernel.function(\"PATTERN\").return"));

        proposals = getCompletionsForPrefix("probe probe process(\"PAT/H/\").");
		assertTrue(proposalsContain(proposals, "process(\"PATH\").begin"));
		assertTrue(proposalsContain(proposals, "process(\"PATH\").end"));

        proposals = getCompletionsForPrefix("probe  process(123).");
		assertTrue(proposalsContain(proposals, "process(PID).begin"));
		assertTrue(proposalsContain(proposals, "process(PID).end"));

        proposals = getCompletionsForPrefix("probe module(\"MPATTERasdfN\").");
		assertTrue(proposalsContain(proposals, "module(\"MPATTERN\").function(\"PATTERN\")"));
		assertTrue(proposalsContain(proposals, "module(\"MPATTERN\").statement(\"PATTERN\")"));
	}

	private ICompletionProposal[] getCompletionsForPrefix(String prefix) throws BadLocationException{
		Document testDocument = new Document(TEST_STP_SCRIPT);
		int offset = TEST_STP_SCRIPT.indexOf("//marker1");
		testDocument.replace(offset, 0, prefix);
		offset += prefix.length();

		STPCompletionProcessor completionProcessor = new STPCompletionProcessor();
		completionProcessor.waitForInitialization();

		ICompletionProposal[] proposals = completionProcessor
				.computeCompletionProposals(testDocument,
						offset);
		return proposals;
	}

	private boolean stapInstalled(){
		try {
			Process process = RuntimeProcessFactory.getFactory().exec(new String[]{"stap", "-V"}, null);
			return (process != null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Test
	public void testFunctionCompletion() throws BadLocationException {
		assumeTrue(stapInstalled());

		Document testDocument = new Document(TEST_STP_SCRIPT);
		@SuppressWarnings("unused")
		MockSTPEditor editor = new MockSTPEditor(testDocument);

		int offset = TEST_STP_SCRIPT.indexOf("//marker1");
		String prefix = "probe syscall.write{user_}";
		testDocument.replace(offset, 0, prefix);
		offset += prefix.length() - 1;

		STPCompletionProcessor completionProcessor = new STPCompletionProcessor();
		completionProcessor.waitForInitialization();

		ICompletionProposal[] proposals = completionProcessor
				.computeCompletionProposals(testDocument,
						offset);

		assertTrue(proposalsContain(proposals, "user_int16"));
		assertTrue(proposalsContain(proposals, "user_int32"));
		assertTrue(proposalsContain(proposals, "user_int64"));
	}

	private boolean proposalsContain(ICompletionProposal[] proposals, String proposal){
		for (ICompletionProposal p : proposals) {
			if (p.getDisplayString().contains(proposal))
				return true;
		}
		return false;
	}

}

Back to the top