< Summary

Class:Azure.Identity.LinuxNativeMethods
Assembly:Azure.Identity
File(s):C:\Git\azure-sdk-for-net\sdk\identity\Azure.Identity\src\LinuxNativeMethods.cs
Covered lines:0
Uncovered lines:23
Coverable lines:23
Total lines:115
Line coverage:0% (0 of 23)
Covered branches:0
Total branches:8
Branch coverage:0% (0 of 8)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
secret_schema_new(...)-0%100%
secret_password_lookup_sync(...)-0%0%
secret_password_store_sync(...)-0%100%
secret_password_clear_sync(...)-0%100%
secret_password_free(...)-0%0%
secret_schema_unref(...)-0%0%
HandleError(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\identity\Azure.Identity\src\LinuxNativeMethods.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Runtime.InteropServices;
 6
 7namespace Azure.Identity
 8{
 9    internal static class LinuxNativeMethods
 10    {
 11        public const string SECRET_COLLECTION_SESSION = "session";
 12
 13        public enum SecretSchemaAttributeType
 14        {
 15            SECRET_SCHEMA_ATTRIBUTE_STRING = 0,
 16            SECRET_SCHEMA_ATTRIBUTE_INTEGER = 1,
 17            SECRET_SCHEMA_ATTRIBUTE_BOOLEAN = 2,
 18        }
 19
 20        public enum SecretSchemaFlags
 21        {
 22            SECRET_SCHEMA_NONE = 0,
 23            SECRET_SCHEMA_DONT_MATCH_NAME = 2,
 24        }
 25
 26        internal struct GError
 27        {
 28            public uint Domain;
 29            public int Code;
 30            public string Message;
 31        }
 32
 33        public static IntPtr secret_schema_new(string name, SecretSchemaFlags flags, string attribute1, SecretSchemaAttr
 34        {
 035            return Imports.secret_schema_new(name, (int)flags, attribute1, (int)attribute1Type, attribute2, (int)attribu
 36        }
 37
 38        public static string secret_password_lookup_sync(IntPtr schemaPtr, IntPtr cancellable, string attribute1Type, st
 39        {
 040            IntPtr passwordPtr = Imports.secret_password_lookup_sync(schemaPtr, cancellable, out IntPtr errorPtr, attrib
 041            HandleError(errorPtr, "An error was encountered while reading secret from keyring");
 042            return passwordPtr != IntPtr.Zero ? Marshal.PtrToStringAnsi(passwordPtr) : null;
 43        }
 44
 45        public static void secret_password_store_sync(IntPtr schemaPtr, string collection, string label, string password
 46        {
 047            _ = Imports.secret_password_store_sync(schemaPtr, collection, label, password, cancellable, out IntPtr error
 048            HandleError(errorPtr, "An error was encountered while writing secret to keyring");
 049        }
 50
 51        public static void secret_password_clear_sync(IntPtr schemaPtr, IntPtr cancellable, string attribute1Type, strin
 52        {
 053            _ = Imports.secret_password_clear_sync(schemaPtr, cancellable, out IntPtr errorPtr, attribute1Type, attribut
 054            HandleError(errorPtr, "An error was encountered while clearing secret from keyring ");
 055        }
 56
 57        public static void secret_password_free(IntPtr passwordPtr)
 58        {
 059            if (passwordPtr != IntPtr.Zero)
 60            {
 061                Imports.secret_password_free(passwordPtr);
 62            }
 063        }
 64
 65        public static void secret_schema_unref(IntPtr schemaPtr)
 66        {
 067            if (schemaPtr != IntPtr.Zero)
 68            {
 069                Imports.secret_schema_unref(schemaPtr);
 70            }
 071        }
 72
 73        private static void HandleError(IntPtr errorPtr, string errorMessage)
 74        {
 075            if (errorPtr == IntPtr.Zero)
 76            {
 077                return;
 78            }
 79
 80            GError error;
 81            try
 82            {
 083                error = Marshal.PtrToStructure<GError>(errorPtr);
 084            }
 085            catch (Exception ex)
 86            {
 087                throw new InvalidOperationException($"An exception was encountered while processing libsecret error: {ex
 88            }
 89
 090            throw new InvalidOperationException($"{errorMessage}, domain:'{error.Domain}', code:'{error.Code}', message:
 91        }
 92
 93        private static class Imports
 94        {
 95            [DllImport("libsecret-1.so.0", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, BestFi
 96            [DefaultDllImportSearchPaths(DllImportSearchPath.System32 | DllImportSearchPath.AssemblyDirectory)]
 97            public static extern IntPtr secret_schema_new(string name, int flags, string attribute1, int attribute1Type,
 98
 99            [DllImport("libsecret-1.so.0", CallingConvention = CallingConvention.StdCall)]
 100            public static extern void secret_schema_unref (IntPtr schema);
 101
 102            [DllImport("libsecret-1.so.0", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, BestFi
 103            public static extern IntPtr secret_password_lookup_sync(IntPtr schema, IntPtr cancellable, out IntPtr error,
 104
 105            [DllImport("libsecret-1.so.0", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, BestFi
 106            public static extern int secret_password_store_sync(IntPtr schema, string collection, string label, string p
 107
 108            [DllImport("libsecret-1.so.0", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, BestFi
 109            public static extern int secret_password_clear_sync(IntPtr schema, IntPtr cancellable, out IntPtr error, str
 110
 111            [DllImport("libsecret-1.so.0", CallingConvention = CallingConvention.StdCall)]
 112            public static extern void secret_password_free(IntPtr password);
 113        }
 114    }
 115}