OmniSciDB  340b00dbf6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OmniSciConnectionTest.java
Go to the documentation of this file.
1 package com.omnisci.jdbc;
2 
3 import static org.junit.Assert.*;
4 
5 import org.apache.thrift.transport.TTransportException;
6 import org.junit.BeforeClass;
7 import org.junit.Test;
8 
9 import java.io.File;
10 import java.io.FileInputStream;
11 import java.io.InputStream;
12 import java.sql.*;
13 import java.sql.SQLException;
14 import java.util.Properties;
15 
16 import javax.net.ssl.SSLPeerUnverifiedException;
17 
18 public class OmniSciConnectionTest {
19  // Property_loader loads the values from 'connection.properties in resources
20  static Properties PROPERTIES = new Property_loader("connection_test.properties");
21  static final String user = PROPERTIES.getProperty("default_super_user");
22  static final String password = PROPERTIES.getProperty("default_user_password");
23  static Properties base_properties;
24  /* Test the basic connection and methods functionality */
25  @BeforeClass
26  public static void setUpBeforeClass() throws Exception {
27  String fileName = System.getProperty("propertiesFileName");
28  base_properties = new Properties();
29  if (fileName == null || fileName.equals("")) {
30  return;
31  }
32  File initialFile = new File(fileName);
33  InputStream inputStream = new FileInputStream(initialFile);
34  base_properties.load(inputStream);
35  }
36  @Test
37  public void tst1_binary_unencrypted() {
38  try {
39  String url = PROPERTIES.getProperty("binary_connection_url") + ":"
40  + PROPERTIES.getProperty("default_db");
41  Connection conn = DriverManager.getConnection(url, user, password);
42  assertNotEquals(null, conn);
43  conn.close();
44  boolean closed = conn.isClosed();
45  assertEquals(true, closed);
46  } catch (SQLException sq) {
47  String err = "Connection test failed " + sq.toString();
48  fail(err);
49  }
50  }
51  @Test
52  public void tst2_http_unencrypted() {
53  try {
54  String url = PROPERTIES.getProperty("http_connection_url") + ":"
55  + PROPERTIES.getProperty("default_db") + ":http";
56  Connection conn = DriverManager.getConnection(url, user, password);
57  assertNotEquals(null, conn);
58  conn.close();
59  boolean closed = conn.isClosed();
60  assertEquals(true, closed);
61  } catch (SQLException sq) {
62  String err = "Connection test failed " + sq.toString();
63  fail(err);
64  }
65  }
66  @Test
67  public void tst3_connect_fail() {
68  try {
69  String url = PROPERTIES.getProperty("failed_connection_url") + ":"
70  + PROPERTIES.getProperty("default_db");
71  Properties pt = new Properties();
72  pt.setProperty("user", user);
73  pt.setProperty("password", password);
74  Connection conn = DriverManager.getConnection(url, pt);
75  } catch (SQLException sq) {
76  assertEquals(sq.getMessage(),
77  "No suitable driver found for jdbc:NOT_omnisci:localhost:6274:omnisci");
78  return;
79  }
80  String err = "Connection should have thrown";
81  fail(err);
82  }
83 
84  @Test
85  public void tst4_connect_url_override() {
86  try {
87  String url = PROPERTIES.getProperty("default_mapd_connection_url") + ":"
88  + PROPERTIES.getProperty("default_db");
89  Properties pt = new Properties();
90  pt.setProperty("user", user);
91  pt.setProperty("password", password);
92  pt.setProperty("db_name", "SomeOtherDB");
93  // Shouldn't fail (url over ride properties.
94  Connection conn = DriverManager.getConnection(url, pt);
95  } catch (SQLException sq) {
96  fail(sq.getMessage());
97  }
98  }
99 
100  @Test
102  try {
103  String url = "jdbc:omnisci:" + base_properties.getProperty("host_name");
104  Connection conn = DriverManager.getConnection(url, base_properties);
105  assertNotEquals(null, conn);
106  conn.close();
107  boolean closed = conn.isClosed();
108  assertEquals(true, closed);
109  } catch (SQLException sq) {
110  String err = "Connection test failed " + sq.toString();
111  fail(err);
112  }
113  }
114 
115  @Test
117  try {
118  String url = PROPERTIES.getProperty("binary_connection_url") + ":"
119  + PROPERTIES.getProperty("default_db") + ":binary_tls";
120 
121  Properties pt = new Properties();
122  pt.setProperty("user", user);
123  pt.setProperty("password", password);
124  Connection conn = DriverManager.getConnection(url, pt);
125  assertNotEquals(null, conn);
126  conn.close();
127  boolean closed = conn.isClosed();
128  assertEquals(true, closed);
129  } catch (SQLException sq) {
130  String err = "Connection test failed " + sq.toString();
131  fail(err);
132  }
133  }
134 
135  @Test
137  try {
138  Properties pt = new Properties();
139  String url = PROPERTIES.getProperty("binary_connection_url") + ":"
140  + PROPERTIES.getProperty("default_db") + ":binary_tls";
141 
142  ClassLoader cl = getClass().getClassLoader();
143  String trust_store = PROPERTIES.getProperty("server_trust_store");
144  trust_store = cl.getResource(trust_store).getPath();
145  pt.setProperty("server_trust_store", trust_store);
146 
147  String sslcert_cl1 = PROPERTIES.getProperty("sslcert_cl1_pkcs12");
148  sslcert_cl1 = cl.getResource(sslcert_cl1).getPath();
149  pt.setProperty("sslcert", sslcert_cl1);
150 
151  pt.setProperty("sslkey_password", PROPERTIES.getProperty("sslkey_password_cl1"));
152 
153  pt.setProperty("user", "pki");
154  pt.setProperty("password", "");
155  pt.setProperty("server_trust_store_pwd",
156  PROPERTIES.getProperty("server_trust_store_password"));
157  pt.setProperty("pkiauth", PROPERTIES.getProperty("pkiauth"));
158 
159  Connection conn = DriverManager.getConnection(url, pt);
160  assertNotEquals(null, conn);
161 
162  Statement statement = conn.createStatement();
163  statement.executeUpdate("drop table if exists test_jdbc_tm_tble");
164 
165  conn.close();
166  boolean closed = conn.isClosed();
167  assertEquals(true, closed);
168  } catch (SQLException sq) {
169  String err = "Connection test failed " + sq.toString();
170  fail(err);
171  }
172  }
173 
174  @Test
176  try {
177  Properties pt = new Properties();
178  String url = PROPERTIES.getProperty("binary_connection_url") + ":"
179  + PROPERTIES.getProperty("default_db") + ":binary_tls";
180 
181  ClassLoader cl = getClass().getClassLoader();
182  String trust_store = PROPERTIES.getProperty("server_trust_store");
183  trust_store = cl.getResource(trust_store).getPath();
184  pt.setProperty("server_trust_store", trust_store);
185 
186  // Connect client 1 whose cert is signed by primary and should work
187  String sslcert_cl2 = PROPERTIES.getProperty("sslcert_cl2");
188  sslcert_cl2 = cl.getResource(sslcert_cl2).getPath();
189  pt.setProperty("sslcert", sslcert_cl2);
190 
191  String sslkey_cl2 = PROPERTIES.getProperty("sslkey_cl2");
192  sslkey_cl2 = cl.getResource(sslkey_cl2).getPath();
193  pt.setProperty("sslkey", sslkey_cl2);
194 
195  pt.setProperty("user", "pki");
196  pt.setProperty("password", "");
197  pt.setProperty("server_trust_store_pwd",
198  PROPERTIES.getProperty("server_trust_store_password"));
199  pt.setProperty("pkiauth", PROPERTIES.getProperty("pkiauth"));
200 
201  Connection conn = DriverManager.getConnection(url, pt);
202  assertNotEquals(null, conn);
203  conn.close();
204  boolean closed = conn.isClosed();
205  fail("Credential should not have been accepted");
206  } catch (SQLException sq) {
207  String err = "Connection test failed " + sq.toString();
208  assertTrue(err.contains("Invalid credentials"));
209  }
210  }
211 
212  @Test
214  try {
215  Properties pt = new Properties();
216  pt.setProperty("user", user);
217  pt.setProperty("password", password);
218  pt.setProperty("protocol", "https_insecure");
219  String url = PROPERTIES.getProperty("https_connection_url") + ":"
220  + PROPERTIES.getProperty("default_db");
221  Connection conn = DriverManager.getConnection(url, pt);
222  assertNotEquals(null, conn);
223  conn.close();
224  boolean closed = conn.isClosed();
225  assertEquals(true, closed);
226  } catch (SQLException sq) {
227  String err = "Connection test failed " + sq.toString();
228  fail(err);
229  }
230  }
231 
232  @Test
234  try {
235  ClassLoader cl = getClass().getClassLoader();
236  String trust_store = PROPERTIES.getProperty("server_trust_store");
237  trust_store = cl.getResource(trust_store).getPath();
238 
239  Properties pt = new Properties();
240  pt.setProperty("server_trust_store", trust_store);
241  pt.setProperty("server_trust_store_pwd",
242  PROPERTIES.getProperty("server_trust_store_password"));
243 
244  pt.setProperty("user", user);
245  pt.setProperty("password", password);
246  pt.setProperty("protocol", "https_insecure");
247 
248  String url = PROPERTIES.getProperty("https_connection_url") + ":"
249  + PROPERTIES.getProperty("default_db");
250  Connection conn = DriverManager.getConnection(url, pt);
251  assertNotEquals(null, conn);
252  conn.close();
253  boolean closed = conn.isClosed();
254  assertEquals(true, closed);
255  } catch (SQLException sq) {
256  String err = "Connection test failed " + sq.toString();
257  fail(err);
258  }
259  }
260 
261  @Test
263  try {
264  Properties pt = new Properties();
265  pt.setProperty("user", user);
266  pt.setProperty("password", password);
267  pt.setProperty("protocol", "https");
268  String url = PROPERTIES.getProperty("https_connection_url") + ":"
269  + PROPERTIES.getProperty("default_db");
270  Connection conn = DriverManager.getConnection(url, pt);
271  assertNotEquals(null, conn);
272  conn.close();
273  boolean closed = conn.isClosed();
274  assertEquals(true, closed);
275  } catch (SQLException sq) {
276  String err = "Connection test failed " + sq.toString();
277  fail(err);
278  }
279  }
280 
281  @Test
283  try {
284  ClassLoader cl = getClass().getClassLoader();
285  String trust_store = PROPERTIES.getProperty("ca_primary_trust_store");
286  trust_store = cl.getResource(trust_store).getPath();
287  Properties pt = new Properties();
288  pt.setProperty("server_trust_store", trust_store);
289  pt.setProperty("server_trust_store_pwd",
290  PROPERTIES.getProperty("server_trust_store_password"));
291 
292  pt.setProperty("user", user);
293  pt.setProperty("password", password);
294  pt.setProperty("protocol", "https");
295 
296  String url = PROPERTIES.getProperty("https_connection_url") + ":"
297  + PROPERTIES.getProperty("default_db");
298 
299  Connection conn = DriverManager.getConnection(url, pt);
300  conn.close();
301  assertNotEquals(null, conn);
302  boolean closed = conn.isClosed();
303  assertEquals(true, closed);
304 
305  assertNotEquals(null, conn);
306  } catch (SQLException sq) {
307  String err = "Connection test failed " + sq.toString();
308  fail(err);
309  }
310  }
311  @Test
313  try {
314  Properties pt = new Properties();
315  String url = PROPERTIES.getProperty("https_connection_url") + ":"
316  + PROPERTIES.getProperty("default_db") + ":https_insecure";
317 
318  ClassLoader cl = getClass().getClassLoader();
319  String trust_store = PROPERTIES.getProperty("server_trust_store");
320  trust_store = cl.getResource(trust_store).getPath();
321  pt.setProperty("server_trust_store", trust_store);
322 
323  String sslcert_cl1 = PROPERTIES.getProperty("sslcert_cl1_pkcs12");
324  sslcert_cl1 = cl.getResource(sslcert_cl1).getPath();
325  pt.setProperty("sslcert", sslcert_cl1);
326 
327  pt.setProperty("sslkey_password", PROPERTIES.getProperty("sslkey_password_cl1"));
328 
329  pt.setProperty("user", "pki");
330  pt.setProperty("password", "");
331  pt.setProperty("server_trust_store_pwd",
332  PROPERTIES.getProperty("server_trust_store_password"));
333  pt.setProperty("pkiauth", PROPERTIES.getProperty("pkiauth"));
334 
335  Connection conn = DriverManager.getConnection(url, pt);
336  Statement statement = conn.createStatement();
337  statement.executeUpdate("drop table if exists test_jdbc_tm_tble");
338  assertNotEquals(null, conn);
339  conn.close();
340  boolean closed = conn.isClosed();
341  assertEquals(true, closed);
342  } catch (SQLException sq) {
343  String err = "Connection test failed " + sq.toString();
344  fail(err);
345  }
346  }
347 }
string url
Definition: setup.py:39