Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Zbigniew Jędrzejewski-Szmek
polkit
Commits
a82fa625
Commit
a82fa625
authored
Nov 03, 2007
by
David Zeuthen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add tests for PolKitError
parent
774da25f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
40 deletions
+65
-40
polkit/polkit-error.c
polkit/polkit-error.c
+52
-34
polkit/polkit-error.h
polkit/polkit-error.h
+6
-2
polkit/polkit-test.c
polkit/polkit-test.c
+5
-0
polkit/polkit-test.h
polkit/polkit-test.h
+2
-4
No files found.
polkit/polkit-error.c
View file @
a82fa625
...
...
@@ -49,6 +49,7 @@
#include "polkit-types.h"
#include "polkit-error.h"
#include "polkit-debug.h"
#include "polkit-test.h"
/**
* PolKitError:
...
...
@@ -78,6 +79,17 @@ polkit_error_is_set (PolKitError *error)
return
error
!=
NULL
;
}
static
const
char
*
error_names
[
POLKIT_ERROR_NUM_ERROR_CODES
]
=
{
"OutOfMemory"
,
"PolicyFileInvalid"
,
"GeneralError"
,
"NotAuthorizedToReadAuthorizationsForOtherUsers"
,
"NotAuthorizedToRevokeAuthorizationsFromOtherUsers"
,
"NotAuthorizedToGrantAuthorization"
,
"AuthorizationAlreadyExists"
,
"NotSupported"
};
/**
* polkit_error_get_error_name:
* @error: the error
...
...
@@ -92,39 +104,10 @@ polkit_error_is_set (PolKitError *error)
const
char
*
polkit_error_get_error_name
(
PolKitError
*
error
)
{
const
char
*
ret
;
g_return_val_if_fail
(
error
!=
NULL
,
NULL
);
g_return_val_if_fail
(
error
->
error_code
>=
0
&&
error
->
error_code
<
POLKIT_ERROR_NUM_ERROR_CODES
,
NULL
);
switch
(
error
->
error_code
)
{
case
POLKIT_ERROR_OUT_OF_MEMORY
:
ret
=
"OutOfMemory"
;
break
;
case
POLKIT_ERROR_POLICY_FILE_INVALID
:
ret
=
"PolicyFileInvalid"
;
break
;
case
POLKIT_ERROR_GENERAL_ERROR
:
ret
=
"GeneralError"
;
break
;
case
POLKIT_ERROR_NOT_AUTHORIZED_TO_READ_AUTHORIZATIONS_FOR_OTHER_USERS
:
ret
=
"NotAuthorizedToReadAuthorizationsForOtherUsers"
;
break
;
case
POLKIT_ERROR_NOT_AUTHORIZED_TO_REVOKE_AUTHORIZATIONS_FROM_OTHER_USERS
:
ret
=
"NotAuthorizedToRevokeAuthorizationsFromOtherUsers"
;
break
;
case
POLKIT_ERROR_NOT_AUTHORIZED_TO_GRANT_AUTHORIZATION
:
ret
=
"NotAuthorizedToGrantAuthorization"
;
break
;
case
POLKIT_ERROR_AUTHORIZATION_ALREADY_EXISTS
:
ret
=
"AuthorizationAlreadyExists"
;
break
;
case
POLKIT_ERROR_NOT_SUPPORTED
:
ret
=
"NotSupported"
;
break
;
default:
ret
=
NULL
;
}
return
ret
;
return
error_names
[
error
->
error_code
];
}
/**
...
...
@@ -184,15 +167,17 @@ polkit_error_free (PolKitError *error)
* @Varargs: printf style arguments
*
* Sets an error. If OOM, the error will be set to a pre-allocated OOM error.
*
* Returns: TRUE if the error was set
**/
void
polkit_bool_t
polkit_error_set_error
(
PolKitError
**
error
,
PolKitErrorCode
error_code
,
const
char
*
format
,
...)
{
va_list
args
;
PolKitError
*
e
;
if
(
error
=
=
NULL
)
return
;
g_return_val_if_fail
(
error
!
=
NULL
,
FALSE
);
g_return_val_if_fail
(
error_code
>=
0
&&
error_code
<
POLKIT_ERROR_NUM_ERROR_CODES
,
FALSE
)
;
e
=
g_new0
(
PolKitError
,
1
);
e
->
is_static
=
FALSE
;
...
...
@@ -202,4 +187,37 @@ polkit_error_set_error (PolKitError **error, PolKitErrorCode error_code, const c
va_end
(
args
);
*
error
=
e
;
return
TRUE
;
}
#ifdef POLKIT_BUILD_TESTS
polkit_bool_t
_test_polkit_error
(
void
)
{
unsigned
int
n
;
PolKitError
*
e
;
char
s
[
256
];
e
=
NULL
;
g_assert
(
!
polkit_error_is_set
(
e
));
g_assert
(
!
polkit_error_set_error
(
&
e
,
-
1
,
"Testing"
));
g_assert
(
!
polkit_error_set_error
(
&
e
,
POLKIT_ERROR_NUM_ERROR_CODES
,
"Testing"
));
for
(
n
=
0
;
n
<
POLKIT_ERROR_NUM_ERROR_CODES
;
n
++
)
{
polkit_error_set_error
(
&
e
,
n
,
"Testing error code %d"
,
n
);
g_assert
(
polkit_error_is_set
(
e
));
g_assert
(
polkit_error_get_error_code
(
e
)
==
n
);
g_assert
(
strcmp
(
polkit_error_get_error_name
(
e
),
error_names
[
n
])
==
0
);
snprintf
(
s
,
sizeof
(
s
),
"Testing error code %d"
,
n
);
g_assert
(
strcmp
(
polkit_error_get_error_message
(
e
),
s
)
==
0
);
polkit_error_free
(
e
);
}
return
TRUE
;
}
#endif
/* POLKIT_BUILD_TESTS */
polkit/polkit-error.h
View file @
a82fa625
...
...
@@ -54,6 +54,8 @@ POLKIT_BEGIN_DECLS
* similar authorization already (modulo time of grant and who granted).
* @POLKIT_ERROR_NOT_SUPPORTED: The operation is not supported by the
* authorization database backend
* @POLKIT_ERROR_NUM_ERROR_CODES: Number of error codes. This may change
* from version to version; do not rely on it.
*
* Errors returned by PolicyKit
*/
...
...
@@ -66,7 +68,9 @@ typedef enum
POLKIT_ERROR_NOT_AUTHORIZED_TO_REVOKE_AUTHORIZATIONS_FROM_OTHER_USERS
,
POLKIT_ERROR_NOT_AUTHORIZED_TO_GRANT_AUTHORIZATION
,
POLKIT_ERROR_AUTHORIZATION_ALREADY_EXISTS
,
POLKIT_ERROR_NOT_SUPPORTED
POLKIT_ERROR_NOT_SUPPORTED
,
POLKIT_ERROR_NUM_ERROR_CODES
}
PolKitErrorCode
;
struct
_PolKitError
;
...
...
@@ -77,7 +81,7 @@ const char *polkit_error_get_error_name (PolKitError *error);
PolKitErrorCode
polkit_error_get_error_code
(
PolKitError
*
error
);
const
char
*
polkit_error_get_error_message
(
PolKitError
*
error
);
void
polkit_error_free
(
PolKitError
*
error
);
void
polkit_error_set_error
(
PolKitError
**
error
,
PolKitErrorCode
error_code
,
const
char
*
format
,
...)
__attribute__
((
__format__
(
__printf__
,
3
,
4
)));
polkit_bool_t
polkit_error_set_error
(
PolKitError
**
error
,
PolKitErrorCode
error_code
,
const
char
*
format
,
...)
__attribute__
((
__format__
(
__printf__
,
3
,
4
)));
POLKIT_END_DECLS
...
...
polkit/polkit-test.c
View file @
a82fa625
...
...
@@ -23,6 +23,8 @@
*
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <polkit/polkit-test.h>
int
...
...
@@ -36,6 +38,9 @@ main (int argc, char *argv[])
if
(
!
_test_polkit_action
())
goto
out
;
if
(
!
_test_polkit_error
())
goto
out
;
ret
=
0
;
out:
...
...
polkit/polkit-test.h
View file @
a82fa625
...
...
@@ -34,11 +34,9 @@
POLKIT_BEGIN_DECLS
#endif
/* POLKIT_BUILD_TESTS */
#ifdef POLKIT_BUILD_TESTS
polkit_bool_t
_test_polkit_action
(
void
);
polkit_bool_t
_test_polkit_error
(
void
);
POLKIT_END_DECLS
#endif
/* POLKIT_TEST_H */
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment