File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\cli\src\test\java\hudson\cli\ConnectionTest.java.html

1 package hudson.cli;
2
3 import hudson.remoting.FastPipedInputStream;
4 import hudson.remoting.FastPipedOutputStream;
5 import org.junit.Assert;
6 import org.junit.Before;
7 import org.junit.Test;
8
9 import javax.crypto.SecretKey;
10 import javax.crypto.spec.SecretKeySpec;
11 import java.io.IOException;
12
13 /**
14 * @author Kohsuke Kawaguchi
15 */
16 public class ConnectionTest extends Assert {
17 Throwable e;
18 private Connection c1;
19 private Connection c2;
20
21 @Before
22 public void setUp() throws IOException {
23 FastPipedInputStream i = new FastPipedInputStream();
24 FastPipedInputStream j = new FastPipedInputStream();
25
26 c1 = new Connection(i,new FastPipedOutputStream(j));
27 c2 = new Connection(j,new FastPipedOutputStream(i));
28 }
29
30 @Test
31 public void testEncyrpt() throws Throwable {
32 final SecretKey sessionKey = new SecretKeySpec(new byte[16],"AES");
33
34 Thread t1 = new Thread() {
35 @Override
36 public void run() {
37 try {
38 c1.encryptConnection(sessionKey,"AES/CFB8/NoPadding").writeUTF("Hello");
39 } catch (Throwable x) {
40 e = x;
41 }
42 }
43 };
44 t1.start();
45
46 Thread t2 = new Thread() {
47 @Override
48 public void run() {
49 try {
50 String data = c2.encryptConnection(sessionKey,"AES/CFB8/NoPadding").readUTF();
51 assertEquals("Hello", data);
52 } catch (Throwable x) {
53 e = x;
54 }
55 }
56 };
57 t2.start();
58
59 t1.join(3000);
60 t2.join(3000);
61
62 if (t1.isAlive() || t2.isAlive()) {
63 t1.interrupt();
64 t2.interrupt();
65 throw new Error("thread is still alive");
66 }
67
68 if (e!=null) throw e;
69 }
70 }
71