View Javadoc
1   /*
2    * Copyright (C) 2015, Google Inc.
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  
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 }