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