1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #include <memory>
31 #include <string>
32 
33 #include "base/file_util.h"
34 #include "base/system_util.h"
35 #include "composer/composer.h"
36 #include "composer/table.h"
37 #include "config/config_handler.h"
38 #include "converter/converter_interface.h"
39 #include "converter/segments.h"
40 #include "engine/engine_factory.h"
41 #include "engine/engine_interface.h"
42 #include "protocol/commands.pb.h"
43 #include "protocol/config.pb.h"
44 #include "request/conversion_request.h"
45 #include "testing/base/public/gunit.h"
46 #include "testing/base/public/mozctest.h"
47 
48 namespace mozc {
49 namespace {
50 
51 using commands::Request;
52 using composer::Composer;
53 using composer::Table;
54 
55 class ConverterRegressionTest : public ::testing::Test {
56  private:
57   const testing::ScopedTmpUserProfileDirectory scoped_profile_dir_;
58 };
59 
TEST_F(ConverterRegressionTest,QueryOfDeathTest)60 TEST_F(ConverterRegressionTest, QueryOfDeathTest) {
61   std::unique_ptr<EngineInterface> engine(EngineFactory::Create());
62   ConverterInterface *converter = engine->GetConverter();
63 
64   CHECK(converter);
65   {
66     Segments segments;
67     EXPECT_TRUE(converter->StartConversion(&segments, "りゅきゅけmぽ"));
68   }
69   {
70     Segments segments;
71     EXPECT_TRUE(converter->StartConversion(&segments, "5.1,||t:1"));
72   }
73   {
74     Segments segments;
75     // Converter returns false, but not crash.
76     EXPECT_FALSE(converter->StartConversion(&segments, ""));
77   }
78   {
79     Segments segments;
80     ConversionRequest conv_request;
81     // Create an empty composer.
82     const Table table;
83     const commands::Request request;
84     composer::Composer composer(&table, &request, nullptr);
85     conv_request.set_composer(&composer);
86     // Converter returns false, but not crash.
87     EXPECT_FALSE(converter->StartConversionForRequest(conv_request, &segments));
88   }
89 }
90 
TEST_F(ConverterRegressionTest,Regression3323108)91 TEST_F(ConverterRegressionTest, Regression3323108) {
92   std::unique_ptr<EngineInterface> engine(EngineFactory::Create());
93   ConverterInterface *converter = engine->GetConverter();
94   Segments segments;
95 
96   EXPECT_TRUE(converter->StartConversion(&segments, "ここではきものをぬぐ"));
97   EXPECT_EQ(3, segments.conversion_segments_size());
98   const ConversionRequest default_request;
99   EXPECT_TRUE(converter->ResizeSegment(&segments, default_request, 1, 2));
100   EXPECT_EQ(2, segments.conversion_segments_size());
101   EXPECT_EQ("きものをぬぐ", segments.conversion_segment(1).key());
102 }
103 
104 }  // namespace
105 }  // namespace mozc
106