WebGLCompatibility implement and add tests for section 6.4

The checks for VertexAttribPointer were already implemented in a
previous patch, so we only add the checks for DrawElements.

BUG=angleproject:1523
BUG=chromium:668223

Change-Id: I5da55f9a7e7479627099c7f77618353a63b75a8e
Reviewed-on: https://chromium-review.googlesource.com/434958
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/tests/gl_tests/WebGLCompatibilityTest.cpp b/src/tests/gl_tests/WebGLCompatibilityTest.cpp
index 2fd0ef8..8f3f71c 100644
--- a/src/tests/gl_tests/WebGLCompatibilityTest.cpp
+++ b/src/tests/gl_tests/WebGLCompatibilityTest.cpp
@@ -627,6 +627,85 @@
     }
 }
 
+// Test that the offset in the index buffer is forced to be a multiple of the element size
+TEST_P(WebGLCompatibilityTest, DrawElementsOffsetRestriction)
+{
+    const std::string &vert =
+        "attribute vec3 a_pos;\n"
+        "void main()\n"
+        "{\n"
+        "    gl_Position = vec4(a_pos, 1.0);\n"
+        "}\n";
+
+    const std::string &frag =
+        "precision highp float;\n"
+        "void main()\n"
+        "{\n"
+        "    gl_FragColor = vec4(1.0);\n"
+        "}\n";
+
+    ANGLE_GL_PROGRAM(program, vert, frag);
+
+    GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
+    ASSERT_NE(-1, posLocation);
+    glUseProgram(program.get());
+
+    const auto &vertices = GetQuadVertices();
+
+    GLBuffer vertexBuffer;
+    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer.get());
+    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * vertices.size(), vertices.data(),
+                 GL_STATIC_DRAW);
+
+    glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
+    glEnableVertexAttribArray(posLocation);
+
+    GLBuffer indexBuffer;
+    const GLubyte indices[] = {0, 0, 0, 0, 0, 0, 0};
+    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer.get());
+    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
+
+    ASSERT_GL_NO_ERROR();
+
+    const char *zeroIndices = nullptr;
+
+    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, zeroIndices);
+    ASSERT_GL_NO_ERROR();
+
+    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, zeroIndices);
+    ASSERT_GL_NO_ERROR();
+
+    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, zeroIndices + 1);
+    EXPECT_GL_ERROR(GL_INVALID_OPERATION);
+}
+
+// Test that the offset and stride in the vertex buffer is forced to be a multiple of the element
+// size
+TEST_P(WebGLCompatibilityTest, VertexAttribPointerOffsetRestriction)
+{
+    const char *zeroOffset = nullptr;
+
+    // Base case, vector of two floats
+    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, zeroOffset);
+    ASSERT_GL_NO_ERROR();
+
+    // Test setting a non-multiple offset
+    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, zeroOffset + 1);
+    EXPECT_GL_ERROR(GL_INVALID_OPERATION);
+    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, zeroOffset + 2);
+    EXPECT_GL_ERROR(GL_INVALID_OPERATION);
+    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, zeroOffset + 3);
+    EXPECT_GL_ERROR(GL_INVALID_OPERATION);
+
+    // Test setting a non-multiple stride
+    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 1, zeroOffset);
+    EXPECT_GL_ERROR(GL_INVALID_OPERATION);
+    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2, zeroOffset);
+    EXPECT_GL_ERROR(GL_INVALID_OPERATION);
+    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 3, zeroOffset);
+    EXPECT_GL_ERROR(GL_INVALID_OPERATION);
+}
+
 // Use this to select which configurations (e.g. which renderer, which GLES major version) these
 // tests should be run against.
 ANGLE_INSTANTIATE_TEST(WebGLCompatibilityTest,