changeset 62:9777f9420be3

merged download pdb's controller fixes and tests
author Evgeniy.Koshkin
date Wed, 20 Nov 2013 20:29:58 +0400
parents 3c889ef68a6f (current diff) fc43a044bba2 (diff)
children 144160a5b010
files .idea/libraries/Common_Impl.xml .idea/libraries/Test_Api_libs.xml tests/tests.iml
diffstat 10 files changed, 208 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/.idea/libraries/Common_Impl.xml	Fri Nov 15 15:23:11 2013 +0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-<component name="libraryTable">
-  <library name="Common-Impl">
-    <CLASSES>
-      <root url="jar://$TeamCityDistribution$/buildAgent/lib/common-impl.jar!/" />
-    </CLASSES>
-    <JAVADOC />
-    <SOURCES />
-  </library>
-</component>
\ No newline at end of file
--- a/.idea/libraries/Test_Api.xml	Fri Nov 15 15:23:11 2013 +0400
+++ b/.idea/libraries/Test_Api.xml	Wed Nov 20 20:29:58 2013 +0400
@@ -2,6 +2,7 @@
   <library name="Test-Api">
     <CLASSES>
       <root url="jar://$TeamCityDistribution$/devPackage/tests/tests-support.jar!/" />
+      <root url="jar://$TeamCityDistribution$/devPackage/tests/tests-integration.jar!/" />
     </CLASSES>
     <JAVADOC />
     <SOURCES />
--- a/.idea/libraries/Test_Api_libs.xml	Fri Nov 15 15:23:11 2013 +0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-<component name="libraryTable">
-  <library name="Test-Api libs">
-    <CLASSES>
-      <root url="jar://$PROJECT_DIR$/lib/jmock/jmock-core-1.2.0.jar!/" />
-      <root url="jar://$TeamCityDistribution$/webapps/ROOT/WEB-INF/lib/xmlrpc-2.0.1.jar!/" />
-    </CLASSES>
-    <JAVADOC />
-    <SOURCES />
-  </library>
-</component>
\ No newline at end of file
--- a/.idea/modules.xml	Fri Nov 15 15:23:11 2013 +0400
+++ b/.idea/modules.xml	Wed Nov 20 20:29:58 2013 +0400
@@ -6,7 +6,7 @@
       <module fileurl="file://$PROJECT_DIR$/common/common.iml" filepath="$PROJECT_DIR$/common/common.iml" />
       <module fileurl="file://$PROJECT_DIR$/idea-resolve-helper.iml" filepath="$PROJECT_DIR$/idea-resolve-helper.iml" />
       <module fileurl="file://$PROJECT_DIR$/server/server.iml" filepath="$PROJECT_DIR$/server/server.iml" />
-      <module fileurl="file://$PROJECT_DIR$/tests/tests.iml" filepath="$PROJECT_DIR$/tests/tests.iml" />
+      <module fileurl="file://$PROJECT_DIR$/server-tests/server-tests.iml" filepath="$PROJECT_DIR$/server-tests/server-tests.iml" />
     </modules>
   </component>
 </project>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server-tests/server-tests.iml	Wed Nov 20 20:29:58 2013 +0400
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="true" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+    <orderEntry type="module" module-name="server" />
+    <orderEntry type="library" name="Test-Api" level="project" />
+    <orderEntry type="library" name="testng" level="project" />
+    <orderEntry type="library" name="jmock" level="project" />
+    <orderEntry type="library" name="Server-Api" level="project" />
+    <orderEntry type="library" name="TeamCity server runtime" level="project" />
+  </component>
+</module>
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server-tests/src/DownloadSymbolsControllerTest.java	Wed Nov 20 20:29:58 2013 +0400
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2000-2013 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import jetbrains.buildServer.controllers.AuthorizationInterceptor;
+import jetbrains.buildServer.controllers.BaseController;
+import jetbrains.buildServer.controllers.BaseControllerTestCase;
+import jetbrains.buildServer.controllers.interceptors.auth.HttpAuthenticationManager;
+import jetbrains.buildServer.serverSide.RunningBuildEx;
+import jetbrains.buildServer.serverSide.auth.Permission;
+import jetbrains.buildServer.serverSide.auth.RoleScope;
+import jetbrains.buildServer.symbols.AuthHelper;
+import jetbrains.buildServer.symbols.DownloadSymbolsController;
+import jetbrains.buildServer.users.SUser;
+import org.apache.commons.httpclient.HttpStatus;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * @author Evgeniy.Koshkin
+ */
+public class DownloadSymbolsControllerTest extends BaseControllerTestCase {
+
+  private MetadataStorageMock myBuildMetadataStorage;
+
+  @Override
+  @BeforeMethod
+  public void setUp() throws Exception {
+    myBuildMetadataStorage = new MetadataStorageMock();
+    super.setUp();
+    myFixture.getServerSettings().setGuestLoginAllowed(true);
+  }
+
+  @Override
+  protected BaseController createController() throws IOException {
+    AuthorizationInterceptor authInterceptor = myFixture.getSingletonService(AuthorizationInterceptor.class);
+    AuthHelper authHelper = new AuthHelper(myFixture.getServerSettings(), myFixture.getUserModel(), myFixture.getSingletonService(HttpAuthenticationManager.class));
+    return new DownloadSymbolsController(myServer, myWebManager, authInterceptor,  myFixture.getSecurityContext(), myBuildMetadataStorage, authHelper);
+  }
+
+  @Test
+  public void request_pdb_simple() throws Throwable {
+    myFixture.getServerSettings().setPerProjectPermissionsEnabled(true);
+    SUser user = myFixture.getUserModel().getGuestUser();
+    user.addRole(RoleScope.projectScope(myProject.getProjectId()), getProjectDevRole());
+    assertTrue(user.isPermissionGrantedForProject(myProject.getProjectId(), Permission.VIEW_BUILD_RUNTIME_DATA));
+
+    myRequest.setRequestURI("mock", getRegisterPdbUrl("secur32.pdb", "8EF4E863187C45E78F4632152CC82FEB"));
+    doGet();
+    assertEquals(HttpStatus.SC_OK, myResponse.getStatus());
+  }
+
+  @Test
+  public void request_pdb_invalid_url() throws Exception {
+    myRequest.setRequestURI("mock", "/app/symbols/foo");
+    doGet();
+    assertEquals(HttpStatus.SC_NOT_FOUND, myResponse.getStatus());
+  }
+
+  @Test
+  public void request_not_existent_pdb() throws Exception {
+    myRequest.setRequestURI("mock", "/app/symbols/fileName/FileId2/fileName");
+    doGet();
+    assertEquals(HttpStatus.SC_NOT_FOUND, myResponse.getStatus());
+  }
+
+  @Test
+  public void request_pdb_unauthorized() throws Exception {
+    myFixture.getServerSettings().setGuestLoginAllowed(false);
+    myRequest.setRequestURI("mock", getRegisterPdbUrl("secur32.pdb", "8EF4E863187C45E78F4632152CC82FEB"));
+    doGet();
+    assertEquals(HttpStatus.SC_UNAUTHORIZED, myResponse.getStatus());
+  }
+
+  @Test
+  public void request_pdb_no_permissions_granted() throws Exception {
+    myRequest.setRequestURI("mock", getRegisterPdbUrl("secur32.pdb", "8EF4E863187C45E78F4632152CC82FEB"));
+    doGet();
+    assertEquals(HttpStatus.SC_UNAUTHORIZED, myResponse.getStatus());
+  }
+
+  private String getRegisterPdbUrl(String fileName, String fileSignature) throws IOException {
+    final File artDirectory = createTempDir();
+    new File(artDirectory, "foo").createNewFile();
+    myBuildType.setArtifactPaths(artDirectory.getAbsolutePath());
+    RunningBuildEx build = startBuild();
+    myBuildMetadataStorage.addEntry(build.getBuildId(), fileName, fileSignature);
+    return String.format("/app/symbols/%s/%s/%s", fileName, fileSignature, fileName);
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server-tests/src/MetadataStorageMock.java	Wed Nov 20 20:29:58 2013 +0400
@@ -0,0 +1,47 @@
+import jetbrains.buildServer.serverSide.metadata.BuildMetadataEntry;
+import jetbrains.buildServer.serverSide.metadata.MetadataStorage;
+import jetbrains.buildServer.symbols.BuildSymbolsIndexProvider;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.*;
+
+class MetadataStorageMock implements MetadataStorage {
+
+  private List<BuildMetadataEntry> myEntries = new ArrayList<BuildMetadataEntry>();
+
+  public void addEntry(final long buildId, final String fileName, final String fileSignature) {
+    myEntries.add(new BuildMetadataEntry() {
+      public long getBuildId() {
+        return buildId;
+      }
+
+      @NotNull
+      public String getKey() {
+        return fileSignature;
+      }
+
+      @NotNull
+      public Map<String, String> getMetadata() {
+        HashMap<String, String> map = new HashMap<String, String>();
+        map.put(BuildSymbolsIndexProvider.ARTIFACT_PATH_KEY, "foo");
+        map.put(BuildSymbolsIndexProvider.FILE_NAME_KEY, fileName);
+        return map;
+      }
+    });
+  }
+
+  @NotNull
+  public Iterator<BuildMetadataEntry> getAllEntries(@NotNull String s) {
+    return myEntries.iterator();
+  }
+
+  @NotNull
+  public Iterator<BuildMetadataEntry> getEntriesByKey(@NotNull String s, @NotNull String s2) {
+    return myEntries.iterator();
+  }
+
+  @NotNull
+  public Iterator<BuildMetadataEntry> getBuildEntry(long l, @NotNull String s) {
+    return myEntries.iterator();
+  }
+}
\ No newline at end of file
--- a/server/src/jetbrains/buildServer/symbols/DownloadSymbolsController.java	Fri Nov 15 15:23:11 2013 +0400
+++ b/server/src/jetbrains/buildServer/symbols/DownloadSymbolsController.java	Wed Nov 20 20:29:58 2013 +0400
@@ -89,14 +89,15 @@
     final String guid = signature.substring(0, signature.length() - 1); //last symbol is PEDebugType
     LOG.debug(String.format("Symbol file requested. File name: %s. Guid: %s.", fileName, guid));
 
+    final String projectId = findRelatedProjectId(guid);
+    if(projectId == null) {
+      WebUtil.notFound(request, response, "File not found", null);
+      return null;
+    }
+
     final SUser user = myAuthHelper.getAuthenticatedUser(request, response, new Predicate<SUser>() {
       public boolean apply(SUser user) {
         try{
-          final String projectId = findRelatedProjectId(guid);
-          if(projectId == null) {
-            WebUtil.notFound(request, response, "File not found", null);
-            return false;
-          }
           boolean hasPermissions = user.isPermissionGrantedForProject(projectId, Permission.VIEW_BUILD_RUNTIME_DATA);
           if(!hasPermissions) response.sendError(HttpServletResponse.SC_FORBIDDEN, String.format("You have no access to PDB files in the project with id %s.", projectId));
           return hasPermissions;
@@ -148,6 +149,10 @@
     final Map<String,String> metadata = entry.getMetadata();
     final String storedFileName = metadata.get(BuildSymbolsIndexProvider.FILE_NAME_KEY);
     final String artifactPath = metadata.get(BuildSymbolsIndexProvider.ARTIFACT_PATH_KEY);
+    if(storedFileName == null || artifactPath == null){
+      LOG.debug(String.format("Metadata stored for guid '%s' is invalid.", guid));
+      return null;
+    }
     if(!storedFileName.equals(fileName)){
       LOG.debug(String.format("File name '%s' stored for guid '%s' differs from requested '%s'.", storedFileName, guid, fileName));
       return null;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/agent-tests.iml	Wed Nov 20 20:29:58 2013 +0400
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="true" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+    <orderEntry type="module" module-name="agent" />
+    <orderEntry type="module" module-name="common" />
+    <orderEntry type="library" name="Test-Api libs" level="project" />
+    <orderEntry type="library" name="Test-Api" level="project" />
+    <orderEntry type="library" name="testng" level="project" />
+    <orderEntry type="library" name="jmock" level="project" />
+    <orderEntry type="library" name="jmock" level="project" />
+    <orderEntry type="library" name="Test-Api" level="project" />
+    <orderEntry type="library" name="Common-Api" level="project" />
+    <orderEntry type="library" name="Agent-Api" level="project" />
+    <orderEntry type="library" name="log4j" level="project" />
+    <orderEntry type="library" scope="RUNTIME" name="TeamCity agent runtime" level="project" />
+    <orderEntry type="library" name="Common-Impl" level="project" />
+    <orderEntry type="library" name="Idea-OpenApi" level="project" />
+  </component>
+</module>
+
--- a/tests/tests.iml	Fri Nov 15 15:23:11 2013 +0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,31 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<module type="JAVA_MODULE" version="4">
-  <component name="NewModuleRootManager" inherit-compiler-output="true">
-    <exclude-output />
-    <content url="file://$MODULE_DIR$">
-      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
-    </content>
-    <orderEntry type="inheritedJdk" />
-    <orderEntry type="sourceFolder" forTests="false" />
-    <orderEntry type="module" module-name="agent" />
-    <orderEntry type="module" module-name="common" />
-    <orderEntry type="module" module-name="server" />
-    <orderEntry type="library" name="Test-Api libs" level="project" />
-    <orderEntry type="library" name="Test-Api" level="project" />
-    <orderEntry type="library" name="testng" level="project" />
-    <orderEntry type="library" name="jmock" level="project" />
-    <orderEntry type="library" name="testng" level="project" />
-    <orderEntry type="library" name="jmock" level="project" />
-    <orderEntry type="library" name="Test-Api" level="project" />
-    <orderEntry type="library" name="Common-Api" level="project" />
-    <orderEntry type="library" name="Agent-Api" level="project" />
-    <orderEntry type="library" name="log4j" level="project" />
-    <orderEntry type="library" name="TeamCity server runtime" level="project" />
-    <orderEntry type="library" scope="RUNTIME" name="TeamCity agent runtime" level="project" />
-    <orderEntry type="library" name="Common-Impl" level="project" />
-    <orderEntry type="library" name="Server-Api" level="project" />
-    <orderEntry type="library" name="Idea-OpenApi" level="project" />
-    <orderEntry type="library" name="Servlet Api" level="project" />
-  </component>
-</module>
-