summaryrefslogtreecommitdiff
path: root/std/archive/tar_test.ts
blob: 1bac623b097818c1983bdc3490e4456e0b929b5a (plain)
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
/**
 * Tar test
 *
 * **test summary**
 * - create a tar archive in memory containing output.txt and dir/tar.ts.
 * - read and deflate a tar archive containing output.txt
 *
 * **to run this test**
 * deno run --allow-read archive/tar_test.ts
 */
import { assertEquals } from "../testing/asserts.ts";

import { resolve } from "../path/mod.ts";
import { Tar, Untar } from "./tar.ts";

const filePath = resolve("archive", "testdata", "example.txt");

Deno.test(async function createTarArchive(): Promise<void> {
  // initialize
  const tar = new Tar();

  // put data on memory
  const content = new TextEncoder().encode("hello tar world!");
  await tar.append("output.txt", {
    reader: new Deno.Buffer(content),
    contentSize: content.byteLength
  });

  // put a file
  await tar.append("dir/tar.ts", { filePath });

  // write tar data to a buffer
  const writer = new Deno.Buffer(),
    wrote = await Deno.copy(writer, tar.getReader());

  /**
   * 3072 = 512 (header) + 512 (content) + 512 (header) + 512 (content)
   *       + 1024 (footer)
   */
  assertEquals(wrote, 3072);
});

Deno.test(async function deflateTarArchive(): Promise<void> {
  const fileName = "output.txt";
  const text = "hello tar world!";

  // create a tar archive
  const tar = new Tar();
  const content = new TextEncoder().encode(text);
  await tar.append(fileName, {
    reader: new Deno.Buffer(content),
    contentSize: content.byteLength
  });

  // read data from a tar archive
  const untar = new Untar(tar.getReader());
  const buf = new Deno.Buffer();
  const result = await untar.extract(buf);
  const untarText = new TextDecoder("utf-8").decode(buf.bytes());

  // tests
  assertEquals(result.fileName, fileName);
  assertEquals(untarText, text);
});

Deno.test(async function appendFileWithLongNameToTarArchive(): Promise<void> {
  // 9 * 15 + 13 = 148 bytes
  const fileName = new Array(10).join("long-file-name/") + "file-name.txt";
  const text = "hello tar world!";

  // create a tar archive
  const tar = new Tar();
  const content = new TextEncoder().encode(text);
  await tar.append(fileName, {
    reader: new Deno.Buffer(content),
    contentSize: content.byteLength
  });

  // read data from a tar archive
  const untar = new Untar(tar.getReader());
  const buf = new Deno.Buffer();
  const result = await untar.extract(buf);
  const untarText = new TextDecoder("utf-8").decode(buf.bytes());

  // tests
  assertEquals(result.fileName, fileName);
  assertEquals(untarText, text);
});