2021-03-24 13:09:40 -04:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2021-11-10 16:34:54 -05:00
|
|
|
using MediaBrowser.Model.Cryptography;
|
2021-03-24 13:09:40 -04:00
|
|
|
using Xunit;
|
|
|
|
|
2021-11-10 16:34:54 -05:00
|
|
|
namespace Jellyfin.Model.Tests.Cryptography
|
2021-03-24 13:09:40 -04:00
|
|
|
{
|
|
|
|
public static class PasswordHashTests
|
|
|
|
{
|
|
|
|
[Fact]
|
|
|
|
public static void Ctor_Null_ThrowsArgumentNullException()
|
|
|
|
{
|
|
|
|
Assert.Throws<ArgumentNullException>(() => new PasswordHash(null!, Array.Empty<byte>()));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public static void Ctor_Empty_ThrowsArgumentException()
|
|
|
|
{
|
|
|
|
Assert.Throws<ArgumentException>(() => new PasswordHash(string.Empty, Array.Empty<byte>()));
|
|
|
|
}
|
|
|
|
|
2021-09-11 16:31:24 -04:00
|
|
|
public static TheoryData<string, PasswordHash> Parse_Valid_TestData()
|
2021-03-24 13:09:40 -04:00
|
|
|
{
|
2021-09-11 16:31:24 -04:00
|
|
|
var data = new TheoryData<string, PasswordHash>();
|
2021-03-24 13:39:33 -04:00
|
|
|
// Id
|
2021-09-11 16:31:24 -04:00
|
|
|
data.Add(
|
2021-03-24 13:09:40 -04:00
|
|
|
"$PBKDF2",
|
2021-09-11 16:31:24 -04:00
|
|
|
new PasswordHash("PBKDF2", Array.Empty<byte>()));
|
2021-03-24 13:09:40 -04:00
|
|
|
|
2021-03-24 13:39:33 -04:00
|
|
|
// Id + parameter
|
2021-09-11 16:31:24 -04:00
|
|
|
data.Add(
|
2021-03-24 13:39:33 -04:00
|
|
|
"$PBKDF2$iterations=1000",
|
2021-03-24 13:09:40 -04:00
|
|
|
new PasswordHash(
|
|
|
|
"PBKDF2",
|
2021-03-24 13:39:33 -04:00
|
|
|
Array.Empty<byte>(),
|
2021-03-24 13:09:40 -04:00
|
|
|
Array.Empty<byte>(),
|
|
|
|
new Dictionary<string, string>()
|
|
|
|
{
|
2021-03-24 13:39:33 -04:00
|
|
|
{ "iterations", "1000" },
|
2021-09-11 16:31:24 -04:00
|
|
|
}));
|
2021-03-24 13:09:40 -04:00
|
|
|
|
2021-03-24 13:39:33 -04:00
|
|
|
// Id + parameters
|
2021-09-11 16:31:24 -04:00
|
|
|
data.Add(
|
2021-03-24 13:39:33 -04:00
|
|
|
"$PBKDF2$iterations=1000,m=120",
|
2021-03-24 13:09:40 -04:00
|
|
|
new PasswordHash(
|
|
|
|
"PBKDF2",
|
2021-03-24 13:39:33 -04:00
|
|
|
Array.Empty<byte>(),
|
2021-03-24 13:09:40 -04:00
|
|
|
Array.Empty<byte>(),
|
|
|
|
new Dictionary<string, string>()
|
|
|
|
{
|
|
|
|
{ "iterations", "1000" },
|
|
|
|
{ "m", "120" }
|
2021-09-11 16:31:24 -04:00
|
|
|
}));
|
2021-03-24 13:09:40 -04:00
|
|
|
|
2021-03-24 13:39:33 -04:00
|
|
|
// Id + hash
|
2021-09-11 16:31:24 -04:00
|
|
|
data.Add(
|
2021-03-24 13:39:33 -04:00
|
|
|
"$PBKDF2$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D",
|
|
|
|
new PasswordHash(
|
|
|
|
"PBKDF2",
|
|
|
|
Convert.FromHexString("62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D"),
|
|
|
|
Array.Empty<byte>(),
|
2021-09-11 16:31:24 -04:00
|
|
|
new Dictionary<string, string>()));
|
2021-03-24 13:39:33 -04:00
|
|
|
|
|
|
|
// Id + salt + hash
|
2021-09-11 16:31:24 -04:00
|
|
|
data.Add(
|
2021-03-24 13:39:33 -04:00
|
|
|
"$PBKDF2$69F420$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D",
|
2021-03-24 13:09:40 -04:00
|
|
|
new PasswordHash(
|
|
|
|
"PBKDF2",
|
|
|
|
Convert.FromHexString("62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D"),
|
|
|
|
Convert.FromHexString("69F420"),
|
2021-09-11 16:31:24 -04:00
|
|
|
new Dictionary<string, string>()));
|
2021-03-24 13:39:33 -04:00
|
|
|
|
|
|
|
// Id + parameter + hash
|
2021-09-11 16:31:24 -04:00
|
|
|
data.Add(
|
2021-03-24 13:39:33 -04:00
|
|
|
"$PBKDF2$iterations=1000$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D",
|
|
|
|
new PasswordHash(
|
|
|
|
"PBKDF2",
|
|
|
|
Convert.FromHexString("62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D"),
|
|
|
|
Array.Empty<byte>(),
|
|
|
|
new Dictionary<string, string>()
|
|
|
|
{
|
|
|
|
{ "iterations", "1000" }
|
2021-09-11 16:31:24 -04:00
|
|
|
}));
|
2021-03-24 13:39:33 -04:00
|
|
|
// Id + parameters + hash
|
2021-09-11 16:31:24 -04:00
|
|
|
data.Add(
|
2021-03-24 13:39:33 -04:00
|
|
|
"$PBKDF2$iterations=1000,m=120$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D",
|
|
|
|
new PasswordHash(
|
|
|
|
"PBKDF2",
|
|
|
|
Convert.FromHexString("62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D"),
|
|
|
|
Array.Empty<byte>(),
|
2021-03-24 13:09:40 -04:00
|
|
|
new Dictionary<string, string>()
|
|
|
|
{
|
|
|
|
{ "iterations", "1000" },
|
|
|
|
{ "m", "120" }
|
2021-09-11 16:31:24 -04:00
|
|
|
}));
|
2021-03-24 13:39:33 -04:00
|
|
|
// Id + parameters + salt + hash
|
2021-09-11 16:31:24 -04:00
|
|
|
data.Add(
|
2021-03-24 13:39:33 -04:00
|
|
|
"$PBKDF2$iterations=1000,m=120$69F420$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D",
|
2021-03-24 13:09:40 -04:00
|
|
|
new PasswordHash(
|
|
|
|
"PBKDF2",
|
2021-03-24 13:39:33 -04:00
|
|
|
Convert.FromHexString("62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D"),
|
|
|
|
Convert.FromHexString("69F420"),
|
2021-03-24 13:09:40 -04:00
|
|
|
new Dictionary<string, string>()
|
|
|
|
{
|
|
|
|
{ "iterations", "1000" },
|
|
|
|
{ "m", "120" }
|
2021-09-11 16:31:24 -04:00
|
|
|
}));
|
|
|
|
return data;
|
2021-03-24 13:09:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
[MemberData(nameof(Parse_Valid_TestData))]
|
|
|
|
public static void Parse_Valid_Success(string passwordHashString, PasswordHash expected)
|
|
|
|
{
|
|
|
|
var passwordHash = PasswordHash.Parse(passwordHashString);
|
|
|
|
Assert.Equal(expected.Id, passwordHash.Id);
|
|
|
|
Assert.Equal(expected.Parameters, passwordHash.Parameters);
|
|
|
|
Assert.Equal(expected.Salt.ToArray(), passwordHash.Salt.ToArray());
|
|
|
|
Assert.Equal(expected.Hash.ToArray(), passwordHash.Hash.ToArray());
|
|
|
|
Assert.Equal(expected.ToString(), passwordHash.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
[InlineData("$PBKDF2")]
|
|
|
|
[InlineData("$PBKDF2$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D")]
|
|
|
|
[InlineData("$PBKDF2$69F420$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D")]
|
|
|
|
[InlineData("$PBKDF2$iterations=1000$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D")]
|
|
|
|
[InlineData("$PBKDF2$iterations=1000,m=120$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D")]
|
2021-03-24 13:39:33 -04:00
|
|
|
[InlineData("$PBKDF2$iterations=1000,m=120$69F420$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D")]
|
2021-03-24 13:09:40 -04:00
|
|
|
[InlineData("$PBKDF2$iterations=1000,m=120")]
|
|
|
|
public static void ToString_Roundtrip_Success(string passwordHash)
|
|
|
|
{
|
|
|
|
Assert.Equal(passwordHash, PasswordHash.Parse(passwordHash).ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public static void Parse_Null_ThrowsArgumentException()
|
|
|
|
{
|
|
|
|
Assert.Throws<ArgumentException>(() => PasswordHash.Parse(null));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public static void Parse_Empty_ThrowsArgumentException()
|
|
|
|
{
|
|
|
|
Assert.Throws<ArgumentException>(() => PasswordHash.Parse(string.Empty));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
[InlineData("$")] // No id
|
|
|
|
[InlineData("$$")] // Empty segments
|
|
|
|
[InlineData("PBKDF2$")] // Doesn't start with $
|
|
|
|
[InlineData("$PBKDF2$$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D")] // Empty segment
|
|
|
|
[InlineData("$PBKDF2$iterations=1000$$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D")] // Empty salt segment
|
2021-03-24 13:39:33 -04:00
|
|
|
[InlineData("$PBKDF2$iterations=1000$69F420$")] // Empty hash segment
|
2021-03-24 13:09:40 -04:00
|
|
|
[InlineData("$PBKDF2$=$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D")] // Invalid parmeter
|
|
|
|
[InlineData("$PBKDF2$=1000$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D")] // Invalid parmeter
|
|
|
|
[InlineData("$PBKDF2$iterations=$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D")] // Invalid parmeter
|
2021-08-15 08:13:20 -04:00
|
|
|
[InlineData("$PBKDF2$iterations=1000$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D$")] // Ends on $
|
|
|
|
[InlineData("$PBKDF2$iterations=1000$69F420$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D$")] // Extra segment
|
|
|
|
[InlineData("$PBKDF2$iterations=1000$69F420$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D$anotherone")] // Extra segment
|
|
|
|
[InlineData("$PBKDF2$iterations=1000$invalidstalt$62FBA410AFCA5B4475F35137AB2E8596B127E4D927BA23F6CC05C067E897042D")] // Invalid salt
|
|
|
|
[InlineData("$PBKDF2$iterations=1000$69F420$invalid hash")] // Invalid hash
|
2021-03-24 13:09:40 -04:00
|
|
|
[InlineData("$PBKDF2$69F420$")] // Empty hash
|
|
|
|
public static void Parse_InvalidFormat_ThrowsFormatException(string passwordHash)
|
|
|
|
{
|
|
|
|
Assert.Throws<FormatException>(() => PasswordHash.Parse(passwordHash));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|