Acl parser + unit tests
This commit is contained in:
parent
b846a60f25
commit
9a5feb9d54
6 changed files with 181 additions and 0 deletions
|
|
@ -19,5 +19,10 @@
|
|||
<PackageVersion Include="Npgsql" Version="8.0.5" />
|
||||
<PackageVersion Include="Pure.DI" Version="2.1.38" />
|
||||
<PackageVersion Include="Xamarin.AndroidX.Core.SplashScreen" Version="1.0.1.1" />
|
||||
|
||||
<PackageVersion Include="coverlet.collector" Version="6.0.2"/>
|
||||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
|
||||
<PackageVersion Include="xunit" Version="2.9.2"/>
|
||||
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2"/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
17
pgLabII.PgUtils.Tests/Acls/AclParserTests.cs
Normal file
17
pgLabII.PgUtils.Tests/Acls/AclParserTests.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
namespace pgLabII.PgUtils.Tests;
|
||||
|
||||
public class AclParserTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("foo=rw/postgres", "foo", "rw", "postgres")]
|
||||
[InlineData("\"Test\"\"=\"=rw/postgres", "Test\"=", "rw", "postgres")]
|
||||
[InlineData("foo=rw/\"Test\"\"=\"", "foo", "rw", "Test\"=")]
|
||||
public void Test1(string aclString, string expectedGrantee, string expectedRights, string expectedGrantor)
|
||||
{
|
||||
AclParser parser = new();
|
||||
Acl acl = parser.Parse(aclString);
|
||||
Assert.Equal(expectedGrantee, acl.Grantee);
|
||||
Assert.Equal(expectedRights, acl.Rights);
|
||||
Assert.Equal(expectedGrantor, acl.Grantor);
|
||||
}
|
||||
}
|
||||
25
pgLabII.PgUtils.Tests/pgLabII.PgUtils.Tests.csproj
Normal file
25
pgLabII.PgUtils.Tests/pgLabII.PgUtils.Tests.csproj
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
||||
<PackageReference Include="xunit" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\pgLabII.PgUtils\pgLabII.PgUtils.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
113
pgLabII.PgUtils/Acls/AclParser.cs
Normal file
113
pgLabII.PgUtils/Acls/AclParser.cs
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
using System.Text;
|
||||
|
||||
namespace pgLabII.PgUtils;
|
||||
|
||||
public class Acl(string grantee, string rights, string grantor)
|
||||
{
|
||||
public string Grantee { get; } = grantee;
|
||||
public string Rights { get; } = rights;
|
||||
public string Grantor { get; } = grantor;
|
||||
}
|
||||
/// <summary>
|
||||
/// Parser for acl's. Acl's have a fairly simple structure grantee=right/grantor see also https://www.postgresql.org/docs/current/ddl-priv.html
|
||||
/// When the grantee is empty it is "PUBLIC" which you can see as a wildcard that matches everybody that can authenticate.
|
||||
/// Because the grantee and grantor name can contain special characters there is an escape mechinism in place.
|
||||
/// When the name contains a conflicting character it is quoted. If the name contains a quote it is doubles.
|
||||
/// </summary>
|
||||
public class AclParser
|
||||
{
|
||||
private int _position = 0;
|
||||
private string _input = null!;
|
||||
|
||||
public Acl Parse(string acl)
|
||||
{
|
||||
_input = acl;
|
||||
_position = 0;
|
||||
ConsumeWhitespace();
|
||||
string grantee = ConsumeRoleName();
|
||||
string rights = ConsumeRights();
|
||||
if (Current() != '/')
|
||||
throw new ArgumentException("Unexpected symbol in acl string");
|
||||
_position++;
|
||||
string grantor = ConsumeRoleName();
|
||||
return new Acl(grantee, rights, grantor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// There is a limited set of possible characters for the rights
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
private string ConsumeRights()
|
||||
{
|
||||
if (Current() != '=')
|
||||
throw new ArgumentException("Unexpected symbol in acl string");
|
||||
_position++;
|
||||
int start = _position;
|
||||
while (NotAtEnd() && Current() != '/')
|
||||
_position++;
|
||||
|
||||
return _input.Substring(start, _position - start);
|
||||
}
|
||||
|
||||
// Whitespace should have been consumed
|
||||
// There should be either a " in which case the name is quoted
|
||||
// a = in which case there is no name ie PUBLIC
|
||||
// all other cases it is an unquoted name
|
||||
private string ConsumeRoleName() =>
|
||||
_input[_position] switch
|
||||
{
|
||||
'=' => "",
|
||||
'"' => ParseQuotedName(),
|
||||
_ => ParseUnquotedName()
|
||||
};
|
||||
|
||||
private string ParseQuotedName()
|
||||
{
|
||||
_position++; // consume opening quote
|
||||
int start = _position;
|
||||
StringBuilder sb = new();
|
||||
|
||||
while (true)
|
||||
{
|
||||
while (NotAtEnd() && Current() != '"')
|
||||
_position++;
|
||||
|
||||
char peek = _position + 1 < _input.Length ? _input[_position + 1] : '\0';
|
||||
if (peek == '"')
|
||||
{
|
||||
_position++;
|
||||
sb.Append(_input.AsSpan(start, _position - start));
|
||||
_position++;
|
||||
start = _position;
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(_input.AsSpan(start, _position - start));
|
||||
_position++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private string ParseUnquotedName()
|
||||
{
|
||||
int start = _position;
|
||||
while (NotAtEnd() && Current() != '=')
|
||||
_position++;
|
||||
|
||||
return _input.Substring(start, _position - start);
|
||||
}
|
||||
|
||||
private void ConsumeWhitespace()
|
||||
{
|
||||
while (NotAtEnd() && char.IsWhiteSpace(Current()))
|
||||
++_position;
|
||||
}
|
||||
|
||||
private bool NotAtEnd() => _position < _input.Length;
|
||||
|
||||
private char Current() => _input[_position];
|
||||
}
|
||||
9
pgLabII.PgUtils/pgLabII.PgUtils.csproj
Normal file
9
pgLabII.PgUtils/pgLabII.PgUtils.csproj
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
12
pgLabII.sln
12
pgLabII.sln
|
|
@ -18,6 +18,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
|
|||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "UI", "UI", "{EBBC2081-5061-4843-B420-AB4C48A5C283}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "pgLabII.PgUtils", "pgLabII.PgUtils\pgLabII.PgUtils.csproj", "{6694EE99-3AEA-4823-B2C3-02F28F575D14}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "pgLabII.PgUtils.Tests", "pgLabII.PgUtils.Tests\pgLabII.PgUtils.Tests.csproj", "{915C5439-4CF5-4625-AB7B-24F0E34E5B5F}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
@ -41,6 +45,14 @@ Global
|
|||
{7AD1DAC8-7FBE-49D5-8614-7321233DB82E}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
|
||||
{7AD1DAC8-7FBE-49D5-8614-7321233DB82E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{7AD1DAC8-7FBE-49D5-8614-7321233DB82E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{6694EE99-3AEA-4823-B2C3-02F28F575D14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{6694EE99-3AEA-4823-B2C3-02F28F575D14}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6694EE99-3AEA-4823-B2C3-02F28F575D14}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6694EE99-3AEA-4823-B2C3-02F28F575D14}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{915C5439-4CF5-4625-AB7B-24F0E34E5B5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{915C5439-4CF5-4625-AB7B-24F0E34E5B5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{915C5439-4CF5-4625-AB7B-24F0E34E5B5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{915C5439-4CF5-4625-AB7B-24F0E34E5B5F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue