File size: 7,768 Bytes
78d2150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "../../unity/unity.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>

/* The function under test is static in the included program source and is
   available here because this test file is included into that translation unit:
   static bool head_bytes (char const *filename, int fd, uintmax_t bytes_to_write);
*/

/* Unity fixtures */
void setUp(void) {
  /* no-op */
}
void tearDown(void) {
  /* no-op */
}

/* Helper: create a temporary file with given content.
   Returns an open fd positioned at start, or -1 on failure. */
static int create_temp_file_with_content(const void *data, size_t len)
{
  char tmpl[] = "/tmp/head_bytes_test_XXXXXX";
  int fd = mkstemp(tmpl);
  if (fd < 0) {
    return -1;
  }

  /* Unlink the file so it is removed automatically. */
  unlink(tmpl);

  /* Write content if any. */
  size_t off = 0;
  while (off < len) {
    ssize_t w = write(fd, (const char*)data + off, len - off);
    if (w < 0) {
      close(fd);
      return -1;
    }
    off += (size_t)w;
  }

  /* Rewind to start for reading. */
  if (lseek(fd, 0, SEEK_SET) < 0) {
    close(fd);
    return -1;
  }

  return fd;
}

/* Helper: capture stdout output of head_bytes while calling it.
   IMPORTANT: No Unity asserts in this function while stdout is redirected. */
typedef struct {
  char *data;
  size_t len;
  bool func_ret;
} CaptureResult;

static CaptureResult capture_head_bytes_output(int fd, uintmax_t n_bytes, const char *filename)
{
  CaptureResult out = { NULL, 0, false };
  int saved_stdout = dup(STDOUT_FILENO);
  if (saved_stdout < 0) {
    return out;
  }

  int pipefd[2];
  if (pipe(pipefd) != 0) {
    close(saved_stdout);
    return out;
  }

  fflush(stdout);

  /* Redirect stdout to pipe write end */
  if (dup2(pipefd[1], STDOUT_FILENO) < 0) {
    close(saved_stdout);
    close(pipefd[0]);
    close(pipefd[1]);
    return out;
  }
  /* Close our copy of the write end; stdout now refers to it. */
  close(pipefd[1]);

  /* Call the function under test; do not use Unity asserts here. */
  bool r = head_bytes(filename, fd, n_bytes);

  /* Ensure all data is flushed to the pipe. */
  fflush(stdout);

  /* Restore stdout; this closes the old fd 1 (the pipe write end). */
  dup2(saved_stdout, STDOUT_FILENO);
  close(saved_stdout);

  /* Read everything from the pipe read end. */
  size_t cap = 1024;
  char *buf = (char *)malloc(cap);
  if (!buf) {
    close(pipefd[0]);
    out.func_ret = r;
    return out;
  }
  size_t total = 0;
  while (1) {
    if (total == cap) {
      size_t new_cap = cap * 2;
      char *nb = (char *)realloc(buf, new_cap);
      if (!nb) {
        free(buf);
        close(pipefd[0]);
        out.func_ret = r;
        return out;
      }
      buf = nb;
      cap = new_cap;
    }
    ssize_t nr = read(pipefd[0], buf + total, cap - total);
    if (nr < 0) {
      free(buf);
      close(pipefd[0]);
      out.func_ret = r;
      return out;
    }
    if (nr == 0) {
      break;
    }
    total += (size_t)nr;
  }
  close(pipefd[0]);

  out.data = buf;
  out.len = total;
  out.func_ret = r;
  return out;
}

/* Tests */

static void free_capture(CaptureResult *c) {
  if (c && c->data) {
    free(c->data);
    c->data = NULL;
  }
}

/* 1) Request 0 bytes: no output, success */
void test_head_bytes_zero_bytes(void)
{
  const char *content = "abcdef";
  int fd = create_temp_file_with_content(content, strlen(content));
  TEST_ASSERT_MESSAGE(fd >= 0, "Failed to create temp file");

  CaptureResult cap = capture_head_bytes_output(fd, 0, "temp-zero");
  close(fd);

  TEST_ASSERT_TRUE(cap.func_ret);
  TEST_ASSERT_EQUAL_UINT64((uint64_t)0, (uint64_t)cap.len);
  free_capture(&cap);
}

/* 2) Request less than file size: partial output, success */
void test_head_bytes_less_than_file_size(void)
{
  const char *content = "Hello, Unity!";
  size_t content_len = strlen(content);
  int fd = create_temp_file_with_content(content, content_len);
  TEST_ASSERT_MESSAGE(fd >= 0, "Failed to create temp file");

  uintmax_t req = 5;
  CaptureResult cap = capture_head_bytes_output(fd, req, "temp-partial");
  close(fd);

  TEST_ASSERT_TRUE(cap.func_ret);
  TEST_ASSERT_EQUAL_UINT64((uint64_t)req, (uint64_t)cap.len);
  TEST_ASSERT_EQUAL_INT(0, memcmp(content, cap.data, (size_t)req));
  free_capture(&cap);
}

/* 3) Request exactly file size: full output, success */
void test_head_bytes_exact_file_size(void)
{
  const char *content = "exactsize";
  size_t content_len = strlen(content);
  int fd = create_temp_file_with_content(content, content_len);
  TEST_ASSERT_MESSAGE(fd >= 0, "Failed to create temp file");

  CaptureResult cap = capture_head_bytes_output(fd, content_len, "temp-exact");
  close(fd);

  TEST_ASSERT_TRUE(cap.func_ret);
  TEST_ASSERT_EQUAL_UINT64((uint64_t)content_len, (uint64_t)cap.len);
  TEST_ASSERT_EQUAL_INT(0, memcmp(content, cap.data, content_len));
  free_capture(&cap);
}

/* 4) Request more than file size: output full file, success (unexpected EOF tolerated) */
void test_head_bytes_more_than_file_size(void)
{
  const char *content = "short";
  size_t content_len = strlen(content);
  int fd = create_temp_file_with_content(content, content_len);
  TEST_ASSERT_MESSAGE(fd >= 0, "Failed to create temp file");

  uintmax_t req = content_len + 10;
  CaptureResult cap = capture_head_bytes_output(fd, req, "temp-more");
  close(fd);

  TEST_ASSERT_TRUE(cap.func_ret);
  TEST_ASSERT_EQUAL_UINT64((uint64_t)content_len, (uint64_t)cap.len);
  TEST_ASSERT_EQUAL_INT(0, memcmp(content, cap.data, content_len));
  free_capture(&cap);
}

/* 5) Empty file with nonzero request: no output, success */
void test_head_bytes_empty_file_request_nonzero(void)
{
  int fd = create_temp_file_with_content("", 0);
  TEST_ASSERT_MESSAGE(fd >= 0, "Failed to create temp file");

  CaptureResult cap = capture_head_bytes_output(fd, 100, "temp-empty");
  close(fd);

  TEST_ASSERT_TRUE(cap.func_ret);
  TEST_ASSERT_EQUAL_UINT64((uint64_t)0, (uint64_t)cap.len);
  free_capture(&cap);
}

/* 6) Multi-buffer read: request > BUFSIZ ensuring multiple reads/writes */
void test_head_bytes_large_multi_buffer(void)
{
  size_t biglen = (size_t)(3 * BUFSIZ + 123);
  char *bigbuf = (char *)malloc(biglen);
  TEST_ASSERT_NOT_NULL_MESSAGE(bigbuf, "Allocation failed for big buffer");
  /* Fill with a pattern */
  for (size_t i = 0; i < biglen; i++) {
    bigbuf[i] = (char)('A' + (int)(i % 26));
  }

  int fd = create_temp_file_with_content(bigbuf, biglen);
  TEST_ASSERT_MESSAGE(fd >= 0, "Failed to create temp file");

  uintmax_t req = (uintmax_t)(2 * BUFSIZ + 10);
  CaptureResult cap = capture_head_bytes_output(fd, req, "temp-large");
  close(fd);

  TEST_ASSERT_TRUE(cap.func_ret);
  TEST_ASSERT_EQUAL_UINT64((uint64_t)req, (uint64_t)cap.len);
  TEST_ASSERT_EQUAL_INT(0, memcmp(bigbuf, cap.data, (size_t)req));

  free_capture(&cap);
  free(bigbuf);
}

/* 7) Read error: invalid fd should return false and produce no stdout output */
void test_head_bytes_read_error_returns_false_and_no_output(void)
{
  int invalid_fd = -1;
  CaptureResult cap = capture_head_bytes_output(invalid_fd, 100, "invalid-fd");
  TEST_ASSERT_FALSE(cap.func_ret);
  TEST_ASSERT_EQUAL_UINT64((uint64_t)0, (uint64_t)cap.len);
  free_capture(&cap);
}

int main(void)
{
  UNITY_BEGIN();
  RUN_TEST(test_head_bytes_zero_bytes);
  RUN_TEST(test_head_bytes_less_than_file_size);
  RUN_TEST(test_head_bytes_exact_file_size);
  RUN_TEST(test_head_bytes_more_than_file_size);
  RUN_TEST(test_head_bytes_empty_file_request_nonzero);
  RUN_TEST(test_head_bytes_large_multi_buffer);
  RUN_TEST(test_head_bytes_read_error_returns_false_and_no_output);
  return UNITY_END();
}