1 /*
2 * Copyright 2020, Andrew Lindesay <apl@lindesay.co.nz>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5 #include "TarArchiveServiceTest.h"
6
7 #include <stdlib.h>
8
9 #include <AutoDeleter.h>
10 #include <File.h>
11 #include <String.h>
12
13 #include <cppunit/TestCaller.h>
14 #include <cppunit/TestSuite.h>
15
16 #include "TarArchiveHeader.h"
17 #include "TarArchiveService.h"
18
19 #include "TarArchiveServiceTestSample.h"
20
21
TarArchiveServiceTest()22 TarArchiveServiceTest::TarArchiveServiceTest()
23 {
24 }
25
26
~TarArchiveServiceTest()27 TarArchiveServiceTest::~TarArchiveServiceTest()
28 {
29 }
30
31
32 void
TestReadHeader()33 TarArchiveServiceTest::TestReadHeader()
34 {
35 BString tarPath;
36 if (CreateSampleFile(tarPath) != B_OK) {
37 printf("! unable to setup the sample tar --> exit");
38 exit(1);
39 }
40
41 BFile *tarFile = new BFile(tarPath, O_RDONLY);
42 tarFile->Seek(2048, SEEK_SET);
43 // known offset in the same data
44
45 TarArchiveHeader header;
46
47 // ----------------------
48 status_t result = TarArchiveService::GetEntry(*tarFile, header);
49 // ----------------------
50
51 CPPUNIT_ASSERT_EQUAL(B_OK, result);
52 CPPUNIT_ASSERT_EQUAL(BString("hicn/somepkg/16.png"), header.FileName());
53 CPPUNIT_ASSERT_EQUAL(657, header.Length());
54 CPPUNIT_ASSERT_EQUAL(TAR_FILE_TYPE_NORMAL, header.FileType());
55
56 delete tarFile;
57
58 DeleteSampleFile(tarPath);
59 }
60
61
62 /*! This method will store the tar file sample into a temporary file and will
63 return the pathname to that file.
64 */
65
66 status_t
CreateSampleFile(BString & path)67 TarArchiveServiceTest::CreateSampleFile(BString& path)
68 {
69 path.SetTo(tmpnam(NULL));
70 BFile* file = new BFile(path, O_WRONLY | O_CREAT);
71 ObjectDeleter<BFile> tarIoDeleter(file);
72 status_t result = file->WriteExactly(kSampleTarPayload, kSampleTarLength);
73
74 if (result != B_OK) {
75 printf("! unable to write the sample data to [%s]\n", path.String());
76 }
77
78 return result;
79 }
80
81
82 status_t
DeleteSampleFile(const BString & path)83 TarArchiveServiceTest::DeleteSampleFile(const BString& path)
84 {
85 if (remove(path.String()) != 0)
86 return B_ERROR;
87 return B_OK;
88 }
89
90
91 /*static*/ void
AddTests(BTestSuite & parent)92 TarArchiveServiceTest::AddTests(BTestSuite& parent)
93 {
94 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("TarArchiveServiceTest");
95
96 suite.addTest(
97 new CppUnit::TestCaller<TarArchiveServiceTest>(
98 "TarArchiveServiceTest::TestReadHeader",
99 &TarArchiveServiceTest::TestReadHeader));
100
101 parent.addTest("TarArchiveServiceTest", &suite);
102 }