summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorBalbir Singh <balbir@linux.vnet.ibm.com>2008-12-17 15:17:57 +0000
committerBalbir Singh <balbir@linux.vnet.ibm.com>2008-12-17 15:17:57 +0000
commitdef5595456cbf96de855ba8d69c8e46e16f2f9c4 (patch)
treec8a1d281dc7c883e055622f9edbb5db9c69fe06d /tests
parent1a61c6f7fb1fb2b9f0092408e024dce34cc49900 (diff)
downloadlibcg-def5595456cbf96de855ba8d69c8e46e16f2f9c4.tar.gz
libcg-def5595456cbf96de855ba8d69c8e46e16f2f9c4.tar.xz
libcg-def5595456cbf96de855ba8d69c8e46e16f2f9c4.zip
libcgroup Test: libcgroup-add-static-array-for-testcase-info-messages
This patch removes all the strncpy() calls that were made before each api call to print the info messages. This patch creates an array of such info messages, initialize them, and then frees them with the help of two functions set_info_msgs(), called at start and free_info_msgs() called at end. The main motto has been to avoid the global variables due to thread safety reasons. Signed-off-by: Sudhir Kumar <skumar@linux.vnet.ibm.com> git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/trunk@266 4f4bb910-9a46-0410-90c8-c897d4f1cd53
Diffstat (limited to 'tests')
-rw-r--r--tests/libcgrouptest.h9
-rw-r--r--tests/libcgrouptest01.c107
2 files changed, 116 insertions, 0 deletions
diff --git a/tests/libcgrouptest.h b/tests/libcgrouptest.h
index 644e176..70f565c 100644
--- a/tests/libcgrouptest.h
+++ b/tests/libcgrouptest.h
@@ -30,6 +30,7 @@
#include <libcgroup.h>
#define SIZE 100 /* Max size of a message to be printed */
+#define NUM_MSGS 10 /* Number of such messsages */
#define PASS 1 /* test passed */
#define FAIL 0 /* test failed */
@@ -55,6 +56,9 @@ enum cgroup_control_val_t {
STRING,
};
+/* Create a matrix of possible info messages */
+char **info;
+
int64_t val_int64;
u_int64_t val_uint64;
bool val_bool;
@@ -83,6 +87,11 @@ static inline void message(int num, int pass, char *api, int ret, char *extra);
static inline void build_path(char *target, char *mountpoint,
char *group, char *file);
+/* Allocate memory and populate info messages */
+void set_info_msgs();
+/* Free the allocated memory for info messages */
+void free_info_msgs();
+
static inline pid_t cgrouptest_gettid()
{
return syscall(__NR_gettid);
diff --git a/tests/libcgrouptest01.c b/tests/libcgrouptest01.c
index bc994ab..efe423a 100644
--- a/tests/libcgrouptest01.c
+++ b/tests/libcgrouptest01.c
@@ -77,6 +77,9 @@ int main(int argc, char *argv[])
tasks_uid = 0;
tasks_gid = 0;
+ /* Initialize the message buffer with info messages */
+ set_info_msgs();
+
/*
* Testsets: Testcases are broadly devided into 3 categories based on
* filesystem(fs) mount scenario. fs not mounted, fs mounted, fs multi
@@ -173,6 +176,9 @@ int main(int argc, char *argv[])
cgroup_free(&nullcgroup);
cgroup_free(&cgroup1);
+ /* Free the allocated memory for info messages */
+ free_info_msgs();
+
break;
case FS_MOUNTED:
@@ -481,6 +487,9 @@ int main(int argc, char *argv[])
cgroup_free(&cgroup2);
cgroup_free(&cgroup3);
+ /* Free the allocated memory for info messages */
+ free_info_msgs();
+
break;
case FS_MULTI_MOUNTED:
@@ -1040,6 +1049,9 @@ int main(int argc, char *argv[])
cgroup_free(&ctl2_cgroup1);
cgroup_free(&ctl2_cgroup2);
+ /* Free the allocated memory for info messages */
+ free_info_msgs();
+
break;
default:
@@ -1324,3 +1336,98 @@ static inline void build_path(char *target, char *mountpoint,
strncat(target, file, FILENAME_MAX);
}
}
+
+/* Initialize the info matrix with possible info messages */
+void set_info_msgs()
+{
+ info = (char **) malloc(NUM_MSGS * sizeof(char *));
+ if (!info) {
+ printf("Could not allocate memory for msg buffer. Check if"
+ " system has sufficient memory. Exiting the testcases...\n");
+ free_info_msgs();
+ exit(1);
+ }
+ for (int k = 0; k < NUM_MSGS; ++k) {
+ info[k] = (char *) malloc(SIZE * sizeof(char));
+ if (!info[k]) {
+ printf("Failed to allocate memory for msg %d. Check "
+ "your system memory. Exiting the testcases...\n", k);
+ free_info_msgs();
+ exit(1);
+ }
+
+ /* Is initialization this way ok or just n seqential lines?? */
+ switch (k) {
+ case 0:
+ strncpy(info[k], " Par: nullcgroup\n", SIZE);
+ break;
+ case 1:
+ strncpy(info[k], " Par: commoncgroup\n", SIZE);
+ break;
+ case 2:
+ strncpy(info[k], " Par: not created group\n", SIZE);
+ break;
+ case 3:
+ strncpy(info[k], " Par: same cgroup\n", SIZE);
+ break;
+ case 4:
+ strncpy(info[k], " Task found in group/s\n", SIZE);
+ break;
+ case 5:
+ strncpy(info[k], " Task not found in group/s\n", SIZE);
+ break;
+ case 6:
+ strncpy(info[k], " Task not found in all"
+ " groups\n", SIZE);
+ break;
+ case 7:
+ strncpy(info[k], " group found under both"
+ " controllers\n", SIZE);
+ break;
+ case 8:
+ strncpy(info[k], " group not found under"
+ " second controller\n", SIZE);
+ break;
+ case 9:
+ strncpy(info[k], " group not found under"
+ " any controller\n", SIZE);
+ break;
+ case 10:
+ strncpy(info[k], " group modified under"
+ " both controllers\n", SIZE);
+ break;
+ case 11:
+ strncpy(info[k], " group not modified under"
+ " second controller\n", SIZE);
+ break;
+ case 12:
+ strncpy(info[k], " group not modified under"
+ " any controller\n", SIZE);
+ break;
+ case 13:
+ strncpy(info[k], " Group deleted from fs\n", SIZE);
+ break;
+ case 14:
+ strncpy(info[k], " Group not deleted from fs\n", SIZE);
+ break;
+ case 15:
+ strncpy(info[k], " Group not deleted globaly\n", SIZE);
+ break;
+ /* Add more messages here and change NUM_MSGS */
+ default:
+ break;
+ }
+ }
+}
+
+/* Free the allocated memory for buffers */
+void free_info_msgs()
+{
+ for (int k = 0; k < NUM_MSGS; ++k) {
+ if (info[k])
+ free(info[k]);
+ }
+
+ if (info)
+ free(info);
+}