[go: up one dir, main page]

Menu

[a443d0]: / tests / src / case0003.c  Maximize  Restore  History

Download this file

134 lines (119 with data), 4.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright (c) 2013-2016, Christian Ferrari <tiian@users.sourceforge.net>
* All rights reserved.
*
* This file is part of FLoM.
*
* FLoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*
* FLoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FLoM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "flom.h"
/*
* Non default values used for tests
*/
const char *nd_resource_name1 = "resource1";
const char *nd_resource_name2 = "resource2";
/*
* Happy path usage with a static handle
*/
void unlock_different_resource(void) {
int ret_cod;
flom_handle_t my_handle;
/* initialize a new handle */
if (FLOM_RC_OK != (ret_cod = flom_handle_init(&my_handle))) {
fprintf(stderr, "flom_handle_init() returned %d, '%s'\n",
ret_cod, flom_strerror(ret_cod));
exit(1);
}
/* get current resource name */
printf("flom_handle_get_resource_name() = '%s'\n",
flom_handle_get_resource_name(&my_handle));
/* set a new resource name */
if (FLOM_RC_OK != (ret_cod = flom_handle_set_resource_name(
&my_handle, nd_resource_name1))) {
fprintf(stderr, "flom_handle_set_resource_name() returned %d, '%s'\n",
ret_cod, flom_strerror(ret_cod));
exit(1);
}
/* get new resource name */
printf("flom_handle_get_resource_name() = '%s'\n",
flom_handle_get_resource_name(&my_handle));
/* check resource name */
if (strcmp(nd_resource_name1,
flom_handle_get_resource_name(&my_handle))) {
fprintf(stderr,
"Unexpected result from flom_handle_set/get_resource_name\n");
exit(1);
}
/* lock acquisition */
if (FLOM_RC_OK != (ret_cod = flom_handle_lock(&my_handle))) {
fprintf(stderr, "flom_handle_lock() returned %d, '%s'\n",
ret_cod, flom_strerror(ret_cod));
exit(1);
} else if (NULL != flom_handle_get_locked_element(&my_handle)) {
printf("staticHandleHappyPath locked element is %s\n",
flom_handle_get_locked_element(&my_handle));
}
/* set a different resource name */
if (FLOM_RC_API_IMMUTABLE_HANDLE != (
ret_cod = flom_handle_set_resource_name(
&my_handle, nd_resource_name2))) {
fprintf(stderr, "flom_handle_set_resource_name() returned %d, '%s'\n",
ret_cod, flom_strerror(ret_cod));
exit(1);
}
/* get new resource name */
printf("flom_handle_get_resource_name() = '%s'\n",
flom_handle_get_resource_name(&my_handle));
/* check resource name: it should not be changed because the resource
is already locked */
if (strcmp(nd_resource_name1,
flom_handle_get_resource_name(&my_handle))) {
fprintf(stderr,
"Unexpected result from flom_handle_set/get_resource_name\n");
exit(1);
}
/* lock release */
if (FLOM_RC_OK != (ret_cod = flom_handle_unlock(&my_handle))) {
fprintf(stderr, "flom_handle_unlock() returned %d, '%s'\n",
ret_cod, flom_strerror(ret_cod));
exit(1);
}
/* handle clean-up (memory release) */
if (FLOM_RC_OK != (ret_cod = flom_handle_clean(&my_handle))) {
fprintf(stderr, "flom_handle_clean() returned %d, '%s'\n",
ret_cod, flom_strerror(ret_cod));
exit(1);
}
}
int main(int argc, char *argv[]) {
int bug = -1;
if (argc != 2) {
fprintf(stderr, "The first parameter must be the bug to exploit\n");
exit(1);
}
bug = strtol(argv[1], NULL, 10);
switch (bug) {
case 1:
/* try to unlock a resource different from the locked one! */
unlock_different_resource();
break;
default:
fprintf(stderr, "The bug # %d is not known, exiting...\n", bug);
exit(1);
} /* switch */
/* exit */
return 0;
}