aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/searchers/test/InputCheckingSearcherTestCase.java
blob: 55269ac0ebd7c3a145a8f3551bcf93ae25774905 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.searchers.test;

import static org.junit.jupiter.api.Assertions.*;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.yahoo.component.chain.Chain;
import com.yahoo.metrics.simple.MetricReceiver;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.searchers.InputCheckingSearcher;
import com.yahoo.text.Utf8;

/**
 * Functional test for InputCheckingSearcher.
 *
 * @author Steinar Knutsen
 */
public class InputCheckingSearcherTestCase {

    Execution execution;

    @BeforeEach
    public void setUp() {
        execution = new Execution(new Chain<Searcher>(new InputCheckingSearcher(MetricReceiver.nullImplementation)),
                                  Execution.Context.createContextStub());
    }

    @AfterEach
    public void tearDown() {
        execution = null;
    }

    @Test
    void testCommonCase() {
        Result r = execution.search(new Query("/search/?query=three+blind+mice"));
        assertNull(r.hits().getErrorHit());
    }

    @Test
    void candidateButAsciiOnly() {
        Result r = execution.search(new Query("/search/?query=a+a+a+a+a+a"));
        assertNull(r.hits().getErrorHit());
    }

    @Test
    void candidateButValid() throws UnsupportedEncodingException {
        Result r = execution.search(new Query("/search/?query=" + URLEncoder.encode("å å å å å å", "UTF-8")));
        assertNull(r.hits().getErrorHit());
    }

    @Test
    void candidateButValidAndOutsideFirst256() throws UnsupportedEncodingException {
        Result r = execution.search(new Query("/search/?query=" + URLEncoder.encode("œ œ œ œ œ œ", "UTF-8")));
        assertNull(r.hits().getErrorHit());
    }


    @Test
    void testDoubleEncoded() throws UnsupportedEncodingException {
        String rawQuery = "å å å å å å";
        byte[] encodedOnce = Utf8.toBytes(rawQuery);
        char[] secondEncodingBuffer = new char[encodedOnce.length];
        for (int i = 0; i < secondEncodingBuffer.length; ++i) {
            secondEncodingBuffer[i] = (char) (encodedOnce[i] & 0xFF);
        }
        String query = new String(secondEncodingBuffer);
        Result r = execution.search(new Query("/search/?query=" + URLEncoder.encode(query, StandardCharsets.UTF_8)));
        assertEquals(1, r.hits().getErrorHit().errors().size());
    }

    @Test
    void testRepeatedConsecutiveTermsInPhrase() {
        Result r = execution.search(new Query("/search/?query=%22a.b.0.0.0.0.0.c%22"));
        assertNull(r.hits().getErrorHit());
        r = execution.search(new Query("/search/?query=%22a.b.0.0.0.0.0.0.c%22"));
        assertNotNull(r.hits().getErrorHit());
        assertEquals("More than 5 occurrences of term '0' in a row detected in phrase : \"a b 0 0 0 0 0 0 c\"",
                r.hits().getErrorHit().errorIterator().next().getDetailedMessage());
        r = execution.search(new Query("/search/?query=a.b.0.0.0.1.0.0.0.c"));
        assertNull(r.hits().getErrorHit());
    }

    @Test
    void testThatMaxRepeatedConsecutiveTermsInPhraseIs5() {
        Result r = execution.search(new Query("/search/?query=%22a.b.0.0.0.0.0.c%22"));
        assertNull(r.hits().getErrorHit());
        r = execution.search(new Query("/search/?query=%22a.b.0.0.0.0.0.0.c%22"));
        assertNotNull(r.hits().getErrorHit());
        assertEquals("More than 5 occurrences of term '0' in a row detected in phrase : \"a b 0 0 0 0 0 0 c\"",
                r.hits().getErrorHit().errorIterator().next().getDetailedMessage());
        r = execution.search(new Query("/search/?query=%22a.b.0.0.0.1.0.0.0.c%22"));
        assertNull(r.hits().getErrorHit());
    }

    @Test
    void testThatMaxRepeatedTermsInPhraseIs10() {
        Result r = execution.search(new Query("/search/?query=%220.a.1.a.2.a.3.a.4.a.5.a.6.a.7.a.9.a%22"));
        assertNull(r.hits().getErrorHit());
        r = execution.search(new Query("/search/?query=%220.a.1.a.2.a.3.a.4.a.5.a.6.a.7.a.8.a.9.a.10.a%22"));
        assertNotNull(r.hits().getErrorHit());
        assertEquals("Phrase contains more than 10 occurrences of term 'a' in phrase : \"0 a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 a\"",
                r.hits().getErrorHit().errorIterator().next().getDetailedMessage());
    }

}