aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/vespa-fileheader-inspect/vespa-fileheader-inspect_test.cpp
blob: 7ea480bf542431e7e76b7f88d3153db7d030d770 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/searchlib/util/fileheadertk.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <vespa/vespalib/util/size_literals.h>
#include <vespa/fastos/file.h>

using namespace search;
using namespace vespalib;

bool writeHeader(const FileHeader &header, const vespalib::string &fileName) {
    FastOS_File file;
    if (!EXPECT_TRUE(file.OpenWriteOnlyTruncate(fileName.c_str()))) {
        return false;
    }
    if (!EXPECT_EQUAL(header.getSize(), header.writeFile(file))) {
        return false;
    }
    return true;
}

vespalib::string readFile(const vespalib::string &fileName) {
    FastOS_File file;
    ASSERT_TRUE(file.OpenReadOnly(fileName.c_str()));

    char buf[4_Ki];
    uint32_t len = file.Read(buf, sizeof(buf));
    EXPECT_LESS(len, sizeof(buf)); // make sure we got everything

    vespalib::string str(buf, len);
    return str;
}

TEST("testError") {
    EXPECT_TRUE(system("../../apps/vespa-fileheader-inspect/vespa-fileheader-inspect notfound.dat") != 0);
}

TEST("testEscape") {
    FileHeader header;
    header.putTag(FileHeader::Tag("fanart", "\fa\na\r\t"));
    ASSERT_TRUE(writeHeader(header, "fileheader.dat"));
    EXPECT_TRUE(system("../../apps/vespa-fileheader-inspect/vespa-fileheader-inspect -q fileheader.dat > out") == 0);
    EXPECT_EQUAL("fanart;string;\\fa\\na\\r\\t\n", readFile("out"));
}

TEST("testDelimiter") {
    FileHeader header;
    header.putTag(FileHeader::Tag("string", "string"));
    ASSERT_TRUE(writeHeader(header, "fileheader.dat"));
    EXPECT_TRUE(system("../../apps/vespa-fileheader-inspect/vespa-fileheader-inspect -d i -q fileheader.dat > out") == 0);
    EXPECT_EQUAL("str\\ingistr\\ingistr\\ing\n", readFile("out"));
}

TEST("testQuiet") {
    FileHeader header;
    FileHeaderTk::addVersionTags(header);
    ASSERT_TRUE(writeHeader(header, "fileheader.dat"));
    EXPECT_TRUE(system("../../apps/vespa-fileheader-inspect/vespa-fileheader-inspect -q fileheader.dat > out") == 0);
    vespalib::string str = readFile("out");
    EXPECT_TRUE(!str.empty());
    for (uint32_t i = 0, numTags = header.getNumTags(); i < numTags; ++i) {
        const FileHeader::Tag &tag = header.getTag(i);
        size_t pos = str.find(tag.getName());
        EXPECT_TRUE(pos != vespalib::string::npos);

        vespalib::asciistream out;
        out << ";" << tag;
        EXPECT_TRUE(str.find(out.str(), pos) != vespalib::string::npos);
    }
}

TEST("testVerbose") {
    FileHeader header;
    FileHeaderTk::addVersionTags(header);
    ASSERT_TRUE(writeHeader(header, "fileheader.dat"));
    EXPECT_TRUE(system("../../apps/vespa-fileheader-inspect/vespa-fileheader-inspect fileheader.dat > out") == 0);
    vespalib::string str = readFile("out");
    EXPECT_TRUE(!str.empty());
    for (uint32_t i = 0, numTags = header.getNumTags(); i < numTags; ++i) {
        const FileHeader::Tag &tag = header.getTag(i);
        EXPECT_TRUE(str.find(tag.getName()) != vespalib::string::npos);

        vespalib::asciistream out;
        out << tag;
        EXPECT_TRUE(str.find(out.str()) != vespalib::string::npos);
    }
}

TEST_MAIN() { TEST_RUN_ALL(); }