~ecryptfs/ecryptfs/oldtrunk2

« back to all changes in this revision

Viewing changes to tests/kernel/lp-524919/test.c

  • Committer: Tyler Hicks
  • Author(s): Colin King
  • Date: 2012-03-26 18:01:52 UTC
  • mfrom: (660.1.5 new-tests)
  • Revision ID: tyhicks@canonical.com-20120326180152-ys6xrbcp76j1yao0
* tests/kernel/lp-524919.sh, tests/kernel/lp-524919/test.c:
  - test case for checking lstat/readlink size

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Author: Colin King <colin.king@canonical.com>
 
3
 *
 
4
 * Copyright (C) 2012 Canonical, Ltd.
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 
19
 */
 
20
 
 
21
#include <unistd.h>
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include <errno.h>
 
26
#include <sys/mman.h>
 
27
#include <sys/types.h>
 
28
#include <sys/stat.h>
 
29
#include <limits.h>
 
30
#include <fcntl.h>
 
31
 
 
32
#define TEST_PASSED     (0)
 
33
#define TEST_FAILED     (1)
 
34
#define TEST_ERROR      (2)
 
35
 
 
36
/*
 
37
 *  https://bugs.launchpad.net/ubuntu/+source/linux/+bug/524919
 
38
 *
 
39
 *  test that readlink() and lstat() size of a symlink are the same length
 
40
 */
 
41
int main(int argc, char **argv)
 
42
{
 
43
        char link[PATH_MAX];
 
44
        char buf[PATH_MAX];
 
45
        struct stat statbuf;
 
46
        int rc = TEST_PASSED;
 
47
        int fd;
 
48
        ssize_t n;
 
49
 
 
50
        if (argc < 2) {
 
51
                fprintf(stderr, "Usage: filename\n");
 
52
                exit(TEST_ERROR);
 
53
        }
 
54
 
 
55
        if ((fd = open(argv[1], O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
 
56
                fprintf(stderr, "cannot create %s : %s\n", argv[1],
 
57
                        strerror(errno));
 
58
                exit(TEST_ERROR);
 
59
        }
 
60
 
 
61
        if (close(fd) < 0) {
 
62
                fprintf(stderr, "close failed %s: %s\n", argv[1],
 
63
                        strerror(errno));
 
64
                rc = TEST_ERROR;
 
65
                goto tidy_file;
 
66
        }
 
67
 
 
68
        snprintf(link, sizeof(link), "%s-symlink", argv[1]);
 
69
        if (symlink(argv[1], link) < 0) {
 
70
                fprintf(stderr, "symlink failed %s: %s\n", argv[1],
 
71
                        strerror(errno));
 
72
                rc = TEST_ERROR;
 
73
                goto tidy_file;
 
74
        }
 
75
 
 
76
        n = readlink(link, buf, sizeof(buf));
 
77
        if (n < 0) {
 
78
                fprintf(stderr, "readlink failed %s: %s\n", argv[1],
 
79
                        strerror(errno));
 
80
                rc = TEST_ERROR;
 
81
                goto tidy_symlink;
 
82
        }
 
83
        if (lstat(link, &statbuf) < 0) {
 
84
                fprintf(stderr, "lstat failed %s: %s\n", argv[1],
 
85
                        strerror(errno));
 
86
                rc = TEST_ERROR;
 
87
                goto tidy_symlink;
 
88
        }
 
89
 
 
90
        /* Should be the same size */
 
91
        if (statbuf.st_size != n)
 
92
                rc = TEST_FAILED;
 
93
 
 
94
tidy_symlink:
 
95
        unlink(link);
 
96
tidy_file:
 
97
        unlink(argv[1]);
 
98
 
 
99
        exit(rc);
 
100
}