~mrooney/ecryptfs/nautilus-integration

« back to all changes in this revision

Viewing changes to src/testcases/truncate_no_open.c

  • Committer: mhalcrow@us.ibm.com
  • Date: 2007-11-06 22:56:01 UTC
  • Revision ID: git-v1:f8357de9d554b274497b5cce9db4347254b7e7eb
Initial import of eCryptfs filesystem userspace utilities (mount helper, daemon component,
etc.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <unistd.h>
 
3
#include <sys/types.h>
 
4
#include <sys/stat.h>
 
5
#include <fcntl.h>
 
6
#include <stdlib.h>
 
7
 
 
8
static char data[] = {
 
9
        'a', 'b', 'c', '\0',
 
10
        '\0', '\0', '\0', '\0',
 
11
        '\0', '\0', '\0', '\0',
 
12
        '\0', '\0', '\0', '\0',
 
13
        '\0', '\0', '\0', '\0',
 
14
        '\0', '\0', '\0', '\0',
 
15
        '\0', '\0', '\0', '\0',
 
16
        '\0', '\0', '\0', '\0'
 
17
};
 
18
#define DATA_SIZE 16
 
19
 
 
20
int main(int argc, char *argv[])
 
21
{
 
22
        int fd;
 
23
        struct stat *fstat;
 
24
        char *filename;
 
25
        ssize_t size;
 
26
#define BUF_SIZE 32
 
27
        char buf[BUF_SIZE];
 
28
        int i;
 
29
        int no_open = 0;
 
30
        int data_size;
 
31
        int rc = 0;
 
32
 
 
33
        if (argc < 2) {
 
34
                printf("Usage: test_truncate_no_open <path>\n");
 
35
                return 1;
 
36
        }
 
37
        if (argc == 3)
 
38
                no_open = 1;
 
39
        filename = argv[1];
 
40
        printf("Test filename: [%s]\n", filename);
 
41
        if (!no_open) {
 
42
                unlink(filename);
 
43
                if ((fd = open(filename, (O_WRONLY | O_CREAT | O_EXCL),
 
44
                               (S_IRUSR | S_IWUSR))) == -1) {
 
45
                        printf("Error attempting to create empty file [%s]\n",
 
46
                               filename);
 
47
                        rc = 1;
 
48
                        goto out;
 
49
                }
 
50
                size = write(fd , "abc" , 3);
 
51
                if (size < 0) {
 
52
                        printf("data not written to file");
 
53
                        rc = 1;
 
54
                        goto out;
 
55
                }
 
56
                close(fd);
 
57
                fstat = malloc(sizeof(struct stat));
 
58
                if (!fstat) {
 
59
                        printf("Unable to allocate memory\n");
 
60
                        rc = 1;
 
61
                        goto out;
 
62
                }
 
63
                stat(filename, fstat);
 
64
                if (fstat->st_size != 3) {
 
65
                        printf("test file should be 3 bytes\n");
 
66
                        printf("test file is [%d] bytes\n", fstat->st_size);
 
67
                        free(fstat);
 
68
                        if (unlink(filename)) {
 
69
                                printf("error deleting file\n");
 
70
                                rc = 1;
 
71
                                goto out;
 
72
                        }
 
73
                        rc = 1;
 
74
                        goto out;
 
75
                }
 
76
                free(fstat);
 
77
        }
 
78
        if (no_open)
 
79
                data_size = DATA_SIZE * 2;
 
80
        else
 
81
                data_size = DATA_SIZE;
 
82
        if ((rc = truncate(filename, data_size)) == -1) {
 
83
                printf("Error attempting to truncate [%s] to [%d] bytes\n",
 
84
                       filename, data_size);
 
85
                rc = 1;
 
86
                goto out;
 
87
        }
 
88
        rc = 0;
 
89
        fstat = malloc(sizeof(struct stat));
 
90
        if (!fstat) {
 
91
                printf("Unable to allocate memory\n");
 
92
                rc = 1;
 
93
                goto out;
 
94
        }
 
95
        stat(filename, fstat);
 
96
        if (fstat->st_size != data_size) {
 
97
                printf("test file should be [%d] bytes\n", data_size);
 
98
                printf("test file is [%d] bytes\n", fstat->st_size);
 
99
                free(fstat);
 
100
                if (unlink(filename)) {
 
101
                        printf("error deleting file\n");
 
102
                        rc = 1;
 
103
                        goto out;
 
104
                }
 
105
                rc = 1;
 
106
                goto out;
 
107
        }
 
108
        free(fstat);
 
109
        if ((fd = open(filename, O_RDONLY)) == -1) {
 
110
                printf("Error attempting to open file [%s]\n", filename);
 
111
                rc = 1;
 
112
                goto out;
 
113
        }
 
114
        size = read(fd, buf, BUF_SIZE);
 
115
        if (size != data_size) {
 
116
                printf("expected to read [%d] bytes; read [%d] bytes instead\n",
 
117
                       data_size, size);
 
118
                rc = 1;
 
119
                goto out;
 
120
        }
 
121
        close(fd);
 
122
        for (i = 0; i < data_size; i++)
 
123
                if (buf[i] != data[i]) {
 
124
                        printf("Data at offset [%d] does not match. Expected "
 
125
                               "data = [0x%.2x]; read data = [0x%.2x]\n", i,
 
126
                               data[i], buf[i]);
 
127
                        rc = 1;
 
128
                        goto out;
 
129
                }
 
130
out:
 
131
        if (rc)
 
132
                printf("truncate_no_open test failed; rc = [%d]\n", rc);
 
133
        else
 
134
                printf("truncate_no_open test succeeded\n");
 
135
        return rc;
 
136
}