/ Check-in [438c5c52]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add unix-only tests to check that the "unzip" program can unpack archives generated by the zipfile extension.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 438c5c5237a801ae78809bf324bb9251fb50250addfc1f8e36659442b0e26ab6
User & Date: dan 2018-01-29 19:47:32
Context
2018-01-30
14:07
Prevent users from creating zipfile() virtual tables without an argument. check-in: 81fdbe0c user: dan tags: trunk
2018-01-29
19:47
Add unix-only tests to check that the "unzip" program can unpack archives generated by the zipfile extension. check-in: 438c5c52 user: dan tags: trunk
18:41
Add aggregate function zipfile() to the zipfile extension. For composing new zip archives in memory. check-in: e364eeac user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/misc/fileio.c.

   487    487     int i;
   488    488     for(i=0; i<=pCur->iLvl; i++){
   489    489       FsdirLevel *pLvl = &pCur->aLvl[i];
   490    490       if( pLvl->pDir ) closedir(pLvl->pDir);
   491    491       sqlite3_free(pLvl->zDir);
   492    492     }
   493    493     sqlite3_free(pCur->zPath);
          494  +  sqlite3_free(pCur->aLvl);
   494    495     pCur->aLvl = 0;
   495    496     pCur->zPath = 0;
   496    497     pCur->zBase = 0;
   497    498     pCur->nBase = 0;
   498    499     pCur->iLvl = -1;
   499    500     pCur->iRowid = 1;
   500    501   }
................................................................................
   502    503   /*
   503    504   ** Destructor for an fsdir_cursor.
   504    505   */
   505    506   static int fsdirClose(sqlite3_vtab_cursor *cur){
   506    507     fsdir_cursor *pCur = (fsdir_cursor*)cur;
   507    508   
   508    509     fsdirResetCursor(pCur);
   509         -  sqlite3_free(pCur->aLvl);
   510    510     sqlite3_free(pCur);
   511    511     return SQLITE_OK;
   512    512   }
   513    513   
   514    514   /*
   515    515   ** Set the error message for the virtual table associated with cursor
   516    516   ** pCur to the results of vprintf(zFmt, ...).

Changes to test/zipfile.test.

    25     25   proc readfile {f} {
    26     26     set fd [open $f]
    27     27     fconfigure $fd -translation binary -encoding binary
    28     28     set data [read $fd]
    29     29     close $fd
    30     30     set data
    31     31   }
           32  +
           33  +if {$::tcl_platform(platform)=="unix" && [catch {exec unzip}]==0} {
           34  +  set ::UNZIP 1
           35  +  load_static_extension db fileio
           36  +  proc do_unzip {file} {
           37  +    forcedelete test_unzip
           38  +    file mkdir test_unzip
           39  +    exec unzip -d test_unzip $file
           40  +  
           41  +    set res [db eval { 
           42  +      SELECT replace(name,'test_unzip/',''),mode,mtime,data 
           43  +      FROM fsdir('test_unzip') 
           44  +      WHERE name!='test_unzip'
           45  +      ORDER BY name
           46  +    }]
           47  +    set res
           48  +  }
           49  +}
    32     50   
    33     51   # Argument $file is the name of a zip archive on disk. This function
    34     52   # executes test cases to check that the results of each of the following 
    35     53   # are the same:
    36     54   #
    37     55   #         SELECT * FROM zipfile($file)
    38     56   #         SELECT * FROM zipfile( readfile($file) )
................................................................................
    54     72     set r2 [db eval $q2]
    55     73     set r3 [db eval $q3]
    56     74     #puts $r1
    57     75     #puts $r2
    58     76     #puts $r3
    59     77   
    60     78     uplevel [list do_test $tn.1 [list set {} $r2] $r1]
    61         -  uplevel [list do_test $tn.1 [list set {} $r3] $r1]
           79  +  uplevel [list do_test $tn.2 [list set {} $r3] $r1]
           80  +}
           81  +
           82  +# Argument $file is a zip file on disk. This command runs tests to:
           83  +#
           84  +#   1. Unpack the archive with unix command [unzip] and compare the 
           85  +#      results to reading the same archive using the zipfile() table
           86  +#      valued function.
           87  +#
           88  +#   2. Creates a new archive with the same contents using the zipfile()
           89  +#      aggregate function as follows:
           90  +#
           91  +#      SELECT writefile('test_unzip.zip',
           92  +#          ( SELECT zipfile(name,mode,mtime,data,method) FROM zipfile($file) )
           93  +#      );
           94  +#
           95  +#      Then tests that unpacking the new archive using [unzip] produces
           96  +#      the same results as in (1).
           97  +#
           98  +proc do_unzip_test {tn file} {
           99  +  if {[info vars ::UNZIP]==""} { return }
          100  +  db func sss strip_slash
          101  +
          102  +  db eval {
          103  +    SELECT writefile('test_unzip.zip',
          104  +        ( SELECT zipfile(name,mode,mtime,data,method) FROM zipfile($file) )
          105  +    );
          106  +  }
          107  +
          108  +  set r1 [db eval { 
          109  +    SELECT sss(name),mode,mtime,data FROM zipfile($file) ORDER BY name
          110  +  }]
          111  +  set r2 [do_unzip $file]
          112  +  set r3 [do_unzip test_unzip.zip]
          113  +
          114  +  uplevel [list do_test $tn.1 [list set {} $r2] $r1]
          115  +  uplevel [list do_test $tn.2 [list set {} $r3] $r1]
          116  +}
          117  +proc strip_slash {in} { regsub {/$} $in {} }
          118  +
          119  +proc do_zip_tests {tn file} {
          120  +  uplevel do_zipfile_blob_test $tn.1 $file
          121  +  uplevel do_unzip_test $tn.2 $file
    62    122   }
    63    123   
    64    124   forcedelete test.zip
    65    125   do_execsql_test 1.0 {
    66    126     CREATE VIRTUAL TABLE temp.zz USING zipfile('test.zip');
    67    127     PRAGMA table_info(zz);
    68    128   } {
................................................................................
    95    155   
    96    156   do_execsql_test 1.2 {
    97    157     SELECT name, mtime, data FROM zipfile('test.zip')
    98    158   } {
    99    159     f.txt 1000000000 abcde 
   100    160     g.txt 1000000002 12345
   101    161   }
   102         -do_zipfile_blob_test 1.2.1 test.zip
          162  +do_zip_tests 1.2a test.zip
   103    163   
   104    164   do_execsql_test 1.3 {
   105    165     INSERT INTO zz(name, mode, mtime, data) VALUES('h.txt', 
   106    166       '-rw-r--r--', 1000000004, 'aaaaaaaaaabbbbbbbbbb'
   107    167     );
   108    168   }
   109         -do_zipfile_blob_test 1.3.1 test.zip
          169  +do_zip_tests 1.3a test.zip
   110    170   
   111    171   do_execsql_test 1.4 {
   112    172     SELECT name, mtime, data, method FROM zipfile('test.zip');
   113    173   } {
   114    174     f.txt 1000000000 abcde 0
   115    175     g.txt 1000000002 12345 0
   116    176     h.txt 1000000004 aaaaaaaaaabbbbbbbbbb 8
................................................................................
   149    209   do_execsql_test 1.6.1 {
   150    210     SELECT name, mode, mtime, data, method FROM zipfile('test.zip');
   151    211   } {
   152    212     f.txt 33188 1000000000 abcde 0
   153    213     h.txt 33188 1000000004 aaaaaaaaaabbbbbbbbbb 8
   154    214     i.txt 33188 1000000006 zxcvb 0
   155    215   }
   156         -do_zipfile_blob_test 1.6.1a test.zip
          216  +do_zip_tests 1.6.1a test.zip
   157    217   
   158    218   do_execsql_test 1.6.2 {
   159    219     UPDATE zz SET mtime=4 WHERE name='i.txt';
   160    220     SELECT name, mode, mtime, data, method FROM zipfile('test.zip');
   161    221   } {
   162    222     f.txt 33188 1000000000 abcde 0
   163    223     h.txt 33188 1000000004 aaaaaaaaaabbbbbbbbbb 8
................................................................................
   168    228     UPDATE zz SET mode='-rw-r--r-x' WHERE name='h.txt';
   169    229     SELECT name, mode, mtime, data, method FROM zipfile('test.zip');
   170    230   } {
   171    231     f.txt 33188 1000000000 abcde 0
   172    232     h.txt 33189 1000000004 aaaaaaaaaabbbbbbbbbb 8
   173    233     i.txt 33188 4 zxcvb 0
   174    234   }
   175         -do_zipfile_blob_test 1.6.3a test.zip
          235  +do_zip_tests 1.6.3a test.zip
   176    236   
   177    237   do_execsql_test 1.6.4 {
   178    238     UPDATE zz SET name = 'blue.txt' WHERE name='f.txt';
   179    239     SELECT name, mode, mtime, data, method FROM zipfile('test.zip');
   180    240   } {
   181    241     blue.txt 33188 1000000000 abcde 0
   182    242     h.txt 33189 1000000004 aaaaaaaaaabbbbbbbbbb 8
   183    243     i.txt 33188 4 zxcvb 0
   184    244   }
   185         -do_zipfile_blob_test 1.6.4a test.zip
          245  +do_zip_tests 1.6.4a test.zip
   186    246   
   187    247   do_execsql_test 1.6.5 {
   188    248     UPDATE zz SET data = 'edcba' WHERE name='blue.txt';
   189    249     SELECT name, mode, mtime, data, method FROM zipfile('test.zip');
   190    250   } {
   191    251     blue.txt 33188 1000000000 edcba 0
   192    252     h.txt 33189 1000000004 aaaaaaaaaabbbbbbbbbb 8
................................................................................
   213    273     i.txt 33188 4 zxcvb 0
   214    274   }
   215    275   
   216    276   #-------------------------------------------------------------------------
   217    277   db close
   218    278   forcedelete test.zip
   219    279   reset_db
          280  +load_static_extension db fileio
   220    281   load_static_extension db zipfile
   221    282   do_execsql_test 2.1 {
   222    283     CREATE VIRTUAL TABLE zzz USING zipfile('test.zip');
   223    284     INSERT INTO zzz(name, mode) VALUES('dirname', 'drwxr-xr-x');
   224    285     SELECT name, mode, data FROM zzz;
   225    286   } {dirname/ 16877 {}}
   226    287   do_execsql_test 2.2 {
................................................................................
   239    300   do_execsql_test 2.4 {
   240    301     SELECT name, mode, data FROM zzz;
   241    302   } {
   242    303     dirname3/ 16877 {}
   243    304     dirname2/ 16877 {}
   244    305     dirname2/file1.txt 33188 abcdefghijklmnop
   245    306   }
   246         -do_zipfile_blob_test 2.4.1 test.zip
          307  +do_zip_tests 2.4a test.zip
   247    308   
   248    309   # If on unix, check that the [unzip] utility can unpack our archive.
   249    310   #
   250    311   if {$::tcl_platform(platform)=="unix"} {
   251    312     do_test 2.5.1 {
   252    313       forcedelete dirname
   253    314       forcedelete dirname2