LCOV - code coverage report
Current view: directory - src/shared/gio - gio_test_base.cc (source / functions) Found Hit Coverage
Test: coverage.lcov Lines: 124 124 100.0 %
Date: 2014-06-18 Functions: 0 0 -

       1                 : /*
       2                 :  * Copyright 2010 The Native Client Authors. All rights reserved.
       3                 :  * Use of this source code is governed by a BSD-style license that can be
       4                 :  * found in the LICENSE file.
       5                 :  */
       6                 : 
       7                 : #include <stdio.h>
       8                 : #include "native_client/src/shared/gio/gio_test_base.h"
       9                 : #include "gtest/gtest.h"
      10                 : 
      11                 : #define EXPECT_RETCODE(_E_, _R_)                                 \
      12                 :   EXPECT_EQ(_E_, _R_);
      13                 : 
      14                 : /* Uncomment and connect to above macro for debugging.
      15                 :     if (0 > _R_) {                                               \
      16                 :       perror("Bad return code");                                 \
      17                 :     }
      18                 : */
      19                 : 
      20             399 : char GioExpectedCharAt(char initial_char, int file_pos) {
      21             399 :   return static_cast<char>(initial_char + file_pos);
      22                 : }
      23                 : 
      24               2 : void GioInitTestMemFile(char* mem_buff, char initial_char, int in_size) {
      25             132 :   for (int i = 0; i < in_size; ++i)
      26              64 :     mem_buff[i] = GioExpectedCharAt(initial_char, i);
      27               2 : }
      28                 : 
      29               5 : void GioReadTestWithOffset(struct Gio* my_file,
      30               5 :                            char initial_char) {
      31               5 :   char* out_buffer;
      32               5 :   int out_size = 16;
      33               5 :   ssize_t ret_code;
      34                 : 
      35               5 :   out_buffer = reinterpret_cast<char*>(malloc(out_size));
      36                 : 
      37                 :   // mf_curpos = 0, 32 left, read 16
      38               5 :   ret_code = my_file->vtbl->Read(my_file, out_buffer, 16);
      39              20 :   EXPECT_RETCODE(16, ret_code);
      40             170 :   for (int i = 0; i < 16; ++i)
      41             320 :     EXPECT_EQ(GioExpectedCharAt(initial_char, i), out_buffer[i]);
      42                 : 
      43                 :   // mf_curpos = 16, 16 left, read 10
      44               5 :   ret_code = my_file->vtbl->Read(my_file, out_buffer, 10);
      45              20 :   EXPECT_RETCODE(10, ret_code);
      46             110 :   for (int i = 0; i < 10; ++i)
      47             200 :     EXPECT_EQ(GioExpectedCharAt(initial_char, i + 16), out_buffer[i]);
      48                 :   // residual value after idx 10
      49              20 :   EXPECT_EQ(GioExpectedCharAt(initial_char, 10), out_buffer[10]);
      50                 : 
      51                 :   // mf_curpos = 26, 6 left, read 8
      52               5 :   ret_code = my_file->vtbl->Read(my_file, out_buffer, 8);
      53              20 :   EXPECT_RETCODE(6, ret_code);
      54              70 :   for (int i = 0; i < 6; ++i)
      55             120 :     EXPECT_EQ(GioExpectedCharAt(initial_char, i + 26), out_buffer[i]);
      56                 :   // residual value after idx 6
      57              20 :   EXPECT_EQ(GioExpectedCharAt(initial_char, 16 + 6), out_buffer[6]);
      58                 : 
      59                 :   // mf_curpos = 32, 0 left, read 16
      60               5 :   ret_code = my_file->vtbl->Read(my_file, out_buffer, 16);
      61              20 :   EXPECT_EQ(0, ret_code);
      62                 : 
      63               5 :   free(out_buffer);
      64               5 : }
      65                 : 
      66                 : /** Should be given a scratch file that can be written to without worry. */
      67               3 : void GioWriteTest(struct Gio* my_file,
      68               3 :                   bool fixed_length) {
      69               3 :   char* in_buffer;
      70               3 :   int in_size = 44;
      71               3 :   char initial_char = 'A';
      72               3 :   char out_char;
      73               3 :   ssize_t ret_code;
      74                 : 
      75               3 :   in_buffer = reinterpret_cast<char*>(malloc(in_size));
      76             270 :   for (int i = 0; i < in_size; ++i)
      77             132 :     in_buffer[i] = GioExpectedCharAt(initial_char, i);
      78                 : 
      79                 :   // mf_curpos = 0, 64 left, write 44
      80               3 :   ret_code = my_file->vtbl->Write(my_file, in_buffer, in_size);
      81              12 :   EXPECT_RETCODE(in_size, ret_code);
      82              12 :   EXPECT_EQ(0, my_file->vtbl->Flush(my_file));
      83                 : 
      84               3 :   ret_code = my_file->vtbl->Seek(my_file, -1, SEEK_CUR);
      85              12 :   EXPECT_RETCODE(in_size - 1, ret_code);
      86               3 :   ret_code = my_file->vtbl->Read(my_file, &out_char, 1);
      87              12 :   EXPECT_EQ(1, ret_code);
      88              12 :   EXPECT_EQ(GioExpectedCharAt(initial_char, in_size - 1), out_char);
      89                 : 
      90                 :   // Windows *requires* hitting EOF before writing more.
      91                 :   // See _flsbuf in _flsbuf.c.
      92               3 :   if (!fixed_length) {
      93               2 :     ret_code = my_file->vtbl->Seek(my_file, 0, SEEK_END);
      94               8 :     EXPECT_EQ(in_size, ret_code);
      95               2 :   }
      96                 : 
      97                 :   // mf_curpos = 44, 20 left, write 10
      98               3 :   ret_code = my_file->vtbl->Write(my_file, in_buffer, 10);
      99              12 :   EXPECT_RETCODE(10, ret_code);
     100              12 :   EXPECT_EQ(0, my_file->vtbl->Flush(my_file));
     101                 : 
     102                 :   // Sample a couple of other spots
     103                 : 
     104                 :   // seek mf_curpos = 40
     105               3 :   ret_code = my_file->vtbl->Seek(my_file, in_size - 4, SEEK_SET);
     106              12 :   EXPECT_RETCODE(in_size - 4, ret_code);
     107               3 :   ret_code = my_file->vtbl->Read(my_file, &out_char, 1);
     108              12 :   EXPECT_EQ(1, ret_code);
     109              12 :   EXPECT_EQ(GioExpectedCharAt(initial_char, in_size - 4), out_char);
     110                 : 
     111                 :   // mf_curpose = 41, advance by 12 and read to get back to 54
     112               3 :   ret_code = my_file->vtbl->Seek(my_file, 12, SEEK_CUR);
     113              12 :   EXPECT_RETCODE(in_size - 3 + 12, ret_code);
     114               3 :   ret_code = my_file->vtbl->Read(my_file, &out_char, 1);
     115              12 :   EXPECT_EQ(1, ret_code);
     116              12 :   EXPECT_EQ(GioExpectedCharAt(initial_char, 9), out_char);
     117                 : 
     118                 :   // Back at the mf_curpos = 54
     119                 : 
     120               3 :   if (fixed_length) {
     121                 :     // mf_curpos = 54, 10 left, write 20
     122               1 :     ret_code = my_file->vtbl->Write(my_file, in_buffer, 20);
     123               4 :     EXPECT_RETCODE(10, ret_code);
     124                 : 
     125               1 :     my_file->vtbl->Seek(my_file, -1, SEEK_CUR);
     126               1 :     my_file->vtbl->Read(my_file, &out_char, 1);
     127               4 :     EXPECT_EQ(GioExpectedCharAt(initial_char, 9), out_char);
     128                 : 
     129                 :     // mf_curpos = 64, 0 left, write 20
     130               1 :     ret_code = my_file->vtbl->Write(my_file, in_buffer, 20);
     131               4 :     EXPECT_RETCODE(0, ret_code);
     132               1 :   }
     133                 : 
     134               3 :   free(in_buffer);
     135               3 : }
     136                 : 
     137               5 : void GioSeekTestWithOffset(struct Gio* my_file,
     138               5 :                            char initial_char,
     139               5 :                            bool wrap_err) {
     140               5 :   char out_char;
     141               5 :   ssize_t ret_code;
     142                 : 
     143                 :   // mf_curpos = 0
     144               5 :   ret_code = my_file->vtbl->Seek(my_file, 15, SEEK_SET);
     145              20 :   EXPECT_RETCODE(15, ret_code);
     146                 : 
     147               5 :   ret_code = my_file->vtbl->Read(my_file, &out_char, 1);
     148              20 :   EXPECT_RETCODE(1, ret_code);
     149              20 :   EXPECT_EQ(GioExpectedCharAt(initial_char, 15), out_char);
     150                 : 
     151                 :   // mf_curpos = 16
     152               5 :   ret_code = my_file->vtbl->Seek(my_file, 4, SEEK_CUR);
     153              20 :   EXPECT_RETCODE(20, ret_code);
     154                 : 
     155               5 :   ret_code = my_file->vtbl->Read(my_file, &out_char, 1);
     156              20 :   EXPECT_RETCODE(1, ret_code);
     157              20 :   EXPECT_EQ(GioExpectedCharAt(initial_char, 20), out_char);
     158                 : 
     159                 :   // mf_curpos = 21
     160               5 :   ret_code = my_file->vtbl->Seek(my_file, -4, SEEK_CUR);
     161              20 :   EXPECT_RETCODE(17, ret_code);
     162                 : 
     163               5 :   ret_code = my_file->vtbl->Read(my_file, &out_char, 1);
     164              20 :   EXPECT_RETCODE(1, ret_code);
     165              20 :   EXPECT_EQ(GioExpectedCharAt(initial_char, 17), out_char);
     166                 : 
     167                 :   // mf_curpos = 17
     168               5 :   ret_code = my_file->vtbl->Seek(my_file, -4, SEEK_END);
     169              20 :   EXPECT_RETCODE(28, ret_code);
     170                 : 
     171               5 :   my_file->vtbl->Read(my_file, &out_char, 1);
     172              20 :   EXPECT_EQ(GioExpectedCharAt(initial_char, 28), out_char);
     173                 : 
     174                 : 
     175                 :   // At this point we try to seek out of bounds in various ways.
     176               5 :   if (wrap_err) {
     177               1 :     const int BAD_SEEK_WHENCE = SEEK_END + 3;
     178                 : 
     179                 :     // mf_curpos = 29
     180               1 :     ret_code = my_file->vtbl->Seek(my_file, 4, SEEK_END);
     181               4 :     EXPECT_RETCODE(-1, ret_code);
     182                 : 
     183               1 :     my_file->vtbl->Read(my_file, &out_char, 1);
     184               4 :     EXPECT_EQ(GioExpectedCharAt(initial_char, 29), out_char);
     185                 : 
     186                 :     // mf_curpos = 30
     187               1 :     ret_code = my_file->vtbl->Seek(my_file, 4, BAD_SEEK_WHENCE);
     188               4 :     EXPECT_RETCODE(-1, ret_code);
     189                 : 
     190               1 :     my_file->vtbl->Read(my_file, &out_char, 1);
     191               4 :     EXPECT_EQ(GioExpectedCharAt(initial_char, 30), out_char);
     192                 : 
     193                 :     // mf_curpos = 31
     194               1 :     ret_code = my_file->vtbl->Seek(my_file, -5, SEEK_SET);
     195               4 :     EXPECT_RETCODE(-1, ret_code);
     196                 : 
     197               1 :     my_file->vtbl->Read(my_file, &out_char, 1);
     198               4 :     EXPECT_EQ(GioExpectedCharAt(initial_char, 31), out_char);
     199               1 :   } else {
     200                 :     // Not testing seek past end and then READING.
     201                 : 
     202                 :     // mf_curpos = 29
     203               4 :     ret_code = my_file->vtbl->Seek(my_file, -5, SEEK_SET);
     204              16 :     EXPECT_RETCODE(-1, ret_code);
     205                 : 
     206                 :     /* Different behavior on Windows vs Posix.
     207                 :        It appears that file_pos after a bad seek is undefined.
     208                 :        TODO(jvoung): Once gio library makes seek behavior standard,
     209                 :        we can re-enable this test. See:
     210                 :        http://code.google.com/p/nativeclient/issues/detail?id=850
     211                 :     my_file->vtbl->Read(my_file, &out_char, 1);
     212                 :     EXPECT_EQ(GioExpectedCharAt(initial_char, 29), out_char);
     213                 :     */
     214                 :   }
     215               5 : }
     216                 : 
     217              13 : void GioCloseTest(struct Gio* my_file) {
     218              13 :   int ret_code;
     219              13 :   ret_code = my_file->vtbl->Close(my_file);
     220              52 :   EXPECT_RETCODE(0, ret_code);
     221              13 :   my_file->vtbl->Dtor(my_file);
     222              13 : }

Generated by: LCOV version 1.7