You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WalkEncryptionTest.java 29KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * Copyright (C) 2015, Andrei Pozolotin.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.transport;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assume.assumeTrue;
  49. import java.io.BufferedReader;
  50. import java.io.ByteArrayInputStream;
  51. import java.io.ByteArrayOutputStream;
  52. import java.io.File;
  53. import java.io.FileInputStream;
  54. import java.io.IOException;
  55. import java.io.InputStream;
  56. import java.io.InputStreamReader;
  57. import java.io.OutputStream;
  58. import java.io.PrintWriter;
  59. import java.net.URL;
  60. import java.nio.charset.Charset;
  61. import java.nio.file.Files;
  62. import java.security.GeneralSecurityException;
  63. import java.security.Provider;
  64. import java.security.Security;
  65. import java.util.ArrayList;
  66. import java.util.Collection;
  67. import java.util.List;
  68. import java.util.Properties;
  69. import java.util.Set;
  70. import java.util.TreeSet;
  71. import java.util.UUID;
  72. import javax.crypto.SecretKeyFactory;
  73. import org.apache.log4j.Logger;
  74. import org.eclipse.jgit.api.Git;
  75. import org.eclipse.jgit.lib.StoredConfig;
  76. import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  77. import org.eclipse.jgit.util.FileUtils;
  78. import org.junit.After;
  79. import org.junit.AfterClass;
  80. import org.junit.Before;
  81. import org.junit.BeforeClass;
  82. import org.junit.FixMethodOrder;
  83. import org.junit.Test;
  84. import org.junit.runner.RunWith;
  85. import org.junit.runners.MethodSorters;
  86. import org.junit.runners.Parameterized;
  87. import org.junit.runners.Parameterized.Parameters;
  88. import org.junit.runners.Suite;
  89. import static org.eclipse.jgit.transport.WalkEncryptionTest.Util.*;
  90. /**
  91. * Amazon S3 encryption pipeline test.
  92. *
  93. * See {@link AmazonS3} {@link WalkEncryption}
  94. *
  95. * Note: CI server must provide amazon credentials (access key, secret key,
  96. * bucket name) via one of methods available in {@link Names}.
  97. *
  98. * Note: long running tests are activated by Maven profile "test.long". There is
  99. * also a separate Eclipse m2e launcher for that. See 'pom.xml' and
  100. * 'WalkEncryptionTest.launch'.
  101. */
  102. @RunWith(Suite.class)
  103. @Suite.SuiteClasses({ //
  104. WalkEncryptionTest.MinimalSet.class, //
  105. WalkEncryptionTest.TestablePBE.class, //
  106. })
  107. public class WalkEncryptionTest {
  108. /**
  109. * Logger setup: ${project_loc}/tst-rsrc/log4j.properties
  110. */
  111. static final Logger logger = Logger.getLogger(WalkEncryptionTest.class);
  112. /**
  113. * Property names used in test session.
  114. */
  115. interface Names {
  116. // Names of discovered test properties.
  117. String TEST_BUCKET = "test.bucket";
  118. // Names of test environment variables for CI.
  119. String ENV_ACCESS_KEY = "JGIT_S3_ACCESS_KEY";
  120. String ENV_SECRET_KEY = "JGIT_S3_SECRET_KEY";
  121. String ENV_BUCKET_NAME = "JGIT_S3_BUCKET_NAME";
  122. // Name of test environment variable file path for CI.
  123. String ENV_CONFIG_FILE = "JGIT_S3_CONFIG_FILE";
  124. // Names of test system properties for CI.
  125. String SYS_ACCESS_KEY = "jgit.s3.access.key";
  126. String SYS_SECRET_KEY = "jgit.s3.secret.key";
  127. String SYS_BUCKET_NAME = "jgit.s3.bucket.name";
  128. // Name of test system property file path for CI.
  129. String SYS_CONFIG_FILE = "jgit.s3.config.file";
  130. // Hard coded name of test properties file for CI.
  131. // File format follows AmazonS3.Keys:
  132. // #
  133. // # Required entries:
  134. // #
  135. // accesskey = your-amazon-access-key # default AmazonS3.Keys
  136. // secretkey = your-amazon-secret-key # default AmazonS3.Keys
  137. // test.bucket = your-bucket-for-testing # custom name, for this test
  138. String CONFIG_FILE = "jgit-s3-config.properties";
  139. // Test properties file in [user home] of CI.
  140. String HOME_CONFIG_FILE = System.getProperty("user.home")
  141. + File.separator + CONFIG_FILE;
  142. // Test properties file in [project work directory] of CI.
  143. String WORK_CONFIG_FILE = System.getProperty("user.dir")
  144. + File.separator + CONFIG_FILE;
  145. // Test properties file in [project test source directory] of CI.
  146. String TEST_CONFIG_FILE = System.getProperty("user.dir")
  147. + File.separator + "tst-rsrc" + File.separator + CONFIG_FILE;
  148. }
  149. /**
  150. * Find test properties from various sources in order of priority.
  151. */
  152. static class Props implements WalkEncryptionTest.Names, AmazonS3.Keys {
  153. static boolean haveEnvVar(String name) {
  154. return System.getenv(name) != null;
  155. }
  156. static boolean haveEnvVarFile(String name) {
  157. return haveEnvVar(name) && new File(name).exists();
  158. }
  159. static boolean haveSysProp(String name) {
  160. return System.getProperty(name) != null;
  161. }
  162. static boolean haveSysPropFile(String name) {
  163. return haveSysProp(name) && new File(name).exists();
  164. }
  165. static void loadEnvVar(String source, String target, Properties props) {
  166. props.put(target, System.getenv(source));
  167. }
  168. static void loadSysProp(String source, String target,
  169. Properties props) {
  170. props.put(target, System.getProperty(source));
  171. }
  172. static boolean haveProp(String name, Properties props) {
  173. return props.containsKey(name);
  174. }
  175. static boolean checkTestProps(Properties props) {
  176. return haveProp(ACCESS_KEY, props) && haveProp(SECRET_KEY, props)
  177. && haveProp(TEST_BUCKET, props);
  178. }
  179. static Properties fromEnvVars() {
  180. if (haveEnvVar(ENV_ACCESS_KEY) && haveEnvVar(ENV_SECRET_KEY)
  181. && haveEnvVar(ENV_BUCKET_NAME)) {
  182. Properties props = new Properties();
  183. loadEnvVar(ENV_ACCESS_KEY, ACCESS_KEY, props);
  184. loadEnvVar(ENV_SECRET_KEY, SECRET_KEY, props);
  185. loadEnvVar(ENV_BUCKET_NAME, TEST_BUCKET, props);
  186. return props;
  187. } else {
  188. return null;
  189. }
  190. }
  191. static Properties fromEnvFile() throws Exception {
  192. if (haveEnvVarFile(ENV_CONFIG_FILE)) {
  193. Properties props = new Properties();
  194. props.load(new FileInputStream(ENV_CONFIG_FILE));
  195. if (checkTestProps(props)) {
  196. return props;
  197. } else {
  198. throw new Error("Environment config file is incomplete.");
  199. }
  200. } else {
  201. return null;
  202. }
  203. }
  204. static Properties fromSysProps() {
  205. if (haveSysProp(SYS_ACCESS_KEY) && haveSysProp(SYS_SECRET_KEY)
  206. && haveSysProp(SYS_BUCKET_NAME)) {
  207. Properties props = new Properties();
  208. loadSysProp(SYS_ACCESS_KEY, ACCESS_KEY, props);
  209. loadSysProp(SYS_SECRET_KEY, SECRET_KEY, props);
  210. loadSysProp(SYS_BUCKET_NAME, TEST_BUCKET, props);
  211. return props;
  212. } else {
  213. return null;
  214. }
  215. }
  216. static Properties fromSysFile() throws Exception {
  217. if (haveSysPropFile(SYS_CONFIG_FILE)) {
  218. Properties props = new Properties();
  219. props.load(new FileInputStream(SYS_CONFIG_FILE));
  220. if (checkTestProps(props)) {
  221. return props;
  222. } else {
  223. throw new Error("System props config file is incomplete.");
  224. }
  225. } else {
  226. return null;
  227. }
  228. }
  229. static Properties fromConfigFile(String path) throws Exception {
  230. File file = new File(path);
  231. if (file.exists()) {
  232. Properties props = new Properties();
  233. props.load(new FileInputStream(file));
  234. if (checkTestProps(props)) {
  235. return props;
  236. } else {
  237. throw new Error("Props config file is incomplete: " + path);
  238. }
  239. } else {
  240. return null;
  241. }
  242. }
  243. /**
  244. * Find test properties from various sources in order of priority.
  245. *
  246. * @return result
  247. * @throws Exception
  248. */
  249. static Properties discover() throws Exception {
  250. Properties props;
  251. if ((props = fromEnvVars()) != null) {
  252. logger.debug(
  253. "Using test properties from environment variables.");
  254. return props;
  255. }
  256. if ((props = fromEnvFile()) != null) {
  257. logger.debug(
  258. "Using test properties from environment variable config file.");
  259. return props;
  260. }
  261. if ((props = fromSysProps()) != null) {
  262. logger.debug("Using test properties from system properties.");
  263. return props;
  264. }
  265. if ((props = fromSysFile()) != null) {
  266. logger.debug(
  267. "Using test properties from system property config file.");
  268. return props;
  269. }
  270. if ((props = fromConfigFile(HOME_CONFIG_FILE)) != null) {
  271. logger.debug(
  272. "Using test properties from hard coded ${user.home} file.");
  273. return props;
  274. }
  275. if ((props = fromConfigFile(WORK_CONFIG_FILE)) != null) {
  276. logger.debug(
  277. "Using test properties from hard coded ${user.dir} file.");
  278. return props;
  279. }
  280. if ((props = fromConfigFile(TEST_CONFIG_FILE)) != null) {
  281. logger.debug(
  282. "Using test properties from hard coded ${project.source} file.");
  283. return props;
  284. }
  285. throw new Error("Can not load test properties form any source.");
  286. }
  287. }
  288. /**
  289. * Collection of test utility methods.
  290. */
  291. static class Util {
  292. static final Charset UTF_8 = Charset.forName("UTF-8");
  293. /**
  294. * Read UTF-8 encoded text file into string.
  295. *
  296. * @param file
  297. * @return result
  298. * @throws Exception
  299. */
  300. static String textRead(File file) throws Exception {
  301. return new String(Files.readAllBytes(file.toPath()), UTF_8);
  302. }
  303. /**
  304. * Write string into UTF-8 encoded file.
  305. *
  306. * @param file
  307. * @param text
  308. * @throws Exception
  309. */
  310. static void textWrite(File file, String text) throws Exception {
  311. Files.write(file.toPath(), text.getBytes(UTF_8));
  312. }
  313. static void verifyFileContent(File fileOne, File fileTwo)
  314. throws Exception {
  315. assertTrue(fileOne.length() > 0);
  316. assertTrue(fileTwo.length() > 0);
  317. String textOne = textRead(fileOne);
  318. String textTwo = textRead(fileTwo);
  319. assertEquals(textOne, textTwo);
  320. }
  321. /**
  322. * Create local folder.
  323. *
  324. * @param folder
  325. * @throws Exception
  326. */
  327. static void folderCreate(String folder) throws Exception {
  328. File path = new File(folder);
  329. assertTrue(path.mkdirs());
  330. }
  331. /**
  332. * Delete local folder.
  333. *
  334. * @param folder
  335. * @throws Exception
  336. */
  337. static void folderDelete(String folder) throws Exception {
  338. File path = new File(folder);
  339. FileUtils.delete(path,
  340. FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
  341. }
  342. /**
  343. * Discover public address of CI server.
  344. *
  345. * @return result
  346. * @throws Exception
  347. */
  348. static String publicAddress() throws Exception {
  349. String service = "http://checkip.amazonaws.com";
  350. URL url = new URL(service);
  351. BufferedReader reader = new BufferedReader(
  352. new InputStreamReader(url.openStream()));
  353. try {
  354. return reader.readLine();
  355. } finally {
  356. reader.close();
  357. }
  358. }
  359. /**
  360. * Discover Password-Based Encryption (PBE) engines providing both
  361. * [SecretKeyFactory] and [AlgorithmParameters].
  362. *
  363. * @return result
  364. */
  365. // https://www.bouncycastle.org/specifications.html
  366. // https://docs.oracle.com/javase/8/docs/technotes/guides/security/SunProviders.html
  367. static List<String> cryptoCipherListPBE() {
  368. return cryptoCipherList("(PBE).*(WITH).+(AND).+");
  369. }
  370. static String securityProviderName(String algorithm) throws Exception {
  371. return SecretKeyFactory.getInstance(algorithm).getProvider()
  372. .getName();
  373. }
  374. static List<String> cryptoCipherList(String regex) {
  375. Set<String> source = Security.getAlgorithms("Cipher");
  376. Set<String> target = new TreeSet<String>();
  377. for (String algo : source) {
  378. algo = algo.toUpperCase();
  379. if (algo.matches(regex)) {
  380. target.add(algo);
  381. }
  382. }
  383. return new ArrayList<String>(target);
  384. }
  385. /**
  386. * Verify if any security provider published the algorithm.
  387. *
  388. * @param algorithm
  389. * @return result
  390. */
  391. static boolean isAlgorithmPresent(String algorithm) {
  392. Set<String> cipherSet = Security.getAlgorithms("Cipher");
  393. for (String source : cipherSet) {
  394. // Standard names are not case-sensitive.
  395. // http://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html
  396. String target = algorithm.toUpperCase();
  397. if (source.equalsIgnoreCase(target)) {
  398. return true;
  399. }
  400. }
  401. return false;
  402. }
  403. /**
  404. * Stream copy.
  405. *
  406. * @param from
  407. * @param into
  408. * @return count
  409. * @throws IOException
  410. */
  411. static long transferStream(InputStream from, OutputStream into)
  412. throws IOException {
  413. byte[] array = new byte[1 * 1024];
  414. long total = 0;
  415. while (true) {
  416. int count = from.read(array);
  417. if (count == -1) {
  418. break;
  419. }
  420. into.write(array, 0, count);
  421. total += count;
  422. }
  423. return total;
  424. }
  425. /**
  426. * Setup proxy during CI build.
  427. *
  428. * @throws Exception
  429. */
  430. // https://wiki.eclipse.org/Hudson#Accessing_the_Internet_using_Proxy
  431. // http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html
  432. static void proxySetup() throws Exception {
  433. String keyNoProxy = "no_proxy";
  434. String keyHttpProxy = "http_proxy";
  435. String keyHttpsProxy = "https_proxy";
  436. String no_proxy = System.getProperty(keyNoProxy,
  437. System.getenv(keyNoProxy));
  438. if (no_proxy != null) {
  439. System.setProperty("http.nonProxyHosts", no_proxy);
  440. logger.info("Proxy NOT: " + no_proxy);
  441. }
  442. String http_proxy = System.getProperty(keyHttpProxy,
  443. System.getenv(keyHttpProxy));
  444. if (http_proxy != null) {
  445. URL url = new URL(http_proxy);
  446. System.setProperty("http.proxyHost", url.getHost());
  447. System.setProperty("http.proxyPort", "" + url.getPort());
  448. logger.info("Proxy HTTP: " + http_proxy);
  449. }
  450. String https_proxy = System.getProperty(keyHttpsProxy,
  451. System.getenv(keyHttpsProxy));
  452. if (https_proxy != null) {
  453. URL url = new URL(https_proxy);
  454. System.setProperty("https.proxyHost", url.getHost());
  455. System.setProperty("https.proxyPort", "" + url.getPort());
  456. logger.info("Proxy HTTPS: " + https_proxy);
  457. }
  458. if (no_proxy == null && http_proxy == null && https_proxy == null) {
  459. logger.info("Proxy not used.");
  460. }
  461. }
  462. /**
  463. * Permit long tests on CI or with manual activation.
  464. *
  465. * @return result
  466. */
  467. static boolean permitLongTests() {
  468. return isBuildCI() || isProfileActive();
  469. }
  470. /**
  471. * Using Maven profile activation, see pom.xml
  472. *
  473. * @return result
  474. */
  475. static boolean isProfileActive() {
  476. return Boolean.parseBoolean(System.getProperty("jgit.test.long"));
  477. }
  478. /**
  479. * Detect if build is running on CI.
  480. *
  481. * @return result
  482. */
  483. static boolean isBuildCI() {
  484. return System.getenv("HUDSON_HOME") != null;
  485. }
  486. }
  487. /**
  488. * Common base for encryption tests.
  489. */
  490. @FixMethodOrder(MethodSorters.NAME_ASCENDING)
  491. public abstract static class Base extends SampleDataRepositoryTestCase {
  492. /**
  493. * S3 URI user used by JGIT to discover connection configuration file.
  494. */
  495. static final String JGIT_USER = "tester-" + System.currentTimeMillis();
  496. /**
  497. * S3 content encoding password used for this test session.
  498. */
  499. static final String JGIT_PASS = "secret-" + System.currentTimeMillis();
  500. /**
  501. * S3 repository configuration file expected by {@link AmazonS3}.
  502. */
  503. static final String JGIT_CONF_FILE = System.getProperty("user.home")
  504. + "/" + JGIT_USER;
  505. /**
  506. * Name representing remote or local JGIT repository.
  507. */
  508. static final String JGIT_REPO_DIR = JGIT_USER + ".jgit";
  509. /**
  510. * Local JGIT repository for this test session.
  511. */
  512. static final String JGIT_LOCAL_DIR = System.getProperty("user.dir")
  513. + "/target/" + JGIT_REPO_DIR;
  514. /**
  515. * Remote JGIT repository for this test session.
  516. */
  517. static final String JGIT_REMOTE_DIR = JGIT_REPO_DIR;
  518. /**
  519. * Generate JGIT S3 connection configuration file.
  520. *
  521. * @param algorithm
  522. * @throws Exception
  523. */
  524. static void configCreate(String algorithm) throws Exception {
  525. Properties props = Props.discover();
  526. props.put(AmazonS3.Keys.PASSWORD, JGIT_PASS);
  527. props.put(AmazonS3.Keys.CRYPTO_ALG, algorithm);
  528. PrintWriter writer = new PrintWriter(JGIT_CONF_FILE);
  529. props.store(writer, "JGIT S3 connection configuration file.");
  530. writer.close();
  531. }
  532. /**
  533. * Remove JGIT connection configuration file.
  534. *
  535. * @throws Exception
  536. */
  537. static void configDelete() throws Exception {
  538. File path = new File(JGIT_CONF_FILE);
  539. FileUtils.delete(path, FileUtils.SKIP_MISSING);
  540. }
  541. /**
  542. * Generate remote URI for the test session.
  543. *
  544. * @return result
  545. * @throws Exception
  546. */
  547. static String amazonURI() throws Exception {
  548. Properties props = Props.discover();
  549. String bucket = props.getProperty(Names.TEST_BUCKET);
  550. assertNotNull(bucket);
  551. return TransportAmazonS3.S3_SCHEME + "://" + JGIT_USER + "@"
  552. + bucket + "/" + JGIT_REPO_DIR;
  553. }
  554. /**
  555. * Create S3 repository folder.
  556. *
  557. * @throws Exception
  558. */
  559. static void remoteCreate() throws Exception {
  560. Properties props = Props.discover();
  561. props.remove(AmazonS3.Keys.PASSWORD); // Disable encryption.
  562. String bucket = props.getProperty(Names.TEST_BUCKET);
  563. AmazonS3 s3 = new AmazonS3(props);
  564. String path = JGIT_REMOTE_DIR + "/";
  565. s3.put(bucket, path, new byte[0]);
  566. logger.debug("remote create: " + JGIT_REMOTE_DIR);
  567. }
  568. /**
  569. * Delete S3 repository folder.
  570. *
  571. * @throws Exception
  572. */
  573. static void remoteDelete() throws Exception {
  574. Properties props = Props.discover();
  575. props.remove(AmazonS3.Keys.PASSWORD); // Disable encryption.
  576. String bucket = props.getProperty(Names.TEST_BUCKET);
  577. AmazonS3 s3 = new AmazonS3(props);
  578. List<String> list = s3.list(bucket, JGIT_REMOTE_DIR);
  579. for (String path : list) {
  580. path = JGIT_REMOTE_DIR + "/" + path;
  581. s3.delete(bucket, path);
  582. }
  583. logger.debug("remote delete: " + JGIT_REMOTE_DIR);
  584. }
  585. /**
  586. * Verify if we can create/delete remote file.
  587. *
  588. * @throws Exception
  589. */
  590. static void remoteVerify() throws Exception {
  591. Properties props = Props.discover();
  592. String bucket = props.getProperty(Names.TEST_BUCKET);
  593. AmazonS3 s3 = new AmazonS3(props);
  594. String file = JGIT_USER + "-" + UUID.randomUUID().toString();
  595. String path = JGIT_REMOTE_DIR + "/" + file;
  596. s3.put(bucket, path, file.getBytes(UTF_8));
  597. s3.delete(bucket, path);
  598. }
  599. /**
  600. * Verify if JRE security policy allows the algorithm.
  601. *
  602. * @param algorithm
  603. * @return result
  604. */
  605. static boolean isAlgorithmAllowed(String algorithm) {
  606. try {
  607. WalkEncryption crypto = new WalkEncryption.ObjectEncryptionJetS3tV2(
  608. algorithm, JGIT_PASS);
  609. verifyCrypto(crypto);
  610. return true;
  611. } catch (IOException e) {
  612. return false; // Encryption failure.
  613. } catch (GeneralSecurityException e) {
  614. throw new Error(e); // Construction failure.
  615. }
  616. }
  617. /**
  618. * Verify round trip encryption.
  619. *
  620. * @param crypto
  621. * @throws IOException
  622. */
  623. static void verifyCrypto(WalkEncryption crypto) throws IOException {
  624. String charset = "UTF-8";
  625. String sourceText = "secret-message Свобода 老子";
  626. String targetText;
  627. byte[] cipherText;
  628. {
  629. byte[] origin = sourceText.getBytes(charset);
  630. ByteArrayOutputStream target = new ByteArrayOutputStream();
  631. OutputStream source = crypto.encrypt(target);
  632. source.write(origin);
  633. source.flush();
  634. source.close();
  635. cipherText = target.toByteArray();
  636. }
  637. {
  638. InputStream source = new ByteArrayInputStream(cipherText);
  639. InputStream target = crypto.decrypt(source);
  640. ByteArrayOutputStream result = new ByteArrayOutputStream();
  641. transferStream(target, result);
  642. targetText = result.toString(charset);
  643. }
  644. assertEquals(sourceText, targetText);
  645. }
  646. /**
  647. * Algorithm is testable when it is present and allowed by policy.
  648. *
  649. * @param algorithm
  650. * @return result
  651. */
  652. static boolean isAlgorithmTestable(String algorithm) {
  653. return isAlgorithmPresent(algorithm)
  654. && isAlgorithmAllowed(algorithm);
  655. }
  656. /**
  657. * Log algorithm, provider, testability.
  658. *
  659. * @param algorithm
  660. * @throws Exception
  661. */
  662. static void reportAlgorithmStatus(String algorithm) throws Exception {
  663. final boolean present = isAlgorithmPresent(algorithm);
  664. final boolean allowed = present && isAlgorithmAllowed(algorithm);
  665. final String provider = present ? securityProviderName(algorithm)
  666. : "N/A";
  667. String status = "Algorithm: " + algorithm + " @ " + provider + "; "
  668. + "present/allowed : " + present + "/" + allowed;
  669. if (allowed) {
  670. logger.info("Testing " + status);
  671. } else {
  672. logger.warn("Missing " + status);
  673. }
  674. }
  675. /**
  676. * Verify if we can perform remote tests.
  677. *
  678. * @return result
  679. */
  680. static boolean isTestConfigPresent() {
  681. try {
  682. Props.discover();
  683. return true;
  684. } catch (Throwable e) {
  685. return false;
  686. }
  687. }
  688. static void reportTestConfigPresent() {
  689. if (isTestConfigPresent()) {
  690. logger.info("Amazon S3 test configuration is present.");
  691. } else {
  692. logger.error(
  693. "Amazon S3 test configuration is missing, tests will not run.");
  694. }
  695. }
  696. /**
  697. * Log public address of CI.
  698. *
  699. * @throws Exception
  700. */
  701. static void reportPublicAddress() throws Exception {
  702. logger.info("Public address: " + publicAddress());
  703. }
  704. /**
  705. * BouncyCastle provider class.
  706. *
  707. * Needs extra dependency, see pom.xml
  708. */
  709. // http://search.maven.org/#artifactdetails%7Corg.bouncycastle%7Cbcprov-jdk15on%7C1.52%7Cjar
  710. static final String PROVIDER_BC = "org.bouncycastle.jce.provider.BouncyCastleProvider";
  711. /**
  712. * Load BouncyCastle provider if present.
  713. */
  714. static void loadBouncyCastle() {
  715. try {
  716. Class<?> provider = Class.forName(PROVIDER_BC);
  717. Provider instance = (Provider) provider
  718. .getConstructor(new Class[] {})
  719. .newInstance(new Object[] {});
  720. Security.addProvider(instance);
  721. logger.info("Loaded " + PROVIDER_BC);
  722. } catch (Throwable e) {
  723. logger.warn("Failed to load " + PROVIDER_BC);
  724. }
  725. }
  726. static void reportLongTests() {
  727. if (permitLongTests()) {
  728. logger.info("Long running tests are enabled.");
  729. } else {
  730. logger.warn("Long running tests are disabled.");
  731. }
  732. }
  733. /**
  734. * Non-PBE algorithm, for error check.
  735. */
  736. static final String ALGO_ERROR = "PBKDF2WithHmacSHA1";
  737. /**
  738. * Default JetS3t algorithm present in most JRE.
  739. */
  740. static final String ALGO_JETS3T = "PBEWithMD5AndDES";
  741. /**
  742. * Minimal strength AES based algorithm present in most JRE.
  743. */
  744. static final String ALGO_MINIMAL_AES = "PBEWithHmacSHA1AndAES_128";
  745. /**
  746. * Selected non-AES algorithm present in BouncyCastle provider.
  747. */
  748. static final String ALGO_BOUNCY_CASTLE_CBC = "PBEWithSHAAndTwofish-CBC";
  749. //////////////////////////////////////////////////
  750. @BeforeClass
  751. public static void initialize() throws Exception {
  752. Transport.register(TransportAmazonS3.PROTO_S3);
  753. proxySetup();
  754. reportLongTests();
  755. reportPublicAddress();
  756. reportTestConfigPresent();
  757. loadBouncyCastle();
  758. if (isTestConfigPresent()) {
  759. remoteCreate();
  760. }
  761. }
  762. @AfterClass
  763. public static void terminate() throws Exception {
  764. configDelete();
  765. folderDelete(JGIT_LOCAL_DIR);
  766. if (isTestConfigPresent()) {
  767. remoteDelete();
  768. }
  769. }
  770. @Before
  771. @Override
  772. public void setUp() throws Exception {
  773. super.setUp();
  774. }
  775. @After
  776. @Override
  777. public void tearDown() throws Exception {
  778. super.tearDown();
  779. }
  780. /**
  781. * Optional encrypted amazon remote JGIT life cycle test.
  782. *
  783. * @param algorithm
  784. * @throws Exception
  785. */
  786. void cryptoTestIfCan(String algorithm) throws Exception {
  787. reportAlgorithmStatus(algorithm);
  788. assumeTrue(isTestConfigPresent());
  789. assumeTrue(isAlgorithmTestable(algorithm));
  790. cryptoTest(algorithm);
  791. }
  792. /**
  793. * Required encrypted amazon remote JGIT life cycle test.
  794. *
  795. * @param algorithm
  796. * @throws Exception
  797. */
  798. void cryptoTest(String algorithm) throws Exception {
  799. remoteDelete();
  800. configCreate(algorithm);
  801. folderDelete(JGIT_LOCAL_DIR);
  802. String uri = amazonURI();
  803. // Local repositories.
  804. File dirOne = db.getWorkTree(); // Provided by setup.
  805. File dirTwo = new File(JGIT_LOCAL_DIR);
  806. // Local verification files.
  807. String nameStatic = "master.txt"; // Provided by setup.
  808. String nameDynamic = JGIT_USER + "-" + UUID.randomUUID().toString();
  809. String remote = "remote";
  810. RefSpec specs = new RefSpec("refs/heads/master:refs/heads/master");
  811. { // Push into remote from local one.
  812. StoredConfig config = db.getConfig();
  813. RemoteConfig remoteConfig = new RemoteConfig(config, remote);
  814. remoteConfig.addURI(new URIish(uri));
  815. remoteConfig.update(config);
  816. config.save();
  817. Git git = Git.open(dirOne);
  818. git.checkout().setName("master").call();
  819. git.push().setRemote(remote).setRefSpecs(specs).call();
  820. git.close();
  821. File fileStatic = new File(dirOne, nameStatic);
  822. assertTrue("Provided by setup", fileStatic.exists());
  823. }
  824. { // Clone from remote into local two.
  825. File fileStatic = new File(dirTwo, nameStatic);
  826. assertFalse("Not Provided by setup", fileStatic.exists());
  827. Git git = Git.cloneRepository().setURI(uri).setDirectory(dirTwo)
  828. .call();
  829. git.close();
  830. assertTrue("Provided by clone", fileStatic.exists());
  831. }
  832. { // Verify static file content.
  833. File fileOne = new File(dirOne, nameStatic);
  834. File fileTwo = new File(dirTwo, nameStatic);
  835. verifyFileContent(fileOne, fileTwo);
  836. }
  837. { // Verify new file commit and push from local one.
  838. File fileDynamic = new File(dirOne, nameDynamic);
  839. assertFalse("Not Provided by setup", fileDynamic.exists());
  840. FileUtils.createNewFile(fileDynamic);
  841. textWrite(fileDynamic, nameDynamic);
  842. assertTrue("Provided by create", fileDynamic.exists());
  843. assertTrue("Need content to encrypt", fileDynamic.length() > 0);
  844. Git git = Git.open(dirOne);
  845. git.add().addFilepattern(nameDynamic).call();
  846. git.commit().setMessage(nameDynamic).call();
  847. git.push().setRemote(remote).setRefSpecs(specs).call();
  848. git.close();
  849. }
  850. { // Verify new file pull from remote into local two.
  851. File fileDynamic = new File(dirTwo, nameDynamic);
  852. assertFalse("Not Provided by setup", fileDynamic.exists());
  853. Git git = Git.open(dirTwo);
  854. git.pull().call();
  855. git.close();
  856. assertTrue("Provided by pull", fileDynamic.exists());
  857. }
  858. { // Verify dynamic file content.
  859. File fileOne = new File(dirOne, nameDynamic);
  860. File fileTwo = new File(dirTwo, nameDynamic);
  861. verifyFileContent(fileOne, fileTwo);
  862. }
  863. }
  864. }
  865. /**
  866. * Test minimal set of algorithms.
  867. */
  868. @FixMethodOrder(MethodSorters.NAME_ASCENDING)
  869. public static class MinimalSet extends Base {
  870. @Test
  871. public void test_A1_ValidURI() throws Exception {
  872. assumeTrue(isTestConfigPresent());
  873. URIish uri = new URIish(amazonURI());
  874. assertTrue("uri=" + uri, TransportAmazonS3.PROTO_S3.canHandle(uri));
  875. }
  876. @Test(expected = Exception.class)
  877. public void test_A2_CryptoError() throws Exception {
  878. assumeTrue(isTestConfigPresent());
  879. cryptoTest(ALGO_ERROR);
  880. }
  881. @Test
  882. public void test_A3_CryptoJetS3tDefault() throws Exception {
  883. cryptoTestIfCan(ALGO_JETS3T);
  884. }
  885. @Test
  886. public void test_A4_CryptoMinimalAES() throws Exception {
  887. cryptoTestIfCan(ALGO_MINIMAL_AES);
  888. }
  889. @Test
  890. public void test_A5_CryptoBouncyCastleCBC() throws Exception {
  891. cryptoTestIfCan(ALGO_BOUNCY_CASTLE_CBC);
  892. }
  893. }
  894. /**
  895. * Test all present and allowed PBE algorithms.
  896. */
  897. // https://github.com/junit-team/junit/wiki/Parameterized-tests
  898. @RunWith(Parameterized.class)
  899. @FixMethodOrder(MethodSorters.NAME_ASCENDING)
  900. public static class TestablePBE extends Base {
  901. @Parameters(name = "Algorithm: {0}")
  902. public static Collection algorimthmList() {
  903. List<String> source = cryptoCipherListPBE();
  904. List<Object[]> target = new ArrayList<Object[]>();
  905. for (String name : source) {
  906. target.add(new Object[] { name });
  907. }
  908. return target;
  909. }
  910. final String algorithm;
  911. public TestablePBE(String algorithm) {
  912. this.algorithm = algorithm;
  913. }
  914. @Test // Can take long time, needs activation.
  915. public void test_B1_Crypto() throws Exception {
  916. assumeTrue(permitLongTests());
  917. cryptoTestIfCan(algorithm);
  918. }
  919. }
  920. }