Skip to content

Commit 1e5c848

Browse files
idodeclareVladimir Kotal
authored andcommitted
Add tests of HCL xref and symbols
1 parent 3c7c9b2 commit 1e5c848

File tree

7 files changed

+306
-0
lines changed

7 files changed

+306
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, 2019-2020, Chris Fraire <[email protected]>.
23+
*/
24+
25+
package org.opengrok.indexer.analysis.hcl;
26+
27+
import org.junit.Test;
28+
29+
import java.io.InputStream;
30+
import java.util.List;
31+
32+
import static org.junit.Assert.assertNotNull;
33+
import static org.opengrok.indexer.util.CustomAssertions.assertSymbolStream;
34+
import static org.opengrok.indexer.util.StreamUtils.readSampleSymbols;
35+
36+
/**
37+
* Represents a container for tests of {@link HCLSymbolTokenizer}.
38+
*/
39+
public class HCLSymbolTokenizerTest {
40+
41+
/**
42+
* Test sample.hcl v. samplesymbols.txt
43+
*/
44+
@Test
45+
public void testTerraformSymbolStream() throws Exception {
46+
testSymbols("analysis/hcl/sample.hcl", "analysis/hcl/samplesymbols.txt");
47+
}
48+
49+
private void testSymbols(String codeResource, String symbolsResource) throws Exception {
50+
InputStream hclRes = getClass().getClassLoader().getResourceAsStream(codeResource);
51+
assertNotNull("Should get resource " + codeResource, hclRes);
52+
InputStream symRes = getClass().getClassLoader().getResourceAsStream(symbolsResource);
53+
assertNotNull("Should get resource " + symbolsResource, symRes);
54+
55+
List<String> expectedSymbols = readSampleSymbols(symRes);
56+
assertSymbolStream(HCLSymbolTokenizer.class, hclRes, expectedSymbols);
57+
}
58+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, 2019-2020, Chris Fraire <[email protected]>.
23+
*/
24+
25+
package org.opengrok.indexer.analysis.hcl;
26+
27+
import org.junit.Test;
28+
import org.opengrok.indexer.analysis.XrefTestBase;
29+
30+
import java.io.IOException;
31+
32+
/**
33+
* Represents a container for tests of {@link HCLXref}.
34+
*/
35+
public class HCLXrefTest extends XrefTestBase {
36+
37+
@Test
38+
public void sampleTest() throws IOException {
39+
writeAndCompare(new HCLAnalyzerFactory(),
40+
"analysis/hcl/sample.hcl",
41+
"analysis/hcl/sample_xref.html", null, 25);
42+
}
43+
44+
@Test
45+
public void shouldCloseTruncatedStringSpan() throws IOException {
46+
writeAndCompare(new HCLAnalyzerFactory(),
47+
"analysis/hcl/truncated.hcl",
48+
"analysis/hcl/truncated_xref.html", null, 1);
49+
}
50+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
*/
6+
7+
/*
8+
* This is derived from Caiyeon goldfish/config/sample.hcl just for testing
9+
* OpenGrok's HCL handling and modified arbitrarily to test other HCL syntax.
10+
*/
11+
12+
# [Required] listener defines how goldfish will listen to incoming connections
13+
listener "tcp" {
14+
# [Required] [Format: "address", "address:port", or ":port"]
15+
# goldfish's listening address and/or port. Simply ":443" would suffice.
16+
address = ":8000"
17+
18+
# [Optional] [Default: 0] [Allowed values: 0, 1]
19+
# set to 1 to disable tls & https
20+
tls_disable = 1
21+
22+
# [Optional] [Default: 0] [Allowed values: 0, 1]
23+
# set to 1 to redirect port 80 to 443 (hard-coded port numbers)
24+
tls_autoredirect = 0
25+
26+
# Option 1: local certificate
27+
certificate "local" {
28+
cert_file = "/path/to/certificate.cert"
29+
key_file = "/path/to/keyfile.pem"
30+
}
31+
32+
# Option 2: using Vault's PKI backend [Requires vault_token at launch time]
33+
# goldfish will request new certificates at half-life and hot-reload,
34+
pki_certificate "pki" {
35+
# [Required]
36+
pki_path = "pki/issue/<role_name>"
37+
common_name = "goldfish.vault.service"
38+
39+
# [Optional] see Vault PKI docs for what these mean
40+
alt_names = ["goldfish.vault.srv", "ui.vault.srv"]
41+
ip_sans = ["10.0.0.10", "127.0.0.1", "172.0.0.1"]
42+
}
43+
}
44+
45+
# [Required] vault defines how goldfish should bootstrap to vault
46+
vault {
47+
# [Required] [Format: "protocol://address:port"]
48+
# This is vault's address. Vault must be up before goldfish is deployed!
49+
address = "http://127.0.0.1:8200"
50+
51+
# [Optional] [Default: 0] [Allowed values: 0, 1]
52+
# Set this to 1 to skip verifying the certificate of vault (e.g. self-signed certs)
53+
tls_skip_verify = 0
54+
55+
# [Required] [Default: "secret/goldfish"]
56+
# This should be a generic secret endpoint where runtime settings are stored
57+
# See wiki for what key values are required in this
58+
runtime_config = "secret/goldfish"
59+
60+
# [Optional] [Default: "auth/approle/login"]
61+
# You can omit this, unless you mounted approle somewhere weird
62+
approle_login = "auth/approle/login"
63+
64+
# [Optional] [Default: "goldfish"]
65+
# You can omit this if you already customized the approle ID to be 'goldfish'
66+
approle_id = "goldfish"
67+
68+
# [Optional] [Default: ""]
69+
# If provided, goldfish will use this CA cert to verify Vault's certificate
70+
# This should be a path to a PEM-encoded CA cert file
71+
ca_cert = ""
72+
73+
# [Optional] [Default: ""]
74+
# See above. This should be a path to a directory instead of a single cert
75+
ca_path = ""
76+
}
77+
78+
# [Optional] [Default: 0] [Allowed values: 0, 1]
79+
# Set to 1 to disable mlock. Implementation is similar to vault - see vault docs for details
80+
# This option will be ignored on unsupported platforms (e.g Windows)
81+
disable_mlock = 0
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>sampleFile - OpenGrok cross reference for /sampleFile</title></head><body>
6+
<a class="l" name="1" href="#1">1</a><span class="c">/*</span>
7+
<a class="l" name="2" href="#2">2</a><span class="c"> * This Source Code Form is subject to the terms of the Mozilla Public</span>
8+
<a class="l" name="3" href="#3">3</a><span class="c"> * License, v. 2.0. If a copy of the MPL was not distributed with this</span>
9+
<a class="l" name="4" href="#4">4</a><span class="c"> * file, You can obtain one at <a href="http://mozilla.org/MPL/2.0/">http://mozilla.org/MPL/2.0/</a>.</span>
10+
<a class="l" name="5" href="#5">5</a><span class="c"> */</span>
11+
<a class="l" name="6" href="#6">6</a>
12+
<a class="l" name="7" href="#7">7</a><span class="c">/*</span>
13+
<a class="l" name="8" href="#8">8</a><span class="c"> * This is derived from Caiyeon <a href="/source/s?path=goldfish/">goldfish</a>/<a href="/source/s?path=goldfish/config/">config</a>/<a href="/source/s?path=goldfish/config/sample.hcl">sample.hcl</a> just for testing</span>
14+
<a class="l" name="9" href="#9">9</a><span class="c"> * OpenGrok&apos;s HCL handling and modified arbitrarily to test other HCL syntax.</span>
15+
<a class="hl" name="10" href="#10">10</a><span class="c"> */</span>
16+
<a class="l" name="11" href="#11">11</a>
17+
<a class="l" name="12" href="#12">12</a><span class="c"># [Required] listener defines how goldfish will listen to incoming connections</span>
18+
<a class="l" name="13" href="#13">13</a><a href="/source/s?defs=listener" class="intelliWindow-symbol" data-definition-place="undefined-in-file">listener</a> <span class="s">&quot;tcp&quot;</span> {
19+
<a class="l" name="14" href="#14">14</a> <span class="c"># [Required] [Format: &quot;address&quot;, &quot;address:port&quot;, or &quot;:port&quot;]</span>
20+
<a class="l" name="15" href="#15">15</a> <span class="c"># goldfish&apos;s listening address <a href="/source/s?path=and/">and</a>/<a href="/source/s?path=and/or">or</a> port. Simply &quot;:443&quot; would suffice.</span>
21+
<a class="l" name="16" href="#16">16</a> <a href="/source/s?defs=address" class="intelliWindow-symbol" data-definition-place="undefined-in-file">address</a> = <span class="s">&quot;:8000&quot;</span>
22+
<a class="l" name="17" href="#17">17</a>
23+
<a class="l" name="18" href="#18">18</a> <span class="c"># [Optional] [Default: 0] [Allowed values: 0, 1]</span>
24+
<a class="l" name="19" href="#19">19</a> <span class="c"># set to 1 to disable tls &amp; https</span>
25+
<a class="hl" name="20" href="#20">20</a> <a href="/source/s?defs=tls_disable" class="intelliWindow-symbol" data-definition-place="undefined-in-file">tls_disable</a> = <span class="n">1</span>
26+
<a class="l" name="21" href="#21">21</a>
27+
<a class="l" name="22" href="#22">22</a> <span class="c"># [Optional] [Default: 0] [Allowed values: 0, 1]</span>
28+
<a class="l" name="23" href="#23">23</a> <span class="c"># set to 1 to redirect port 80 to 443 (hard-coded port numbers)</span>
29+
<a class="l" name="24" href="#24">24</a> <a href="/source/s?defs=tls_autoredirect" class="intelliWindow-symbol" data-definition-place="undefined-in-file">tls_autoredirect</a> = <span class="n">0</span>
30+
<a class="l" name="25" href="#25">25</a>
31+
<a class="l" name="26" href="#26">26</a> <span class="c"># Option 1: local certificate</span>
32+
<a class="l" name="27" href="#27">27</a> <a href="/source/s?defs=certificate" class="intelliWindow-symbol" data-definition-place="undefined-in-file">certificate</a> <span class="s">&quot;local&quot;</span> {
33+
<a class="l" name="28" href="#28">28</a> <a href="/source/s?defs=cert_file" class="intelliWindow-symbol" data-definition-place="undefined-in-file">cert_file</a> = <span class="s">&quot;/<a href="/source/s?path=/path/">path</a>/<a href="/source/s?path=/path/to/">to</a>/<a href="/source/s?path=/path/to/certificate.cert">certificate.cert</a>&quot;</span>
34+
<a class="l" name="29" href="#29">29</a> <a href="/source/s?defs=key_file" class="intelliWindow-symbol" data-definition-place="undefined-in-file">key_file</a> = <span class="s">&quot;/<a href="/source/s?path=/path/">path</a>/<a href="/source/s?path=/path/to/">to</a>/<a href="/source/s?path=/path/to/keyfile.pem">keyfile.pem</a>&quot;</span>
35+
<a class="hl" name="30" href="#30">30</a> }
36+
<a class="l" name="31" href="#31">31</a>
37+
<a class="l" name="32" href="#32">32</a> <span class="c"># Option 2: using Vault&apos;s PKI backend [Requires vault_token at launch time]</span>
38+
<a class="l" name="33" href="#33">33</a> <span class="c"># goldfish will request new certificates at half-life and hot-reload,</span>
39+
<a class="l" name="34" href="#34">34</a> <a href="/source/s?defs=pki_certificate" class="intelliWindow-symbol" data-definition-place="undefined-in-file">pki_certificate</a> <span class="s">&quot;pki&quot;</span> {
40+
<a class="l" name="35" href="#35">35</a> <span class="c"># [Required]</span>
41+
<a class="l" name="36" href="#36">36</a> <a href="/source/s?defs=pki_path" class="intelliWindow-symbol" data-definition-place="undefined-in-file">pki_path</a> = <span class="s">&quot;<a href="/source/s?path=pki/">pki</a>/<a href="/source/s?path=pki/issue">issue</a>/&lt;role_name&gt;&quot;</span>
42+
<a class="l" name="37" href="#37">37</a> <a href="/source/s?defs=common_name" class="intelliWindow-symbol" data-definition-place="undefined-in-file">common_name</a> = <span class="s">&quot;goldfish.vault.service&quot;</span>
43+
<a class="l" name="38" href="#38">38</a>
44+
<a class="l" name="39" href="#39">39</a> <span class="c"># [Optional] see Vault PKI docs for what these mean</span>
45+
<a class="hl" name="40" href="#40">40</a> <a href="/source/s?defs=alt_names" class="intelliWindow-symbol" data-definition-place="undefined-in-file">alt_names</a> = [<span class="s">&quot;goldfish.vault.srv&quot;</span>, <span class="s">&quot;ui.vault.srv&quot;</span>]
46+
<a class="l" name="41" href="#41">41</a> <a href="/source/s?defs=ip_sans" class="intelliWindow-symbol" data-definition-place="undefined-in-file">ip_sans</a> = [<span class="s">&quot;10.0.0.10&quot;</span>, <span class="s">&quot;127.0.0.1&quot;</span>, <span class="s">&quot;172.0.0.1&quot;</span>]
47+
<a class="l" name="42" href="#42">42</a> }
48+
<a class="l" name="43" href="#43">43</a>}
49+
<a class="l" name="44" href="#44">44</a>
50+
<a class="l" name="45" href="#45">45</a><span class="c"># [Required] vault defines how goldfish should bootstrap to vault</span>
51+
<a class="l" name="46" href="#46">46</a><a href="/source/s?defs=vault" class="intelliWindow-symbol" data-definition-place="undefined-in-file">vault</a> {
52+
<a class="l" name="47" href="#47">47</a> <span class="c"># [Required] [Format: &quot;protocol://address:port&quot;]</span>
53+
<a class="l" name="48" href="#48">48</a> <span class="c"># This is vault&apos;s address. Vault must be up before goldfish is deployed!</span>
54+
<a class="l" name="49" href="#49">49</a> <a href="/source/s?defs=address" class="intelliWindow-symbol" data-definition-place="undefined-in-file">address</a> = <span class="s">&quot;<a href="http://127.0.0.1:8200">http://127.0.0.1:8200</a>&quot;</span>
55+
<a class="hl" name="50" href="#50">50</a>
56+
<a class="l" name="51" href="#51">51</a> <span class="c"># [Optional] [Default: 0] [Allowed values: 0, 1]</span>
57+
<a class="l" name="52" href="#52">52</a> <span class="c"># Set this to 1 to skip verifying the certificate of vault (e.g. self-signed certs)</span>
58+
<a class="l" name="53" href="#53">53</a> <a href="/source/s?defs=tls_skip_verify" class="intelliWindow-symbol" data-definition-place="undefined-in-file">tls_skip_verify</a> = <span class="n">0</span>
59+
<a class="l" name="54" href="#54">54</a>
60+
<a class="l" name="55" href="#55">55</a> <span class="c"># [Required] [Default: &quot;<a href="/source/s?path=secret/">secret</a>/<a href="/source/s?path=secret/goldfish">goldfish</a>&quot;]</span>
61+
<a class="l" name="56" href="#56">56</a> <span class="c"># This should be a generic secret endpoint where runtime settings are stored</span>
62+
<a class="l" name="57" href="#57">57</a> <span class="c"># See wiki for what key values are required in this</span>
63+
<a class="l" name="58" href="#58">58</a> <a href="/source/s?defs=runtime_config" class="intelliWindow-symbol" data-definition-place="undefined-in-file">runtime_config</a> = <span class="s">&quot;<a href="/source/s?path=secret/">secret</a>/<a href="/source/s?path=secret/goldfish">goldfish</a>&quot;</span>
64+
<a class="l" name="59" href="#59">59</a>
65+
<a class="hl" name="60" href="#60">60</a> <span class="c"># [Optional] [Default: &quot;<a href="/source/s?path=auth/">auth</a>/<a href="/source/s?path=auth/approle/">approle</a>/<a href="/source/s?path=auth/approle/login">login</a>&quot;]</span>
66+
<a class="l" name="61" href="#61">61</a> <span class="c"># You can omit this, unless you mounted approle somewhere weird</span>
67+
<a class="l" name="62" href="#62">62</a> <a href="/source/s?defs=approle_login" class="intelliWindow-symbol" data-definition-place="undefined-in-file">approle_login</a> = <span class="s">&quot;<a href="/source/s?path=auth/">auth</a>/<a href="/source/s?path=auth/approle/">approle</a>/<a href="/source/s?path=auth/approle/login">login</a>&quot;</span>
68+
<a class="l" name="63" href="#63">63</a>
69+
<a class="l" name="64" href="#64">64</a> <span class="c"># [Optional] [Default: &quot;goldfish&quot;]</span>
70+
<a class="l" name="65" href="#65">65</a> <span class="c"># You can omit this if you already customized the approle ID to be &apos;goldfish&apos;</span>
71+
<a class="l" name="66" href="#66">66</a> <a href="/source/s?defs=approle_id" class="intelliWindow-symbol" data-definition-place="undefined-in-file">approle_id</a> = <span class="s">&quot;goldfish&quot;</span>
72+
<a class="l" name="67" href="#67">67</a>
73+
<a class="l" name="68" href="#68">68</a> <span class="c"># [Optional] [Default: &quot;&quot;]</span>
74+
<a class="l" name="69" href="#69">69</a> <span class="c"># If provided, goldfish will use this CA cert to verify Vault&apos;s certificate</span>
75+
<a class="hl" name="70" href="#70">70</a> <span class="c"># This should be a path to a PEM-encoded CA cert file</span>
76+
<a class="l" name="71" href="#71">71</a> <a href="/source/s?defs=ca_cert" class="intelliWindow-symbol" data-definition-place="undefined-in-file">ca_cert</a> = <span class="s">&quot;&quot;</span>
77+
<a class="l" name="72" href="#72">72</a>
78+
<a class="l" name="73" href="#73">73</a> <span class="c"># [Optional] [Default: &quot;&quot;]</span>
79+
<a class="l" name="74" href="#74">74</a> <span class="c"># See above. This should be a path to a directory instead of a single cert</span>
80+
<a class="l" name="75" href="#75">75</a> <a href="/source/s?defs=ca_path" class="intelliWindow-symbol" data-definition-place="undefined-in-file">ca_path</a> = <span class="s">&quot;&quot;</span>
81+
<a class="l" name="76" href="#76">76</a>}
82+
<a class="l" name="77" href="#77">77</a>
83+
<a class="l" name="78" href="#78">78</a><span class="c"># [Optional] [Default: 0] [Allowed values: 0, 1]</span>
84+
<a class="l" name="79" href="#79">79</a><span class="c"># Set to 1 to disable mlock. Implementation is similar to vault - see vault docs for details</span>
85+
<a class="hl" name="80" href="#80">80</a><span class="c"># This option will be ignored on unsupported platforms (e.g Windows)</span>
86+
<a class="l" name="81" href="#81">81</a><a href="/source/s?defs=disable_mlock" class="intelliWindow-symbol" data-definition-place="undefined-in-file">disable_mlock</a> = <span class="n">0</span>
87+
<a class="l" name="82" href="#82">82</a></body>
88+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
listener
2+
address
3+
tls_disable
4+
tls_autoredirect
5+
certificate
6+
cert_file
7+
key_file
8+
pki_certificate
9+
pki_path
10+
common_name
11+
alt_names
12+
ip_sans
13+
vault
14+
address
15+
tls_skip_verify
16+
runtime_config
17+
approle_login
18+
approle_id
19+
ca_cert
20+
ca_path
21+
disable_mlock
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"Oops this string is not ter
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>sampleFile - OpenGrok cross reference for /sampleFile</title></head><body>
6+
<a class="l" name="1" href="#1">1</a> <span class="s">&quot;Oops this string is not ter</span></body>
7+
</html>

0 commit comments

Comments
 (0)