1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.transport;
45
46 import static org.eclipse.jgit.transport.RemoteRefUpdate.Status.REJECTED_OTHER_REASON;
47 import static org.junit.Assert.assertEquals;
48
49 import java.io.IOException;
50 import java.util.HashMap;
51 import java.util.Map;
52
53 import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
54 import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
55 import org.eclipse.jgit.lib.Constants;
56 import org.eclipse.jgit.lib.NullProgressMonitor;
57 import org.eclipse.jgit.lib.ObjectId;
58 import org.eclipse.jgit.lib.ObjectInserter;
59 import org.eclipse.jgit.lib.RefUpdate;
60 import org.eclipse.jgit.lib.Repository;
61 import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
62 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
63 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
64 import org.junit.After;
65 import org.junit.Before;
66 import org.junit.Test;
67
68 public class PushConnectionTest {
69 private URIish uri;
70 private TestProtocol<Object> testProtocol;
71 private Object ctx = new Object();
72 private InMemoryRepository server;
73 private InMemoryRepository client;
74 private ObjectId obj1;
75 private ObjectId obj2;
76 private ObjectId obj3;
77 private String refName = "refs/tags/blob";
78
79 @Before
80 public void setUp() throws Exception {
81 server = newRepo("server");
82 client = newRepo("client");
83 testProtocol = new TestProtocol<>(
84 null,
85 new ReceivePackFactory<Object>() {
86 @Override
87 public ReceivePack create(Object req, Repository db)
88 throws ServiceNotEnabledException,
89 ServiceNotAuthorizedException {
90 return new ReceivePack(db);
91 }
92 });
93 uri = testProtocol.register(ctx, server);
94
95 try (ObjectInserter ins = server.newObjectInserter()) {
96 obj1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("test"));
97 obj3 = ins.insert(Constants.OBJ_BLOB, Constants.encode("not"));
98 ins.flush();
99
100 RefUpdate u = server.updateRef(refName);
101 u.setNewObjectId(obj1);
102 assertEquals(RefUpdate.Result.NEW, u.update());
103 }
104
105 try (ObjectInserter ins = client.newObjectInserter()) {
106 obj2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("file"));
107 ins.flush();
108 }
109 }
110
111 @After
112 public void tearDown() {
113 Transport.unregister(testProtocol);
114 }
115
116 private static InMemoryRepository newRepo(String name) {
117 return new InMemoryRepository(new DfsRepositoryDescription(name));
118 }
119
120 @Test
121 public void testWrongOldIdDoesNotReplace() throws IOException {
122 RemoteRefUpdate rru = new RemoteRefUpdate(null, null, obj2, refName,
123 false, null, obj3);
124
125 Map<String, RemoteRefUpdate> updates = new HashMap<>();
126 updates.put(rru.getRemoteName(), rru);
127
128 Transport tn = testProtocol.open(uri, client, "server");
129 try {
130 PushConnection connection = tn.openPush();
131 try {
132 connection.push(NullProgressMonitor.INSTANCE, updates);
133 } finally {
134 connection.close();
135 }
136 } finally {
137 tn.close();
138 }
139
140 assertEquals(REJECTED_OTHER_REASON, rru.getStatus());
141 assertEquals("invalid old id sent", rru.getMessage());
142 }
143 }