OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HeavyAIConnectionTest.java
Go to the documentation of this file.
1 package ai.heavy.jdbc;
2 
3 import static org.junit.Assert.*;
4 
5 import org.junit.BeforeClass;
6 import org.junit.Test;
7 
8 import java.io.File;
9 import java.io.FileInputStream;
10 import java.io.InputStream;
11 import java.sql.*;
12 import java.sql.SQLException;
13 import java.util.Properties;
14 
15 public class HeavyAIConnectionTest {
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  static Properties base_properties;
20  /* Test the basic connection and methods functionality */
21  @BeforeClass
22  public static void setUpBeforeClass() throws Exception {
23  String fileName = System.getProperty("propertiesFileName");
24  base_properties = new Properties();
25  if (fileName == null || fileName.equals("")) {
26  return;
27  }
28  File initialFile = new File(fileName);
29  InputStream inputStream = new FileInputStream(initialFile);
30  base_properties.load(inputStream);
31  }
32 
33  @Test
34  public void tst1_binary_unencrypted() {
35  try {
36  String url = PROPERTIES.getProperty("binary_connection_url") + ":"
37  + PROPERTIES.getProperty("default_db");
38  Connection conn = DriverManager.getConnection(url, user, password);
39  assertNotEquals(null, conn);
40  conn.close();
41  boolean closed = conn.isClosed();
42  assertEquals(true, closed);
43  } catch (SQLException sq) {
44  String err = "Connection test failed " + sq.toString();
45  fail(err);
46  }
47  }
48 
49  @Test
51  String url = null;
52  try {
53  url = PROPERTIES.getProperty("query_connection_url1");
54  Connection conn = DriverManager.getConnection(url, user, password);
55  assertNotEquals(null, conn);
56  conn.close();
57  boolean closed = conn.isClosed();
58  assertEquals(true, closed);
59 
60  } catch (SQLException sq) {
61  String err = "Connection test failed for url " + url + ":" + sq.toString();
62  fail(err);
63  }
64  }
65  @Test
67  String url = null;
68  url = PROPERTIES.getProperty("query_connection_url2");
69  try {
70  Connection conn = DriverManager.getConnection(url, user, password);
71  } catch (SQLException re) {
72  assertEquals(re.getMessage(), "Invalid value supplied for max rows XXX");
73  }
74  }
75  @Test
77  String url = null;
78  try {
79  url = PROPERTIES.getProperty("query_connection_url3");
80  Connection conn = DriverManager.getConnection(url, user, password);
81  assertNotEquals(null, conn);
82  conn.close();
83  boolean closed = conn.isClosed();
84  assertEquals(true, closed);
85 
86  } catch (SQLException sq) {
87  String err = "Connection test failed for url " + url + ":" + sq.toString();
88  fail(err);
89  }
90  }
91 
92  @Test
94  String url = null;
95  try {
96  url = PROPERTIES.getProperty("query_connection_url4");
97  Connection conn = DriverManager.getConnection(url, user, password);
98  assertNotEquals(null, conn);
99  conn.close();
100  boolean closed = conn.isClosed();
101  assertEquals(true, closed);
102 
103  } catch (SQLException sq) {
104  String err = "Connection test failed for url " + url + ":" + sq.toString();
105  fail(err);
106  }
107  }
108 
109  @Test
111  String url = null;
112  try {
113  url = PROPERTIES.getProperty("query_connection_url5");
114  Connection conn = DriverManager.getConnection(url);
115  assertNotEquals(null, conn);
116  conn.close();
117  boolean closed = conn.isClosed();
118  assertEquals(true, closed);
119 
120  } catch (SQLException sq) {
121  String err = "Connection test failed for url " + url + ":" + sq.toString();
122  fail(err);
123  }
124  }
125  @Test
126  public void tst1_url_too_long() {
127  try {
128  String url = "jdbc:omnisci:l3:6666:l5:l6:l7:l8:l9:l10:l11:l12:50000:l14:l15";
129  Connection conn = DriverManager.getConnection(url, user, password);
130  } catch (SQLException sq) {
131  assertEquals(sq.getMessage(),
132  "Invalid number of arguments provided in url [15]. Maximum allowed [13]");
133  }
134  }
135  @Test
136  public void tst2_http_unencrypted() {
137  try {
138  String url = PROPERTIES.getProperty("http_connection_url") + ":"
139  + PROPERTIES.getProperty("default_db") + ":http";
140  Connection conn = DriverManager.getConnection(url, user, password);
141  assertNotEquals(null, conn);
142  conn.close();
143  boolean closed = conn.isClosed();
144  assertEquals(true, closed);
145  } catch (SQLException sq) {
146  String err = "Connection test failed " + sq.toString();
147  fail(err);
148  }
149  }
150  @Test
151  public void tst3_connect_fail() {
152  try {
153  String url = PROPERTIES.getProperty("failed_connection_url") + ":"
154  + PROPERTIES.getProperty("default_db");
155  Properties pt = new Properties();
156  pt.setProperty("user", user);
157  pt.setProperty("password", password);
158  Connection conn = DriverManager.getConnection(url, pt);
159  } catch (SQLException sq) {
160  // for different servers the exact string may be different
161  assertTrue(
162  sq.getMessage().contains("No suitable driver found for jdbc:NOT_heavyai"));
163  return;
164  }
165  String err = "Connection should have thrown";
166  fail(err);
167  }
168 
169  @Test
171  try {
172  String url = PROPERTIES.getProperty("default_db_connection_url") + ":"
173  + PROPERTIES.getProperty("default_db");
174  Properties pt = new Properties();
175  pt.setProperty("user", user);
176  pt.setProperty("password", password);
177  pt.setProperty("db_name", "SomeOtherDB");
178  // Shouldn't fail (url over ride properties.
179  Connection conn = DriverManager.getConnection(url, pt);
180  } catch (SQLException sq) {
181  fail(sq.getMessage());
182  }
183  }
184 
185  @Test
187  try {
188  String url = "jdbc:omnisci:" + base_properties.getProperty("host_name");
189  Connection conn = DriverManager.getConnection(url, base_properties);
190  assertNotEquals(null, conn);
191  conn.close();
192  boolean closed = conn.isClosed();
193  assertEquals(true, closed);
194  } catch (SQLException sq) {
195  String err = "Connection test failed " + sq.toString();
196  fail(err);
197  }
198  }
199 
200  @Test
202  try {
203  String url = PROPERTIES.getProperty("binary_connection_url") + ":"
204  + PROPERTIES.getProperty("default_db") + ":binary_tls";
205 
206  Properties pt = new Properties();
207  pt.setProperty("user", user);
208  pt.setProperty("password", password);
209  Connection conn = DriverManager.getConnection(url, pt);
210  assertNotEquals(null, conn);
211  conn.close();
212  boolean closed = conn.isClosed();
213  assertEquals(true, closed);
214  } catch (SQLException sq) {
215  String err = "Connection test failed " + sq.toString();
216  fail(err);
217  }
218  }
219 
220  @Test
222  try {
223  Properties pt = new Properties();
224  String url = PROPERTIES.getProperty("binary_connection_url") + ":"
225  + PROPERTIES.getProperty("default_db") + ":binary_tls";
226 
227  ClassLoader cl = getClass().getClassLoader();
228  String trust_store = PROPERTIES.getProperty("server_trust_store");
229  trust_store = cl.getResource(trust_store).getPath();
230  pt.setProperty("server_trust_store", trust_store);
231 
232  String sslcert_cl1 = PROPERTIES.getProperty("sslcert_cl1_pkcs12");
233  sslcert_cl1 = cl.getResource(sslcert_cl1).getPath();
234  pt.setProperty("sslcert", sslcert_cl1);
235 
236  pt.setProperty("sslkey_password", PROPERTIES.getProperty("sslkey_password_cl1"));
237 
238  pt.setProperty("user", "pki");
239  pt.setProperty("password", "");
240  pt.setProperty("server_trust_store_pwd",
241  PROPERTIES.getProperty("server_trust_store_password"));
242  pt.setProperty("pkiauth", PROPERTIES.getProperty("pkiauth"));
243 
244  Connection conn = DriverManager.getConnection(url, pt);
245  assertNotEquals(null, conn);
246 
247  Statement statement = conn.createStatement();
248  statement.executeUpdate("drop table if exists test_jdbc_tm_tble");
249 
250  conn.close();
251  boolean closed = conn.isClosed();
252  assertEquals(true, closed);
253  } catch (SQLException sq) {
254  String err = "Connection test failed " + sq.toString();
255  fail(err);
256  }
257  }
258 
259  @Test
261  try {
262  Properties pt = new Properties();
263  String url = PROPERTIES.getProperty("binary_connection_url") + ":"
264  + PROPERTIES.getProperty("default_db") + ":binary_tls";
265 
266  ClassLoader cl = getClass().getClassLoader();
267  String trust_store = PROPERTIES.getProperty("server_trust_store");
268  trust_store = cl.getResource(trust_store).getPath();
269  pt.setProperty("server_trust_store", trust_store);
270 
271  // Connect client 1 whose cert is signed by primary and should work
272  String sslcert_cl2 = PROPERTIES.getProperty("sslcert_cl2");
273  sslcert_cl2 = cl.getResource(sslcert_cl2).getPath();
274  pt.setProperty("sslcert", sslcert_cl2);
275 
276  String sslkey_cl2 = PROPERTIES.getProperty("sslkey_cl2");
277  sslkey_cl2 = cl.getResource(sslkey_cl2).getPath();
278  pt.setProperty("sslkey", sslkey_cl2);
279 
280  pt.setProperty("user", "pki");
281  pt.setProperty("password", "");
282  pt.setProperty("server_trust_store_pwd",
283  PROPERTIES.getProperty("server_trust_store_password"));
284  pt.setProperty("pkiauth", PROPERTIES.getProperty("pkiauth"));
285 
286  Connection conn = DriverManager.getConnection(url, pt);
287  assertNotEquals(null, conn);
288  conn.close();
289  boolean closed = conn.isClosed();
290  fail("Credential should not have been accepted");
291  } catch (SQLException sq) {
292  String err = "Connection test failed " + sq.toString();
293  assertTrue(err.contains("Invalid credentials"));
294  }
295  }
296 
297  @Test
299  try {
300  Properties pt = new Properties();
301  pt.setProperty("user", user);
302  pt.setProperty("password", password);
303  pt.setProperty("protocol", "https_insecure");
304  String url = PROPERTIES.getProperty("https_connection_url") + ":"
305  + PROPERTIES.getProperty("default_db");
306  Connection conn = DriverManager.getConnection(url, pt);
307  assertNotEquals(null, conn);
308  conn.close();
309  boolean closed = conn.isClosed();
310  assertEquals(true, closed);
311  } catch (SQLException sq) {
312  String err = "Connection test failed " + sq.toString();
313  fail(err);
314  }
315  }
316 
317  @Test
319  try {
320  ClassLoader cl = getClass().getClassLoader();
321  String trust_store = PROPERTIES.getProperty("server_trust_store");
322  trust_store = cl.getResource(trust_store).getPath();
323 
324  Properties pt = new Properties();
325  pt.setProperty("server_trust_store", trust_store);
326  pt.setProperty("server_trust_store_pwd",
327  PROPERTIES.getProperty("server_trust_store_password"));
328 
329  pt.setProperty("user", user);
330  pt.setProperty("password", password);
331  pt.setProperty("protocol", "https_insecure");
332 
333  String url = PROPERTIES.getProperty("https_connection_url") + ":"
334  + PROPERTIES.getProperty("default_db");
335  Connection conn = DriverManager.getConnection(url, pt);
336  assertNotEquals(null, conn);
337  conn.close();
338  boolean closed = conn.isClosed();
339  assertEquals(true, closed);
340  } catch (SQLException sq) {
341  String err = "Connection test failed " + sq.toString();
342  fail(err);
343  }
344  }
345 
346  @Test
348  try {
349  Properties pt = new Properties();
350  pt.setProperty("user", user);
351  pt.setProperty("password", password);
352  pt.setProperty("protocol", "https");
353  String url = PROPERTIES.getProperty("https_connection_url") + ":"
354  + PROPERTIES.getProperty("default_db");
355  Connection conn = DriverManager.getConnection(url, pt);
356  assertNotEquals(null, conn);
357  conn.close();
358  boolean closed = conn.isClosed();
359  assertEquals(true, closed);
360  } catch (SQLException sq) {
361  String err = "Connection test failed " + sq.toString();
362  fail(err);
363  }
364  }
365 
366  @Test
368  try {
369  ClassLoader cl = getClass().getClassLoader();
370  String trust_store = PROPERTIES.getProperty("ca_primary_trust_store");
371  trust_store = cl.getResource(trust_store).getPath();
372  Properties pt = new Properties();
373  pt.setProperty("server_trust_store", trust_store);
374  pt.setProperty("server_trust_store_pwd",
375  PROPERTIES.getProperty("server_trust_store_password"));
376 
377  pt.setProperty("user", user);
378  pt.setProperty("password", password);
379  pt.setProperty("protocol", "https");
380 
381  String url = PROPERTIES.getProperty("https_connection_url") + ":"
382  + PROPERTIES.getProperty("default_db");
383 
384  Connection conn = DriverManager.getConnection(url, pt);
385  conn.close();
386  assertNotEquals(null, conn);
387  boolean closed = conn.isClosed();
388  assertEquals(true, closed);
389 
390  assertNotEquals(null, conn);
391  } catch (SQLException sq) {
392  String err = "Connection test failed " + sq.toString();
393  fail(err);
394  }
395  }
396  @Test
398  try {
399  Properties pt = new Properties();
400  String url = PROPERTIES.getProperty("https_connection_url") + ":"
401  + PROPERTIES.getProperty("default_db") + ":https_insecure";
402 
403  ClassLoader cl = getClass().getClassLoader();
404  String trust_store = PROPERTIES.getProperty("server_trust_store");
405  trust_store = cl.getResource(trust_store).getPath();
406  pt.setProperty("server_trust_store", trust_store);
407 
408  String sslcert_cl1 = PROPERTIES.getProperty("sslcert_cl1_pkcs12");
409  sslcert_cl1 = cl.getResource(sslcert_cl1).getPath();
410  pt.setProperty("sslcert", sslcert_cl1);
411 
412  pt.setProperty("sslkey_password", PROPERTIES.getProperty("sslkey_password_cl1"));
413 
414  pt.setProperty("user", "pki");
415  pt.setProperty("password", "");
416  pt.setProperty("server_trust_store_pwd",
417  PROPERTIES.getProperty("server_trust_store_password"));
418  pt.setProperty("pkiauth", PROPERTIES.getProperty("pkiauth"));
419 
420  Connection conn = DriverManager.getConnection(url, pt);
421  Statement statement = conn.createStatement();
422  statement.executeUpdate("drop table if exists test_jdbc_tm_tble");
423  assertNotEquals(null, conn);
424  conn.close();
425  boolean closed = conn.isClosed();
426  assertEquals(true, closed);
427  } catch (SQLException sq) {
428  String err = "Connection test failed " + sq.toString();
429  fail(err);
430  }
431  }
432 }
tuple conn
Definition: report.py:41