Skip to content

Commit

Permalink
Changing .NET random temp file generation to be truly random.
Browse files Browse the repository at this point in the history
The .NET bindings now use GUIDs as part of the file names for files and
directories created in the temp directory. This is to work around
potential collisions in random number generation encountered when using
the .NET Random class, particularly in different processes.

Fixes issue SeleniumHQ#6935.
  • Loading branch information
jimevans committed Feb 4, 2014
1 parent 6e5a845 commit 66a3a0d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 7 deletions.
4 changes: 2 additions & 2 deletions dotnet/src/webdriver/Firefox/FirefoxProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public bool AssumeUntrustedCertificateIssuer
/// <returns>The constructed <see cref="FirefoxProfile"/>.</returns>
public static FirefoxProfile FromBase64String(string base64)
{
string destinationDirectory = FileUtilities.GenerateRandomTempDirectoryName("webdriver{0}.duplicated");
string destinationDirectory = FileUtilities.GenerateRandomTempDirectoryName("webdriver.{0}.duplicated");
byte[] zipContent = Convert.FromBase64String(base64);
using (MemoryStream zipStream = new MemoryStream(zipContent))
{
Expand Down Expand Up @@ -355,7 +355,7 @@ internal void AddWebDriverExtension()
/// <returns>A random directory name for the profile.</returns>
private static string GenerateProfileDirectoryName()
{
return FileUtilities.GenerateRandomTempDirectoryName("anonymous{0}.webdriver-profile");
return FileUtilities.GenerateRandomTempDirectoryName("anonymous.{0}.webdriver-profile");
}

/// <summary>
Expand Down
5 changes: 1 addition & 4 deletions dotnet/src/webdriver/Internal/FileUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ namespace OpenQA.Selenium.Internal
/// </summary>
internal static class FileUtilities
{
private static Random tempFileGenerator = new Random();

/// <summary>
/// Recursively copies a directory.
/// </summary>
Expand Down Expand Up @@ -182,8 +180,7 @@ public static string GetCurrentDirectory()
/// <returns>The full path to the random directory name in the temporary directory.</returns>
public static string GenerateRandomTempDirectoryName(string directoryPattern)
{
string randomNumber = tempFileGenerator.Next().ToString(CultureInfo.InvariantCulture);
string directoryName = string.Format(CultureInfo.InvariantCulture, directoryPattern, randomNumber);
string directoryName = string.Format(CultureInfo.InvariantCulture, directoryPattern, Guid.NewGuid().ToString("N"));
return Path.Combine(Path.GetTempPath(), directoryName);
}
}
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Safari/SafariDriverCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private void CloseSafariProcess()

private string PrepareConnectFile()
{
string directoryName = FileUtilities.GenerateRandomTempDirectoryName("SafariDriverConnect{0}");
string directoryName = FileUtilities.GenerateRandomTempDirectoryName("SafariDriverConnect.{0}");
this.temporaryDirectoryPath = Path.Combine(Path.GetTempPath(), directoryName);
string tempFileName = Path.Combine(this.temporaryDirectoryPath, "connect.html");
string contents = string.Format(CultureInfo.InvariantCulture, "<!DOCTYPE html><script>window.location = '{0}';</script>", this.server.ServerUri.ToString());
Expand Down

0 comments on commit 66a3a0d

Please sign in to comment.